39391-vm/backend/src/db/models/materials.js
2026-03-30 14:19:48 +00:00

192 lines
2.4 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const materials = sequelize.define(
'materials',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
category: {
type: DataTypes.ENUM,
values: [
"music",
"liturgy",
"drama"
],
},
material_type: {
type: DataTypes.ENUM,
values: [
"pdf",
"video",
"audio",
"link",
"other"
],
},
external_url: {
type: DataTypes.TEXT,
},
is_published: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
published_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
materials.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.materials.belongsTo(db.courses, {
as: 'course',
foreignKey: {
name: 'courseId',
},
constraints: false,
});
db.materials.belongsTo(db.users, {
as: 'uploaded_by',
foreignKey: {
name: 'uploaded_byId',
},
constraints: false,
});
db.materials.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.materials.hasMany(db.file, {
as: 'attachments',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.materials.getTableName(),
belongsToColumn: 'attachments',
},
});
db.materials.belongsTo(db.users, {
as: 'createdBy',
});
db.materials.belongsTo(db.users, {
as: 'updatedBy',
});
};
return materials;
};