fix: #2239
All checks were successful
Build & Deploy on Dev / build (push) Successful in 48s

This commit is contained in:
Adisak 2026-02-06 14:55:59 +07:00
parent 1696890f74
commit 8a649086f7
3 changed files with 242 additions and 164 deletions

View file

@ -33,7 +33,7 @@ import { PosMaster } from "../entities/PosMaster";
import { Profile } from "../entities/Profile";
import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission";
import { checkQueueInProgress, setLogDataDiff } from "../interfaces/utils";
import { checkQueueInProgress, resolveNodeId, resolveNodeLevel, setLogDataDiff } from "../interfaces/utils";
import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
@ -5516,6 +5516,8 @@ export class OrganizationController extends Controller {
}
}
const orgDna = await new permission().checkDna(request, request.user.sub)
let level: any = resolveNodeLevel(orgDna);
const orgRootData = await AppDataSource.getRepository(OrgRoot)
.createQueryBuilder("orgRoot")
@ -5623,6 +5625,36 @@ export class OrganizationController extends Controller {
.getMany()
: [];
const cannotViewRootPosMaster =
(_privilege.privilege === "PARENT") ||
(_privilege.privilege === "BROTHER" && level > 1) ||
(_privilege.privilege === "CHILD" && level != 0) ||
(_privilege.privilege === "NORMAL" && level != 0);
const cannotViewChild1PosMaster =
(_privilege.privilege === "PARENT" && level > 1) ||
(_privilege.privilege === "BROTHER" && level > 2) ||
(_privilege.privilege === "CHILD" && level !== 1) ||
(_privilege.privilege === "NORMAL" && level !== 1);
const cannotViewChild2PosMaster =
(_privilege.privilege === "PARENT" && level > 2) ||
(_privilege.privilege === "BROTHER" && level > 3) ||
(_privilege.privilege === "CHILD" && level !== 2) ||
(_privilege.privilege === "NORMAL" && level !== 2);
const cannotViewChild3PosMaster =
(_privilege.privilege === "PARENT" && level > 3) ||
(_privilege.privilege === "BROTHER" && level > 4) ||
(_privilege.privilege === "CHILD" && level !== 3) ||
(_privilege.privilege === "NORMAL" && level !== 3);
const cannotViewChild4PosMaster =
(_privilege.privilege === "PARENT" && level > 4) ||
(_privilege.privilege === "CHILD" && level !== 4) ||
(_privilege.privilege === "NORMAL" && level !== 4);
// const formattedData = orgRootData.map((orgRoot) => {
const formattedData = await Promise.all(
orgRootData.map(async (orgRoot) => {
@ -5637,27 +5669,29 @@ export class OrganizationController extends Controller {
orgRootName: orgRoot.orgRootName,
labelName:
orgRoot.orgRootName + " " + orgRoot.orgRootCode + "00" + " " + orgRoot.orgRootShortName,
posMaster: await Promise.all(
orgRoot.posMasters
.filter(
(x) =>
x.orgChild1Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgRoot.orgRootShortName} ${x.posMasterNo}`,
orgTreeId: orgRoot.id,
orgLevel: 0,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
posMaster:
cannotViewRootPosMaster ? [] :
await Promise.all(
orgRoot.posMasters
.filter(
(x) =>
x.orgChild1Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgRoot.orgRootShortName} ${x.posMasterNo}`,
orgTreeId: orgRoot.id,
orgLevel: 0,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
children: await Promise.all(
orgChild1Data
.filter((orgChild1) => orgChild1.orgRootId === orgRoot.id)
@ -5685,27 +5719,29 @@ export class OrganizationController extends Controller {
"00" +
" " +
orgRoot.orgRootShortName,
posMaster: await Promise.all(
orgChild1.posMasters
.filter(
(x) =>
x.orgChild2Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild1.orgChild1ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild1.id,
orgLevel: 1,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
posMaster:
cannotViewChild1PosMaster ? [] :
await Promise.all(
orgChild1.posMasters
.filter(
(x) =>
x.orgChild2Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild1.orgChild1ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild1.id,
orgLevel: 1,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
children: await Promise.all(
orgChild2Data
@ -5741,27 +5777,29 @@ export class OrganizationController extends Controller {
"00" +
" " +
orgRoot.orgRootShortName,
posMaster: await Promise.all(
orgChild2.posMasters
.filter(
(x) =>
x.orgChild3Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild2.orgChild2ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild2.id,
orgLevel: 2,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
posMaster:
cannotViewChild2PosMaster ? [] :
await Promise.all(
orgChild2.posMasters
.filter(
(x) =>
x.orgChild3Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild2.orgChild2ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild2.id,
orgLevel: 2,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
children: await Promise.all(
orgChild3Data
@ -5804,27 +5842,29 @@ export class OrganizationController extends Controller {
"00" +
" " +
orgRoot.orgRootShortName,
posMaster: await Promise.all(
orgChild3.posMasters
.filter(
(x) =>
x.orgChild4Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild3.orgChild3ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild3.id,
orgLevel: 3,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
posMaster:
cannotViewChild3PosMaster ? [] :
await Promise.all(
orgChild3.posMasters
.filter(
(x) =>
x.orgChild4Id == null &&
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild3.orgChild3ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild3.id,
orgLevel: 3,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
children: await Promise.all(
orgChild4Data
@ -5874,26 +5914,28 @@ export class OrganizationController extends Controller {
"00" +
" " +
orgRoot.orgRootShortName,
posMaster: await Promise.all(
orgChild4.posMasters
.filter(
(x) =>
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild4.orgChild4ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild4.id,
orgLevel: 4,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
posMaster:
cannotViewChild4PosMaster ? [] :
await Promise.all(
orgChild4.posMasters
.filter(
(x) =>
// x.current_holderId != null &&
x.isDirector === true,
)
// .sort((a, b) => a.posMasterOrder - b.posMasterOrder) // Sort by posMasterOrder ASC
// .slice(0, 3) // Select the first 3 rows
.map(async (x) => ({
posmasterId: x.id,
posNo: `${orgChild4.orgChild4ShortName} ${x.posMasterNo}`,
orgTreeId: orgChild4.id,
orgLevel: 4,
fullNameCurrentHolder:
x.current_holder == null
? null
: `${x.current_holder.prefix}${x.current_holder.firstName} ${x.current_holder.lastName}`,
})),
),
})),
),
})),

View file

@ -305,6 +305,22 @@ class CheckAuth {
public async PermissionOrgUserUpdate(req: RequestWithUser, system: string, profileId: string) {
return await this.PermissionOrgByUser(req, system, "UPDATE", profileId);
}
public async checkDna(request: RequestWithUser, keycloakId: any) {
try {
const result = await new CallAPI().GetData(
request,
`/org/finddna-by-keycloak/${keycloakId}`,
false
);
return result;
} catch (error) {
console.error("Error calling API:", error);
throw error;
}
}
}
export default CheckAuth;

View file

@ -72,7 +72,7 @@ export async function calculateGovAge(profileId: string, type: string, bkk?: boo
});
// console.log("endDateFristRec", endDateFristRec.dateGovernment);
const calculateDuration = (startDate: any, endDate: any , inclusive = false) => {
const calculateDuration = (startDate: any, endDate: any, inclusive = false) => {
if (inclusive) {
endDate = new Date(endDate.getTime() + 86400000); // +1 วัน
}
@ -107,9 +107,9 @@ export async function calculateGovAge(profileId: string, type: string, bkk?: boo
});
}
// const firstStartDate = new Date(records[0].date);
const firstStartDate = !bkk
? profile?.dateAppoint ? profile?.dateAppoint : new Date()
: profile?.dateStart ? profile?.dateStart : new Date() ;
const firstStartDate = !bkk
? profile?.dateAppoint ? profile?.dateAppoint : new Date()
: profile?.dateStart ? profile?.dateStart : new Date();
const firstEndDate = endDateFristRec ? new Date(endDateFristRec.dateGovernment) : new Date();
// console.log("firstStartDate1", firstStartDate);
@ -143,37 +143,37 @@ export async function calculateGovAge(profileId: string, type: string, bkk?: boo
for (let i = 0; i < records_middle.length; i++) {
const current = records_middle[i];
const next = records_middle[i + 1];
// นับเฉพาะช่วงที่เป็นราชการ
if (current.isGovernment === true) {
const startDate = new Date(current.dateGovernment);
const endDate = next ? new Date(next.dateGovernment) : new Date();
const { years, months, days } = calculateDuration(startDate, endDate);
totalYears2 += years;
totalMonths2 += months;
totalDays2 += days;
// console.log(`✔ นับช่วง ${startDate.toISOString()} → ${endDate.toISOString()} ได้ ${years} ปี ${months} เดือน ${days} วัน`);
}
}
// else {
// console.log(`❌ ไม่รวมช่วง ${current.dateGovernment} เพราะ isGovernment = false`);
// }
}
//ตั้งแต่วันที่กลับมารับราขการไปจนถึงรวมถึงวันที่ปัจจุบัน
const adjustTotal = (years:number, months:number, days:number) => {
const adjustTotal = (years: number, months: number, days: number) => {
const daysInThisMonth = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
if (days >= daysInThisMonth) {
months += Math.floor(days / daysInThisMonth);
days %= daysInThisMonth;
}
if (months >= 12) {
years += Math.floor(months / 12);
months %= 12;
}
return { years, months, days };
};
@ -192,10 +192,10 @@ export async function calculateGovAge(profileId: string, type: string, bkk?: boo
const finalAdjusted = adjustTotal(sumYears, sumMonths, sumDays);
return {
year: finalAdjusted.years,
month: finalAdjusted.months,
day: finalAdjusted.days,
};
year: finalAdjusted.years,
month: finalAdjusted.months,
day: finalAdjusted.days,
};
}
export function calculateRetireDate(birthDate: Date) {
@ -262,11 +262,11 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
.getOne();
const draftRevision = await AppDataSource.getRepository(OrgRevision)
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false")
.getOne();
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false")
.getOne();
if (!currentRevision && !draftRevision) {
return;
}
@ -354,31 +354,31 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
}
export async function removePostMasterAct(profileId: string) {
const currentRevision = await AppDataSource.getRepository(OrgRevision)
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
const currentRevision = await AppDataSource.getRepository(OrgRevision)
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
if (!currentRevision) {
return;
}
if (!currentRevision) {
return;
}
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
.andWhere("posMaster.current_holderId = :profileId", { profileId })
.getOne();
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
.andWhere("posMaster.current_holderId = :profileId", { profileId })
.getOne();
if (!findProfileInposMaster) {
return;
}
if (!findProfileInposMaster) {
return;
}
const posMasterAct = await AppDataSource.getRepository(PosMasterAct)
.createQueryBuilder("posMasterAct")
.where("posMasterAct.posMasterChildId = :posMasterChildId", { posMasterChildId: findProfileInposMaster.id })
.getMany();
await AppDataSource.getRepository(PosMasterAct).remove(posMasterAct);
const posMasterAct = await AppDataSource.getRepository(PosMasterAct)
.createQueryBuilder("posMasterAct")
.where("posMasterAct.posMasterChildId = :posMasterChildId", { posMasterChildId: findProfileInposMaster.id })
.getMany();
await AppDataSource.getRepository(PosMasterAct).remove(posMasterAct);
}
export async function checkReturnCommandType(commandId: string) {
@ -567,22 +567,22 @@ export function editLogSequence(req: RequestWithUser, index: number, data: LogSe
export async function checkQueueInProgress(queueName: string) {
const axios = require('axios');
// console.log("Checking queue in progress");
const res = await axios.get(`${process.env.RABBIT_API_URL}/api/queues/%2F/${queueName}`, {
auth: { username: process.env.RABBIT_USER , password: process.env.RABBIT_PASS },
});
// console.log("Checking queue in progress");
const res = await axios.get(`${process.env.RABBIT_API_URL}/api/queues/%2F/${queueName}`, {
auth: { username: process.env.RABBIT_USER, password: process.env.RABBIT_PASS },
});
const q = res.data;
const q = res.data;
// console.log(`Queue "${queueName}" has:`);
// console.log(` - ${q.messages_ready} messages ready`);
// console.log(` - ${q.messages_unacknowledged} messages in progress (unacked)`);
// console.log(`Queue "${queueName}" has:`);
// console.log(` - ${q.messages_ready} messages ready`);
// console.log(` - ${q.messages_unacknowledged} messages in progress (unacked)`);
if (q.messages_unacknowledged > 0) {
return true;
}
if (q.messages_unacknowledged > 0) {
return true;
}
return false;
return false;
}
export function chunkArray(array: any, size: number) {
@ -594,7 +594,7 @@ export function chunkArray(array: any, size: number) {
}
export async function PayloadSendNoti(commandId: string) {
if (!commandId)
if (!commandId)
return "";
const commandRepository = AppDataSource.getRepository(Command);
const _command = await commandRepository.findOne({
@ -606,8 +606,8 @@ export async function PayloadSendNoti(commandId: string) {
if (!_command || !_command.commandType)
return "";
const _payload = {
name: _command && _command.commandType
? `คำสั่ง${_command.commandType.name}`
name: _command && _command.commandType
? `คำสั่ง${_command.commandType.name}`
: "",
url: `${process.env.API_URL}/salary/file/ระบบออกคำสั่ง/คำสั่ง/${commandId}/คำสั่ง`,
isReport: true,
@ -616,8 +616,8 @@ export async function PayloadSendNoti(commandId: string) {
let attachments = {}
if (_command.commandType.isUploadAttachment === true) {
const _payloadAtt = {
name: _command && _command.commandType
? `เอกสารแนบท้ายคำสั่ง${_command.commandType.name}`
name: _command && _command.commandType
? `เอกสารแนบท้ายคำสั่ง${_command.commandType.name}`
: "",
url: `${process.env.API_URL}/salary/file/ระบบออกคำสั่ง/แนบท้าย/${commandId}/แนบท้าย`,
isReport: true,
@ -733,3 +733,23 @@ export function commandTypePath(commandCode: string): string | null {
return null;
}
}
export function resolveNodeLevel(data: any) {
if (data.child4DnaId) return 4;
if (data.child3DnaId) return 3;
if (data.child2DnaId) return 2;
if (data.child1DnaId) return 1;
if (data.rootDnaId) return 0;
return null;
}
export function resolveNodeId(data: any) {
return (
data.child4DnaId ??
data.child3DnaId ??
data.child2DnaId ??
data.child1DnaId ??
data.rootDnaId ??
null
);
}