update: add create for service examination
This commit is contained in:
@ -74,6 +74,89 @@ export class ServiceExaminationController {
|
||||
}
|
||||
}
|
||||
|
||||
static async create(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||
/*
|
||||
#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<void | Response> {
|
||||
/*
|
||||
#swagger.tags = ['Service Examination']
|
||||
@ -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();
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user