update
This commit is contained in:
@ -396,7 +396,7 @@ export class RoomToPharmacyController {
|
|||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: {
|
schema: {
|
||||||
$ref: "#/components/schemas/room_to_pharmacy"
|
$ref: "#/components/schemas/room_to_pharmacy"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -404,14 +404,8 @@ export class RoomToPharmacyController {
|
|||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
const schema = Joi.object().keys({
|
const schema = Joi.object().keys({
|
||||||
room_id: Joi.string()
|
room_id: Joi.string().uuid().required(),
|
||||||
.uuid()
|
pharmacy_room_id: Joi.string().uuid().required(),
|
||||||
.required()
|
|
||||||
.label("Room ID"),
|
|
||||||
pharmacy_room_id: Joi.string()
|
|
||||||
.uuid()
|
|
||||||
.required()
|
|
||||||
.label("Pharmacy Room ID"),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
req.body.room_id = req.params['room_id'];
|
req.body.room_id = req.params['room_id'];
|
||||||
@ -420,36 +414,51 @@ export class RoomToPharmacyController {
|
|||||||
|
|
||||||
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
|
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
|
||||||
|
|
||||||
let data = await repo.findOneBy({
|
// ✅ cari berdasarkan room saja
|
||||||
room: { id: param.room_id },
|
let data = await repo.findOne({
|
||||||
pharmacy_room: { id: param.pharmacy_room_id }
|
where: {
|
||||||
|
room: { id: param.room_id }
|
||||||
|
},
|
||||||
|
relations: ["room", "pharmacy_room"]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (data != null) {
|
if (!data) {
|
||||||
data.updated_by = req.auth?.data.name;
|
throw new Error("RoomToPharmacy data not found");
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
} catch (e: unknown) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
const err = e as Error;
|
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