32629/backend/src/db/models/work_orders.js
2025-07-04 22:03:52 +00:00

84 lines
1.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 work_orders = sequelize.define(
'work_orders',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
scheduled_date: {
type: DataTypes.DATE,
},
status: {
type: DataTypes.ENUM,
values: ['pending', 'in_progress', 'completed'],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
work_orders.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.work_orders.hasMany(db.maintenance_logs, {
as: 'maintenance_logs_work_order',
foreignKey: {
name: 'work_orderId',
},
constraints: false,
});
//end loop
db.work_orders.belongsTo(db.users, {
as: 'assigned_to',
foreignKey: {
name: 'assigned_toId',
},
constraints: false,
});
db.work_orders.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.work_orders.belongsTo(db.users, {
as: 'createdBy',
});
db.work_orders.belongsTo(db.users, {
as: 'updatedBy',
});
};
return work_orders;
};