2026-02-17 23:49:42 +00:00

189 lines
2.3 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 alerts = sequelize.define(
'alerts',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
alert_type: {
type: DataTypes.ENUM,
values: [
"low_stock",
"expired_stock",
"near_expiry",
"quarantine",
"negative_stock"
],
},
severity: {
type: DataTypes.ENUM,
values: [
"info",
"warning",
"critical"
],
},
triggered_at: {
type: DataTypes.DATE,
},
acknowledged_at: {
type: DataTypes.DATE,
},
is_resolved: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
resolved_at: {
type: DataTypes.DATE,
},
message: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
alerts.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.alerts.belongsTo(db.products, {
as: 'product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.alerts.belongsTo(db.warehouses, {
as: 'warehouse',
foreignKey: {
name: 'warehouseId',
},
constraints: false,
});
db.alerts.belongsTo(db.storage_locations, {
as: 'storage_location',
foreignKey: {
name: 'storage_locationId',
},
constraints: false,
});
db.alerts.belongsTo(db.users, {
as: 'acknowledged_by',
foreignKey: {
name: 'acknowledged_byId',
},
constraints: false,
});
db.alerts.belongsTo(db.users, {
as: 'createdBy',
});
db.alerts.belongsTo(db.users, {
as: 'updatedBy',
});
};
return alerts;
};