import { param, get, post, getModelSchemaRef, del, requestBody, patch, } from '@loopback/rest'; import { CONTENT_TYPE, ErrorCodes, STATUS_CODE, SuccessResponse, } from '@sourcefuse-npm/alivio-lib'; import {authenticate, STRATEGY} from 'loopback4-authentication'; import {authorize} from 'loopback4-authorization'; import {MoodFactorLogsService} from '../services'; import {inject} from '@loopback/core'; import {Followup, FollowupAlert, FollowupCountDto} from '../models'; import {AppointmentStatus} from '../utility'; import moment from 'moment'; import { Filter } from '@loopback/repository'; const patientFollowupPath = '/patients/{id}/follow-ups'; const followupAlerts='follow-ups'; export class FollowupController { constructor( @inject('services.MoodFactorLogsService') protected patientService: MoodFactorLogsService, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${patientFollowupPath}/`, { responses: { [STATUS_CODE.OK]: { description: 'Array of Followup model instances', content: { 'application/json': { schema: { items: getModelSchemaRef(Followup, { includeRelations: true, }), }, }, }, }, }, }) async getPatientFollowups( @param.path.number('id') id: number, @param.header.string('Authorization') token?: string, @param.query.number('limit', {required: false}) limit?: number, @param.query.number('offset', {required: false}) offset?: number, @param.query.date('filterDate', {required: false}) filterDate?: Date, ): Promise { let filter: Filter = { where: { patientId: id, }, limit:limit, offset:offset, order: ['createdOn DESC'], }; if (filterDate) { const startEndDates = { startDate: new Date(moment(filterDate).startOf('day').toString()), endDate: new Date( moment(filterDate) .endOf('day') .toString(), ), }; filter = { where: { patientId: id, createdOn: {between: [startEndDates.startDate, startEndDates.endDate]}, }, limit:limit, offset:offset, order: ['createdOn DESC'], }; } return this.patientService.getFollowups( id, token, filter ); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @post(`${patientFollowupPath}/`, { responses: { [STATUS_CODE.OK]: { description: 'patient Followup model instance', content: {'application/json': {schema: getModelSchemaRef(Followup)}}, }, }, }) async addPatientFollowup( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRef(Followup), }, }, }) followup: Omit, @param.path.number('id') id: number, @param.header.string('Authorization') token?: string, ): Promise { return this.patientService.addFollowup( id, followup, token, ); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @del(`${patientFollowupPath}/{followupId}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'delete Followup successfully', content: { [CONTENT_TYPE.JSON]: {schema: getModelSchemaRef(Followup)}, }, }, ...ErrorCodes, }, }) async deleteFollowup( @param.path.number('id') id: number, @param.path.number('followupId') followupId: number, @param.header.string('Authorization') token?: string, ): Promise { this.patientService.deleteFollowup(id, followupId, token); return new SuccessResponse({ success: true, }); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${patientFollowupPath}/{followupId}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'fetch Followup details', content: { [CONTENT_TYPE.JSON]: {schema: getModelSchemaRef(Followup)}, }, }, ...ErrorCodes, }, }) async getFollowup( @param.path.number('id') id: number, @param.path.number('followupId') followupId: number, @param.header.string('Authorization') token?: string, ): Promise { return this.patientService.getFollowup(id, followupId, token); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @patch(`${patientFollowupPath}/{followupId}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'update Followup details', content: { [CONTENT_TYPE.JSON]: {schema: getModelSchemaRef(Followup)}, }, }, ...ErrorCodes, }, }) async updateFollowup( @param.path.number('id') id: number, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRef(Followup), }, }, }) followup: Omit, @param.path.number('followupId') followupId: number, @param.header.string('Authorization') token?: string, ): Promise { this.patientService.updateFollowup(id, followupId, followup, token); return new SuccessResponse({ success: true, }); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${followupAlerts}`, { responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Get Follow up alerts', content: { [CONTENT_TYPE.JSON]: {schema: getModelSchemaRef(FollowupAlert)}, }, }, ...ErrorCodes, }, }) async getFollowupAlerts( @param.header.string('Authorization') token?: string, ): Promise { return this.patientService.getFollowupAlerts(token); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @get(`${patientFollowupPath}/status-count`, { responses: { [STATUS_CODE.OK]: { description: 'Gets the patient follow ups count', content: { 'application/json': { schema: { items: getModelSchemaRef(Followup, { includeRelations: true, }), }, }, }, }, }, }) async getPatientFollowupStatusCount( @param.path.number('id') id: number, @param.header.string('Authorization') token?: string, ): Promise { const result=await this.patientService.getPatientFollowupsCount( id, token ); const responce= new FollowupCountDto(); responce.upComingCount= result.filter(appt=>appt.followUpStatus===Number(AppointmentStatus.Upcoming)).length ?? 0; responce.cancelledCount= result.filter(appt=>appt.followUpStatus===Number(AppointmentStatus.Cancelled)).length ?? 0; responce.successCount= result.filter(appt=>appt.followUpStatus===Number(AppointmentStatus.Successfull)).length ?? 0; responce.unCancelledCount= result.filter(appt=>appt.followUpStatus===Number(AppointmentStatus.Unsuccessfull)).length ?? 0; return responce; } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: ['*']}) @patch(`${followupAlerts}/{followupId}/status`, { responses: { [STATUS_CODE.OK]: { description: 'Update Followup Status', content: { 'application/json': { schema: { items: getModelSchemaRef(Followup, { includeRelations: true, }), }, }, }, }, }, }) async updatePatientFollowupStatus( @param.path.number('followupId') followupId: number, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRef(FollowupAlert), }, }, }) followupAlert: Omit, @param.header.string('Authorization') token?: string, ): Promise { this.patientService.updatePatientFollowup(followupId,followupAlert, token); return new SuccessResponse({ success: true, }); } }