diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 87d5f4e6..1a3a3edb 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -616,12 +616,15 @@ export class CommandController extends Controller { requestBody: { commandNo?: string | null; commandYear?: number | null }, @Request() request: RequestWithUser, ) { - const command = await this.commandRepository.findOne({ where: { id: id } }); + const command = await this.commandRepository.findOne({ + where: { id: id }, + }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); } const copy = new Command(); Object.assign(copy, { ...command, ...requestBody, id: undefined }); + copy.status = "NEW"; copy.createdUserId = request.user.sub; copy.createdFullName = request.user.name; copy.createdAt = new Date(); @@ -629,6 +632,57 @@ export class CommandController extends Controller { copy.lastUpdateFullName = request.user.name; copy.lastUpdatedAt = new Date(); await this.commandRepository.save(copy); + + const commandRecives = await this.commandReciveRepository.find({ + where: { commandId: id }, + }); + const commandSends = await this.commandSendRepository.find({ + where: { commandId: id }, + }); + const commandSendCCs = await this.commandSendCCRepository.find({ + where: { commandSendId: In(commandSends.map((x) => x.id)) }, + }); + commandRecives.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(), + }; + }); + await this.commandRepository.save(commandRecives); + + commandSends.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(), + }; + }); + await this.commandRepository.save(commandSends); + + 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(), + }; + }); + await this.commandRepository.save(commandSendCCs); return new HttpSuccess(copy.id); }