import { DataTypes, Model, Optional } from 'sequelize'; import { sequelize } from '@config/database'; import { User } from './User'; import { Priority, WorkflowStatus } from '../types/common.types'; interface WorkflowRequestAttributes { requestId: string; requestNumber: string; initiatorId: string; templateType: 'CUSTOM' | 'TEMPLATE'; title: string; description: string; priority: Priority; status: WorkflowStatus; currentLevel: number; totalLevels: number; totalTatHours: number; submissionDate?: Date; closureDate?: Date; conclusionRemark?: string; aiGeneratedConclusion?: string; isDraft: boolean; isDeleted: boolean; createdAt: Date; updatedAt: Date; } interface WorkflowRequestCreationAttributes extends Optional {} class WorkflowRequest extends Model implements WorkflowRequestAttributes { public requestId!: string; public requestNumber!: string; public initiatorId!: string; public templateType!: 'CUSTOM' | 'TEMPLATE'; public title!: string; public description!: string; public priority!: Priority; public status!: WorkflowStatus; public currentLevel!: number; public totalLevels!: number; public totalTatHours!: number; public submissionDate?: Date; public closureDate?: Date; public conclusionRemark?: string; public aiGeneratedConclusion?: string; public isDraft!: boolean; public isDeleted!: boolean; public createdAt!: Date; public updatedAt!: Date; // Associations public initiator?: User; } WorkflowRequest.init( { requestId: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, field: 'request_id' }, requestNumber: { type: DataTypes.STRING(20), allowNull: false, unique: true, field: 'request_number' }, initiatorId: { type: DataTypes.UUID, allowNull: false, field: 'initiator_id', references: { model: 'users', key: 'user_id' } }, templateType: { type: DataTypes.STRING(20), defaultValue: 'CUSTOM', field: 'template_type' }, title: { type: DataTypes.STRING(500), allowNull: false }, description: { type: DataTypes.TEXT, allowNull: false }, priority: { type: DataTypes.ENUM('STANDARD', 'EXPRESS'), defaultValue: 'STANDARD' }, status: { type: DataTypes.ENUM('DRAFT', 'PENDING', 'IN_PROGRESS', 'APPROVED', 'REJECTED', 'CLOSED'), defaultValue: 'DRAFT' }, currentLevel: { type: DataTypes.INTEGER, defaultValue: 1, field: 'current_level' }, totalLevels: { type: DataTypes.INTEGER, defaultValue: 1, field: 'total_levels', validate: { max: 10 } }, totalTatHours: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, field: 'total_tat_hours' }, submissionDate: { type: DataTypes.DATE, allowNull: true, field: 'submission_date' }, closureDate: { type: DataTypes.DATE, allowNull: true, field: 'closure_date' }, conclusionRemark: { type: DataTypes.TEXT, allowNull: true, field: 'conclusion_remark' }, aiGeneratedConclusion: { type: DataTypes.TEXT, allowNull: true, field: 'ai_generated_conclusion' }, isDraft: { type: DataTypes.BOOLEAN, defaultValue: true, field: 'is_draft' }, isDeleted: { type: DataTypes.BOOLEAN, defaultValue: false, field: 'is_deleted' }, createdAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, field: 'created_at' }, updatedAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, field: 'updated_at' } }, { sequelize, modelName: 'WorkflowRequest', tableName: 'workflow_requests', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', indexes: [ { fields: ['initiator_id'] }, { fields: ['status'] }, { unique: true, fields: ['request_number'] }, { fields: ['created_at'] } ] } ); // Associations WorkflowRequest.belongsTo(User, { as: 'initiator', foreignKey: 'initiatorId', targetKey: 'userId' }); export { WorkflowRequest };