import { Model, DataTypes, Sequelize } from 'sequelize'; import { RELOCATION_TYPES, RELOCATION_STAGES } from '../../common/config/constants.js'; export interface RelocationRequestAttributes { id: string; requestId: string; outletId: string; dealerId: string; relocationType: typeof RELOCATION_TYPES[keyof typeof RELOCATION_TYPES]; newAddress: string; newCity: string; newState: string; newDistrictId: string | null; newStateId: string | null; reason: string; currentStage: typeof RELOCATION_STAGES[keyof typeof RELOCATION_STAGES]; status: string; progressPercentage: number; documents: any[]; timeline: any[]; } export interface RelocationRequestInstance extends Model, RelocationRequestAttributes { } export default (sequelize: Sequelize) => { const RelocationRequest = sequelize.define('RelocationRequest', { 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' } }, relocationType: { type: DataTypes.ENUM(...Object.values(RELOCATION_TYPES)), allowNull: false }, newAddress: { type: DataTypes.TEXT, allowNull: false }, newCity: { type: DataTypes.STRING, allowNull: false }, newState: { type: DataTypes.STRING, allowNull: false }, newDistrictId: { type: DataTypes.UUID, allowNull: true, references: { model: 'districts', key: 'id' } }, newStateId: { type: DataTypes.UUID, allowNull: true, references: { model: 'states', key: 'id' } }, reason: { type: DataTypes.TEXT, allowNull: false }, currentStage: { type: DataTypes.ENUM(...Object.values(RELOCATION_STAGES)), defaultValue: RELOCATION_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: 'relocation_requests', timestamps: true, indexes: [ { fields: ['requestId'] }, { fields: ['outletId'] }, { fields: ['dealerId'] }, { fields: ['currentStage'] } ] }); (RelocationRequest as any).associate = (models: any) => { RelocationRequest.belongsTo(models.Outlet, { foreignKey: 'outletId', as: 'outlet' }); RelocationRequest.belongsTo(models.User, { foreignKey: 'dealerId', as: 'dealer' }); RelocationRequest.belongsTo(models.District, { foreignKey: 'newDistrictId', as: 'newDistrict' }); RelocationRequest.belongsTo(models.State, { foreignKey: 'newStateId', as: 'newStateRef' }); RelocationRequest.hasMany(models.Worknote, { foreignKey: 'requestId', as: 'worknotes', scope: { requestType: 'relocation' }, constraints: false }); // Note: Participants are computed dynamically based on outlet location hierarchy // See getRequestById in relocation.controller.ts }; return RelocationRequest; };