update: pharmacy to room list

This commit is contained in:
wayanrivan
2026-04-15 14:20:40 +07:00
parent 59b5fe9c97
commit 50d48134c0

View File

@ -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> {
/*