From 4663ff350446f8bf0a692567aaf42f834d990963 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 16 Feb 2024 16:15:53 +0700 Subject: [PATCH] fix bug --- src/controllers/SalaryController.ts | 93 ++++++++++--------------- src/controllers/SalaryRankController.ts | 2 +- src/entities/SalaryRanks.ts | 5 -- 3 files changed, 37 insertions(+), 63 deletions(-) diff --git a/src/controllers/SalaryController.ts b/src/controllers/SalaryController.ts index 23f68b3..4364fee 100644 --- a/src/controllers/SalaryController.ts +++ b/src/controllers/SalaryController.ts @@ -18,12 +18,13 @@ import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { AppDataSource } from "../database/data-source"; -import { In, IsNull, Not } from "typeorm"; +import { In, IsNull, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { SalaryRanks } from "../entities/SalaryRanks"; import { query } from "express"; +import { randomUUID } from "crypto"; @Route("api/v1/salary") @Tags("Salary") @@ -260,7 +261,7 @@ export class Salary extends Controller { return error; } } - + /** * API รายการผังเงินเดือน * @@ -335,63 +336,41 @@ export class Salary extends Controller { } } - // /** - // * API copy ผังเงินเดือน - // * - // * @summary SLR_008 - copy ผังเงินเดือน #8 - // * - // */ - // @Post("copy") - // async copySalary( - // @Body() body: { id: string }, - // @Request() request: { user: Record } - // ) { - // const salary = await this.salaryRepository.findOne({ - // relations: ["posLevel_", "posType_", "salaryRanks_"], - // where:{id:body.id} - // }); + /** + * API copy ผังเงินเดือน + * + * @summary SLR_008 - copy ผังเงินเดือน #8 + * + */ + @Post("copy") + async copySalary( + @Body() body: { id: string }, + @Request() request: { user: Record }, + ) { + const salary = await this.salaryRepository.findOne({ + relations: ["posLevel_", "posType_", "salaryRanks_"], + where: { id: body.id }, + }); - // if(!salary){ - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเงินเดือนจากไอดีนี้ : " + body.id) - // } + if (!salary) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเงินเดือนจากไอดีนี้ : " + body.id); + } - // const salaryRank = await this.salaryRankRepository.find({ - // where:{salaryId:salary?.id} - // }); + const salaryRank = await this.salaryRankRepository.find({ + where: { salaryId: salary?.id }, + }); - // try { - // const newSalary = new Salarys(); - // newSalary.isActive = false; - // newSalary.salaryType = salary.salaryType; - // newSalary.posTypeId = salary.posTypeId - // newSalary.posLevelId = salary.posLevelId - // newSalary.createdUserId = request.user.sub, - // newSalary.createdFullName = request.user.name, - // newSalary.createdAt = new Date(), - // newSalary.lastUpdateUserId = request.user.sub, - // newSalary.lastUpdateFullName = request.user.name, - // newSalary.lastUpdatedAt = new Date(), - // await this.salaryRepository.save(newSalary); + const newSalary = { ...salary, id: randomUUID() }; - // const newRanks = salary.salaryRanks_.map((rank) => { - // newRanks.forEach(async (x: any) => { - // var dataId = x.id; - // delete x.id; - // const data = Object.assign(new SalaryRanks(), x); - // data.salaryId = newSalary.id, - // data.createdUserId = request.user.sub, - // data.createdFullName = request.user.name, - // data.createdAt = new Date(), - // data.lastUpdateUserId = request.user.sub, - // data.lastUpdateFullName = request.user.name, - // data.lastUpdatedAt = new Date(), - // await this.salaryRankRepository.save(data); - // }); - // }); - - // return new HttpSuccess({ id: newSalary.id }); - // } catch (error) { - // return error; - // } - // } + await this.salaryRepository.save(newSalary); + + await Promise.all( + salaryRank.map(async (v) => { + const newSalaryRank = { ...v, id: randomUUID() }; + await this.salaryRankRepository.save(newSalaryRank); + }), + ); + + return new HttpSuccess({ id: newSalary.id }); + } } diff --git a/src/controllers/SalaryRankController.ts b/src/controllers/SalaryRankController.ts index 66e751a..adae86d 100644 --- a/src/controllers/SalaryRankController.ts +++ b/src/controllers/SalaryRankController.ts @@ -23,7 +23,7 @@ import { import { Salarys } from "../entities/Salarys"; @Route("api/v1/salary/rate") @Tags("SalaryRank") -// @Security("bearerAuth") + @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", diff --git a/src/entities/SalaryRanks.ts b/src/entities/SalaryRanks.ts index b26d9d4..7b6d46c 100644 --- a/src/entities/SalaryRanks.ts +++ b/src/entities/SalaryRanks.ts @@ -69,11 +69,6 @@ export class SalaryRanks extends EntityBase { }) isNext: boolean; - @Column({ - comment: "ลำดับ", - }) - rank: number; - @ManyToOne(() => Salarys, (salarys) => salarys.salaryRanks_) @JoinColumn({ name: "salaryId" }) salarys_: Salarys;