update
This commit is contained in:
@ -383,7 +383,7 @@ export class RoomToPharmacyController {
|
||||
#swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}]
|
||||
|
||||
|
||||
#swagger.parameters['room_id'] = {
|
||||
in: 'path',
|
||||
description: 'Room ID.',
|
||||
@ -396,7 +396,7 @@ export class RoomToPharmacyController {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/room_to_pharmacy"
|
||||
$ref: "#/components/schemas/room_to_pharmacy"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -404,14 +404,8 @@ export class RoomToPharmacyController {
|
||||
*/
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
room_id: Joi.string()
|
||||
.uuid()
|
||||
.required()
|
||||
.label("Room ID"),
|
||||
pharmacy_room_id: Joi.string()
|
||||
.uuid()
|
||||
.required()
|
||||
.label("Pharmacy Room ID"),
|
||||
room_id: Joi.string().uuid().required(),
|
||||
pharmacy_room_id: Joi.string().uuid().required(),
|
||||
});
|
||||
|
||||
req.body.room_id = req.params['room_id'];
|
||||
@ -420,36 +414,51 @@ export class RoomToPharmacyController {
|
||||
|
||||
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
|
||||
|
||||
let data = await repo.findOneBy({
|
||||
room: { id: param.room_id },
|
||||
pharmacy_room: { id: param.pharmacy_room_id }
|
||||
// ✅ cari berdasarkan room saja
|
||||
let data = await repo.findOne({
|
||||
where: {
|
||||
room: { id: param.room_id }
|
||||
},
|
||||
relations: ["room", "pharmacy_room"]
|
||||
});
|
||||
|
||||
if (data != null) {
|
||||
data.updated_by = req.auth?.data.name;
|
||||
await repo.save(data);
|
||||
} else {
|
||||
const room = await OrmHelper.DB.getRepository(Room).findOneBy({ id: param.room_id });
|
||||
if (!room) throw { message: "Room not found" };
|
||||
|
||||
const pharmacyRoom = await OrmHelper.DB.getRepository(Room).findOneBy({ id: param.pharmacy_room_id });
|
||||
if (!pharmacyRoom) throw { message: "Pharmacy Room not found" };
|
||||
|
||||
data = new RoomToPharmacy();
|
||||
data.room = room;
|
||||
data.pharmacy_room = pharmacyRoom;
|
||||
data.created_by = req.auth?.data.name;
|
||||
data.updated_by = req.auth?.data.name;
|
||||
|
||||
await OrmHelper.DB.manager.save(data);
|
||||
if (!data) {
|
||||
throw new Error("RoomToPharmacy data not found");
|
||||
}
|
||||
|
||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data);
|
||||
// ✅ ambil pharmacy room baru
|
||||
const pharmacyRoom = await OrmHelper.DB.getRepository(Room).findOneBy({
|
||||
id: param.pharmacy_room_id
|
||||
});
|
||||
|
||||
if (!pharmacyRoom) {
|
||||
throw new Error("Pharmacy Room not found");
|
||||
}
|
||||
|
||||
// ✅ update
|
||||
data.pharmacy_room = pharmacyRoom;
|
||||
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);
|
||||
return ReturnHelper.errorResponse(
|
||||
res,
|
||||
500,
|
||||
401,
|
||||
Language.lang.failed_update,
|
||||
err.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user