Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document, Schema as MongooseSchema } from 'mongoose'; // ============================================================ // APPROVAL DEFINITION — the blueprint for an approval workflow // ============================================================ @Schema({ timestamps: true, collection: 'approval_definitions' }) export class ApprovalDefinition extends Document { @Prop({ required: true, index: true }) tenantId: string; @Prop({ required: true }) name: string; @Prop({ default: null }) description: string; @Prop({ default: null, index: true }) moduleKey: string; // Which ERP module this belongs to @Prop({ required: true }) entityType: string; // e.g. 'LeaveRequest', 'Expense', 'PurchaseOrder' @Prop({ default: true }) isActive: boolean; @Prop({ default: 1 }) version: number; @Prop({ default: 'DRAFT' }) status: string; // 'DRAFT' | 'PUBLISHED' @Prop({ default: null }) createdBy: string; } export const ApprovalDefinitionSchema = SchemaFactory.createForClass(ApprovalDefinition); ApprovalDefinitionSchema.index({ tenantId: 1, entityType: 1 }); // ============================================================ // APPROVAL STEP DEFINITION — ordered steps within a definition // ============================================================ @Schema({ timestamps: true, collection: 'approval_step_definitions' }) export class ApprovalStepDefinition extends Document { @Prop({ required: true, index: true }) approvalDefinitionId: string; @Prop({ required: true }) sequence: number; @Prop({ required: true }) approverType: string; // 'USER' | 'ROLE' | 'REPORTING_MANAGER' | 'DEPARTMENT' | 'BRANCH' @Prop({ default: null }) approverId: string; // userId or roleId depending on approverType @Prop({ default: 'ANY' }) approvalMode: string; // 'ANY' | 'ALL' — any approver vs all approvers @Prop({ default: 72 }) dueDateHours: number; // SLA for this step @Prop({ default: false }) autoEscalate: boolean; } export const ApprovalStepDefinitionSchema = SchemaFactory.createForClass( ApprovalStepDefinition, ); // ============================================================ // APPROVAL REQUEST — a submitted instance of an approval // ============================================================ @Schema({ timestamps: true, collection: 'approval_requests' }) export class ApprovalRequest extends Document { @Prop({ required: true, index: true }) tenantId: string; @Prop({ required: true, index: true }) approvalDefinitionId: string; @Prop({ required: true }) entityType: string; @Prop({ required: true }) entityId: string; @Prop({ required: true }) submittedBy: string; @Prop({ required: true, default: 'PENDING', index: true }) status: string; // 'PENDING' | 'APPROVED' | 'REJECTED' | 'CHANGES_REQUESTED' | 'CANCELLED' @Prop({ default: 1 }) currentStepSequence: number; @Prop({ default: 1 }) __v: number; // Optimistic concurrency version @Prop({ type: MongooseSchema.Types.Mixed, default: null }) metadata: any; } export const ApprovalRequestSchema = SchemaFactory.createForClass(ApprovalRequest); ApprovalRequestSchema.index({ tenantId: 1, status: 1, submittedBy: 1 }); // ============================================================ // APPROVAL STEP INSTANCE — runtime state of each step // ============================================================ @Schema({ timestamps: true, collection: 'approval_step_instances' }) export class ApprovalStepInstance extends Document { @Prop({ required: true, index: true }) requestId: string; @Prop({ required: true }) sequence: number; @Prop({ required: true, default: 'PENDING' }) status: string; // 'PENDING' | 'APPROVED' | 'REJECTED' | 'SKIPPED' @Prop({ required: true }) assignedTo: string; // userId or roleId @Prop({ default: null }) completedAt: Date; @Prop({ default: null }) dueAt: Date; } export const ApprovalStepInstanceSchema = SchemaFactory.createForClass(ApprovalStepInstance); // ============================================================ // APPROVAL ACTION — immutable action history // ============================================================ @Schema({ timestamps: true, collection: 'approval_actions' }) export class ApprovalAction extends Document { @Prop({ required: true, index: true }) requestId: string; @Prop({ required: true }) stepSequence: number; @Prop({ required: true }) userId: string; @Prop({ required: true }) action: string; // 'APPROVE' | 'REJECT' | 'REQUEST_CHANGES' | 'CANCEL' | 'RESUBMIT' @Prop({ default: null }) comment: string; @Prop({ type: [String], default: [] }) attachments: string[]; // File IDs } export const ApprovalActionSchema = SchemaFactory.createForClass(ApprovalAction); // ============================================================ // APPROVAL DELEGATION // ============================================================ @Schema({ timestamps: true, collection: 'approval_delegations' }) export class ApprovalDelegation extends Document { @Prop({ required: true, index: true }) tenantId: string; @Prop({ required: true }) fromUserId: string; @Prop({ required: true }) toUserId: string; @Prop({ required: true }) startsAt: Date; @Prop({ required: true }) endsAt: Date; @Prop({ default: true }) isActive: boolean; } export const ApprovalDelegationSchema = SchemaFactory.createForClass(ApprovalDelegation); |