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 | import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document, Schema as MongooseSchema } from 'mongoose'; // ════════════════════════════════════════════════════════════════ // CALENDAR DEFINITION (tenant-level working calendar template) // ════════════════════════════════════════════════════════════════ @Schema({ collection: 'mdm_calendar_definitions', timestamps: true }) export class CalendarDefinition extends Document { @Prop({ required: true }) definitionCode!: string; @Prop({ required: true }) definitionName!: string; @Prop({ type: MongooseSchema.Types.ObjectId }) tenantId?: MongooseSchema.Types.ObjectId; @Prop({ required: true, enum: ['gregorian', 'fiscal', 'islamic', 'buddhist'], default: 'gregorian', }) calendarSystem!: string; @Prop({ required: true }) countryCode!: string; @Prop({ type: [Number], default: [0, 6] }) weekendDays!: number[]; // 0=Sun, 6=Sat @Prop({ default: true }) active!: boolean; } export const CalendarDefinitionSchema = SchemaFactory.createForClass(CalendarDefinition); CalendarDefinitionSchema.index( { definitionCode: 1, tenantId: 1 }, { unique: true, sparse: true }, ); // ════════════════════════════════════════════════════════════════ // FINANCIAL YEAR // ════════════════════════════════════════════════════════════════ @Schema({ collection: 'mdm_financial_years', timestamps: true }) export class FinancialYear extends Document { @Prop({ required: true }) yearCode!: string; // FY2025-26 @Prop({ required: true }) yearLabel!: string; // 2025-26 @Prop({ required: true }) startDate!: Date; // 2025-04-01 @Prop({ required: true }) endDate!: Date; // 2026-03-31 @Prop({ required: true }) calendarYear!: number; // 2025 @Prop({ type: MongooseSchema.Types.ObjectId }) tenantId?: MongooseSchema.Types.ObjectId; @Prop({ required: true, enum: ['open', 'closed', 'locked', 'archived'], default: 'open', }) status!: string; @Prop({ default: false }) isCurrent!: boolean; @Prop({ type: MongooseSchema.Types.ObjectId }) closedBy?: MongooseSchema.Types.ObjectId; @Prop() closedAt?: Date; @Prop({ type: MongooseSchema.Types.ObjectId }) lockedBy?: MongooseSchema.Types.ObjectId; @Prop() lockedAt?: Date; } export const FinancialYearSchema = SchemaFactory.createForClass(FinancialYear); FinancialYearSchema.index( { yearCode: 1, tenantId: 1 }, { unique: true, sparse: true }, ); FinancialYearSchema.index({ startDate: 1, endDate: 1, tenantId: 1 }); FinancialYearSchema.index({ isCurrent: 1, tenantId: 1 }); // ════════════════════════════════════════════════════════════════ // FINANCIAL PERIOD (Month/Quarter within a FY) // ════════════════════════════════════════════════════════════════ @Schema({ collection: 'mdm_financial_periods', timestamps: true }) export class FinancialPeriod extends Document { @Prop({ required: true }) periodCode!: string; // P01-FY2025-26 @Prop({ required: true }) periodName!: string; // April 2025 @Prop({ required: true }) yearCode!: string; @Prop({ type: MongooseSchema.Types.ObjectId, required: true }) financialYearId!: MongooseSchema.Types.ObjectId; @Prop({ type: MongooseSchema.Types.ObjectId }) tenantId?: MongooseSchema.Types.ObjectId; @Prop({ required: true, enum: ['month', 'quarter', 'half_year'] }) periodType!: string; @Prop({ required: true }) periodNumber!: number; // 1-12 for month, 1-4 for quarter @Prop({ required: true }) startDate!: Date; @Prop({ required: true }) endDate!: Date; @Prop({ required: true, enum: ['open', 'closed', 'locked'], default: 'open' }) status!: string; @Prop({ type: MongooseSchema.Types.ObjectId }) closedBy?: MongooseSchema.Types.ObjectId; @Prop() closedAt?: Date; } export const FinancialPeriodSchema = SchemaFactory.createForClass(FinancialPeriod); FinancialPeriodSchema.index( { periodCode: 1, tenantId: 1 }, { unique: true, sparse: true }, ); FinancialPeriodSchema.index({ yearCode: 1, periodNumber: 1, tenantId: 1 }); FinancialPeriodSchema.index({ startDate: 1, endDate: 1, status: 1 }); // ════════════════════════════════════════════════════════════════ // ACCOUNTING PERIOD // Sub-period for GL/AR/AP posting // ════════════════════════════════════════════════════════════════ @Schema({ collection: 'mdm_accounting_periods', timestamps: true }) export class AccountingPeriod extends Document { @Prop({ required: true }) periodCode!: string; @Prop({ required: true, type: MongooseSchema.Types.ObjectId }) financialPeriodId!: MongooseSchema.Types.ObjectId; @Prop({ required: true }) yearCode!: string; @Prop({ type: MongooseSchema.Types.ObjectId }) tenantId?: MongooseSchema.Types.ObjectId; @Prop({ type: [String], default: [] }) allowedModules!: string[]; // hr, projects, crm, finance etc @Prop({ required: true, enum: ['open', 'soft_close', 'hard_close', 'locked'], default: 'open', }) status!: string; @Prop({ type: MongooseSchema.Types.Mixed }) closureNotes?: any; @Prop({ type: MongooseSchema.Types.ObjectId }) closedBy?: MongooseSchema.Types.ObjectId; @Prop() closedAt?: Date; } export const AccountingPeriodSchema = SchemaFactory.createForClass(AccountingPeriod); AccountingPeriodSchema.index( { periodCode: 1, tenantId: 1 }, { unique: true, sparse: true }, ); AccountingPeriodSchema.index({ financialPeriodId: 1, status: 1 }); // ════════════════════════════════════════════════════════════════ // PERIOD LOCK DEFINITION // Rules for automatic period locks // ════════════════════════════════════════════════════════════════ @Schema({ collection: 'mdm_period_lock_definitions', timestamps: true }) export class PeriodLockDefinition extends Document { @Prop({ required: true }) lockCode!: string; @Prop({ required: true }) lockName!: string; @Prop({ type: MongooseSchema.Types.ObjectId }) tenantId?: MongooseSchema.Types.ObjectId; @Prop({ required: true }) moduleKey!: string; @Prop({ required: true, enum: ['days_after_period_end', 'day_of_month', 'manual'], }) lockTrigger!: string; @Prop() daysAfterEnd?: number; @Prop() dayOfMonth?: number; @Prop({ default: false }) requiresApprovalToReopen!: boolean; @Prop({ default: true }) active!: boolean; } export const PeriodLockDefinitionSchema = SchemaFactory.createForClass(PeriodLockDefinition); PeriodLockDefinitionSchema.index( { lockCode: 1, tenantId: 1 }, { unique: true, sparse: true }, ); PeriodLockDefinitionSchema.index({ moduleKey: 1, tenantId: 1 }); |