update: list in room to pharmacy

This commit is contained in:
wayanrivan
2026-04-09 15:52:35 +07:00
parent c4aed0c142
commit 81b9105523
2 changed files with 147 additions and 69 deletions

View File

@ -36,31 +36,99 @@ export class RoomToPharmacyController {
});
let param: Paging = await schema.validateAsync(req.query);
let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
var query = await RoomToPharmacyModel.list(filter);
const offset = (param.page - 1) * param.limit;
let limit = param.limit;
const limit = param.limit;
const res_count = query;
const res_list = query
.orderBy("room_to_pharmacy." + param.order_field, param.order_direction)
.offset(offset)
.limit(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");
if (param.with_deleted) {
res_count.withDeleted();
res_list.withDeleted();
roomQuery.withDeleted();
}
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);
// 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 });
}
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, 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
);
} catch (e: unknown) {
log.error(e);
const err = e as Error;
@ -173,14 +241,14 @@ export class RoomToPharmacyController {
try {
const schema = Joi.object().keys({
pharmacy_room_id: Joi.string()
.uuid()
.required()
.label("Pharmacy Room ID"),
.uuid()
.required()
.label("Pharmacy Room ID"),
room_id: Joi.string()
.uuid()
.required()
.label("Room ID"),
.uuid()
.required()
.label("Room ID"),
// room_ids: Joi.array()
// .items(Joi.string().uuid().required())
@ -229,7 +297,7 @@ export class RoomToPharmacyController {
}
}
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
/*
#swagger.tags = ['RoomToPharmacy']
#swagger.security = [{
@ -253,14 +321,14 @@ export class RoomToPharmacyController {
id: Joi.string().uuid().required().label('ID'),
pharmacy_room_id: Joi.string()
.uuid()
.required()
.label("Pharmacy Room ID"),
.uuid()
.required()
.label("Pharmacy Room ID"),
room_id: Joi.string()
.uuid()
.required()
.label("Room ID"),
.uuid()
.required()
.label("Room ID"),
// room_ids: Joi.array()
// .items(Joi.string().uuid().required())
@ -295,26 +363,26 @@ export class RoomToPharmacyController {
}
static async detail(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
/*
#swagger.tags = ['RoomToPharmacy']
#swagger.security = [{ "bearerAuth": [] }]
#swagger.parameters['id'] = { description: '', in: 'path', type: 'string' }
*/
try {
const schema = Joi.object().keys({
id: Joi.string().uuid().required().label("RoomToPharmacy Id"),
});
/*
#swagger.tags = ['RoomToPharmacy']
#swagger.security = [{ "bearerAuth": [] }]
#swagger.parameters['id'] = { description: '', in: 'path', type: 'string' }
*/
try {
const schema = Joi.object().keys({
id: Joi.string().uuid().required().label("RoomToPharmacy Id"),
});
let param: any = await schema.validateAsync(req.params);
let param: any = await schema.validateAsync(req.params);
let data = await RoomToPharmacyModel.list({ "id": param.id }).then((q) => q.getOne());
if (!data) throw { message: "RoomToPharmacy Not Found" };
let data = await RoomToPharmacyModel.list({ "id": param.id }).then((q) => q.getOne());
if (!data) throw { message: "RoomToPharmacy Not Found" };
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
} catch (e: unknown) {
log.error(e);
const err = e as Error;
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_not_found, err.message);
}
}
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
} catch (e: unknown) {
log.error(e);
const err = e as Error;
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_not_found, err.message);
}
}
}

View File

@ -1,24 +1,34 @@
import { SelectQueryBuilder } from "typeorm";
import CommonHelper from "../helpers/common";
import { OrmHelper } from "../helpers/orm";
import { RoomToPharmacy } from "entity";
import { RoomToPharmacy, Room } from "entity";
export class RoomToPharmacyModel {
static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
let whereAttr: string[] = [];
let whereVal: any = {};
if (filter && Object.keys(filter).length > 0) {
whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter, "room_to_pharmacy").whereAttr];
whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter, "room_to_pharmacy").whereVal };
}
const repo = OrmHelper.DB.getRepository(Room);
var query = repo.createQueryBuilder("room_to_pharmacy")
.leftJoinAndSelect("room_to_pharmacy.room", "room")
.leftJoinAndSelect("room_to_pharmacy.pharmacy_room", "pharmacy_room");
if (whereAttr.length != 0) {
query = query.where(whereAttr.join(" and "), whereVal);
}
const query = repo
.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',
]
});
return query;
}