38803-vm/backend/src/db/models/notifications.js
2026-02-27 03:22:43 +00:00

207 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 notifications = sequelize.define(
'notifications',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
alert_type: {
type: DataTypes.ENUM,
values: [
"trip_upcoming",
"trip_overdue",
"oil_due",
"filter_due",
"document_expiry",
"violation_due",
"installment_due"
],
},
title_text: {
type: DataTypes.TEXT,
},
message_text: {
type: DataTypes.TEXT,
},
due_at: {
type: DataTypes.DATE,
},
severity: {
type: DataTypes.ENUM,
values: [
"info",
"warning",
"critical"
],
},
status: {
type: DataTypes.ENUM,
values: [
"new",
"seen",
"resolved"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
notifications.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.notifications.belongsTo(db.companies, {
as: 'company',
foreignKey: {
name: 'companyId',
},
constraints: false,
});
db.notifications.belongsTo(db.trucks, {
as: 'truck',
foreignKey: {
name: 'truckId',
},
constraints: false,
});
db.notifications.belongsTo(db.drivers, {
as: 'driver',
foreignKey: {
name: 'driverId',
},
constraints: false,
});
db.notifications.belongsTo(db.trips, {
as: 'trip',
foreignKey: {
name: 'tripId',
},
constraints: false,
});
db.notifications.belongsTo(db.violations, {
as: 'violation',
foreignKey: {
name: 'violationId',
},
constraints: false,
});
db.notifications.belongsTo(db.users, {
as: 'createdBy',
});
db.notifications.belongsTo(db.users, {
as: 'updatedBy',
});
};
return notifications;
};