diff --git a/src/controllers/registrationfee.ts b/src/controllers/registrationfee.ts new file mode 100644 index 0000000..887673f --- /dev/null +++ b/src/controllers/registrationfee.ts @@ -0,0 +1,116 @@ +import { NextFunction, Response } from "express"; +import { RegistrationFee, Service } from "entity"; +import { Request } from "express-jwt"; +import Joi from "joi"; +import { ILogObj, Logger } from "tslog"; +import CommonHelper from "../helpers/common"; +import { ReturnHelper } from "../helpers/express/return"; +import { Language } from "../langs/lang"; +import { OrmHelper } from "../helpers/orm"; +import moment from "moment"; + +const log: Logger = new Logger({ + name: "[RegistrationFee]", + type: "pretty", +}); + +const RegistrationFeeCreateAndUpdateSchema = { + service: Joi.array().items(Joi.string()).optional().label("Service"), + type: Joi.string().valid("emergency", "outpatient").required().label("Type"), +}; + +export class RegistrationFeeController { + static async create(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['Registration Fee'] + #swagger.security = [{ + "bearerAuth": [] + }] + #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/registration_fee" } } } } + */ + try { + const schema = Joi.object().keys({ + ...RegistrationFeeCreateAndUpdateSchema + }); + + const param: any = await schema.validateAsync(req.body); + + if (param.service && param.service.length > 0) { + for (const serviceId of param.service) { + const serviceExist = await OrmHelper.DB.getRepository(Service) + .createQueryBuilder("Service") + .where("Service.id = :id", { id: serviceId }) + .getOne(); + + if (!serviceExist) throw { message: `Service ID ${serviceId} not found` }; + } + } + + const data = new RegistrationFee(); + data.type = param.type; + data.service = Array.isArray(param.service) ? param.service : []; + data.created_by = req.auth.data.name; + + await OrmHelper.DB.getRepository(RegistrationFee).save(data); + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_insert, data); + } catch (e: unknown) { + log.error(e); + const err = e as Error; + return ReturnHelper.errorResponse(res, 500, 401, Language.lang.failed_insert, err.message); + } + } + + static async update(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['Registration Fee'] + #swagger.security = [{ "bearerAuth": [] }] + #swagger.parameters['id'] = { description: 'Room ID', in: 'path', type: 'string' } + #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/registration_fee" } } } } + */ + const queryRunner = OrmHelper.DB.createQueryRunner(); + await queryRunner.startTransaction(); + + try { + const schema = Joi.object().keys({ + ...RegistrationFeeCreateAndUpdateSchema, + id: Joi.string().uuid().required().label("ID"), + }); + + const param: any = await schema.validateAsync(req.body); + + if (param.service && param.service.length > 0) { + for (const serviceId of param.service) { + const serviceExist = await OrmHelper.DB.getRepository(Service) + .createQueryBuilder("Service") + .where("Service.id = :id", { id: serviceId }) + .getOne(); + + if (!serviceExist) throw { message: `Service ID ${serviceId} not found` }; + } + } + + const registrationfee = await OrmHelper.DB.getRepository(RegistrationFee) + .findOne({ where: { id: param.id } }); + + if (!registrationfee) throw { message: "Registration Fee " + Language.lang.failed_not_found }; + + registrationfee.type = param.type; + registrationfee.service = Array.isArray(param.service) ? param.service : []; + registrationfee.updated_by = req.auth.data.name; + + await OrmHelper.DB.manager.save(registrationfee); + + await queryRunner.commitTransaction(); + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, registrationfee); + } 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(); + } + } +} diff --git a/src/helpers/orm.ts b/src/helpers/orm.ts index 1e005ec..8b9febd 100644 --- a/src/helpers/orm.ts +++ b/src/helpers/orm.ts @@ -1,7 +1,7 @@ import config from 'config'; import { DataSource } from "typeorm"; import { ILogObj, Logger } from 'tslog'; -import { FareType, RoomStock, Administrativu, Aldeia, District, Menu, Munisipo, Suco, Application, Nationality, Diagnosis, DiagnosticProcedure, Room, ServiceType, ServiceClass, Service, ServiceFare, ServicePackage, Department, DoctorSchedule, MeasurementUnit, ReferenceRange, QueueMonitoring, ExaminationDetail, ExaminationType, User, UserRole, HrmsEmployee, HrmsDepartment, HrmsPosition, HrmsShift, HospitalInformation, ItemOrigin, ItemType, ItemTypeDetail, Unit, ItemCategory, ItemStatus, UsageInstructions, UsageTime, ItemClass, ItemClassDetail, GenericName, Factory, Supplier, FactoryToSupplier, ItemMaster, ItemGroup, PharmacyInfo, ItemPrice, SellingPricePercentage, PatientGroup, PatientGuarantor, InitialStock, DoctorItem, DoctorItemPackage, UserToRoom, RoomToPharmacy, PlanningVerificator, ScheduleSeries, ScheduleException, CardType, PaymentMethod } from 'entity' +import { RegistrationFee,FareType, RoomStock, Administrativu, Aldeia, District, Menu, Munisipo, Suco, Application, Nationality, Diagnosis, DiagnosticProcedure, Room, ServiceType, ServiceClass, Service, ServiceFare, ServicePackage, Department, DoctorSchedule, MeasurementUnit, ReferenceRange, QueueMonitoring, ExaminationDetail, ExaminationType, User, UserRole, HrmsEmployee, HrmsDepartment, HrmsPosition, HrmsShift, HospitalInformation, ItemOrigin, ItemType, ItemTypeDetail, Unit, ItemCategory, ItemStatus, UsageInstructions, UsageTime, ItemClass, ItemClassDetail, GenericName, Factory, Supplier, FactoryToSupplier, ItemMaster, ItemGroup, PharmacyInfo, ItemPrice, SellingPricePercentage, PatientGroup, PatientGuarantor, InitialStock, DoctorItem, DoctorItemPackage, UserToRoom, RoomToPharmacy, PlanningVerificator, ScheduleSeries, ScheduleException, CardType, PaymentMethod } from 'entity' export class OrmHelper { static DB: DataSource = null @@ -52,7 +52,8 @@ export class OrmHelper { ScheduleException, FareType, CardType, - PaymentMethod + PaymentMethod, + RegistrationFee ], subscribers: [], migrations: [], diff --git a/src/routes/private.ts b/src/routes/private.ts index 20dd6dc..81480fe 100644 --- a/src/routes/private.ts +++ b/src/routes/private.ts @@ -62,6 +62,7 @@ import { ScheduleController } from '../controllers/schedule'; import { FareTypeController } from '../controllers/faretype'; import { CardTypeController } from '../controllers/card_type'; import { PaymentMethodController } from '../controllers/payment-method'; +import { RegistrationFeeController } from '../controllers/registrationfee'; export class RoutePrivate { static setup(app: express.Application) { @@ -476,5 +477,8 @@ export class RoutePrivate { app.put('/api/payment-method/update/:id', PaymentMethodController.update) app.delete('/api/payment-method/delete/:id/:hard', PaymentMethodController.delete) app.put('/api/payment-method/restore/:id', PaymentMethodController.restore) + + app.post('/api/registration-fee/create', RegistrationFeeController.create) + app.put('/api/registration-fee/update/:id', RegistrationFeeController.update) } } diff --git a/src/swagger/builder.js b/src/swagger/builder.js index 5235156..2d45df0 100644 --- a/src/swagger/builder.js +++ b/src/swagger/builder.js @@ -456,6 +456,10 @@ const doc = { payment_method: { $name: "Name (string)", }, + registration_fee:{ + type: "emergency | outpatient", + service: ["uuid-string-1", "uuid-string-2"] + } }, parameters: {}, securitySchemes: {