Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Update encounter date to pick visitStartDatetime #1405

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,22 @@ export class FormCreationService {

private setDefaultValues(form: Form, createFormParams: CreateFormParams) {
const { session } = createFormParams;
const config = this.configResourceService.getConfig();
let currentDate;

if (config.customEncounterDatetime === true) {
const visitStartDatetime = moment(this.singleSpaPropsService.getProp('visitStartDatetime')).format();
// If the visit start date is before the current date, use the visit start date as the default date.
if (visitStartDatetime && moment(visitStartDatetime).isBefore(currentDate, 'date')) {
currentDate = visitStartDatetime;
} else {
currentDate = moment().format();
}
} else {
currentDate = moment().format();
}

// Encounter date and time.
const currentDate = moment().format();
const encounterDate = form.searchNodeByQuestionId('encDate');
if (encounterDate.length > 0) {
encounterDate[0].control.setValue(currentDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { SingleSpaPropsService } from '../single-spa-props/single-spa-props.serv
import { v4 } from 'uuid';
import { VisitResourceService } from '../openmrs-api/visit-resource.service';
import { PatientResourceService } from '../openmrs-api/patient-resource.service';
import { ConfigResourceService } from '../services/config-resource.service';

/**
* The result of submitting a form via the {@link FormSubmissionService.submitPayload} function.
Expand All @@ -42,6 +43,7 @@ export class FormSubmissionService {
private readonly singleSpaPropsService: SingleSpaPropsService,
private readonly visitResourceService: VisitResourceService,
private readonly patientResourceService: PatientResourceService,
private readonly configResourceService: ConfigResourceService,
) {}

public submitPayload(form: Form): Observable<FormSubmissionResult> {
Expand Down Expand Up @@ -147,20 +149,31 @@ export class FormSubmissionService {
if (!encounterCreate) {
return of(undefined);
}
const config = this.configResourceService.getConfig();

const visitUuid = this.singleSpaPropsService.getPropOrThrow('visitUuid');
const visitStartDatetime = this.singleSpaPropsService.getProp('visitStartDatetime');
const visitStopDatetime = this.singleSpaPropsService.getProp('visitStopDatetime');

if (encounterCreate.uuid) {
if (visitStartDatetime && new Date(encounterCreate.encounterDatetime) < new Date(visitStartDatetime)) {
return this.visitResourceService
.updateVisitDates(visitUuid, encounterCreate.encounterDatetime, visitStopDatetime)
.pipe(switchMap(() => this.updateOrSaveEncounter(encounterCreate)));
} else if (visitStopDatetime && new Date(encounterCreate.encounterDatetime) > new Date(visitStopDatetime)) {
return this.visitResourceService
.updateVisitDates(visitUuid, visitStartDatetime, encounterCreate.encounterDatetime)
.pipe(switchMap(() => this.updateOrSaveEncounter(encounterCreate)));
if (config.customEncounterDatetime === false) {
if (encounterCreate.uuid) {
if (
visitStartDatetime &&
new Date(encounterCreate.encounterDatetime) < new Date(visitStartDatetime) &&
this.confirmVisitDateAdjustment()
) {
return this.visitResourceService
.updateVisitDates(visitUuid, encounterCreate.encounterDatetime, visitStopDatetime)
.pipe(switchMap(() => this.updateOrSaveEncounter(encounterCreate)));
} else if (
visitStopDatetime &&
new Date(encounterCreate.encounterDatetime) > new Date(visitStopDatetime) &&
this.confirmVisitDateAdjustment()
) {
return this.visitResourceService
.updateVisitDates(visitUuid, visitStartDatetime, encounterCreate.encounterDatetime)
.pipe(switchMap(() => this.updateOrSaveEncounter(encounterCreate)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { HttpClient } from '@angular/common/http';

import { WindowRef } from '../window-ref';
import { Form } from '@openmrs/ngx-formentry';
import { MetaData } from '../types';
import { MetaData, PatientProgram } from '../types';
import { parseDate, showNotification, showToast } from '@openmrs/esm-framework';
import { SingleSpaPropsService } from '../single-spa-props/single-spa-props.service';
import { EncounterResourceService } from './encounter-resource.service';
import moment from 'moment';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class ProgramResourceService {
Expand Down Expand Up @@ -57,24 +59,28 @@ export class ProgramResourceService {
location: locationUuuid,
};

this.httpClient.post(this.programEnrollmentUrl(), payload).subscribe(
(response) => {
showToast({
title: 'Program enrollment',
description: 'Patient has been enrolled successfully',
kind: 'success',
});
},
(err) => {
// void created encounter to prevent enrollment missing an encounter
this.encounterResourceService.voidEncounter(encounterUuid);
showNotification({
title: 'Enrollment error',
description: 'An error occurred during care program enrollment, this encounter has been voided',
kind: 'error',
});
},
);
this.isPatientEnrolled(patientUuid, programUuid).subscribe((result: boolean) => {
if (!result) {
this.httpClient.post(this.programEnrollmentUrl(), payload).subscribe(
(response) => {
showToast({
title: 'Program enrollment',
description: 'Patient has been enrolled successfully',
kind: 'success',
});
},
(err) => {
// void created encounter to prevent enrollment missing an encounter
this.encounterResourceService.voidEncounter(encounterUuid);
showNotification({
title: 'Enrollment error',
description: 'An error occurred during care program enrollment, this encounter has been voided',
kind: 'error',
});
},
);
}
});
}

public discontinuePatientFromCareProgram(discontinuationDate: string, encounterUuid: string) {
Expand Down Expand Up @@ -114,4 +120,18 @@ export class ProgramResourceService {
},
);
}

public isPatientEnrolled(patientUuid: string, programUuid: string): Observable<boolean> {
const url = this.programEnrollmentUrl() + `?patient=${patientUuid}&v=full`;
return this.httpClient.get(url).pipe(
map((response: any) => {
if (response && response.results) {
return !!response.results.find((patientProgram: PatientProgram) => {
return patientProgram.program.uuid === programUuid && patientProgram.dateCompleted === null;
});
}
return false;
}),
);
}
}
1 change: 1 addition & 0 deletions packages/esm-form-entry-app/src/app/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export interface FormEntryConfig {
moduleExport: 'default' | string;
}[];
appointmentsResourceUrl: string;
customEncounterDatetime: boolean;
}

export interface ListResult<T> {
Expand Down
6 changes: 6 additions & 0 deletions packages/esm-form-entry-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ export const configSchema = {
_description:
'Custom URL to load resources required for appointment monthly schedule feature (under `dataSources`).',
},
customEncounterDatetime: {
_type: 'Boolean',
_default: false,
_description:
'Weather to default the encounterDate to visitStartDatetime if the visitStartDatetime is before current Date.',
},
};
Loading