From 2529d7d43f82def071d0b4fc528a501ee7d2d482 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 15 Mar 2024 09:21:56 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20path=20=E0=B8=AD?= =?UTF-8?q?=E0=B8=99=E0=B8=B8=E0=B8=A1=E0=B8=B1=E0=B8=95=E0=B8=B4=E0=B9=80?= =?UTF-8?q?=E0=B8=87=E0=B8=B4=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B7=E0=B8=AD?= =?UTF-8?q?=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 137 +++++++++++++--------- 1 file changed, 84 insertions(+), 53 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index c03d219..44865ef 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -4,18 +4,16 @@ import { Post, Put, Delete, - Patch, Route, Security, Tags, Body, Path, Request, - Example, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; -import { DeepPartial, In, IsNull, Not, Between, MoreThan, Like, Brackets } from "typeorm"; +import { In, Not, MoreThan, Brackets } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; @@ -28,7 +26,6 @@ import { PosLevel } from "../entities/PosLevel"; import { Salarys } from "../entities/Salarys"; import { SalaryRanks } from "../entities/SalaryRanks"; import CallAPI from "../interfaces/call-api"; -import { Int32 } from "typeorm/browser"; @Route("api/v1/salary/period") @Tags("Salary") @@ -1966,19 +1963,23 @@ export class SalaryPeriodController extends Controller { * API เจ้าหน้าที่ส่ง ผอ * * - * @param {string} orgId Guid, *Id รอบเงินเดือน + * @param {string} periodId Guid, *Id รอบเงินเดือน + * @param {string} rootId Guid, *Id สำนัก */ - @Get("officer/approve/{orgId}") - async OfficerApprove(@Path() orgId: string) { - const salaryOrg = await this.salaryOrgRepository.findOne({ - where: { id: orgId }, + @Get("officer/approve/{periodId}/{rootId}") + async OfficerApprove(@Path() periodId: string, rootId: string) { + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: periodId }, + relations: ["salaryOrgs"], }); - if (!salaryOrg) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - if (salaryOrg.status != "PENDING") - throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ยังไม่ไม่สามารถอนุมัติได้"); - - salaryOrg.status = "WAITHEAD1"; - await this.salaryOrgRepository.save(salaryOrg); + if (!salaryPeriod) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); + await Promise.all( + salaryPeriod.salaryOrgs.map(async (x) => { + x.status = "WAITHEAD1"; + await this.salaryPeriodRepository.save(salaryPeriod); + }), + ); return new HttpSuccess(); } @@ -1987,18 +1988,25 @@ export class SalaryPeriodController extends Controller { * * * @param {string} orgId Guid, *Id รอบเงินเดือน + * @param {string} rootId Guid, *Id สำนัก */ - @Get("head/approve/{orgId}") - async HeadApprove(@Path() orgId: string) { - const salaryOrg = await this.salaryOrgRepository.findOne({ - where: { id: orgId }, + @Get("head/approve/{periodId}/{rootId}") + async HeadApprove(@Path() periodId: string, rootId: string) { + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: periodId }, + relations: ["salaryOrgs"], }); - if (!salaryOrg) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - if (salaryOrg.status != "WAITHEAD1") - throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ยังไม่ไม่สามารถอนุมัติได้"); + if (!salaryPeriod) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - salaryOrg.status = "WAITOWNER1"; - await this.salaryOrgRepository.save(salaryOrg); + await Promise.all( + salaryPeriod.salaryOrgs + .filter((x) => x.rootId == rootId) + .map(async (x) => { + x.status = "WAITOWNER1"; + await this.salaryPeriodRepository.save(salaryPeriod); + }), + ); return new HttpSuccess(); } @@ -2007,18 +2015,25 @@ export class SalaryPeriodController extends Controller { * * * @param {string} orgId Guid, *Id รอบเงินเดือน + * @param {string} rootId Guid, *Id สำนัก */ - @Get("owner/approve/{orgId}") - async OwnerApprove(@Path() orgId: string) { - const salaryOrg = await this.salaryOrgRepository.findOne({ - where: { id: orgId }, + @Get("owner/approve/{periodId}/{rootId}") + async OwnerApprove(@Path() periodId: string, rootId: string) { + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: periodId }, + relations: ["salaryOrgs"], }); - if (!salaryOrg) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - if (salaryOrg.status != "WAITOWNER1") - throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ยังไม่ไม่สามารถอนุมัติได้"); + if (!salaryPeriod) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - salaryOrg.status = "REPORT"; - await this.salaryOrgRepository.save(salaryOrg); + await Promise.all( + salaryPeriod.salaryOrgs + .filter((x) => x.rootId == rootId) + .map(async (x) => { + x.status = "REPORT"; + await this.salaryPeriodRepository.save(salaryPeriod); + }), + ); return new HttpSuccess(); } @@ -2027,25 +2042,33 @@ export class SalaryPeriodController extends Controller { * * * @param {string} orgId Guid, *Id รอบเงินเดือน + * @param {string} rootId Guid, *Id สำนัก */ - @Put("owner/comment/{orgId}") + @Put("owner/comment/{periodId}/{rootId}") async WaitOwnerApprove( - @Path() orgId: string, + @Path() periodId: string, + rootId: string, @Body() body: { titleRecommend: string; }, ) { - const salaryOrg = await this.salaryOrgRepository.findOne({ - where: { id: orgId }, + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: periodId }, + relations: ["salaryOrgs"], }); - if (!salaryOrg) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - if (salaryOrg.status != "WAITOWNER1") - throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ยังไม่ไม่สามารถอนุมัติได้"); + if (!salaryPeriod) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - salaryOrg.status = "WAITHEAD2"; - salaryOrg.ownerRecommend = body.titleRecommend; - await this.salaryOrgRepository.save(salaryOrg); + await Promise.all( + salaryPeriod.salaryOrgs + .filter((x) => x.rootId == rootId) + .map(async (x) => { + x.status = "WAITHEAD2"; + x.ownerRecommend = body.titleRecommend; + await this.salaryPeriodRepository.save(salaryPeriod); + }), + ); return new HttpSuccess(); } @@ -2054,25 +2077,33 @@ export class SalaryPeriodController extends Controller { * * * @param {string} orgId Guid, *Id รอบเงินเดือน + * @param {string} rootId Guid, *Id สำนัก */ - @Put("head/comment/{orgId}") + @Put("head/comment/{periodId}/{rootId}") async WaitHeadApprove( - @Path() orgId: string, + @Path() periodId: string, + rootId: string, @Body() body: { titleRecommend: string; }, ) { - const salaryOrg = await this.salaryOrgRepository.findOne({ - where: { id: orgId }, + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: periodId }, + relations: ["salaryOrgs"], }); - if (!salaryOrg) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - if (salaryOrg.status != "WAITOFFICER2") - throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ยังไม่ไม่สามารถอนุมัติได้"); + if (!salaryPeriod) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลรอบผังเงินเดือนนี้"); - salaryOrg.status = "REPORT"; - salaryOrg.headRecommend = body.titleRecommend; - await this.salaryOrgRepository.save(salaryOrg); + await Promise.all( + salaryPeriod.salaryOrgs + .filter((x) => x.rootId == rootId) + .map(async (x) => { + x.status = "REPORT"; + x.ownerRecommend = body.titleRecommend; + await this.salaryPeriodRepository.save(salaryPeriod); + }), + ); return new HttpSuccess(); } }