31257/backend/src/db/models/patients.js
2025-05-05 10:41:48 +00:00

128 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 patients = sequelize.define(
'patients',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
full_name: {
type: DataTypes.TEXT,
},
date_of_birth: {
type: DataTypes.DATE,
},
gender: {
type: DataTypes.ENUM,
values: ['male', 'female'],
},
nationality: {
type: DataTypes.TEXT,
},
national_id: {
type: DataTypes.TEXT,
},
contact_information: {
type: DataTypes.TEXT,
},
emergency_contact: {
type: DataTypes.TEXT,
},
address: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
patients.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.patients.hasMany(db.appointments, {
as: 'appointments_patient',
foreignKey: {
name: 'patientId',
},
constraints: false,
});
db.patients.hasMany(db.emr_records, {
as: 'emr_records_patient',
foreignKey: {
name: 'patientId',
},
constraints: false,
});
db.patients.hasMany(db.imaging_orders, {
as: 'imaging_orders_patient',
foreignKey: {
name: 'patientId',
},
constraints: false,
});
db.patients.hasMany(db.lab_orders, {
as: 'lab_orders_patient',
foreignKey: {
name: 'patientId',
},
constraints: false,
});
db.patients.hasMany(db.pharmacy_orders, {
as: 'pharmacy_orders_patient',
foreignKey: {
name: 'patientId',
},
constraints: false,
});
//end loop
db.patients.belongsTo(db.organization, {
as: 'organization',
foreignKey: {
name: 'organizationId',
},
constraints: false,
});
db.patients.belongsTo(db.users, {
as: 'createdBy',
});
db.patients.belongsTo(db.users, {
as: 'updatedBy',
});
};
return patients;
};