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); }