Dealer_Onboarding_Backend/models/ConstitutionalChange.js

88 lines
2.4 KiB
JavaScript

const { CONSTITUTIONAL_CHANGE_TYPES, CONSTITUTIONAL_STAGES } = require('../config/constants');
module.exports = (sequelize, DataTypes) => {
const ConstitutionalChange = sequelize.define('ConstitutionalChange', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
requestId: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
outletId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'outlets',
key: 'id'
}
},
dealerId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},
changeType: {
type: DataTypes.ENUM(Object.values(CONSTITUTIONAL_CHANGE_TYPES)),
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: false
},
currentStage: {
type: DataTypes.ENUM(Object.values(CONSTITUTIONAL_STAGES)),
defaultValue: CONSTITUTIONAL_STAGES.DD_ADMIN_REVIEW
},
status: {
type: DataTypes.STRING,
defaultValue: 'Pending'
},
progressPercentage: {
type: DataTypes.INTEGER,
defaultValue: 0
},
documents: {
type: DataTypes.JSON,
defaultValue: []
},
timeline: {
type: DataTypes.JSON,
defaultValue: []
}
}, {
tableName: 'constitutional_changes',
timestamps: true,
indexes: [
{ fields: ['requestId'] },
{ fields: ['outletId'] },
{ fields: ['dealerId'] },
{ fields: ['currentStage'] }
]
});
ConstitutionalChange.associate = (models) => {
ConstitutionalChange.belongsTo(models.Outlet, {
foreignKey: 'outletId',
as: 'outlet'
});
ConstitutionalChange.belongsTo(models.User, {
foreignKey: 'dealerId',
as: 'dealer'
});
ConstitutionalChange.hasMany(models.Worknote, {
foreignKey: 'requestId',
as: 'worknotes',
scope: { requestType: 'constitutional' }
});
};
return ConstitutionalChange;
};