diff --git a/src/controllers/OrganizationUnauthorizeController.ts b/src/controllers/OrganizationUnauthorizeController.ts index c1baff3b..fa67daa0 100644 --- a/src/controllers/OrganizationUnauthorizeController.ts +++ b/src/controllers/OrganizationUnauthorizeController.ts @@ -25,6 +25,18 @@ import { EmployeePosDict } from "../entities/EmployeePosDict"; import { ProfileSalary } from "../entities/ProfileSalary"; import { ProfileInsignia } from "../entities/ProfileInsignia"; import { PosMasterHistory } from "../entities/PosMasterHistory"; +import { + ProfileAbsentLate, + CreateProfileAbsentLate, + CreateProfileAbsentLateBatch, +} from "../entities/ProfileAbsentLate"; +import { ProfileAbsentLateHistory } from "../entities/ProfileAbsentLateHistory"; +import { + ProfileEmployeeAbsentLate, + CreateProfileEmployeeAbsentLate, + CreateProfileEmployeeAbsentLateBatch, +} from "../entities/ProfileEmployeeAbsentLate"; +import { ProfileEmployeeAbsentLateHistory } from "../entities/ProfileEmployeeAbsentLateHistory"; @Route("api/v1/org/unauthorize") @Tags("OrganizationUnauthorize") @Response( @@ -47,6 +59,10 @@ export class OrganizationUnauthorizeController extends Controller { private salaryRepo = AppDataSource.getRepository(ProfileSalary); private posMasterHistoryRepository = AppDataSource.getRepository(PosMasterHistory); private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); + private absentLateRepo = AppDataSource.getRepository(ProfileAbsentLate); + private absentLateHistoryRepo = AppDataSource.getRepository(ProfileAbsentLateHistory); + private empAbsentLateRepo = AppDataSource.getRepository(ProfileEmployeeAbsentLate); + private empAbsentLateHistoryRepo = AppDataSource.getRepository(ProfileEmployeeAbsentLateHistory); @Post("user/reset-password") async forgetPassword( @Body() @@ -3432,4 +3448,110 @@ export class OrganizationUnauthorizeController extends Controller { return new HttpSuccess(profile_); } + + /** + * API สร้างข้อมูลการมาสาย/ขาดราชการของข้าราชการ (สำหรับ schedule) + * @summary API สร้างข้อมูลการมาสาย/ขาดราชการของข้าราชการ (สำหรับ Job schedule) + */ + @Post("profile/absent-late/batch") + async newAbsentLateBatch(@Body() body: CreateProfileAbsentLateBatch) { + // กรณีไม่มีข้อมูลส่งมา (วันที่ไม่มีคนขาด/มาสาย) + if (!body.records || body.records.length === 0) { + return new HttpSuccess({ count: 0, ids: [] }); + } + + const profileIds = [...new Set(body.records.map((r) => r.profileId))]; + const profiles = await this.profileRepo.findBy({ + id: In(profileIds), + }); + + const foundProfileIds = new Set(profiles.map((p) => p.id)); + const validRecords = body.records.filter((r) => foundProfileIds.has(r.profileId)); + + // กรณีไม่พบ profile เลย + if (validRecords.length === 0) { + return new HttpSuccess({ count: 0, ids: [] }); + } + + const meta = { + createdUserId: "SYSTEM", + createdFullName: "SYSTEM", + lastUpdateUserId: "SYSTEM", + lastUpdateFullName: "SYSTEM", + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + const records = validRecords.map((item) => { + const data = new ProfileAbsentLate(); + Object.assign(data, { ...item, ...meta }); + return data; + }); + + const result = await this.absentLateRepo.save(records); + + // บันทึก history สำหรับแต่ละ record + const historyRecords = result.map((data) => { + const history = new ProfileAbsentLateHistory(); + Object.assign(history, { ...data, id: undefined }); + history.profileAbsentLateId = data.id; + return history; + }); + await this.absentLateHistoryRepo.save(historyRecords); + + return new HttpSuccess({ count: result.length, ids: result.map((r) => r.id) }); + } + + /** + * API สร้างข้อมูลการมาสาย/ขาดราชการของลูกจ้างประจำ (สำหรับ schedule) + * @summary API สร้างข้อมูลการมาสาย/ขาดราชการของลูกจ้างประจำ (สำหรับ schedule) + */ + @Post("profile-employee/absent-late/batch") + async newEmpAbsentLateBatch(@Body() body: CreateProfileEmployeeAbsentLateBatch) { + // กรณีไม่มีข้อมูลส่งมา (วันที่ไม่มีคนขาด/มาสาย) + if (!body.records || body.records.length === 0) { + return new HttpSuccess({ count: 0, ids: [] }); + } + + const profileIds = [...new Set(body.records.map((r) => r.profileEmployeeId))]; + const profiles = await this.profileEmpRepo.findBy({ + id: In(profileIds), + }); + + const foundProfileIds = new Set(profiles.map((p) => p.id)); + const validRecords = body.records.filter((r) => foundProfileIds.has(r.profileEmployeeId)); + + // กรณีไม่พบ profile เลย + if (validRecords.length === 0) { + return new HttpSuccess({ count: 0, ids: [] }); + } + + const meta = { + createdUserId: "SYSTEM", + createdFullName: "SYSTEM", + lastUpdateUserId: "SYSTEM", + lastUpdateFullName: "SYSTEM", + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + const records = validRecords.map((item) => { + const data = new ProfileEmployeeAbsentLate(); + Object.assign(data, { ...item, ...meta }); + return data; + }); + + const result = await this.empAbsentLateRepo.save(records); + + // บันทึก history สำหรับแต่ละ record + const historyRecords = result.map((data) => { + const history = new ProfileEmployeeAbsentLateHistory(); + Object.assign(history, { ...data, id: undefined }); + history.profileEmployeeAbsentLateId = data.id; + return history; + }); + await this.empAbsentLateHistoryRepo.save(historyRecords); + + return new HttpSuccess({ count: result.length, ids: result.map((r) => r.id) }); + } }