All files / src/domains/hr/leave/schemas leave-cf-optional.schema.ts

0% Statements 0/80
0% Branches 0/12
100% Functions 0/0
0% Lines 0/70

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                                                                                                                                                                                                                                                                                                                       
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
// ─────────────────────────────────────────
// Optional Holiday Allowance
// ─────────────────────────────────────────
export type OptionalHolidayAllowanceDocument = OptionalHolidayAllowance &
  Document;
 
@Schema({ collection: 'optional_holiday_allowances', timestamps: true })
export class OptionalHolidayAllowance {
  @Prop({ required: true }) tenantId: string;
  @Prop({ required: true }) leavePeriodId: string;
  @Prop() branchId: string;
  @Prop() departmentId: string;
  @Prop({ required: true }) maxSelections: number;
  @Prop() selectionDeadline: string; // YYYY-MM-DD
  @Prop({ default: true }) replacementAllowed: boolean;
  @Prop({ default: false }) approvalRequired: boolean;
  @Prop({ enum: ['active', 'inactive'], default: 'active' }) status: string;
  @Prop() createdBy: string;
}
 
export const OptionalHolidayAllowanceSchema = SchemaFactory.createForClass(
  OptionalHolidayAllowance,
);
OptionalHolidayAllowanceSchema.index({ tenantId: 1, leavePeriodId: 1 });
 
// ─────────────────────────────────────────
// Employee Optional Holiday Selection
// ─────────────────────────────────────────
export type EmployeeOptionalHolidaySelectionDocument =
  EmployeeOptionalHolidaySelection & Document;
 
@Schema({
  collection: 'employee_optional_holiday_selections',
  timestamps: true,
})
export class EmployeeOptionalHolidaySelection {
  @Prop({ required: true }) tenantId: string;
  @Prop({ required: true }) employeeId: string;
  @Prop({ required: true }) allowanceId: string;
  @Prop({ required: true }) holidayId: string; // from holiday catalog
  @Prop({ required: true }) holidayDate: string;
  @Prop({
    enum: ['selected', 'approved', 'rejected', 'cancelled'],
    default: 'selected',
  })
  status: string;
  @Prop() approvedBy: string;
  @Prop() cancelReason: string;
  @Prop() selectedAt: Date;
}
 
export const EmployeeOptionalHolidaySelectionSchema =
  SchemaFactory.createForClass(EmployeeOptionalHolidaySelection);
EmployeeOptionalHolidaySelectionSchema.index({
  tenantId: 1,
  employeeId: 1,
  allowanceId: 1,
});
EmployeeOptionalHolidaySelectionSchema.index({ tenantId: 1, holidayDate: 1 });
 
// ─────────────────────────────────────────
// Leave Carry Forward Rule
// ─────────────────────────────────────────
export type LeaveCarryForwardRuleDocument = LeaveCarryForwardRule & Document;
 
@Schema({ collection: 'leave_carry_forward_rules', timestamps: true })
export class LeaveCarryForwardRule {
  @Prop({ required: true }) tenantId: string;
  @Prop({ required: true }) policyId: string;
  @Prop({ required: true }) leaveTypeId: string;
  @Prop({ enum: ['fixed', 'percentage'] }) limitType: string;
  @Prop({ required: true }) limitValue: number; // days or %
  @Prop() minimumBalanceRequired: number;
  @Prop({ default: false }) prorateEnabled: boolean;
  @Prop({ default: false }) noticeRestriction: boolean;
  @Prop() expiryMonths: number; // months after which CF expires
  @Prop({ enum: ['All', 'Active', 'Confirmed'], default: 'All' })
  eligibleStatus: string;
  @Prop({ enum: ['active', 'inactive'], default: 'active' }) status: string;
}
 
export const LeaveCarryForwardRuleSchema = SchemaFactory.createForClass(
  LeaveCarryForwardRule,
);
LeaveCarryForwardRuleSchema.index({ tenantId: 1, policyId: 1, leaveTypeId: 1 });
 
// ─────────────────────────────────────────
// Leave Carry Forward Execution
// ─────────────────────────────────────────
export type LeaveCarryForwardExecutionDocument = LeaveCarryForwardExecution &
  Document;
 
@Schema({ collection: 'leave_carry_forward_executions', timestamps: true })
export class LeaveCarryForwardExecution {
  @Prop({ required: true }) tenantId: string;
  @Prop({ required: true }) fromPeriodId: string;
  @Prop({ required: true }) toPeriodId: string;
  @Prop({ required: true }) idempotencyKey: string;
  @Prop({
    enum: ['running', 'completed', 'failed', 'dry-run'],
    default: 'running',
  })
  status: string;
  @Prop({ default: false }) isDryRun: boolean;
  @Prop({ default: 0 }) totalEmployees: number;
  @Prop({ default: 0 }) processedCount: number;
  @Prop({ default: 0 }) failedCount: number;
  @Prop() completedAt: Date;
  @Prop() triggeredBy: string;
}
 
export const LeaveCarryForwardExecutionSchema = SchemaFactory.createForClass(
  LeaveCarryForwardExecution,
);
LeaveCarryForwardExecutionSchema.index({
  tenantId: 1,
  fromPeriodId: 1,
  toPeriodId: 1,
});
LeaveCarryForwardExecutionSchema.index(
  { tenantId: 1, idempotencyKey: 1 },
  { unique: true },
);
 
// ─────────────────────────────────────────
// Leave Carry Forward Item
// ─────────────────────────────────────────
export type LeaveCarryForwardItemDocument = LeaveCarryForwardItem & Document;
 
@Schema({ collection: 'leave_carry_forward_items', timestamps: true })
export class LeaveCarryForwardItem {
  @Prop({ required: true }) tenantId: string;
  @Prop({ required: true }) executionId: string;
  @Prop({ required: true }) employeeId: string;
  @Prop({ required: true }) leaveTypeId: string;
  @Prop({ required: true }) carryForwardQuantity: number;
  @Prop({ required: true }) unit: string;
  @Prop() expiresAt: Date;
  @Prop() ledgerId: string;
  @Prop({ enum: ['success', 'failed', 'skipped', 'dry-run'] }) status: string;
  @Prop() errorMessage: string;
}
 
export const LeaveCarryForwardItemSchema = SchemaFactory.createForClass(
  LeaveCarryForwardItem,
);
LeaveCarryForwardItemSchema.index({ tenantId: 1, executionId: 1 });
LeaveCarryForwardItemSchema.index({
  tenantId: 1,
  employeeId: 1,
  leaveTypeId: 1,
});