Merge branch 'develop' of github.com:Frappet/bma-ehr-organization into develop
This commit is contained in:
commit
6472d6bcdb
4 changed files with 109 additions and 116 deletions
|
|
@ -37,6 +37,7 @@ async function main() {
|
||||||
const APP_PORT = +(process.env.APP_PORT || 3000);
|
const APP_PORT = +(process.env.APP_PORT || 3000);
|
||||||
|
|
||||||
const cronTime = "0 8 * * * *"; // ตั้งเวลาทุกวันเวลา 08:00:00
|
const cronTime = "0 8 * * * *"; // ตั้งเวลาทุกวันเวลา 08:00:00
|
||||||
|
// const cronTime = "*/10 * * * * *";
|
||||||
cron.schedule(cronTime, async () => {
|
cron.schedule(cronTime, async () => {
|
||||||
try {
|
try {
|
||||||
const orgController = new OrganizationController();
|
const orgController = new OrganizationController();
|
||||||
|
|
|
||||||
|
|
@ -1091,9 +1091,8 @@ export class CommandController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
async cronjobCommand(@Request() request?: RequestWithUser) {
|
async cronjobCommand(@Request() request?: RequestWithUser) {
|
||||||
console.log(request);
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
today.setHours(7, 0, 0, 0); //+7 เพื่อให้ตรง local time (อาจจะต้องใช้ moment)
|
today.setUTCHours(0, 0, 0, 0);
|
||||||
const tomorrow = new Date(today);
|
const tomorrow = new Date(today);
|
||||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||||
|
|
||||||
|
|
@ -1982,6 +1981,9 @@ export class CommandController extends Controller {
|
||||||
level?: string | null;
|
level?: string | null;
|
||||||
unStigma?: string | null;
|
unStigma?: string | null;
|
||||||
commandId?: string | null;
|
commandId?: string | null;
|
||||||
|
amount?: Double | null;
|
||||||
|
positionSalaryAmount?: Double | null;
|
||||||
|
mouthSalaryAmount?: Double | null;
|
||||||
}[];
|
}[];
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
|
|
@ -2019,11 +2021,9 @@ export class CommandController extends Controller {
|
||||||
profile.profileSalary.length > 0 ? profile.profileSalary[0].positionPathSide : null,
|
profile.profileSalary.length > 0 ? profile.profileSalary[0].positionPathSide : null,
|
||||||
positionExecutive:
|
positionExecutive:
|
||||||
profile.profileSalary.length > 0 ? profile.profileSalary[0].positionExecutive : null,
|
profile.profileSalary.length > 0 ? profile.profileSalary[0].positionExecutive : null,
|
||||||
amount: profile.profileSalary.length > 0 ? profile.profileSalary[0].amount : null,
|
amount: item.amount ? item.amount : null,
|
||||||
positionSalaryAmount:
|
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
|
||||||
profile.profileSalary.length > 0 ? profile.profileSalary[0].positionSalaryAmount : null,
|
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
|
||||||
mouthSalaryAmount:
|
|
||||||
profile.profileSalary.length > 0 ? profile.profileSalary[0].mouthSalaryAmount : null,
|
|
||||||
order:
|
order:
|
||||||
profile.profileSalary.length >= 0
|
profile.profileSalary.length >= 0
|
||||||
? profile.profileSalary.length > 0
|
? profile.profileSalary.length > 0
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import permission from "../interfaces/permission";
|
||||||
import { PermissionOrg } from "../entities/PermissionOrg";
|
import { PermissionOrg } from "../entities/PermissionOrg";
|
||||||
import { setLogDataDiff } from "../interfaces/utils";
|
import { setLogDataDiff } from "../interfaces/utils";
|
||||||
import { AuthRole } from "../entities/AuthRole";
|
import { AuthRole } from "../entities/AuthRole";
|
||||||
|
import { sendToQueue, sendToQueueOrg } from "../services/rabbitmq";
|
||||||
|
|
||||||
@Route("api/v1/org")
|
@Route("api/v1/org")
|
||||||
@Tags("Organization")
|
@Tags("Organization")
|
||||||
|
|
@ -120,7 +121,6 @@ export class OrganizationController extends Controller {
|
||||||
@Body() requestBody: CreateOrgRevision,
|
@Body() requestBody: CreateOrgRevision,
|
||||||
@Request() request: RequestWithUser,
|
@Request() request: RequestWithUser,
|
||||||
) {
|
) {
|
||||||
console.log("fucntion draft");
|
|
||||||
//new main revision
|
//new main revision
|
||||||
const before = null;
|
const before = null;
|
||||||
const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision;
|
const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision;
|
||||||
|
|
@ -3414,76 +3414,18 @@ export class OrganizationController extends Controller {
|
||||||
orgRevisionDraft.orgRevisionIsCurrent = true;
|
orgRevisionDraft.orgRevisionIsCurrent = true;
|
||||||
orgRevisionDraft.orgRevisionIsDraft = false;
|
orgRevisionDraft.orgRevisionIsDraft = false;
|
||||||
await this.orgRevisionRepository.save(orgRevisionDraft);
|
await this.orgRevisionRepository.save(orgRevisionDraft);
|
||||||
|
const msg = {
|
||||||
const posMaster = await this.posMasterRepository.find({
|
data: {
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
id: orgRevisionDraft.id,
|
||||||
relations: [
|
status: "NOW",
|
||||||
"orgRoot",
|
lastUpdateUserId: request.user.sub,
|
||||||
"orgChild4",
|
lastUpdateFullName: request.user.name,
|
||||||
"orgChild3",
|
lastUpdatedAt: new Date(),
|
||||||
"orgChild2",
|
},
|
||||||
"orgChild1",
|
user: request.user,
|
||||||
"positions",
|
token: request.headers["authorization"],
|
||||||
"positions.posLevel",
|
};
|
||||||
"positions.posType",
|
sendToQueueOrg(msg);
|
||||||
"positions.posExecutive",
|
|
||||||
],
|
|
||||||
});
|
|
||||||
await Promise.all(
|
|
||||||
posMaster.map(async (item) => {
|
|
||||||
// if(item.next_holderId != null){
|
|
||||||
if (item.next_holderId != null) {
|
|
||||||
const profile = await this.profileRepo.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 this.profileRepo.save(profile);
|
|
||||||
}
|
|
||||||
// const profileSalary = await this.salaryRepository.findOne({
|
|
||||||
// where: { profileId: item.next_holderId },
|
|
||||||
// order: { createdAt: "DESC" },
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const shortName =
|
|
||||||
// item != null && item.orgChild4 != null
|
|
||||||
// ? `${item.orgChild4.orgChild4ShortName}${item.posMasterNo}`
|
|
||||||
// : item != null && item?.orgChild3 != null
|
|
||||||
// ? `${item.orgChild3.orgChild3ShortName}${item.posMasterNo}`
|
|
||||||
// : item != null && item?.orgChild2 != null
|
|
||||||
// ? `${item.orgChild2.orgChild2ShortName}${item.posMasterNo}`
|
|
||||||
// : item != null && item?.orgChild1 != null
|
|
||||||
// ? `${item.orgChild1.orgChild1ShortName}${item.posMasterNo}`
|
|
||||||
// : item != null && item?.orgRoot != null
|
|
||||||
// ? `${item.orgRoot.orgRootShortName}${item.posMasterNo}`
|
|
||||||
// : null;
|
|
||||||
// await new FunctionMain().newSalaryFunction(request, {
|
|
||||||
// profileId: item.next_holderId,
|
|
||||||
// date: new Date(),
|
|
||||||
// amount: profileSalary?.amount ?? null,
|
|
||||||
// positionSalaryAmount: profileSalary?.positionSalaryAmount ?? null,
|
|
||||||
// mouthSalaryAmount: profileSalary?.mouthSalaryAmount ?? null,
|
|
||||||
// posNo: shortName,
|
|
||||||
// position: position?.positionName ?? _null,
|
|
||||||
// positionLine: position?.positionField ?? _null,
|
|
||||||
// positionPathSide: position?.positionArea ?? _null,
|
|
||||||
// positionExecutive: position?.posExecutive?.posExecutiveName ?? _null,
|
|
||||||
// positionType: position?.posType?.posTypeName ?? _null,
|
|
||||||
// positionLevel: position?.posLevel?.posLevelName ?? _null,
|
|
||||||
// refCommandNo: null,
|
|
||||||
// templateDoc: "ปรับโครงสร้าง",
|
|
||||||
// });
|
|
||||||
}
|
|
||||||
item.current_holderId = item.next_holderId;
|
|
||||||
item.next_holderId = null;
|
|
||||||
await this.posMasterRepository.save(item);
|
|
||||||
// }
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3492,7 +3434,10 @@ export class OrganizationController extends Controller {
|
||||||
*/
|
*/
|
||||||
async cronjobRevision() {
|
async cronjobRevision() {
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
today.setHours(0, 0, 0, 0); // Set time to the beginning of the day
|
today.setUTCHours(0, 0, 0, 0); // Set time to the beginning of the day
|
||||||
|
const tomorrow = new Date(today);
|
||||||
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||||
|
|
||||||
const orgRevisionPublish = await this.orgRevisionRepository
|
const orgRevisionPublish = await this.orgRevisionRepository
|
||||||
.createQueryBuilder("orgRevision")
|
.createQueryBuilder("orgRevision")
|
||||||
.where("orgRevision.orgRevisionIsDraft = false")
|
.where("orgRevision.orgRevisionIsDraft = false")
|
||||||
|
|
@ -3503,8 +3448,9 @@ export class OrganizationController extends Controller {
|
||||||
.createQueryBuilder("orgRevision")
|
.createQueryBuilder("orgRevision")
|
||||||
.where("orgRevision.orgRevisionIsDraft = true")
|
.where("orgRevision.orgRevisionIsDraft = true")
|
||||||
.andWhere("orgRevision.orgRevisionIsCurrent = false")
|
.andWhere("orgRevision.orgRevisionIsCurrent = false")
|
||||||
.andWhere("DATE(orgRevision.orgPublishDate) = :today", { today })
|
.andWhere("orgRevision.orgPublishDate BETWEEN :today AND :tomorrow", { today, tomorrow })
|
||||||
.getOne();
|
.getOne();
|
||||||
|
|
||||||
if (!orgRevisionDraft) {
|
if (!orgRevisionDraft) {
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
}
|
}
|
||||||
|
|
@ -3517,17 +3463,26 @@ export class OrganizationController extends Controller {
|
||||||
orgRevisionDraft.orgRevisionIsDraft = false;
|
orgRevisionDraft.orgRevisionIsDraft = false;
|
||||||
await this.orgRevisionRepository.save(orgRevisionDraft);
|
await this.orgRevisionRepository.save(orgRevisionDraft);
|
||||||
|
|
||||||
const posMaster = await this.posMasterRepository.find({
|
// const posMaster = await this.posMasterRepository.find({
|
||||||
where: { orgRevisionId: orgRevisionDraft.id },
|
// where: { orgRevisionId: orgRevisionDraft.id },
|
||||||
});
|
// });
|
||||||
posMaster.forEach(async (item) => {
|
// posMaster.forEach(async (item) => {
|
||||||
// if(item.next_holderId != null){
|
// // if(item.next_holderId != null){
|
||||||
item.current_holderId = item.next_holderId;
|
// item.current_holderId = item.next_holderId;
|
||||||
item.next_holderId = null;
|
// item.next_holderId = null;
|
||||||
await this.posMasterRepository.save(item);
|
// await this.posMasterRepository.save(item);
|
||||||
// }
|
// // }
|
||||||
});
|
// });
|
||||||
|
const msg = {
|
||||||
|
data: {
|
||||||
|
id: orgRevisionDraft.id,
|
||||||
|
status: "ON_SCHEDULE",
|
||||||
|
lastUpdateUserId: "system",
|
||||||
|
lastUpdateFullName: "system",
|
||||||
|
lastUpdatedAt: new Date(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sendToQueueOrg(msg);
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,23 @@ import CallAPI from "../interfaces/call-api";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatusCode from "../interfaces/http-status";
|
import HttpStatusCode from "../interfaces/http-status";
|
||||||
import { RequestWithUser } from "../middlewares/user";
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
|
import { PosMaster } from "../entities/PosMaster";
|
||||||
|
import { Profile } from "../entities/Profile";
|
||||||
|
|
||||||
export let sendToQueue: (payload: any) => void;
|
export let sendToQueue: (payload: any) => void;
|
||||||
|
export let sendToQueueOrg: (payload: any) => void;
|
||||||
export async function init() {
|
export async function init() {
|
||||||
//----> (1) Producer
|
//----> (1) Producer
|
||||||
if (!process.env.AMQ_URL || !process.env.AMQ_QUEUE) return;
|
if (!process.env.AMQ_URL || !process.env.AMQ_QUEUE || !process.env.AMQ_QUEUE_ORG) return;
|
||||||
|
|
||||||
const { AMQ_URL: url, AMQ_QUEUE: queue } = process.env; //----> (1.2) get url and queue from .env
|
const { AMQ_URL: url, AMQ_QUEUE: queue, AMQ_QUEUE_ORG: queue_org } = process.env; //----> (1.2) get url and queue from .env
|
||||||
|
|
||||||
const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol
|
const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol
|
||||||
|
|
||||||
const channel = await connection.createChannel(); //----> (1.4) create Channel
|
const channel = await connection.createChannel(); //----> (1.4) create Channel
|
||||||
|
|
||||||
channel.assertQueue(queue, { durable: true }); //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ)
|
channel.assertQueue(queue, { durable: true }); //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ)
|
||||||
|
channel.assertQueue(queue_org, { durable: true });
|
||||||
channel.prefetch(1);
|
channel.prefetch(1);
|
||||||
|
|
||||||
sendToQueue = (payload: any, persistent = true) => {
|
sendToQueue = (payload: any, persistent = true) => {
|
||||||
|
|
@ -29,10 +32,14 @@ export async function init() {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sendToQueueOrg = (payload: any, persistent = true) => {
|
||||||
|
channel.sendToQueue(queue_org, Buffer.from(JSON.stringify(payload)), { persistent });
|
||||||
|
};
|
||||||
|
|
||||||
console.log("[AMQ] Listening for message...");
|
console.log("[AMQ] Listening for message...");
|
||||||
|
|
||||||
createConsumer(queue, channel, handler); //----> (3) Process Consumer
|
createConsumer(queue, channel, handler); //----> (3) Process Consumer
|
||||||
// createConsumer(queue1, channel, handler1);
|
createConsumer(queue_org, channel, handler_org);
|
||||||
// createConsumer(queue2, channel, handler2);
|
// createConsumer(queue2, channel, handler2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,24 +114,54 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// async function handler(msg: amqp.ConsumeMessage): Promise<boolean> { //----> condition before process consumer
|
async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
|
||||||
// const repo = AppDataSource.getRepository(Command);
|
//----> condition before process consume
|
||||||
|
const repoPosmaster = AppDataSource.getRepository(PosMaster);
|
||||||
// const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = JSON.parse(
|
const repoProfile = AppDataSource.getRepository(Profile);
|
||||||
// msg.content.toString(),
|
const { data, token, user } = JSON.parse(msg.content.toString());
|
||||||
// );
|
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
|
||||||
|
try {
|
||||||
// const record = await repo.findOne({
|
const posMaster = await repoPosmaster.find({
|
||||||
// where: { id },
|
where: { orgRevisionId: id },
|
||||||
// });
|
relations: [
|
||||||
|
"orgRoot",
|
||||||
// if (!record) return true;
|
"orgChild4",
|
||||||
|
"orgChild3",
|
||||||
// Object.assign(record, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
|
"orgChild2",
|
||||||
|
"orgChild1",
|
||||||
// const result = await repo.save(record).catch((e) => console.log(e));
|
"positions",
|
||||||
|
"positions.posLevel",
|
||||||
// if (result) return true;
|
"positions.posType",
|
||||||
|
"positions.posExecutive",
|
||||||
// return false;
|
],
|
||||||
// }
|
});
|
||||||
|
await Promise.all(
|
||||||
|
posMaster.map(async (item) => {
|
||||||
|
if (item.next_holderId != null && status == "NOW") {
|
||||||
|
const profile = await repoProfile.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 repoProfile.save(profile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
item.current_holderId = item.next_holderId;
|
||||||
|
item.next_holderId = null;
|
||||||
|
item.lastUpdateUserId = lastUpdateUserId;
|
||||||
|
item.lastUpdateFullName = lastUpdateFullName;
|
||||||
|
item.lastUpdatedAt = lastUpdatedAt;
|
||||||
|
await repoPosmaster.save(item).catch((e) => console.log(e));
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
console.log("[AMQ] Excecute Organization Success");
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue