33881/backend/src/db/models/appointments.js
2025-09-04 10:46:34 +00:00

106 lines
1.9 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 appointments = sequelize.define(
'appointments',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
start_time: {
type: DataTypes.DATE,
},
end_time: {
type: DataTypes.DATE,
},
status: {
type: DataTypes.ENUM,
values: [
'pending',
'confirmed',
'on_route',
'in_progress',
'completed',
'invoiced',
'cancelled',
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
appointments.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.appointments.hasMany(db.payments, {
as: 'payments_appointment',
foreignKey: {
name: 'appointmentId',
},
constraints: false,
});
//end loop
db.appointments.belongsTo(db.users, {
as: 'client',
foreignKey: {
name: 'clientId',
},
constraints: false,
});
db.appointments.belongsTo(db.services, {
as: 'service',
foreignKey: {
name: 'serviceId',
},
constraints: false,
});
db.appointments.belongsTo(db.partners, {
as: 'partners',
foreignKey: {
name: 'partnersId',
},
constraints: false,
});
db.appointments.belongsTo(db.users, {
as: 'createdBy',
});
db.appointments.belongsTo(db.users, {
as: 'updatedBy',
});
};
return appointments;
};