diff --git a/src/controllers/service_examination.ts b/src/controllers/service_examination.ts index 676bab0..659846a 100644 --- a/src/controllers/service_examination.ts +++ b/src/controllers/service_examination.ts @@ -18,15 +18,15 @@ const log: Logger = new Logger({ export class ServiceExaminationController { static async list(req: Request, res: Response, next: NextFunction): Promise { /* - #swagger.tags = ['Service Examination'] - #swagger.security = [{ "bearerAuth": [] }] - #swagger.parameters['filter'] = { description: 'Filter: plain text or JSON object (service_id, etc.)', in: 'query', type: 'string' } - #swagger.parameters['limit'] = { in: 'query', required: true, type: 'number' } - #swagger.parameters['page'] = { in: 'query', required: true, type: 'number' } - #swagger.parameters['with_deleted'] = { in: 'query', required: true, type: 'boolean' } - #swagger.parameters['order_field'] = { in: 'query', required: true, type: 'string' } - #swagger.parameters['order_direction'] = { in: 'query', required: true, schema: { '@enum': ['ASC', 'DESC'] } } - */ + #swagger.tags = ['Service Examination'] + #swagger.security = [{ "bearerAuth": [] }] + #swagger.parameters['filter'] = { description: 'Filter: plain text or JSON object (service_id, etc.)', in: 'query', type: 'string' } + #swagger.parameters['limit'] = { in: 'query', required: true, type: 'number' } + #swagger.parameters['page'] = { in: 'query', required: true, type: 'number' } + #swagger.parameters['with_deleted'] = { in: 'query', required: true, type: 'boolean' } + #swagger.parameters['order_field'] = { in: 'query', required: true, type: 'string' } + #swagger.parameters['order_direction'] = { in: 'query', required: true, schema: { '@enum': ['ASC', 'DESC'] } } + */ try { const schema = Joi.object().keys({ filter: Joi.string().allow("").optional().label("Filter"), @@ -74,13 +74,96 @@ export class ServiceExaminationController { } } + static async create(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['Service Examination'] + #swagger.security = [{ "bearerAuth": [] }] + #swagger.requestBody = { + required: true, + content: { + "application/json": { + schema: { $ref: "#/components/schemas/serviceExamination" } + } + } + } + */ + + const queryRunner = OrmHelper.DB.createQueryRunner(); + await queryRunner.startTransaction(); + + try { + const schema = Joi.object().keys({ + service_id: Joi.string().uuid().required().label("Service ID"), + examination_detail_ids: Joi.array() + .items(Joi.string().uuid()) + .optional() + .label("Examination Detail IDs"), + }); + + let param: any = await schema.validateAsync(req.body); + + // ✅ ambil service + let service = await ServiceModel.list({ "Service.id": param.service_id }) + .then((q) => q.leftJoinAndSelect("Service.serviceType", "st") + .leftJoinAndSelect("Service.examination_details", "ed") + .getOne()); + + if (!service) throw { message: "Service " + Language.lang.failed_not_found }; + + // ✅ ambil examination details + let examinationDetails: any[] = []; + if (param.examination_detail_ids && param.examination_detail_ids.length > 0) { + for (const examinationDetailId of param.examination_detail_ids) { + const examinationDetail = await ExaminationDetailModel + .list({ "ExaminationDetail.id": examinationDetailId }) + .then((q) => q.getOne()); + + if (examinationDetail) { + examinationDetails.push(examinationDetail); + } + } + } + + // ✅ assign (CREATE behavior) + service.examination_details = examinationDetails; + service.created_by = req.auth?.data.name; + service.updated_by = req.auth?.data.name; + + await queryRunner.manager.save(service); + + await queryRunner.commitTransaction(); + + return ReturnHelper.successResponseAny( + res, + 200, + Language.lang.success_insert, + service + ); + + } catch (e: unknown) { + await queryRunner.rollbackTransaction(); + log.error(e); + const err = e as Error; + + return ReturnHelper.errorResponse( + res, + 400, + 401, + Language.lang.failed_view, + err.message + ); + } finally { + await queryRunner.release(); + } + } + static async update(req: Request, res: Response, next: NextFunction): Promise { /* - #swagger.tags = ['Service Examination'] - #swagger.security = [{ "bearerAuth": [] }] - #swagger.parameters['id'] = { in: 'path', type: 'string' } - #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/serviceExamination" } } } } - */ + #swagger.tags = ['Service Examination'] + #swagger.security = [{ "bearerAuth": [] }] + #swagger.parameters['id'] = { in: 'path', type: 'string' } + #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/serviceExamination" } } } } + */ const queryRunner = OrmHelper.DB.createQueryRunner(); await queryRunner.startTransaction(); @@ -108,7 +191,7 @@ export class ServiceExaminationController { } service.examination_details = examinationDetails; - service.updated_by = req.auth.data.name; + service.updated_by = req.auth?.data.name; await queryRunner.manager.save(service); await queryRunner.commitTransaction(); diff --git a/src/routes/private.ts b/src/routes/private.ts index 2d6a308..d49a5d0 100644 --- a/src/routes/private.ts +++ b/src/routes/private.ts @@ -270,6 +270,7 @@ export class RoutePrivate { app.put('/api/examination-type/restore/:id', ExaminationTypeController.restore) app.get('/api/service-examination/list', ServiceExaminationController.list) + app.post('/api/service-examination/create', ServiceExaminationController.create) app.put('/api/service-examination/:id', ServiceExaminationController.update) app.get('/api/hospital-information/view', HospitalInformationController.view)