From c72e1bf3c997875811afe327de935062389cd01e Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Tue, 29 Apr 2025 17:54:32 +0700 Subject: [PATCH] noti employee --- src/controllers/CommandController.ts | 590 ++++++++++++------------ src/controllers/ImportDataController.ts | 116 ++--- 2 files changed, 341 insertions(+), 365 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index b6af2bfd..e5232e4d 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1742,17 +1742,18 @@ export class CommandController extends Controller { return { no: Extension.ToThaiNumber((idx + 1).toString()), - org: commandCode != "C-PM-21" - ? (_child4 == null ? "" : _child4 + "\n") + - (_child3 == null ? "" : _child3 + "\n") + - (_child2 == null ? "" : _child2 + "\n") + - (_child1 == null ? "" : _child1 + "\n") + - (_root == null ? "" : _root) - : (profile?.child4Temp == null ? "" : profile?.child4Temp + "\n") + - (profile?.child3Temp == null ? "" : profile?.child3Temp + "\n") + - (profile?.child2Temp == null ? "" : profile?.child2Temp + "\n") + - (profile?.child1Temp == null ? "" : profile?.child1Temp + "\n") + - (profile?.rootTemp == null ? "" : profile?.rootTemp), + org: + commandCode != "C-PM-21" + ? (_child4 == null ? "" : _child4 + "\n") + + (_child3 == null ? "" : _child3 + "\n") + + (_child2 == null ? "" : _child2 + "\n") + + (_child1 == null ? "" : _child1 + "\n") + + (_root == null ? "" : _root) + : (profile?.child4Temp == null ? "" : profile?.child4Temp + "\n") + + (profile?.child3Temp == null ? "" : profile?.child3Temp + "\n") + + (profile?.child2Temp == null ? "" : profile?.child2Temp + "\n") + + (profile?.child1Temp == null ? "" : profile?.child1Temp + "\n") + + (profile?.rootTemp == null ? "" : profile?.rootTemp), fullName: `${x.prefix}${x.firstName} ${x.lastName}`, citizenId: Extension.ToThaiNumber(x.citizenId), position: profile?.position ? profile?.position : "-", @@ -1762,24 +1763,27 @@ export class CommandController extends Controller { `${profile?.posType.posTypeShortName} ${profile?.posLevel.posLevelName}`, ) : "-", - posNo: commandCode != "C-PM-21" - ? shortName - ? Extension.ToThaiNumber(shortName) - : "-" - : Extension.ToThaiNumber(`${profile?.rootShortNameTemp}${profile?.posMasterNoTemp}`) , + posNo: + commandCode != "C-PM-21" + ? shortName + ? Extension.ToThaiNumber(shortName) + : "-" + : Extension.ToThaiNumber( + `${profile?.rootShortNameTemp}${profile?.posMasterNoTemp}`, + ), amount: x.amount ? Extension.ToThaiNumber(x.amount.toLocaleString()) : "-", - dateRetire: profile?.dateRetire - ? Extension.ToThaiNumber( - Extension.ToThaiShortDate_monthYear(profile?.dateRetire) - ) + dateRetire: profile?.dateRetire + ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile?.dateRetire)) : profile?.birthDate && commandCode == "C-PM-21" ? Extension.ToThaiNumber( - Extension.ToThaiShortDate_monthYear( - new Date( - profile.birthDate.getFullYear() + 60, profile.birthDate.getMonth(), profile.birthDate.getDate() - ) + Extension.ToThaiShortDate_monthYear( + new Date( + profile.birthDate.getFullYear() + 60, + profile.birthDate.getMonth(), + profile.birthDate.getDate(), + ), + ), ) - ) : "-", dateExecute: command.commandExcecuteDate ? Extension.ToThaiNumber( @@ -2421,12 +2425,10 @@ export class CommandController extends Controller { "C-PM-22", "C-PM-23", "C-PM-24", - "C-PM-34", "C-PM-36", "C-PM-37", - "C-PM-37", - "C-PM-37", - "C-PM-37", + "C-PM-42", + "C-PM-43", ].includes(commandCode) ) { let posMaster = await this.employeePosMasterRepository.find({ @@ -2953,10 +2955,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -2965,35 +2967,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -3129,10 +3131,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -3141,35 +3143,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -3324,10 +3326,10 @@ export class CommandController extends Controller { const roleKeycloak = await this.roleKeycloakRepo.findOne({ where: { name: Like("USER") }, }); - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -3336,35 +3338,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -3541,10 +3543,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -3553,35 +3555,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -3702,10 +3704,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -3714,35 +3716,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -4011,10 +4013,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -4023,35 +4025,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -4492,10 +4494,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -4504,35 +4506,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -4764,10 +4766,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -4776,35 +4778,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -4968,10 +4970,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.data.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -4980,35 +4982,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -5137,7 +5139,7 @@ export class CommandController extends Controller { commandName: item.commandName, remark: item.remark, posNumCodeSit: _posNumCodeSit, - posNumCodeSitAbb: _posNumCodeSitAbb + posNumCodeSitAbb: _posNumCodeSitAbb, }); await removeProfileInOrganize(profile.id, "OFFICER"); const clearProfile = await checkCommandType(String(item.commandId)); @@ -5219,10 +5221,10 @@ export class CommandController extends Controller { const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); const _null: any = null; - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.data.find(x => x.bodySalarys?.commandId)?.bodySalarys?.commandId ?? "" } + where: { id: body.data.find((x) => x.bodySalarys?.commandId)?.bodySalarys?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -5231,35 +5233,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -5724,10 +5726,10 @@ export class CommandController extends Controller { const roleKeycloak = await this.roleKeycloakRepo.findOne({ where: { name: Like("USER") }, }); - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.refIds.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.refIds.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -5736,35 +5738,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( @@ -6274,10 +6276,10 @@ export class CommandController extends Controller { }[]; }, ) { - let _posNumCodeSit: string = "" - let _posNumCodeSitAbb: string = "" + let _posNumCodeSit: string = ""; + let _posNumCodeSitAbb: string = ""; const _command = await this.commandRepository.findOne({ - where: { id: body.refIds.find(x => x.commandId)?.commandId ?? "" } + where: { id: body.refIds.find((x) => x.commandId)?.commandId ?? "" }, }); if (_command) { if (_command?.isBangkok?.toLocaleUpperCase() == "OFFICE") { @@ -6286,35 +6288,35 @@ export class CommandController extends Controller { isDeputy: true, orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } + orgRevisionIsDraft: false, + }, }, - relations: ["orgRevision"] - }) + relations: ["orgRevision"], + }); _posNumCodeSit = orgRootDeputy ? orgRootDeputy?.orgRootName : "สำนักปลัดกรุงเทพมหานคร"; _posNumCodeSitAbb = orgRootDeputy ? orgRootDeputy?.orgRootShortName : "สนป."; - } - else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { - _posNumCodeSit = "กรุงเทพมหานคร" - _posNumCodeSitAbb = "กทม." - } - else { + } else if (_command?.isBangkok?.toLocaleUpperCase() == "BANGKOK") { + _posNumCodeSit = "กรุงเทพมหานคร"; + _posNumCodeSitAbb = "กทม."; + } else { let _profileAdmin = await this.profileRepository.findOne({ - where: { + where: { keycloak: _command?.createdUserId.toString(), current_holders: { orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } + orgRevisionIsDraft: false, + }, + }, }, - relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"] + relations: ["current_holders", "current_holders.orgRevision", "current_holders.orgRoot"], }); - _posNumCodeSit = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? "" - _posNumCodeSitAbb = _profileAdmin?.current_holders - .find(x => x.orgRoot.orgRootShortName)?.orgRoot.orgRootShortName ?? "" + _posNumCodeSit = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootName)?.orgRoot.orgRootName ?? + ""; + _posNumCodeSitAbb = + _profileAdmin?.current_holders.find((x) => x.orgRoot.orgRootShortName)?.orgRoot + .orgRootShortName ?? ""; } } await Promise.all( diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index 9acea89f..f1c1ead8 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -435,31 +435,38 @@ export class ImportDataController extends Controller { // ]), // }) .leftJoinAndSelect("profile.profileSalary", "profileSalary") - // .where({citizenId: "1720700018869"}) + // .where({ citizenId: "3101702379675" }) .where("profileSalary.id IS NULL") // .skip(0) // .take(10000) .getManyAndCount(); - var _profiles: ProfileSalary[] = []; - const filePath = path.join(__dirname, "salaryProfile1.csv"); - // CSV Header - let csvData = `"id","createdAt","createdUserId","lastUpdatedAt","lastUpdateUserId","createdFullName","lastUpdateFullName","profileId","profileEmployeeId","order","commandNo","commandYear","commandDateSign","commandDateAffect","commandCode","commandName","posNoAbb","posNo","positionName","positionType","positionLevel","positionCee","orgRoot","orgChild1","orgChild2","orgChild3","orgChild4","positionExecutive","amount","amountSpecial","positionSalaryAmount","mouthSalaryAmount","remark","dateGovernment","isGovernment","commandId","refId"\n`; - - fs.appendFile(filePath, csvData, (err) => { - if (err) { - console.error("Error writing CSV file:", err); - } else { - console.log("Salary profiles successfully written to salaryProfile.csv"); - } - }); for await (const _item of profiles) { + // ดึงข้อมูลมาโดยไม่ใส่ order const existingProfile = await this.HR_POSITION_OFFICERRepo.find({ where: { CIT: _item.citizenId, FLAG_PERSON_TYPE: "1" }, - order: { - MP_POS_DATE: "ASC", - ORDER_MOVE_POSITION: "ASC", - }, }); + + // sort ด้วย JavaScript + existingProfile.sort((a, b) => { + let dateA = new Date().getTime(); + let dateB = new Date().getTime(); + + if (a.MP_POS_DATE) { + const [datePart] = a.MP_POS_DATE.split(" "); + const [day, month, year] = datePart.split("/"); + dateA = new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`).getTime(); + } + if (b.MP_POS_DATE) { + const [datePart] = b.MP_POS_DATE.split(" "); + const [day, month, year] = datePart.split("/"); + dateB = new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`).getTime(); + } + if (dateA !== dateB) { + return dateA - dateB; // ASC + } + return a.ORDER_MOVE_POSITION - b.ORDER_MOVE_POSITION; // ASC + }); + let order = 1; for await (const item of existingProfile) { rowCount++; @@ -851,38 +858,8 @@ export class ImportDataController extends Controller { profileSalary.lastUpdateFullName = request.user.name; profileSalary.createdAt = new Date().toISOString().split("T")[0]; profileSalary.lastUpdatedAt = new Date().toISOString().split("T")[0]; - // const result = uuidv7(); - // profileSalary.id = result; console.log(">>>>>>>>>>>>>>>>>>>" + rowCount); - // // Generate SQL INSERT Statement using TypeORM QueryBuilder but don't execute it - // const queryBuilder = AppDataSource.createQueryBuilder() - // .insert() - // .into(ProfileSalary) - // .values(profileSalary); - - // const sql = queryBuilder.getSql(); - // sqlStatements.push(sql); - // _profiles.push(profileSalary); - // save to file - // Define the output file path - - // CSV Header - // let csvData = `"id","createdAt","createdUserId","lastUpdatedAt","lastUpdateUserId","createdFullName","lastUpdateFullName","profileId","profileEmployeeId","order","commandNo","commandYear","commandDateSign","commandDateAffect","commandCode","commandName","posNoAbb","posNo","positionName","positionType","positionLevel","positionCee","orgRoot","orgChild1","orgChild2","orgChild3","orgChild4","positionExecutive","amount","amountSpecial","positionSalaryAmount","mouthSalaryAmount","remark","dateGovernment","isGovernment","commandId"\n`; - - // Loop through each salary profile and format data as CSV - // _profiles.forEach((profile) => { - // csvData = `"${profileSalary.id || "NULL"}","${profileSalary.createdAt || "NULL"}","${profileSalary.createdUserId || "NULL"}","${profileSalary.lastUpdatedAt || "NULL"}","${profileSalary.lastUpdateUserId || "NULL"}","${profileSalary.createdFullName || "NULL"}","${profileSalary.lastUpdateFullName || "NULL"}","${profileSalary.profileId || "NULL"}","${profileSalary.profileEmployeeId || "NULL"}","${profileSalary.order || "NULL"}","${profileSalary.commandNo || "NULL"}","${profileSalary.commandYear || "NULL"}","${profileSalary.commandDateSign || "NULL"}","${profileSalary.commandDateAffect || "NULL"}","${profileSalary.commandCode || "NULL"}","${profileSalary.commandName || "NULL"}","${profileSalary.posNoAbb || "NULL"}","${profileSalary.posNo || "NULL"}","${profileSalary.positionName || "NULL"}","${profileSalary.positionType || "NULL"}","${profileSalary.positionLevel || "NULL"}","${profileSalary.positionCee || "NULL"}","${profileSalary.orgRoot || "NULL"}","${profileSalary.orgChild1 || "NULL"}","${profileSalary.orgChild2 || "NULL"}","${profileSalary.orgChild3 || "NULL"}","${profileSalary.orgChild4 || "NULL"}","${profileSalary.positionExecutive || "NULL"}","${profileSalary.amount || 0}","${profileSalary.amountSpecial || 0}","${profileSalary.positionSalaryAmount || 0}","${profileSalary.mouthSalaryAmount || 0}","${profileSalary.remark || "NULL"}","${profileSalary.dateGovernment || "NULL"}","${profileSalary.isGovernment || "NULL"}","${profileSalary.commandId || "NULL"}","${profileSalary.refId || "NULL"}"\n`; - // // }); - - // // Write the CSV data to the file - // fs.appendFile(filePath, csvData.replace('"NULL"', "NULL"), (err) => { - // if (err) { - // console.error("Error writing CSV file:", err); - // } else { - // console.log("Salary profiles successfully written to salaryProfile.csv: " + rowCount); - // } - // }); try { await this.salaryRepo.save(profileSalary); } catch (error) { @@ -891,26 +868,6 @@ export class ImportDataController extends Controller { } order = 1; } - // // console.log(rowCount); - // // Define the output file path - // const filePath = path.join(__dirname, "salaryProfile.csv"); - - // // CSV Header - // let csvData = `"id","createdAt","createdUserId","lastUpdatedAt","lastUpdateUserId","createdFullName","lastUpdateFullName","profileId","profileEmployeeId","order","commandNo","commandYear","commandDateSign","commandDateAffect","commandCode","commandName","posNoAbb","posNo","positionName","positionType","positionLevel","positionCee","orgRoot","orgChild1","orgChild2","orgChild3","orgChild4","positionExecutive","amount","amountSpecial","positionSalaryAmount","mouthSalaryAmount","remark","dateGovernment","isGovernment","commandId"\n`; - - // // Loop through each salary profile and format data as CSV - // _profiles.forEach((profile) => { - // csvData += `"${profile.id || "NULL"}","${profile.createdAt?.toISOString()?.split("T")[0] || "NULL"}","${profile.createdUserId || "NULL"}","${profile.lastUpdatedAt?.toISOString()?.split("T")[0] || "NULL"}","${profile.lastUpdateUserId || "NULL"}","${profile.createdFullName || "NULL"}","${profile.lastUpdateFullName || "NULL"}","${profile.profileId || "NULL"}","${profile.profileEmployeeId || "NULL"}","${profile.order || "NULL"}","${profile.commandNo || "NULL"}","${profile.commandYear || "NULL"}","${profile.commandDateSign?.toISOString()?.split("T")[0] || "NULL"}","${profile.commandDateAffect?.toISOString()?.split("T")[0] || "NULL"}","${profile.commandCode || "NULL"}","${profile.commandName || "NULL"}","${profile.posNoAbb || "NULL"}","${profile.posNo || "NULL"}","${profile.positionName || "NULL"}","${profile.positionType || "NULL"}","${profile.positionLevel || "NULL"}","${profile.positionCee || "NULL"}","${profile.orgRoot || "NULL"}","${profile.orgChild1 || "NULL"}","${profile.orgChild2 || "NULL"}","${profile.orgChild3 || "NULL"}","${profile.orgChild4 || "NULL"}","${profile.positionExecutive || "NULL"}","${profile.amount || 0}","${profile.amountSpecial || 0}","${profile.positionSalaryAmount || 0}","${profile.mouthSalaryAmount || 0}","${profile.remark || "NULL"}","${profile.dateGovernment?.toISOString()?.split("T")[0] || "NULL"}","${profile.isGovernment || "NULL"}","${profile.commandId || "NULL"}"\n`; - // }); - - // // Write the CSV data to the file - // fs.writeFile(filePath, csvData, (err) => { - // if (err) { - // console.error("Error writing CSV file:", err); - // } else { - // console.log("Salary profiles successfully written to salaryProfile.csv"); - // } - // }); return new HttpSuccess(); } @@ -965,10 +922,27 @@ export class ImportDataController extends Controller { for await (const _item of profiles) { const existingProfile = await this.HR_POSITION_EMPLOYEERepo.find({ where: { CIT: _item.citizenId, FLAG_PERSON_TYPE: "6" }, - order: { - MP_POS_DATE: "ASC", - ORDER_MOVE_POSITION: "ASC", - }, + }); + + // sort ด้วย JavaScript + existingProfile.sort((a, b) => { + let dateA = new Date().getTime(); + let dateB = new Date().getTime(); + + if (a.MP_POS_DATE) { + const [datePart] = a.MP_POS_DATE.split(" "); + const [day, month, year] = datePart.split("/"); + dateA = new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`).getTime(); + } + if (b.MP_POS_DATE) { + const [datePart] = b.MP_POS_DATE.split(" "); + const [day, month, year] = datePart.split("/"); + dateB = new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`).getTime(); + } + if (dateA !== dateB) { + return dateA - dateB; // ASC + } + return a.ORDER_MOVE_POSITION - b.ORDER_MOVE_POSITION; // ASC }); let order = 1; for await (const item of existingProfile) {