32275/backend/src/db/models/abstracts.js
2025-06-16 14:31:37 +00:00

144 lines
2.7 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 abstracts = sequelize.define(
'abstracts',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
text_body: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
'Submitted',
'UnderReview',
'Rejected',
'Accepted',
'RegPending',
'RegApproved',
'FinalPending',
'FinalReceived',
],
},
reg_number: {
type: DataTypes.TEXT,
},
score_avg: {
type: DataTypes.DECIMAL,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
abstracts.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.abstracts.hasMany(db.reviews, {
as: 'reviews_abstract',
foreignKey: {
name: 'abstractId',
},
constraints: false,
});
//end loop
db.abstracts.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.abstracts.belongsTo(db.categories, {
as: 'category',
foreignKey: {
name: 'categoryId',
},
constraints: false,
});
db.abstracts.belongsTo(db.sub_topics, {
as: 'sub_topic',
foreignKey: {
name: 'sub_topicId',
},
constraints: false,
});
db.abstracts.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.abstracts.hasMany(db.file, {
as: 'reg_proof_file',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.abstracts.getTableName(),
belongsToColumn: 'reg_proof_file',
},
});
db.abstracts.hasMany(db.file, {
as: 'files',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.abstracts.getTableName(),
belongsToColumn: 'files',
},
});
db.abstracts.belongsTo(db.users, {
as: 'createdBy',
});
db.abstracts.belongsTo(db.users, {
as: 'updatedBy',
});
};
return abstracts;
};