All files / src/domains/hr/attendance/schemas device-integration.schema.ts

0% Statements 0/53
0% Branches 0/8
100% Functions 0/0
0% Lines 0/45

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                                                                                                                                                                                                                                                                                                       
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
@Schema({ timestamps: true, collection: 'attendance_devices' })
export class AttendanceDevice extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({ required: true })
  deviceCode: string;
 
  @Prop({ required: true })
  deviceName: string;
 
  @Prop({ required: true })
  providerKey: string; // e.g., 'zkt', 'matrix', 'mock', 'webhook'
 
  @Prop()
  ipAddress: string;
 
  @Prop()
  macAddress: string;
 
  @Prop()
  branchId: string;
 
  @Prop()
  locationId: string;
 
  @Prop({ type: MongooseSchema.Types.Mixed })
  connectionConfig: any; // e.g., credentials, port
 
  @Prop()
  timezone: string;
 
  @Prop({ default: 'active' })
  status: string; // active, inactive, offline, error
 
  @Prop({ default: null })
  lastSuccessfulSync: Date;
}
export const AttendanceDeviceSchema =
  SchemaFactory.createForClass(AttendanceDevice);
AttendanceDeviceSchema.index({ tenantId: 1, deviceCode: 1 }, { unique: true });
 
@Schema({ timestamps: true, collection: 'attendance_device_employee_maps' })
export class AttendanceDeviceEmployeeMap extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({
    required: true,
    type: MongooseSchema.Types.ObjectId,
    ref: 'AttendanceDevice',
  })
  deviceId: string;
 
  @Prop({ required: true })
  employeeId: string;
 
  @Prop({ required: true })
  deviceEmployeeId: string; // The ID used within the biometric device
}
export const AttendanceDeviceEmployeeMapSchema = SchemaFactory.createForClass(
  AttendanceDeviceEmployeeMap,
);
AttendanceDeviceEmployeeMapSchema.index(
  { tenantId: 1, deviceId: 1, employeeId: 1 },
  { unique: true },
);
 
@Schema({
  timestamps: { createdAt: true, updatedAt: false },
  collection: 'attendance_device_logs',
})
export class AttendanceDeviceLog extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({
    required: true,
    type: MongooseSchema.Types.ObjectId,
    ref: 'AttendanceDevice',
  })
  deviceId: string;
 
  @Prop({ required: true })
  deviceEmployeeId: string;
 
  @Prop({ required: true })
  timestamp: Date;
 
  @Prop()
  punchTypeRaw: string; // In, Out, etc. raw from device
 
  @Prop({ default: false })
  processed: boolean; // True once converted to an AttendancePunch
 
  @Prop({
    type: MongooseSchema.Types.ObjectId,
    ref: 'AttendancePunch',
    default: null,
  })
  punchId: string;
 
  @Prop({ type: MongooseSchema.Types.Mixed })
  rawData: any;
}
export const AttendanceDeviceLogSchema =
  SchemaFactory.createForClass(AttendanceDeviceLog);
AttendanceDeviceLogSchema.index(
  { tenantId: 1, deviceId: 1, deviceEmployeeId: 1, timestamp: -1 },
  { unique: true },
);
 
@Schema({ timestamps: true, collection: 'attendance_device_syncs' })
export class AttendanceDeviceSync extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({
    required: true,
    type: MongooseSchema.Types.ObjectId,
    ref: 'AttendanceDevice',
  })
  deviceId: string;
 
  @Prop({ required: true })
  syncType: string; // Pull, PushWebhook
 
  @Prop({ required: true })
  status: string; // success, partial, failed
 
  @Prop({ default: 0 })
  recordsFetched: number;
 
  @Prop({ default: 0 })
  recordsProcessed: number;
 
  @Prop()
  errorMessage: string;
 
  @Prop({ default: null })
  syncCursor: string; // ID or timestamp to resume from
}
export const AttendanceDeviceSyncSchema =
  SchemaFactory.createForClass(AttendanceDeviceSync);