diff --git a/src/controllers/InsigniaController.ts b/src/controllers/InsigniaController.ts index 8e3a49db..c1355f2d 100644 --- a/src/controllers/InsigniaController.ts +++ b/src/controllers/InsigniaController.ts @@ -197,4 +197,37 @@ export class InsigniaController extends Controller { })); return new HttpSuccess(mapInsigniaAll); } + + /** + * API จัดลำดับแสดงผลเครื่องราชอิสริยาภรณ์ + * + * @summary ORG_038 - จัดลำดับแสดงผลเครื่องราชอิสริยาภรณ์ (ADMIN) # + * + */ + @Post("sort") + async Sort(@Body() requestBody: { insigniaTypeId: string; sortId: string[] }) { + + const insigniaType = await this.insigniaTypeRepository.findOne({ + where: { id: requestBody.insigniaTypeId } + }); + if (!insigniaType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับเครื่องราชอิสริยาภรณ์นี้"); + } + + const insignia = await this.insigniaRepository.find({ + select: ["id", "level"], + where: { insigniaTypeId: requestBody.insigniaTypeId } + }); + if (!insignia) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชอิสริยาภรณ์นี้"); + } + + const sortLevel = insignia.map((data) => ({ + id: data.id, + level: requestBody.sortId.indexOf(data.id) + 1, + })); + + await this.insigniaRepository.save(sortLevel); + return new HttpSuccess(); + } }