feat(room): add enpoints list grouped by location
This commit is contained in:
@ -518,4 +518,47 @@ export class RoomController {
|
||||
return ReturnHelper.errorResponse(res, 500, 401, Language.lang.failed_view, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async listLocation(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||
/*
|
||||
#swagger.tags = ['Room']
|
||||
#swagger.security = [{ "bearerAuth": [] }]
|
||||
*/
|
||||
try {
|
||||
const query = await RoomModel.list();
|
||||
const list_data = await query
|
||||
.select(["Room.id AS id", "Room.room AS room", "Room.location AS location"])
|
||||
.orderBy("Room.location", "ASC")
|
||||
.addOrderBy("Room.room", "ASC")
|
||||
.getRawMany();
|
||||
|
||||
const grouped_data = list_data.reduce(
|
||||
(acc: Record<string, Array<{ id: string; room: string }>>, item: { id: string; room: string; location: string }) => {
|
||||
const locationKey = item.location || "unknown";
|
||||
if (!acc[locationKey]) {
|
||||
acc[locationKey] = [];
|
||||
}
|
||||
|
||||
acc[locationKey].push({
|
||||
id: item.id,
|
||||
room: item.room,
|
||||
});
|
||||
|
||||
return acc;
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
const formatted_data = Object.entries(grouped_data).map(([location, rooms]) => ({
|
||||
location,
|
||||
rooms,
|
||||
}));
|
||||
|
||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, formatted_data);
|
||||
} catch (e: unknown) {
|
||||
log.error(e);
|
||||
const err = e as Error;
|
||||
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,6 +155,7 @@ export class RoutePrivate {
|
||||
app.post("/api/room/upload-picture", RoomController.uploadpicture);
|
||||
app.delete("/api/room/delete-picture", RoomController.deletepicture);
|
||||
app.get("/api/room/get-template-picture", RoomController.gettemplatepicture);
|
||||
app.get("/api/room/list-location", RoomController.listLocation);
|
||||
|
||||
app.get("/api/laboratory/list", LaboratoryController.list);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user