All files / src/domains/mdm/infrastructure/schemas status-reason.schema.ts

0% Statements 0/79
0% Branches 0/25
100% Functions 0/0
0% Lines 0/71

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                                                                                                                                                                                                                                                                                               
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
// ════════════════════════════════════════════════════════════════
// STATUS DEFINITION
// Master list of statuses per entity type
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_status_definitions', timestamps: true })
export class StatusDefinition extends Document {
  @Prop({ required: true }) statusCode!: string; // ACTIVE, INACTIVE, PENDING, ON_HOLD
  @Prop({ required: true }) statusName!: string;
  @Prop() description?: string;
  @Prop({ required: true }) entityTypeKey!: string; // employee, lead, leave_request, project
  @Prop({
    required: true,
    enum: ['initial', 'intermediate', 'terminal', 'paused'],
    default: 'intermediate',
  })
  statusClass!: string;
  @Prop({ type: [String], default: [] }) allowedTransitionsTo!: string[]; // valid next statuses
  @Prop() displayColor?: string; // hex color for UI
  @Prop() displayIcon?: string; // icon key
  @Prop({ default: false }) requiresReason!: boolean;
  @Prop({ default: false }) requiresApproval!: boolean;
  @Prop({ default: false }) notifyOnEntry!: boolean;
  @Prop({ default: 0 }) sortOrder!: number;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId; // null = global
  @Prop({ default: false }) systemManaged!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const StatusDefinitionSchema =
  SchemaFactory.createForClass(StatusDefinition);
StatusDefinitionSchema.index(
  { statusCode: 1, entityTypeKey: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
StatusDefinitionSchema.index({ entityTypeKey: 1, active: 1 });
StatusDefinitionSchema.index({ entityTypeKey: 1, statusClass: 1 });
 
// ════════════════════════════════════════════════════════════════
// PRIORITY DEFINITION
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_priority_definitions', timestamps: true })
export class PriorityDefinition extends Document {
  @Prop({ required: true }) priorityCode!: string; // CRITICAL, HIGH, MEDIUM, LOW
  @Prop({ required: true }) priorityName!: string;
  @Prop() description?: string;
  @Prop({ required: true }) entityTypeKey!: string; // task, helpdesk_ticket, approval_request
  @Prop({ required: true }) priorityLevel!: number; // 1 = highest, ascending
  @Prop() displayColor?: string;
  @Prop() displayIcon?: string;
  @Prop() slaHours?: number; // target resolution SLA
  @Prop() escalationHours?: number;
  @Prop({ default: 0 }) sortOrder!: number;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: false }) systemManaged!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const PriorityDefinitionSchema =
  SchemaFactory.createForClass(PriorityDefinition);
PriorityDefinitionSchema.index(
  { priorityCode: 1, entityTypeKey: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
PriorityDefinitionSchema.index({
  entityTypeKey: 1,
  priorityLevel: 1,
  active: 1,
});
 
// ════════════════════════════════════════════════════════════════
// REASON GROUP  (groups related reasons, e.g. LEAVE_CANCELLATION)
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_reason_groups', timestamps: true })
export class ReasonGroup extends Document {
  @Prop({ required: true }) groupCode!: string; // LEAVE_CANCELLATION, LEAD_LOST, REJECTION
  @Prop({ required: true }) groupName!: string;
  @Prop() description?: string;
  @Prop({ required: true }) entityTypeKey!: string; // which domain uses this group
  @Prop({
    required: true,
    enum: [
      'cancellation',
      'rejection',
      'loss',
      'resignation',
      'termination',
      'hold',
      'exception',
      'other',
    ],
  })
  groupCategory!: string;
  @Prop({ default: false }) requiresComment!: boolean; // must provide free text too
  @Prop({ default: false }) requiresDocument!: boolean;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: false }) systemManaged!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const ReasonGroupSchema = SchemaFactory.createForClass(ReasonGroup);
ReasonGroupSchema.index(
  { groupCode: 1, entityTypeKey: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
ReasonGroupSchema.index({ entityTypeKey: 1, groupCategory: 1, active: 1 });
 
// ════════════════════════════════════════════════════════════════
// REASON DEFINITION
// Individual reason within a group
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_reason_definitions', timestamps: true })
export class ReasonDefinition extends Document {
  @Prop({ required: true }) reasonCode!: string; // BETTER_OFFER, RELOCATION, PERSONAL_REASONS
  @Prop({ required: true }) reasonName!: string;
  @Prop() description?: string;
  @Prop({ required: true }) groupCode!: string;
  @Prop({ required: true }) entityTypeKey!: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  parentReasonId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: false }) requiresComment!: boolean; // overrides group setting
  @Prop({ default: false }) requiresDocument!: boolean;
  @Prop({ default: 0 }) sortOrder!: number;
  @Prop() displayColor?: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: false }) systemManaged!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const ReasonDefinitionSchema =
  SchemaFactory.createForClass(ReasonDefinition);
ReasonDefinitionSchema.index(
  { reasonCode: 1, groupCode: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
ReasonDefinitionSchema.index({ groupCode: 1, active: 1, sortOrder: 1 });
ReasonDefinitionSchema.index({ entityTypeKey: 1, active: 1 });