This commit is contained in:
wayanrivan
2026-04-23 14:24:36 +07:00
2 changed files with 44 additions and 36 deletions

View File

@ -108,10 +108,11 @@ export class RoomServicesController {
}
// Controller - List by Service (Service-sided)
static async listByService(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
static async listServices(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
/*
#swagger.tags = ['Room Services']
#swagger.security = [{ "bearerAuth": [] }]
#swagger.parameters['room_id'] = { description: 'Room ID', in: 'path', type: 'string' }
#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' }
@ -130,47 +131,54 @@ export class RoomServicesController {
});
let param: Paging = await schema.validateAsync(req.query);
const roomId = req.params["room_id"];
const room = await RoomModel.list({ "Room.id": roomId }).then((q) => q.getOne());
if (!room) throw { message: "Room " + Language.lang.failed_not_found };
let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
// Get total count of unique services
const query = await RoomServicesModel.listByService(filter, param.with_deleted);
const total_count_data = await query.getCount();
var query = await ServiceModel.list(filter);
// Get all services with rooms
const services = await query
const offset = (param.page - 1) * param.limit;
let limit = param.limit;
const res_count = query;
const res_list = query
.select([
"Service.id",
"Service.name",
"Service.created_at",
"Service.created_by",
"serviceType.id",
"serviceType.name",
"serviceFare.id",
"serviceFare.fare",
"faretype.id",
"faretype.name",
])
.leftJoin("Service.serviceType", "serviceType")
.leftJoin("Service.rooms", "room")
.leftJoin("room.serviceclass", "roomClass")
.innerJoin("Service.servicefare", "serviceFare")
.innerJoin("serviceFare.serviceClass", "serviceClass", "serviceClass.id = roomClass.id")
.innerJoin("serviceFare.faretype", "faretype")
.where("room.id = :roomId", { roomId })
.orderBy("Service." + param.order_field, param.order_direction)
.skip((param.page - 1) * param.limit)
.take(param.limit)
.getMany();
.skip(offset)
.take(limit);
// Transform data: Format service with rooms
const list_data = services.map(service => ({
id: service.id,
name: service.name,
rooms: service.rooms ? service.rooms.map(room => ({
room_id: room.id,
room: room.room,
code: room.code,
description: room.description,
status: room.status,
department: room.department ? {
id: room.department.id,
name: room.department.name
} : null
})) : []
}));
if (param.with_deleted) {
res_count.withDeleted();
res_list.withDeleted();
}
const count_data = list_data.length;
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);
return ReturnHelper.successResponselist(
res,
200,
Language.lang.success_view,
count_data,
param.page,
total_count_data,
list_data
);
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;

View File

@ -160,7 +160,7 @@ export class RoutePrivate {
app.get("/api/pharmacy/list", PharmacyController.list);
app.get("/api/room-services/list", RoomServicesController.list);
app.get("/api/room-services/list-by-service", RoomServicesController.listByService);
app.get("/api/room-services/list-services/:room_id", RoomServicesController.listServices);
app.get("/api/room-services/detail/:id", RoomServicesController.detail);
app.put("/api/room-services/:id", RoomServicesController.update);