diff --git a/src/controllers/examination_type.ts b/src/controllers/examination_type.ts index 1b18833..9a028f5 100644 --- a/src/controllers/examination_type.ts +++ b/src/controllers/examination_type.ts @@ -7,6 +7,7 @@ import CommonHelper from "../helpers/common"; import { ReturnHelper } from "../helpers/express/return"; import { Language } from "../langs/lang"; import { ExaminationTypeModel } from "../model/examination_type"; +import { ServiceModel } from "../model/service"; import { OrmHelper } from "../helpers/orm"; import moment from "moment"; @@ -48,6 +49,7 @@ export class ExaminationTypeController { const res_count = query; const res_list = query + .leftJoinAndSelect("ExaminationType.services", "services") .orderBy("ExaminationType." + param.order_field, param.order_direction) .offset(offset) .limit(limit); @@ -82,6 +84,7 @@ export class ExaminationTypeController { const schema = Joi.object().keys({ name: Joi.string().max(256).required().label("Name"), status: Joi.string().required().label("Status"), + service_ids: Joi.array().items(Joi.string().uuid()).optional().label("Service IDs"), }); let param: any = await schema.validateAsync(req.body); @@ -90,9 +93,18 @@ export class ExaminationTypeController { let exist = await ExaminationTypeModel.list(filter).then((q) => q.getOne()); if (exist) throw { message: "Examination Type " + Language.lang.failed_duplicate }; + let services: any[] = []; + if (param.service_ids && param.service_ids.length > 0) { + for (const serviceId of param.service_ids) { + const svc = await ServiceModel.list({ id: serviceId }).then((q) => q.getOne()); + if (svc) services.push(svc); + } + } + let examinationType = new ExaminationType(); examinationType.name = param.name; examinationType.status = param.status; + examinationType.services = services; examinationType.created_by = req.auth.data.name; examinationType.updated_by = req.auth.data.name; await queryRunner.manager.save(examinationType); @@ -123,7 +135,8 @@ export class ExaminationTypeController { let param: any = await schema.validateAsync(req.params); - let examinationType = await ExaminationTypeModel.list({ "ExaminationType.id": param.id }).then((q) => q.getOne()); + let examinationType = await ExaminationTypeModel.list({ "ExaminationType.id": param.id }) + .then((q) => q.leftJoinAndSelect("ExaminationType.services", "services").getOne()); if (!examinationType) throw { message: "Examination Type Not Found" }; @@ -152,6 +165,7 @@ export class ExaminationTypeController { id: Joi.string().uuid().required().label("Examination Type ID"), name: Joi.string().max(256).required().label("Name"), status: Joi.string().required().label("Status"), + service_ids: Joi.array().items(Joi.string().uuid()).optional().label("Service IDs"), }); let param: any = await schema.validateAsync(req.body); @@ -160,12 +174,22 @@ export class ExaminationTypeController { let exist = await ExaminationTypeModel.list(filter).then((q) => q.getOne()); if (exist && exist.id != param.id) throw { message: "Examination Type " + Language.lang.failed_duplicate }; - let examinationType = await ExaminationTypeModel.list({ "ExaminationType.id": param.id }).then((q) => q.getOne()); + let examinationType = await ExaminationTypeModel.list({ "ExaminationType.id": param.id }) + .then((q) => q.leftJoinAndSelect("ExaminationType.services", "services").getOne()); if (!examinationType) throw { message: "Examination Type " + Language.lang.failed_not_found }; + let services: any[] = []; + if (param.service_ids && param.service_ids.length > 0) { + for (const serviceId of param.service_ids) { + const svc = await ServiceModel.list({ id: serviceId }).then((q) => q.getOne()); + if (svc) services.push(svc); + } + } + examinationType.name = param.name; examinationType.status = param.status; + examinationType.services = services; examinationType.updated_by = req.auth.data.name; await queryRunner.manager.save(examinationType); diff --git a/src/swagger/builder.js b/src/swagger/builder.js index 9aa571f..b4984b7 100644 --- a/src/swagger/builder.js +++ b/src/swagger/builder.js @@ -171,7 +171,8 @@ const doc = { }, examinationType: { $name: "Examination Type name", - $status: { "@enum": ["Y", "N"] } + $status: { "@enum": ["Y", "N"] }, + $service_ids: ["uuid-string"] }, examinationDetail: { $name: "Examination Detail name",