From de0e2a9643853cefbdb7eebc5b3dc85936094cc1 Mon Sep 17 00:00:00 2001 From: kittapath Date: Fri, 27 Sep 2024 11:10:17 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=A5=E0=B8=9A?= =?UTF-8?q?=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 44 +++++++++++++++------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 1a3a3edb..c7ce311b 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -598,6 +598,7 @@ export class CommandController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับสำเนาคำสั่ง"); } + await this.commandSendCCRepository.delete({ commandSendId: commandSendId }); await this.commandSendRepository.delete(commandSendId); return new HttpSuccess(); } @@ -638,14 +639,13 @@ export class CommandController extends Controller { }); const commandSends = await this.commandSendRepository.find({ where: { commandId: id }, - }); - const commandSendCCs = await this.commandSendCCRepository.find({ - where: { commandSendId: In(commandSends.map((x) => x.id)) }, + relations: ["commandSendCCs"], }); commandRecives.map((x: any) => { delete x.id; return { ...x, + commandId: copy.id, createdUserId: request.user.sub, createdFullName: request.user.name, createdAt: new Date(), @@ -654,12 +654,14 @@ export class CommandController extends Controller { lastUpdatedAt: new Date(), }; }); - await this.commandRepository.save(commandRecives); + await this.commandReciveRepository.save(commandRecives); - commandSends.map((x: any) => { + commandSends.map(async (x: any) => { delete x.id; - return { + let commandSend = { ...x, + commandSendCCs: [], + commandId: copy.id, createdUserId: request.user.sub, createdFullName: request.user.name, createdAt: new Date(), @@ -667,22 +669,24 @@ export class CommandController extends Controller { lastUpdateFullName: request.user.name, lastUpdatedAt: new Date(), }; - }); - await this.commandRepository.save(commandSends); + await this.commandSendRepository.save(commandSend); - commandSendCCs.map((x: any) => { - delete x.id; - return { - ...x, - createdUserId: request.user.sub, - createdFullName: request.user.name, - createdAt: new Date(), - lastUpdateUserId: request.user.sub, - lastUpdateFullName: request.user.name, - lastUpdatedAt: new Date(), - }; + let commandSendCC = await x.commandSendCCs.map((y: any) => { + delete y.id; + return { + ...y, + commandSendId: commandSend.id, + createdUserId: request.user.sub, + createdFullName: request.user.name, + createdAt: new Date(), + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + lastUpdatedAt: new Date(), + }; + }); + await this.commandSendCCRepository.save(commandSendCC); }); - await this.commandRepository.save(commandSendCCs); + return new HttpSuccess(copy.id); }