40480-vm/backend/src/db/models/prescriptions.js
2026-07-25 11:58:50 +00:00

160 lines
2.0 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 prescriptions = sequelize.define(
'prescriptions',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
issued_at: {
type: DataTypes.DATE,
},
title: {
type: DataTypes.TEXT,
},
content: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"draft",
"signed",
"canceled"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
prescriptions.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.prescriptions.belongsTo(db.clinics, {
as: 'clinic',
foreignKey: {
name: 'clinicId',
},
constraints: false,
});
db.prescriptions.belongsTo(db.patients, {
as: 'patient',
foreignKey: {
name: 'patientId',
},
constraints: false,
});
db.prescriptions.belongsTo(db.users, {
as: 'professional',
foreignKey: {
name: 'professionalId',
},
constraints: false,
});
db.prescriptions.hasMany(db.file, {
as: 'files',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.prescriptions.getTableName(),
belongsToColumn: 'files',
},
});
db.prescriptions.belongsTo(db.users, {
as: 'createdBy',
});
db.prescriptions.belongsTo(db.users, {
as: 'updatedBy',
});
};
return prescriptions;
};