38407-vm/backend/src/db/models/study_materials.js
2026-02-13 16:37:10 +00:00

222 lines
2.6 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 study_materials = sequelize.define(
'study_materials',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title_om: {
type: DataTypes.TEXT,
},
title_am: {
type: DataTypes.TEXT,
},
title_en: {
type: DataTypes.TEXT,
},
material_type: {
type: DataTypes.ENUM,
values: [
"past_exam_paper",
"revision_notes",
"practice_questions",
"reference_list",
"video_link",
"other"
],
},
description: {
type: DataTypes.TEXT,
},
external_url: {
type: DataTypes.TEXT,
},
visibility: {
type: DataTypes.ENUM,
values: [
"public",
"students_only"
],
},
published_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
study_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.study_materials.belongsTo(db.grades, {
as: 'grade',
foreignKey: {
name: 'gradeId',
},
constraints: false,
});
db.study_materials.belongsTo(db.subjects, {
as: 'subject',
foreignKey: {
name: 'subjectId',
},
constraints: false,
});
db.study_materials.belongsTo(db.streams, {
as: 'stream',
foreignKey: {
name: 'streamId',
},
constraints: false,
});
db.study_materials.belongsTo(db.staff_members, {
as: 'uploaded_by',
foreignKey: {
name: 'uploaded_byId',
},
constraints: false,
});
db.study_materials.hasMany(db.file, {
as: 'file',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.study_materials.getTableName(),
belongsToColumn: 'file',
},
});
db.study_materials.belongsTo(db.users, {
as: 'createdBy',
});
db.study_materials.belongsTo(db.users, {
as: 'updatedBy',
});
};
return study_materials;
};