Skip to content

Commit

Permalink
(feat) Update enrollment and discontinuation date to pick correct tim…
Browse files Browse the repository at this point in the history
…e zone. (openmrs#1719)

* (fix) fix enroll and discontinue date on program enrollment to pick the correct timezone

* (update) fix the submitted state to be last state change before closing form
  • Loading branch information
donaldkibet authored and usamaidrsk committed Mar 11, 2024
1 parent e731eb3 commit 3dc41bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ export class FeWrapperComponent implements OnInit, OnDestroy {
({ encounter }) => {
this.onPostResponse(encounter);
const isOffline = this.singleSpaPropsService.getProp('isOffline', false);
this.changeState('submitted');

if (!isOffline && encounter?.uuid) {
this.encounterResourceService
Expand All @@ -235,7 +234,7 @@ export class FeWrapperComponent implements OnInit, OnDestroy {
title: this.form.schema.display ?? this.form.schema.name,
timeoutInMs: 5000,
});

this.changeState('submitted');
this.closeForm();
},
(error: Error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { HttpClient } from '@angular/common/http';
import { WindowRef } from '../window-ref';
import { Form } from '@openmrs/ngx-formentry';
import { MetaData } from '../types';
import { parseDate, restBaseUrl, showSnackbar } from '@openmrs/esm-framework';
import { restBaseUrl, showSnackbar } from '@openmrs/esm-framework';
import { SingleSpaPropsService } from '../single-spa-props/single-spa-props.service';
import { EncounterResourceService } from './encounter-resource.service';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);

@Injectable()
export class ProgramResourceService {
Expand All @@ -32,32 +34,22 @@ export class ProgramResourceService {
const enrollmentDate = this.getProgramDate(form, isEnrollment, enrollmentDateQuestionId);
const discontinuationDate = this.getProgramDate(form, !isEnrollment, discontinuationDateQuestionId);
const locationUuid = this.getUserLocationUuid(form);
const utcOffset = form.valueProcessingInfo.utcOffset ?? '+0300';

isEnrollment
? this.enrollPatientToCareProgram(enrollmentDate, uuid, locationUuid, encounterUuid)
: this.discontinuePatientFromCareProgram(discontinuationDate, encounterUuid);
}

private getProgramDate(form: Form, condition: boolean, questionId?: string): string | undefined {
return condition && questionId ? this.getNodeValueById(form, questionId) : undefined;
}

private getNodeValueById(form: Form, questionId: string): string | undefined {
return form.searchNodeByQuestionId(questionId)?.[0]?.control.value;
}

private getUserLocationUuid(form: Form): string {
return form.dataSourcesContainer.dataSources['userLocation'].uuid;
? this.enrollPatientToCareProgram(enrollmentDate, uuid, locationUuid, encounterUuid, utcOffset)
: this.discontinuePatientFromCareProgram(discontinuationDate, encounterUuid, utcOffset);
}

public enrollPatientToCareProgram(
enrollmentDate: string,
programUuid: string,
locationUuuid: string,
locationUuid: string,
encounterUuid: string,
utcOffset: string,
) {
const patientUuid = this.singleSpaService.getPropOrThrow('patientUuid');
const enrolledDate = enrollmentDate ? enrollmentDate : new Date().toISOString();
const enrolledDate = enrollmentDate ? enrollmentDate : dayjs(new Date()).utcOffset(utcOffset).format();
const inEditModeEncounterUuid = this.singleSpaService.getProp('encounterUuid');
// Should not enroll patient if in edit mode
if (inEditModeEncounterUuid) {
Expand All @@ -66,9 +58,9 @@ export class ProgramResourceService {
const payload = {
patient: patientUuid,
program: programUuid,
dateEnrolled: parseDate(enrolledDate),
dateEnrolled: enrolledDate,
dateCompleted: null,
location: locationUuuid,
location: locationUuid,
};

this.httpClient.post(this.programEnrollmentUrl(), payload).subscribe(
Expand All @@ -93,9 +85,8 @@ export class ProgramResourceService {
);
}

public discontinuePatientFromCareProgram(discontinuationDate: string, encounterUuid: string) {
const { enrollmenrDetails: enrollmentDetails } = this.singleSpaService.getPropOrThrow('additionalProps');

public discontinuePatientFromCareProgram(discontinuationDate: string, encounterUuid: string, utcOffset: string) {
const { enrollmentDetails } = this.singleSpaService.getPropOrThrow('additionalProps');
const currentDateTime = new Date();
const hour = currentDateTime.getHours();
const minutes = currentDateTime.getMinutes();
Expand All @@ -104,11 +95,10 @@ export class ProgramResourceService {
const discontinuationDateTime = dayjs(discontinuationDate ?? new Date())
.set('hour', hour)
.set('minute', minutes)
.set('second', seconds)
.toISOString();
.set('second', seconds);
const payload = {
dateEnrolled: dayjs(enrollmentDetails.dateEnrolled).toISOString(),
dateCompleted: discontinuationDateTime,
dateEnrolled: dayjs(enrollmentDetails.dateEnrolled).utcOffset(utcOffset).format(),
dateCompleted: discontinuationDateTime.utcOffset(utcOffset).format(),
};

this.httpClient.post(`${this.programEnrollmentUrl()}/${enrollmentDetails?.uuid}`, payload).subscribe(
Expand All @@ -134,4 +124,16 @@ export class ProgramResourceService {
},
);
}

private getProgramDate(form: Form, condition: boolean, questionId?: string): string | undefined {
return condition && questionId ? this.getNodeValueById(form, questionId) : undefined;
}

private getNodeValueById(form: Form, questionId: string): string | undefined {
return form.searchNodeByQuestionId(questionId)?.[0]?.control.value;
}

private getUserLocationUuid(form: Form): string {
return form.dataSourcesContainer.dataSources['userLocation'].uuid;
}
}

0 comments on commit 3dc41bb

Please sign in to comment.