แบบร่างลูกจ้าง

This commit is contained in:
kittapath 2025-02-26 17:05:13 +07:00
parent 5dab73c141
commit a064753efd
3 changed files with 757 additions and 2 deletions

View file

@ -8,6 +8,9 @@ import HttpStatusCode from "../interfaces/http-status";
import { RequestWithUser } from "../middlewares/user";
import { PosMaster } from "../entities/PosMaster";
import { Profile } from "../entities/Profile";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
import { ProfileEmployee } from "../entities/ProfileEmployee";
export let sendToQueue: (payload: any) => void;
export let sendToQueueOrg: (payload: any) => void;
@ -42,7 +45,7 @@ export async function init() {
console.log("[AMQ] Listening for message...");
createConsumer(queue, channel, handler), //----> (3) Process Consumer
createConsumer(queue_org, channel, handler_org);
createConsumer(queue_org, channel, handler_org);
// createConsumer(queue2, channel, handler2);
}
@ -125,7 +128,10 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
//----> condition before process consume
const repoPosmaster = AppDataSource.getRepository(PosMaster);
const repoEmployeePosmaster = AppDataSource.getRepository(EmployeePosMaster);
const repoEmployeeTempPosmaster = AppDataSource.getRepository(EmployeeTempPosMaster);
const repoProfile = AppDataSource.getRepository(Profile);
const repoProfileEmployee = AppDataSource.getRepository(ProfileEmployee);
const { data, token, user } = JSON.parse(msg.content.toString());
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
try {
@ -166,6 +172,60 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
await repoPosmaster.save(item).catch((e) => console.log(e));
}),
);
const employeePosMaster = await repoEmployeePosmaster.find({
where: { orgRevisionId: id },
relations: ["positions", "positions.posLevel", "positions.posType"],
});
await Promise.all(
employeePosMaster.map(async (item) => {
if (item.next_holderId != null && status == "NOW") {
const profile = await repoProfileEmployee.findOne({
where: { id: item.next_holderId == null ? "" : item.next_holderId },
});
const position = await item.positions.find((x) => x.positionIsSelected == true);
const _null: any = null;
if (profile != null) {
profile.posLevelId = position?.posLevelId ?? _null;
profile.posTypeId = position?.posTypeId ?? _null;
profile.position = position?.positionName ?? _null;
await repoProfileEmployee.save(profile);
}
}
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
await repoEmployeePosmaster.save(item).catch((e) => console.log(e));
}),
);
const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({
where: { orgRevisionId: id },
relations: ["positions", "positions.posLevel", "positions.posType"],
});
await Promise.all(
employeeTempPosMaster.map(async (item) => {
if (item.next_holderId != null && status == "NOW") {
const profile = await repoProfileEmployee.findOne({
where: { id: item.next_holderId == null ? "" : item.next_holderId },
});
const position = await item.positions.find((x) => x.positionIsSelected == true);
const _null: any = null;
if (profile != null) {
profile.posLevelId = position?.posLevelId ?? _null;
profile.posTypeId = position?.posTypeId ?? _null;
profile.position = position?.positionName ?? _null;
await repoProfileEmployee.save(profile);
}
}
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
await repoEmployeeTempPosmaster.save(item).catch((e) => console.log(e));
}),
);
console.log("[AMQ] Excecute Organization Success");
return true;
} catch (error) {
@ -173,4 +233,3 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
return false;
}
}