update: add create for service examination
This commit is contained in:
@ -18,15 +18,15 @@ const log: Logger<ILogObj> = new Logger({
|
|||||||
export class ServiceExaminationController {
|
export class ServiceExaminationController {
|
||||||
static async list(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
static async list(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['Service Examination']
|
#swagger.tags = ['Service Examination']
|
||||||
#swagger.security = [{ "bearerAuth": [] }]
|
#swagger.security = [{ "bearerAuth": [] }]
|
||||||
#swagger.parameters['filter'] = { description: 'Filter: plain text or JSON object (service_id, etc.)', in: 'query', type: 'string' }
|
#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['limit'] = { in: 'query', required: true, type: 'number' }
|
||||||
#swagger.parameters['page'] = { 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['with_deleted'] = { in: 'query', required: true, type: 'boolean' }
|
||||||
#swagger.parameters['order_field'] = { in: 'query', required: true, type: 'string' }
|
#swagger.parameters['order_field'] = { in: 'query', required: true, type: 'string' }
|
||||||
#swagger.parameters['order_direction'] = { in: 'query', required: true, schema: { '@enum': ['ASC', 'DESC'] } }
|
#swagger.parameters['order_direction'] = { in: 'query', required: true, schema: { '@enum': ['ASC', 'DESC'] } }
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
const schema = Joi.object().keys({
|
const schema = Joi.object().keys({
|
||||||
filter: Joi.string().allow("").optional().label("Filter"),
|
filter: Joi.string().allow("").optional().label("Filter"),
|
||||||
@ -74,13 +74,96 @@ 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> {
|
static async update(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['Service Examination']
|
#swagger.tags = ['Service Examination']
|
||||||
#swagger.security = [{ "bearerAuth": [] }]
|
#swagger.security = [{ "bearerAuth": [] }]
|
||||||
#swagger.parameters['id'] = { in: 'path', type: 'string' }
|
#swagger.parameters['id'] = { in: 'path', type: 'string' }
|
||||||
#swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/serviceExamination" } } } }
|
#swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/serviceExamination" } } } }
|
||||||
*/
|
*/
|
||||||
const queryRunner = OrmHelper.DB.createQueryRunner();
|
const queryRunner = OrmHelper.DB.createQueryRunner();
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
|
|
||||||
@ -108,7 +191,7 @@ export class ServiceExaminationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
service.examination_details = examinationDetails;
|
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.manager.save(service);
|
||||||
|
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
|
|||||||
@ -270,6 +270,7 @@ export class RoutePrivate {
|
|||||||
app.put('/api/examination-type/restore/:id', ExaminationTypeController.restore)
|
app.put('/api/examination-type/restore/:id', ExaminationTypeController.restore)
|
||||||
|
|
||||||
app.get('/api/service-examination/list', ServiceExaminationController.list)
|
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.put('/api/service-examination/:id', ServiceExaminationController.update)
|
||||||
|
|
||||||
app.get('/api/hospital-information/view', HospitalInformationController.view)
|
app.get('/api/hospital-information/view', HospitalInformationController.view)
|
||||||
|
|||||||
Reference in New Issue
Block a user