Skip to content

Commit

Permalink
(update) fix the submitted state to be last state change before closi…
Browse files Browse the repository at this point in the history
…ng form
  • Loading branch information
donaldkibet committed Mar 6, 2024
1 parent 270f0b8 commit 3d531d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 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,15 +4,10 @@ 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';
import timezone from 'dayjs/plugin/timezone';

dayjs.extend(utc);
dayjs.extend(timezone);
import moment from 'moment';

@Injectable()
export class ProgramResourceService {
Expand All @@ -28,8 +23,6 @@ export class ProgramResourceService {
}

public handlePatientCareProgram(form: Form, encounterUuid: string): void {
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
dayjs.tz.setDefault(timeZone);
const careProgramMeta: MetaData | undefined = form.schema.meta?.programs;
if (!careProgramMeta) {
return;
Expand All @@ -39,32 +32,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,
locationUuid: string,
encounterUuid: string,
utcOffset: string,
) {
const patientUuid = this.singleSpaService.getPropOrThrow('patientUuid');
const enrolledDate = enrollmentDate ? enrollmentDate : dayjs();
const enrolledDate = enrollmentDate ? enrollmentDate : moment(new Date()).utcOffset(utcOffset).format();
const inEditModeEncounterUuid = this.singleSpaService.getProp('encounterUuid');
// Should not enroll patient if in edit mode
if (inEditModeEncounterUuid) {
Expand Down Expand Up @@ -100,24 +83,23 @@ export class ProgramResourceService {
);
}

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

public discontinuePatientFromCareProgram(discontinuationDate: string, encounterUuid: string, utcOffset: string) {
const { enrollmenrDetails } = this.singleSpaService.getPropOrThrow('additionalProps');
const currentDateTime = new Date();
const hour = currentDateTime.getHours();
const minutes = currentDateTime.getMinutes();
const seconds = currentDateTime.getSeconds();

const discontinuationDateTime = dayjs(discontinuationDate ?? new Date())
const discontinuationDateTime = moment(discontinuationDate ?? new Date())
.set('hour', hour)
.set('minute', minutes)
.set('second', seconds);
const payload = {
dateEnrolled: dayjs(enrollmentDetails.dateEnrolled),
dateCompleted: discontinuationDateTime,
dateEnrolled: moment(enrollmenrDetails.dateEnrolled).utcOffset(utcOffset).format(),
dateCompleted: discontinuationDateTime.utcOffset(utcOffset).format(),
};

this.httpClient.post(`${this.programEnrollmentUrl()}/${enrollmentDetails?.uuid}`, payload).subscribe(
this.httpClient.post(`${this.programEnrollmentUrl()}/${enrollmenrDetails?.uuid}`, payload).subscribe(
(resp) => {
showSnackbar({
title: 'Program discontinuation',
Expand All @@ -140,4 +122,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 3d531d6

Please sign in to comment.