From 528af00d969447b21019e1020781d767f2991ca5 Mon Sep 17 00:00:00 2001 From: Muhammad Rayhan Ardiansyah Date: Tue, 31 Mar 2026 17:54:14 +0700 Subject: [PATCH] fixing room to pharmacy --- src/controllers/room_to_pharmacy.ts | 148 +++++++++++++++++++++++----- src/routes/private.ts | 2 + 2 files changed, 125 insertions(+), 25 deletions(-) diff --git a/src/controllers/room_to_pharmacy.ts b/src/controllers/room_to_pharmacy.ts index b2af2b3..afa8ed1 100644 --- a/src/controllers/room_to_pharmacy.ts +++ b/src/controllers/room_to_pharmacy.ts @@ -176,40 +176,49 @@ export class RoomToPharmacyController { .uuid() .required() .label("Pharmacy Room ID"), + + room_id: Joi.string() + .uuid() + .required() + .label("Room ID"), - room_ids: Joi.array() - .items(Joi.string().uuid().required()) - .min(1) - .required() - .label("Room IDs") + // room_ids: Joi.array() + // .items(Joi.string().uuid().required()) + // .min(1) + // .required() + // .label("Room IDs") }); const param: any = await schema.validateAsync(req.body); - const RoomToPharmacyRepo = OrmHelper.DB.getRepository(RoomToPharmacy); - const roomRepo = OrmHelper.DB.getRepository(Room); + const data = new RoomToPharmacy() + data.pharmacy_room = param.pharmacy_room_id; + data.room = param.room_id; + data.created_by = req.auth.data.name; - await RoomToPharmacyRepo - .createQueryBuilder() - .delete() - .from(RoomToPharmacy) - .where("pharmacy_room_id = :pharmacyRoomId", { pharmacyRoomId: param.pharmacy_room_id }) - .execute(); + await OrmHelper.DB.manager.save(data); - let relation : RoomToPharmacy; - for (const roomId of param.room_ids) { - relation = new RoomToPharmacy(); - relation.room = await roomRepo.findOneBy({ id: roomId }); - relation.pharmacy_room = await roomRepo.findOneBy({ id: param.pharmacy_room_id }); - relation.created_by = req.auth.data.name; + // await RoomToPharmacyRepo + // .createQueryBuilder() + // .delete() + // .from(RoomToPharmacy) + // .where("pharmacy_room_id = :pharmacyRoomId", { pharmacyRoomId: param.pharmacy_room_id }) + // .execute(); - await RoomToPharmacyRepo.save(relation); - } + // let relation : RoomToPharmacy; + // for (const roomId of param.room_ids) { + // relation = new RoomToPharmacy(); + // relation.room = await roomRepo.findOneBy({ id: roomId }); + // relation.pharmacy_room = await roomRepo.findOneBy({ id: param.pharmacy_room_id }); + // relation.created_by = req.auth.data.name; - const data = await RoomToPharmacyRepo.createQueryBuilder("room_to_pharmacy") - .leftJoinAndSelect("room_to_pharmacy.room", "room") - .leftJoinAndSelect("room_to_pharmacy.pharmacy_room", "pharmacy_room") - .where("room_to_pharmacy.pharmacy_room_id = :pharmacyRoomId", { pharmacyRoomId: param.pharmacy_room_id }).getMany(); + // await RoomToPharmacyRepo.save(relation); + // } + + // const data = await RoomToPharmacyRepo.createQueryBuilder("room_to_pharmacy") + // .leftJoinAndSelect("room_to_pharmacy.room", "room") + // .leftJoinAndSelect("room_to_pharmacy.pharmacy_room", "pharmacy_room") + // .where("room_to_pharmacy.pharmacy_room_id = :pharmacyRoomId", { pharmacyRoomId: param.pharmacy_room_id }).getMany(); return ReturnHelper.successResponseAny(res, 200, Language.lang.success_insert, data); } catch (e: unknown) { @@ -219,4 +228,93 @@ export class RoomToPharmacyController { return ReturnHelper.errorResponse(res, 500, 401, Language.lang.failed_insert, err.message); } } + + static async update(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['RoomToPharmacy'] + #swagger.security = [{ + "bearerAuth": [] + }] + + #swagger.requestBody = { + required: true, + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/room_to_pharmacy" + } + } + } + } + */ + try { + const schema = Joi.object().keys({ + + id: Joi.string().uuid().required().label('ID'), + + pharmacy_room_id: Joi.string() + .uuid() + .required() + .label("Pharmacy Room ID"), + + room_id: Joi.string() + .uuid() + .required() + .label("Room ID"), + + // room_ids: Joi.array() + // .items(Joi.string().uuid().required()) + // .min(1) + // .required() + // .label("Room IDs") + }); + + req.body.id = req.params['id']; + + const param: any = await schema.validateAsync(req.body); + + const repo = OrmHelper.DB.getRepository(RoomToPharmacy); + + const data = await repo.findOneBy({ id: param.id }); + + if (data != null) { + data.pharmacy_room = param.pharmacy_room_id; + data.room = param.room_id; + data.updated_by = req.auth.data.name; + + await repo.save(data); + } + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data); + } catch (e: unknown) { + log.error(e); + const err = e as Error; + + return ReturnHelper.errorResponse(res, 500, 401, Language.lang.failed_update, err.message); + } + } + + static async detail(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['RoomToPharmacy'] + #swagger.security = [{ "bearerAuth": [] }] + #swagger.parameters['id'] = { description: '', in: 'path', type: 'string' } + */ + try { + const schema = Joi.object().keys({ + id: Joi.string().uuid().required().label("RoomToPharmacy Id"), + }); + + let param: any = await schema.validateAsync(req.params); + + let data = await RoomToPharmacyModel.list({ "id": param.id }).then((q) => q.getOne()); + if (!data) throw { message: "RoomToPharmacy Not Found" }; + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data); + } catch (e: unknown) { + log.error(e); + const err = e as Error; + return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_not_found, err.message); + } + } } \ No newline at end of file diff --git a/src/routes/private.ts b/src/routes/private.ts index 5a1009d..2de78c8 100644 --- a/src/routes/private.ts +++ b/src/routes/private.ts @@ -450,6 +450,8 @@ export class RoutePrivate { app.get('/api/room-to-pharmacy/list', RoomToPharmacyController.list) app.get('/api/room-to-pharmacy/export', RoomToPharmacyController.export) app.post('/api/room-to-pharmacy/create', RoomToPharmacyController.create) + app.put('/api/room-to-pharmacy/update/:id', RoomToPharmacyController.update) + app.get('/api/room-to-pharmacy/view/:id', RoomToPharmacyController.detail) app.get('/api/pharmacy/planning-verificator/list', PlanningVerificatorController.list) app.get('/api/pharmacy/planning-verificator/export', PlanningVerificatorController.export)