update: pharmacy to room list
This commit is contained in:
@ -32,109 +32,189 @@ export class RoomToPharmacyController {
|
|||||||
limit: Joi.number().required().min(1).label("Limit"),
|
limit: Joi.number().required().min(1).label("Limit"),
|
||||||
with_deleted: Joi.bool().required().label("With Deleted"),
|
with_deleted: Joi.bool().required().label("With Deleted"),
|
||||||
order_field: Joi.string().required().label("Order Field"),
|
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);
|
const filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
||||||
let 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 offset = (param.page - 1) * param.limit;
|
||||||
const limit = param.limit;
|
|
||||||
|
|
||||||
const roomQuery = OrmHelper.DB.getRepository(Room)
|
const res_count = query;
|
||||||
.createQueryBuilder("room")
|
const res_list = query
|
||||||
.leftJoinAndSelect("room.department", "department")
|
.leftJoinAndSelect("room_to_pharmacy.pharmacy_room", "emrregistration")
|
||||||
.leftJoinAndMapMany(
|
.leftJoinAndSelect("room_to_pharmacy.room", "room")
|
||||||
"room.roomToPharmacies",
|
.orderBy("room_to_pharmacy." + param.order_field, param.order_direction)
|
||||||
RoomToPharmacy,
|
.offset(offset)
|
||||||
"room_to_pharmacy",
|
.limit(param.limit);
|
||||||
"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) {
|
if (param.with_deleted) {
|
||||||
roomQuery.withDeleted();
|
res_count.withDeleted();
|
||||||
|
res_list.withDeleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter tambahan jika ada
|
const current_page = param.page;
|
||||||
if (filter.room) {
|
const total_count_data = await res_count.getCount();
|
||||||
roomQuery.andWhere("room.room ILIKE :room", { room: `%${filter.room}%` });
|
const list_data = await res_list.getMany();
|
||||||
}
|
const count_data = CommonHelper.countObject(list_data);
|
||||||
if (filter.status) {
|
|
||||||
roomQuery.andWhere("room.status = :status", { status: filter.status });
|
|
||||||
}
|
|
||||||
|
|
||||||
const total_count_data = await roomQuery.clone().getCount();
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
||||||
|
|
||||||
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) {
|
} catch (e: unknown) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
const err = e as Error;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 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> {
|
static async export(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user