Merge branch 'main' of https://git.shiblysolution.id/SAUDE-TL/service-master-data
This commit is contained in:
@ -103,7 +103,7 @@ export class ExaminationDetailController {
|
||||
name: Joi.string().max(256).required().label("Name"),
|
||||
order_no: Joi.string().max(256).required().label("Order No."),
|
||||
measurement_unit_id: Joi.string().uuid().required().label("Measurement Unit ID"),
|
||||
reference_range_id: Joi.string().uuid().required().label("Reference Range ID"),
|
||||
reference_range_ids: Joi.array().items(Joi.string().uuid()).min(1).required().label("Reference Range IDs"),
|
||||
status: Joi.string().required().label("Status"),
|
||||
});
|
||||
|
||||
@ -112,17 +112,22 @@ export class ExaminationDetailController {
|
||||
let measurementUnit = await MeasurementUnitModel.list({ "MeasurementUnit.id": param.measurement_unit_id }).then((q) => q.getOne());
|
||||
if (!measurementUnit) throw { message: "Measurement Unit not found" };
|
||||
|
||||
let referenceRange = await ReferenceRangeModel.list({ "ReferenceRange.id": param.reference_range_id }).then((q) => q.getOne());
|
||||
if (!referenceRange) throw { message: "Reference Range not found" };
|
||||
const referenceRanges = await Promise.all(
|
||||
param.reference_range_ids.map(async (id: string) => {
|
||||
const rr = await ReferenceRangeModel.list({ "ReferenceRange.id": id }).then((q) => q.getOne());
|
||||
if (!rr) throw { message: `Reference Range ${id} not found` };
|
||||
return rr;
|
||||
})
|
||||
);
|
||||
|
||||
let examinationDetail = new ExaminationDetail();
|
||||
examinationDetail.name = param.name;
|
||||
examinationDetail.order_no = param.order_no;
|
||||
examinationDetail.measurement_unit = measurementUnit;
|
||||
examinationDetail.reference_range = referenceRange;
|
||||
examinationDetail.reference_range = referenceRanges;
|
||||
examinationDetail.status = param.status;
|
||||
examinationDetail.created_by = req.auth.data.name;
|
||||
examinationDetail.updated_by = req.auth.data.name;
|
||||
examinationDetail.created_by = req.auth?.data.name;
|
||||
examinationDetail.updated_by = req.auth?.data.name;
|
||||
await queryRunner.manager.save(examinationDetail);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
@ -181,7 +186,7 @@ export class ExaminationDetailController {
|
||||
name: Joi.string().max(256).required().label("Name"),
|
||||
order_no: Joi.string().max(256).required().label("Order No."),
|
||||
measurement_unit_id: Joi.string().uuid().required().label("Measurement Unit ID"),
|
||||
reference_range_id: Joi.string().uuid().required().label("Reference Range ID"),
|
||||
reference_range_ids: Joi.array().items(Joi.string().uuid()).min(1).required().label("Reference Range IDs"),
|
||||
status: Joi.string().required().label("Status"),
|
||||
});
|
||||
|
||||
@ -194,15 +199,20 @@ export class ExaminationDetailController {
|
||||
let measurementUnit = await MeasurementUnitModel.list({ "MeasurementUnit.id": param.measurement_unit_id }).then((q) => q.getOne());
|
||||
if (!measurementUnit) throw { message: "Measurement Unit not found" };
|
||||
|
||||
let referenceRange = await ReferenceRangeModel.list({ "ReferenceRange.id": param.reference_range_id }).then((q) => q.getOne());
|
||||
if (!referenceRange) throw { message: "Reference Range not found" };
|
||||
const referenceRanges = await Promise.all(
|
||||
param.reference_range_ids.map(async (id: string) => {
|
||||
const rr = await ReferenceRangeModel.list({ "ReferenceRange.id": id }).then((q) => q.getOne());
|
||||
if (!rr) throw { message: `Reference Range ${id} not found` };
|
||||
return rr;
|
||||
})
|
||||
);
|
||||
|
||||
examinationDetail.name = param.name;
|
||||
examinationDetail.order_no = param.order_no;
|
||||
examinationDetail.measurement_unit = measurementUnit;
|
||||
examinationDetail.reference_range = referenceRange;
|
||||
examinationDetail.reference_range = referenceRanges;
|
||||
examinationDetail.status = param.status;
|
||||
examinationDetail.updated_by = req.auth.data.name;
|
||||
examinationDetail.updated_by = req.auth?.data.name;
|
||||
await queryRunner.manager.save(examinationDetail);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
@ -239,7 +249,7 @@ export class ExaminationDetailController {
|
||||
|
||||
if (!examinationDetail) throw { message: "Examination Detail " + Language.lang.failed_not_found };
|
||||
|
||||
examinationDetail.deleted_by = req.auth.data.name;
|
||||
examinationDetail.deleted_by = req.auth?.data.name;
|
||||
examinationDetail.deleted_at = moment().toDate();
|
||||
await OrmHelper.DB.manager.save(examinationDetail);
|
||||
affected = 1;
|
||||
@ -288,4 +298,4 @@ export class ExaminationDetailController {
|
||||
return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed_restore, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -141,7 +141,7 @@ export class FareTypeController {
|
||||
// res_count.andWhere("munisipo.id = :munisipoId", { munisipoId: filterObj.munisipo_id });
|
||||
// }
|
||||
|
||||
const res_list = repo.createQueryBuilder("FareType");
|
||||
const res_list = repo.createQueryBuilder("FareType").orderBy("FareType.name", "ASC");
|
||||
|
||||
// Handle foreign key filter with relation
|
||||
// if (filterObj.munisipo_id) {
|
||||
|
||||
@ -83,7 +83,7 @@ export class ItemMasterController {
|
||||
"unit.large_unit",
|
||||
"unit.small_unit",
|
||||
"item_master.pack_size",
|
||||
]);
|
||||
]).orderBy("item_master.item_name", "ASC");
|
||||
|
||||
const total_count_data = await query.getCount();
|
||||
const list_data = await query.getMany();
|
||||
|
||||
@ -79,7 +79,7 @@ export class ItemOriginController {
|
||||
query.select([
|
||||
"item_origin.id",
|
||||
"item_origin.origin_name",
|
||||
]);
|
||||
]).orderBy("item_origin.origin_name", "ASC");
|
||||
|
||||
const total_count_data = await query.getCount();
|
||||
const list_data = await query.getMany();
|
||||
|
||||
@ -79,7 +79,7 @@ export class SupplierController {
|
||||
query.select([
|
||||
"supplier.id",
|
||||
"supplier.supplier_name",
|
||||
]);
|
||||
]).orderBy("supplier.supplier_name", "ASC");
|
||||
|
||||
const total_count_data = await query.getCount();
|
||||
const list_data = await query.getMany();
|
||||
|
||||
@ -77,7 +77,7 @@ export class RefferalHospitalController {
|
||||
var query = await RefferalHospitalModel.list();
|
||||
|
||||
const res_count = query;
|
||||
const res_list = query.orderBy("refferal_hospital.created_at", "DESC");
|
||||
const res_list = query.orderBy("refferal_hospital.refferal_hospital_name", "ASC");
|
||||
|
||||
const current_page = 1;
|
||||
const total_count_data = await res_count.getCount();
|
||||
|
||||
@ -15,7 +15,7 @@ const log: Logger<ILogObj> = new Logger({
|
||||
});
|
||||
|
||||
const RegistrationFeeCreateAndUpdateSchema = {
|
||||
service: Joi.array().items(Joi.string()).optional().label("Service"),
|
||||
service: Joi.array().items(Joi.string().allow(null,"").uuid()).optional().label("Service"),
|
||||
type: Joi.string().valid("emergency", "outpatient").required().label("Type"),
|
||||
};
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ import fileUpload from "express-fileupload";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { Brackets } from "typeorm";
|
||||
|
||||
const log: Logger<ILogObj> = new Logger({
|
||||
name: "[RoomController]",
|
||||
@ -92,24 +93,60 @@ export class RoomController {
|
||||
/*
|
||||
#swagger.tags = ['Room']
|
||||
#swagger.security = [{ "bearerAuth": [] }]
|
||||
*/
|
||||
#swagger.parameters['id'] = {
|
||||
in: 'query',
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'Department Id (optional)'
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
id: Joi.string().uuid().optional().label("Department Id"),
|
||||
});
|
||||
|
||||
let param: any = await schema.validateAsync(req.query);
|
||||
|
||||
const query = await RoomModel.list();
|
||||
|
||||
query.select([
|
||||
"Room.id",
|
||||
"Room.room",
|
||||
]);
|
||||
query
|
||||
.select([
|
||||
"Room.id",
|
||||
"Room.room",
|
||||
])
|
||||
.orderBy("Room.room", "ASC");
|
||||
|
||||
// ✅ optional filter by departmentId
|
||||
if (param.id) {
|
||||
query.andWhere("Room.departmentId = :departmentId", {
|
||||
departmentId: param.id,
|
||||
});
|
||||
}
|
||||
|
||||
const total_count_data = await query.getCount();
|
||||
const list_data = await query.getMany();
|
||||
const count_data = CommonHelper.countObject(list_data);
|
||||
|
||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, 1, total_count_data, list_data);
|
||||
return ReturnHelper.successResponselist(
|
||||
res,
|
||||
200,
|
||||
Language.lang.success_view,
|
||||
count_data,
|
||||
1,
|
||||
total_count_data,
|
||||
list_data
|
||||
);
|
||||
} catch (e: unknown) {
|
||||
log.error(e);
|
||||
const err = e as Error;
|
||||
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||
|
||||
return ReturnHelper.errorResponse(
|
||||
res,
|
||||
400,
|
||||
401,
|
||||
Language.lang.failed_view,
|
||||
err.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -32,109 +32,189 @@ export class RoomToPharmacyController {
|
||||
limit: Joi.number().required().min(1).label("Limit"),
|
||||
with_deleted: Joi.bool().required().label("With Deleted"),
|
||||
order_field: Joi.string().required().label("Order Field"),
|
||||
order_direction: Joi.string().allow("asc", "desc").required().label("Order Direction"),
|
||||
order_direction: Joi.string().allow("ASC", "DESC").required().label("Order Direction"),
|
||||
});
|
||||
const param: Paging = await schema.validateAsync(req.query);
|
||||
|
||||
let param: Paging = await schema.validateAsync(req.query);
|
||||
let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
||||
const filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
||||
const filterAny = filter.any && filter.any !== "" ? filter.any : "";
|
||||
let filterObj = { ...filter } as any;
|
||||
delete filterObj.any;
|
||||
|
||||
const query = await RoomToPharmacyModel.list(filterObj);
|
||||
const offset = (param.page - 1) * param.limit;
|
||||
const limit = param.limit;
|
||||
|
||||
const roomQuery = OrmHelper.DB.getRepository(Room)
|
||||
.createQueryBuilder("room")
|
||||
.leftJoinAndSelect("room.department", "department")
|
||||
.leftJoinAndMapMany(
|
||||
"room.roomToPharmacies",
|
||||
RoomToPharmacy,
|
||||
"room_to_pharmacy",
|
||||
"room_to_pharmacy.room_id = room.id"
|
||||
)
|
||||
.leftJoinAndMapOne(
|
||||
"room_to_pharmacy.pharmacy_room",
|
||||
Room,
|
||||
"pharmacy_room",
|
||||
"pharmacy_room.id = room_to_pharmacy.pharmacy_room_id"
|
||||
)
|
||||
.where("department.id IN (:...departmentIds)", {
|
||||
departmentIds: [
|
||||
'42bc42a7-e2a7-4f95-a788-9d9e51a51a20',
|
||||
'3fc39ff1-b4bc-4276-92ef-def7ca9432fb',
|
||||
'a0d2ef11-6927-4e5c-8628-6acf26351071',
|
||||
]
|
||||
})
|
||||
.orderBy("room." + param.order_field, param.order_direction as "ASC" | "DESC");
|
||||
const res_count = query;
|
||||
const res_list = query
|
||||
.leftJoinAndSelect("room_to_pharmacy.pharmacy_room", "emrregistration")
|
||||
.leftJoinAndSelect("room_to_pharmacy.room", "room")
|
||||
.orderBy("room_to_pharmacy." + param.order_field, param.order_direction)
|
||||
.offset(offset)
|
||||
.limit(param.limit);
|
||||
|
||||
if (param.with_deleted) {
|
||||
roomQuery.withDeleted();
|
||||
res_count.withDeleted();
|
||||
res_list.withDeleted();
|
||||
}
|
||||
|
||||
// Filter tambahan jika ada
|
||||
if (filter.room) {
|
||||
roomQuery.andWhere("room.room ILIKE :room", { room: `%${filter.room}%` });
|
||||
}
|
||||
if (filter.status) {
|
||||
roomQuery.andWhere("room.status = :status", { status: filter.status });
|
||||
}
|
||||
const current_page = param.page;
|
||||
const total_count_data = await res_count.getCount();
|
||||
const list_data = await res_list.getMany();
|
||||
const count_data = CommonHelper.countObject(list_data);
|
||||
|
||||
const total_count_data = await roomQuery.clone().getCount();
|
||||
|
||||
const rooms = await roomQuery
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.getMany();
|
||||
|
||||
// Transform: { room_name: { pharmacy_room_1: {}, pharmacy_room_2: {} } }
|
||||
const list_data_return = rooms.map((room: any) => {
|
||||
const roomToPharmacies: any[] = room.roomToPharmacies ?? [];
|
||||
|
||||
const pharmacyRooms: Record<string, any> = {};
|
||||
roomToPharmacies.forEach((rtp: any, index: number) => {
|
||||
const pharmacy: Room | null = rtp.pharmacy_room ?? null;
|
||||
pharmacyRooms[`pharmacy_room_${index + 1}`] = pharmacy ? {
|
||||
id: pharmacy.id,
|
||||
room: pharmacy.room,
|
||||
code: pharmacy.code,
|
||||
description: pharmacy.description ?? null,
|
||||
status: pharmacy.status,
|
||||
} : null;
|
||||
});
|
||||
|
||||
return {
|
||||
rooms: {
|
||||
id: room.id,
|
||||
room: room.room,
|
||||
code: room.code,
|
||||
description: room.description ?? null,
|
||||
status: room.status,
|
||||
department: room.department ? {
|
||||
id: room.department.id,
|
||||
name: room.department.name,
|
||||
} : null,
|
||||
pharmacy_rooms: Object.keys(pharmacyRooms).length > 0
|
||||
? pharmacyRooms
|
||||
: null,
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
const count_data = CommonHelper.countObject(list_data_return);
|
||||
|
||||
return ReturnHelper.successResponselist(
|
||||
res,
|
||||
200,
|
||||
Language.lang.success_view,
|
||||
count_data,
|
||||
param.page,
|
||||
total_count_data,
|
||||
list_data_return
|
||||
);
|
||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
||||
} catch (e: unknown) {
|
||||
log.error(e);
|
||||
const err = e as Error;
|
||||
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||
}
|
||||
}
|
||||
// static async list(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||
// /*
|
||||
// #swagger.tags = ['RoomToPharmacy']
|
||||
// #swagger.security = [{ "bearerAuth": [] }]
|
||||
// #swagger.parameters['filter'] = { description: '', in: 'query', type: 'string' }
|
||||
// #swagger.parameters['limit'] = { in: 'query', required: true, type: 'number' }
|
||||
// #swagger.parameters['page'] = { in: 'query', required: true, type: 'number' }
|
||||
// #swagger.parameters['with_deleted'] = { in: 'query', required: true, type: 'boolean' }
|
||||
// #swagger.parameters['order_field'] = { in: 'query', required: true, type: 'string' }
|
||||
// #swagger.parameters['order_direction'] = { in: 'query', required: true, schema: { '@enum': ['ASC', 'DESC'] } }
|
||||
// */
|
||||
// try {
|
||||
// const schema = Joi.object().keys({
|
||||
// filter: Joi.string().allow("").optional().label("Filter"),
|
||||
// page: Joi.number().required().min(1).label("Page"),
|
||||
// limit: Joi.number().required().min(1).label("Limit"),
|
||||
// with_deleted: Joi.bool().required().label("With Deleted"),
|
||||
// order_field: Joi.string().required().label("Order Field"),
|
||||
// order_direction: Joi.string().allow("asc", "desc").required().label("Order Direction"),
|
||||
// });
|
||||
|
||||
// let param: Paging = await schema.validateAsync(req.query);
|
||||
// let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
||||
|
||||
// const offset = (param.page - 1) * param.limit;
|
||||
// const limit = param.limit;
|
||||
|
||||
// // const roomQuery = OrmHelper.DB.getRepository(Room)
|
||||
// // .createQueryBuilder("room")
|
||||
// // .leftJoinAndSelect("room.department", "department")
|
||||
// // .leftJoinAndMapMany(
|
||||
// // "room.roomToPharmacies",
|
||||
// // RoomToPharmacy,
|
||||
// // "room_to_pharmacy",
|
||||
// // "room_to_pharmacy.room_id = room.id"
|
||||
// // )
|
||||
// // .leftJoinAndMapOne(
|
||||
// // "room_to_pharmacy.pharmacy_room",
|
||||
// // Room,
|
||||
// // "pharmacy_room",
|
||||
// // "pharmacy_room.id = room_to_pharmacy.pharmacy_room_id"
|
||||
// // )
|
||||
// // .where("department.id IN (:...departmentIds)", {
|
||||
// // departmentIds: [
|
||||
// // '42bc42a7-e2a7-4f95-a788-9d9e51a51a20',
|
||||
// // '3fc39ff1-b4bc-4276-92ef-def7ca9432fb',
|
||||
// // 'a0d2ef11-6927-4e5c-8628-6acf26351071',
|
||||
// // ]
|
||||
// // })
|
||||
// // .orderBy("room." + param.order_field, param.order_direction as "ASC" | "DESC");
|
||||
|
||||
// const roomQuery = OrmHelper.DB.getRepository(Room)
|
||||
// .createQueryBuilder("room")
|
||||
// .leftJoinAndSelect("room.department", "department")
|
||||
// .leftJoinAndSelect("room.roomToPharmacies", "room_to_pharmacy")
|
||||
// // .leftJoinAndMapMany(
|
||||
// // "room.roomToPharmacies",
|
||||
// // RoomToPharmacy,
|
||||
// // "room_to_pharmacy",
|
||||
// // "room_to_pharmacy.room_id = room.id"
|
||||
// // )
|
||||
// // .leftJoinAndMapOne(
|
||||
// // "room_to_pharmacy.pharmacy_room",
|
||||
// // Room,
|
||||
// // "pharmacy_room",
|
||||
// // "pharmacy_room.id = room_to_pharmacy.pharmacy_room_id"
|
||||
// // )
|
||||
// .where("department.id IN (:...departmentIds)", {
|
||||
// departmentIds: [
|
||||
// '42bc42a7-e2a7-4f95-a788-9d9e51a51a20',
|
||||
// '3fc39ff1-b4bc-4276-92ef-def7ca9432fb',
|
||||
// 'a0d2ef11-6927-4e5c-8628-6acf26351071',
|
||||
// ]
|
||||
// })
|
||||
// .orderBy("room." + param.order_field, param.order_direction as "ASC" | "DESC");
|
||||
|
||||
// if (param.with_deleted) {
|
||||
// roomQuery.withDeleted();
|
||||
// }
|
||||
|
||||
// // Filter tambahan jika ada
|
||||
// if (filter.room) {
|
||||
// roomQuery.andWhere("room.room ILIKE :room", { room: `%${filter.room}%` });
|
||||
// }
|
||||
// if (filter.status) {
|
||||
// roomQuery.andWhere("room.status = :status", { status: filter.status });
|
||||
// }
|
||||
|
||||
// const total_count_data = await roomQuery.clone().getCount();
|
||||
|
||||
// const rooms = await roomQuery
|
||||
// .offset(offset)
|
||||
// .limit(limit)
|
||||
// .getMany();
|
||||
|
||||
// // Transform: { room_name: { pharmacy_room_1: {}, pharmacy_room_2: {} } }
|
||||
// const list_data_return = rooms.map((room: any) => {
|
||||
// const roomToPharmacies: any[] = room.roomToPharmacies ?? [];
|
||||
|
||||
// const pharmacyRooms: Record<string, any> = {};
|
||||
// roomToPharmacies.forEach((rtp: any, index: number) => {
|
||||
// const pharmacy: Room | null = rtp.pharmacy_room ?? null;
|
||||
// pharmacyRooms[`pharmacy_room_${index + 1}`] = pharmacy ? {
|
||||
// id: pharmacy.id,
|
||||
// room: pharmacy.room,
|
||||
// code: pharmacy.code,
|
||||
// description: pharmacy.description ?? null,
|
||||
// status: pharmacy.status,
|
||||
// } : null;
|
||||
// });
|
||||
|
||||
// return {
|
||||
// rooms: {
|
||||
// id: room.id,
|
||||
// room: room.room,
|
||||
// code: room.code,
|
||||
// description: room.description ?? null,
|
||||
// status: room.status,
|
||||
// department: room.department ? {
|
||||
// id: room.department.id,
|
||||
// name: room.department.name,
|
||||
// } : null,
|
||||
// pharmacy_rooms: Object.keys(pharmacyRooms).length > 0
|
||||
// ? pharmacyRooms
|
||||
// : null,
|
||||
// }
|
||||
// };
|
||||
// });
|
||||
|
||||
// const count_data = CommonHelper.countObject(list_data_return);
|
||||
|
||||
// return ReturnHelper.successResponselist(
|
||||
// res,
|
||||
// 200,
|
||||
// Language.lang.success_view,
|
||||
// count_data,
|
||||
// param.page,
|
||||
// total_count_data,
|
||||
// list_data_return
|
||||
// );
|
||||
// } catch (e: unknown) {
|
||||
// log.error(e);
|
||||
// const err = e as Error;
|
||||
// return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||
// }
|
||||
// }
|
||||
|
||||
static async export(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||
/*
|
||||
@ -303,7 +383,7 @@ export class RoomToPharmacyController {
|
||||
#swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}]
|
||||
|
||||
|
||||
#swagger.parameters['room_id'] = {
|
||||
in: 'path',
|
||||
description: 'Room ID.',
|
||||
@ -316,7 +396,7 @@ export class RoomToPharmacyController {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/room_to_pharmacy"
|
||||
$ref: "#/components/schemas/room_to_pharmacy"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -324,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'];
|
||||
@ -340,20 +414,36 @@ 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) {
|
||||
// ambil pharmacy room
|
||||
const pharmacyRoom = await OrmHelper.DB.getRepository(Room).findOneBy({
|
||||
id: param.pharmacy_room_id
|
||||
});
|
||||
|
||||
if (!pharmacyRoom) {
|
||||
throw new Error("Pharmacy Room not found");
|
||||
}
|
||||
|
||||
if (data) {
|
||||
// ✅ UPDATE (replace pharmacy_room)
|
||||
data.pharmacy_room = pharmacyRoom;
|
||||
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" };
|
||||
// ✅ INSERT baru
|
||||
const room = await OrmHelper.DB.getRepository(Room).findOneBy({
|
||||
id: param.room_id
|
||||
});
|
||||
|
||||
const pharmacyRoom = await OrmHelper.DB.getRepository(Room).findOneBy({ id: param.pharmacy_room_id });
|
||||
if (!pharmacyRoom) throw { message: "Pharmacy Room not found" };
|
||||
if (!room) throw new Error("Room not found");
|
||||
|
||||
data = new RoomToPharmacy();
|
||||
data.room = room;
|
||||
@ -361,15 +451,27 @@ export class RoomToPharmacyController {
|
||||
data.created_by = req.auth?.data.name;
|
||||
data.updated_by = req.auth?.data.name;
|
||||
|
||||
await OrmHelper.DB.manager.save(data);
|
||||
await repo.save(data);
|
||||
}
|
||||
|
||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, 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