update: Add service class relation to room entity and update related code

This commit is contained in:
wayanrivan
2026-03-30 08:25:01 +07:00
parent dc1e88a331
commit 2a62ce3fd3
2 changed files with 15 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { Room, Paging } from "entity"; import { Room, Paging, Service } from "entity";
import { NextFunction, Response } from "express"; import { NextFunction, Response } from "express";
import { Request } from "express-jwt"; import { Request } from "express-jwt";
import Joi from "joi"; import Joi from "joi";
@ -8,6 +8,7 @@ import { ReturnHelper } from "../helpers/express/return";
import { Language } from "../langs/lang"; import { Language } from "../langs/lang";
import { RoomModel } from "../model/room"; import { RoomModel } from "../model/room";
import { DepartmentModel } from "../model/department"; import { DepartmentModel } from "../model/department";
import { ServiceClassModel } from "../model/service_class";
import { OrmHelper } from "../helpers/orm"; import { OrmHelper } from "../helpers/orm";
import moment from "moment"; import moment from "moment";
import fileUpload from "express-fileupload"; import fileUpload from "express-fileupload";
@ -56,6 +57,7 @@ export class RoomController {
const res_count = query; const res_count = query;
const res_list = query const res_list = query
.leftJoinAndSelect("Room.department", "department") .leftJoinAndSelect("Room.department", "department")
.leftJoinAndSelect("Room.serviceclass", "serviceclass")
.orderBy("Room." + param.order_field, param.order_direction) .orderBy("Room." + param.order_field, param.order_direction)
.offset(offset) .offset(offset)
.limit(param.limit); .limit(param.limit);
@ -125,6 +127,7 @@ export class RoomController {
code: Joi.string().max(128).required().label("Code"), code: Joi.string().max(128).required().label("Code"),
description: Joi.string().allow("").max(512).optional().label("Description"), description: Joi.string().allow("").max(512).optional().label("Description"),
department_id: Joi.string().uuid().required().label("Department ID"), department_id: Joi.string().uuid().required().label("Department ID"),
serviceclass: Joi.string().uuid().required().label("Service Class ID"),
status: Joi.string().required().label("Status"), status: Joi.string().required().label("Status"),
location: Joi.string().required().label("Location"), location: Joi.string().required().label("Location"),
picture: Joi.string().required().label("picture"), picture: Joi.string().required().label("picture"),
@ -138,11 +141,15 @@ export class RoomController {
let department = await DepartmentModel.list({ id: param.department_id }).then((q) => q.getOne()); let department = await DepartmentModel.list({ id: param.department_id }).then((q) => q.getOne());
if (!department) throw { message: "Department not found" }; if (!department) throw { message: "Department not found" };
let serviceclass = await ServiceClassModel.list({ id: param.serviceclass }).then((q) => q.getOne());
if (!serviceclass) throw { message: "Class not found" };
let room = new Room(); let room = new Room();
room.room = param.room; room.room = param.room;
room.code = param.code; room.code = param.code;
room.description = param.description || null; room.description = param.description || null;
room.department = department; room.department = department;
room.serviceclass = param.serviceclass;
room.status = param.status; room.status = param.status;
room.location = param.location; room.location = param.location;
room.picture = param.picture; room.picture = param.picture;
@ -174,7 +181,7 @@ export class RoomController {
}); });
let param: any = await schema.validateAsync(req.params); let param: any = await schema.validateAsync(req.params);
let room = await RoomModel.list({ "Room.id": param.id }) let room = await RoomModel.list({ "Room.id": param.id })
.then((q) => q.leftJoinAndSelect("Room.department", "department").getOne()); .then((q) => q.leftJoinAndSelect("Room.department", "department").leftJoinAndSelect("Room.serviceclass", "serviceclass").getOne());
if (!room) throw { message: "Room Not Found" }; if (!room) throw { message: "Room Not Found" };
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, room); return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, room);
} catch (e: unknown) { } catch (e: unknown) {
@ -201,6 +208,7 @@ export class RoomController {
code: Joi.string().max(128).required().label("Code"), code: Joi.string().max(128).required().label("Code"),
description: Joi.string().allow("").max(512).optional().label("Description"), description: Joi.string().allow("").max(512).optional().label("Description"),
department_id: Joi.string().uuid().required().label("Department ID"), department_id: Joi.string().uuid().required().label("Department ID"),
serviceclass: Joi.string().uuid().required().label("Service Class ID"),
status: Joi.string().required().label("Status"), status: Joi.string().required().label("Status"),
location: Joi.string().required().label("Location"), location: Joi.string().required().label("Location"),
picture: Joi.string().required().label("picture"), picture: Joi.string().required().label("picture"),
@ -213,12 +221,16 @@ export class RoomController {
let department = await DepartmentModel.list({ id: param.department_id }).then((q) => q.getOne()); let department = await DepartmentModel.list({ id: param.department_id }).then((q) => q.getOne());
if (!department) throw { message: "Department not found" }; if (!department) throw { message: "Department not found" };
let serviceclass = await ServiceClassModel.list({ id: param.serviceclass }).then((q) => q.getOne());
if (!serviceclass) throw { message: "Class not found" };
let room = await RoomModel.list({ "Room.id": param.id }).then((q) => q.getOne()); let room = await RoomModel.list({ "Room.id": param.id }).then((q) => q.getOne());
if (!room) throw { message: "Room " + Language.lang.failed_not_found }; if (!room) throw { message: "Room " + Language.lang.failed_not_found };
room.room = param.room; room.room = param.room;
room.code = param.code; room.code = param.code;
room.description = param.description || null; room.description = param.description || null;
room.serviceclass = param.serviceclass;
room.department = department; room.department = department;
room.status = param.status; room.status = param.status;
room.location = param.location; room.location = param.location;

View File

@ -111,6 +111,7 @@ const doc = {
$code: "R001", $code: "R001",
$description: "Optional description", $description: "Optional description",
$department_id: "uuid-string", $department_id: "uuid-string",
$serviceclass: "uuid-string",
$status: "Y", $status: "Y",
$location: "Room location", $location: "Room location",
$picture: "Room picture name", $picture: "Room picture name",