import {inject} from '@loopback/core'; import { Count, CountSchema, Filter, FilterExcludingWhere, Where, } from '@loopback/repository'; import { post, param, get, getModelSchemaRef, patch, put, del, requestBody, HttpErrors, } from '@loopback/rest'; import { PermissionKey, STATUS_CODE, IAuthUserWithPermissions, } from '@sourcefuse-npm/alivio-lib'; import { authenticate, AuthenticationBindings, STRATEGY, } from 'loopback4-authentication'; import {authorize, AuthorizeErrorKeys} from 'loopback4-authorization'; import {PatientCarePlanDetails} from '../models'; import { DiPatientCaregiverUserService, PatientCarePlanDetailsService, } from '../services'; const patientCarePlanDetailPath = '/patient-care-plan-details'; export class PatientCarePlanDetailsController { constructor( @inject('services.PatientDetailsService') public patientCarePlanDetailsService: PatientCarePlanDetailsService, @inject('services.DiPatientCaregiverUserService') protected readonly patientCgUserService: DiPatientCaregiverUserService, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @post(`${patientCarePlanDetailPath}`, { responses: { [STATUS_CODE.OK]: { description: 'Create PatientCarePlanDetails model instance', content: { 'application/json': { schema: getModelSchemaRef(PatientCarePlanDetails), }, }, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(PatientCarePlanDetails, { title: 'NewPatientCarePlanDetails', exclude: ['id'], }), }, }, }) patientCarePlanDetails: Omit, @param.header.string('Authorization') token: string, @inject(AuthenticationBindings.CURRENT_USER) currentUser: IAuthUserWithPermissions, ): Promise { const isAccessAllowed = await this.patientCgUserService.isPatientAccessAllowed( currentUser, Number(patientCarePlanDetails.patientId), // eslint-disable-next-line @typescript-eslint/no-explicit-any PermissionKey.UpdateOwnPatient, token, ); if (!isAccessAllowed) { throw new HttpErrors.Forbidden(AuthorizeErrorKeys.NotAllowedAccess); } const plans = await this.patientCarePlanDetailsService.findByPatientId( patientCarePlanDetails.patientId, token, ); if (plans[0]) { return plans[0]; } else { return this.patientCarePlanDetailsService.create( patientCarePlanDetails, token, ); } } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${patientCarePlanDetailPath}/count`, { responses: { [STATUS_CODE.OK]: { description: 'PatientCarePlanDetails model count', content: {'application/json': {schema: CountSchema}}, }, }, }) async count( @param.where(PatientCarePlanDetails) where?: Where, @param.header.string('Authorization') token?: string, ): Promise { return this.patientCarePlanDetailsService.count(where, token); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${patientCarePlanDetailPath}`, { responses: { [STATUS_CODE.OK]: { description: 'Array of PatientCarePlanDetails model instances', content: { 'application/json': { schema: { type: 'array', items: getModelSchemaRef(PatientCarePlanDetails, { includeRelations: true, }), }, }, }, }, }, }) async find( @param.filter(PatientCarePlanDetails) filter?: Filter, @param.header.string('Authorization') token?: string, ): Promise { return this.patientCarePlanDetailsService.find(filter, token); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @patch(`${patientCarePlanDetailPath}`, { responses: { [STATUS_CODE.OK]: { description: 'PatientCarePlanDetails PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(PatientCarePlanDetails, {partial: true}), }, }, }) patientCarePlanDetails: PatientCarePlanDetails, @param.header.string('Authorization') token?: string, @param.where(PatientCarePlanDetails) where?: Where, ): Promise { return this.patientCarePlanDetailsService.updateAll( patientCarePlanDetails, where, token, ); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${patientCarePlanDetailPath}/{id}`, { responses: { [STATUS_CODE.OK]: { description: 'Get PatientCarePlanDetails model instance', content: { 'application/json': { schema: getModelSchemaRef(PatientCarePlanDetails, { includeRelations: true, }), }, }, }, }, }) async findById( @param.path.number('id') id: number, @param.filter(PatientCarePlanDetails, {exclude: 'where'}) filter?: FilterExcludingWhere, @param.header.string('Authorization') token?: string, ): Promise { return this.patientCarePlanDetailsService.findById(id, filter, token); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @patch(`${patientCarePlanDetailPath}/{id}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'PatientCarePlanDetails PATCH success', }, }, }) async updateById( @param.path.number('id') id: number, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(PatientCarePlanDetails, {partial: true}), }, }, }) patientCarePlanDetails: PatientCarePlanDetails, @param.header.string('Authorization') token?: string, ): Promise { await this.patientCarePlanDetailsService.updateById( id, patientCarePlanDetails, token, ); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @put(`${patientCarePlanDetailPath}/{id}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'PatientCarePlanDetails PUT success', }, }, }) async replaceById( @param.path.number('id') id: number, @requestBody() patientCarePlanDetails: PatientCarePlanDetails, @param.header.string('Authorization') token?: string, ): Promise { await this.patientCarePlanDetailsService.replaceById( id, patientCarePlanDetails, token, ); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @del(`${patientCarePlanDetailPath}/{id}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'PatientCarePlanDetails DELETE success', }, }, }) async deleteById( @param.path.number('id') id: number, @param.header.string('Authorization') token?: string, ): Promise { await this.patientCarePlanDetailsService.deleteById(id, token); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get('/patient/care-plan-details/{patient_id}', { responses: { [STATUS_CODE.OK]: { description: 'Get patient PatientCarePlanDetails model instance', content: { 'application/json': { schema: getModelSchemaRef(PatientCarePlanDetails, { includeRelations: true, }), }, }, }, }, }) async findCarePlanByPatientId( @param.path.number('patient_id') patientId: number, @inject(AuthenticationBindings.CURRENT_USER) currentUser: IAuthUserWithPermissions, @param.header.string('Authorization') token?: string, ): Promise { const isAccessAllowed = await this.patientCgUserService.isPatientAccessAllowed( currentUser, Number(patientId), // eslint-disable-next-line @typescript-eslint/no-explicit-any PermissionKey.UpdateOwnPatient, token, ); if (!isAccessAllowed) { throw new HttpErrors.Forbidden(AuthorizeErrorKeys.NotAllowedAccess); } const plans = await this.patientCarePlanDetailsService.findByPatientId( patientId, token, ); return plans.length ? plans[0] : ({} as PatientCarePlanDetails); } }