From b065236609a8141ce2e7da7c0140827d1189876a Mon Sep 17 00:00:00 2001 From: AJAL ODORA JONATHAN <43242517+ODORA0@users.noreply.github.com> Date: Wed, 22 Jan 2025 18:08:55 +0300 Subject: [PATCH] Delete resource file, refactor component to use afterPostSubmission endpoint --- src/index.ts | 8 +- .../billing-submission-action.ts | 102 ++++++------------ .../billing.resource.ts | 49 --------- 3 files changed, 35 insertions(+), 124 deletions(-) delete mode 100644 src/post-submission-handlers/billing.resource.ts diff --git a/src/index.ts b/src/index.ts index c8fb0b0..a942d4f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,10 +15,7 @@ import AllEncounters from "./encounters/encounters.component"; import PatientSummary from "./patient-summary/patient-summary.component"; import CareAndTreatment from "./care-and-treatment/care-and-treatment.component"; import versionTwoNavigationButton from "./app-menu-navigation/app-menu-navigation"; -import { - registerPostSubmissionAction, - getPostSubmissionActionById, -} from "@openmrs/openmrs-form-engine-lib"; +import { registerPostSubmissionAction } from "@openmrs/openmrs-form-engine-lib"; const moduleName = "@ohri/openmrs-esm-rwanda-app"; @@ -35,10 +32,7 @@ export const importTranslation = require.context( ); export function startupApp() { - console.log("Initializing Rwanda ESM..."); defineConfigSchema(moduleName, configSchema); - - console.log("Registering BillingSubmissionAction..."); registerPostSubmissionAction({ name: "BillingSubmissionAction", load: () => import("./post-submission-handlers/billing-submission-action"), diff --git a/src/post-submission-handlers/billing-submission-action.ts b/src/post-submission-handlers/billing-submission-action.ts index 4e6667a..8837682 100644 --- a/src/post-submission-handlers/billing-submission-action.ts +++ b/src/post-submission-handlers/billing-submission-action.ts @@ -1,86 +1,52 @@ import { type PostSubmissionAction } from "@openmrs/openmrs-form-engine-lib"; -import { - getConfig, - openmrsFetch, - type OpenmrsResource, -} from "@openmrs/esm-framework"; -import { - BillingConfig, - BillingData, - BillingResponse, -} from "./billing.resource"; - -const getUuid = ( - resource: string | OpenmrsResource | undefined -): string | undefined => { - if (!resource) { - return undefined; - } - if (typeof resource === "string") { - return resource; - } - return resource.uuid; -}; +import { openmrsFetch, showSnackbar, showToast } from "@openmrs/esm-framework"; const BillingSubmissionAction: PostSubmissionAction = { - applyAction: async function ({ patient, encounters, sessionMode }) { + applyAction: async function ({ patient, encounters, sessionMode }, config) { try { if (sessionMode !== "enter") { - console.log("Not a new form submission, skipping billing"); - return; + return true; } - console.log("Starting billing submission process"); - const config: BillingConfig = await getConfig( - "@ohri/openmrs-esm-billing-app" - ); - const encounter = encounters[0]; + const billingEndpoint = "/ws/rest/v1/mohbilling/afterPostSubmission"; - // Extract observations from encounter - const getObsValue = (conceptUuid: string) => { - const obs = encounter.obs?.find( - (observation) => observation.concept.uuid === conceptUuid - ); - return obs ? obs.value : null; + const obsData = { + obs: encounters[0].obs.map((ob) => ({ + uuid: ob.uuid, + })), }; - // Get values for billing - const diseaseType = getObsValue(config.concepts.diseaseType); - const consultationType = getObsValue(config.concepts.consultationType); - const department = getObsValue(config.concepts.department); - const isAdmitted = getObsValue(config.concepts.isAdmitted); + const response = await openmrsFetch(billingEndpoint, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(obsData), + }); - if (!diseaseType || !consultationType || !department) { - console.error("Missing required billing information"); - return; + if (response.ok) { + showSnackbar({ + title: "Post Submission Action", + subtitle: "Patient bill has been created successfully", + kind: "success", + timeoutInMs: 4000, + }); + } else { + showToast({ + description: `Billing submission failed: ${response.statusText}`, + kind: "error", + }); } - // Prepare billing data - const billingData: BillingData = { - patientUuid: patient.id, - encounterUuid: encounter.uuid, - diseaseType, - consultationType, - department, - isAdmitted: isAdmitted || false, - dateCreated: new Date().toISOString(), - location: getUuid(encounter.location), - provider: encounter.encounterProviders?.[0]?.provider?.uuid, - }; - - console.log("Billing data prepared:", billingData); return true; - - /* TODO: Uncomment when API is ready - const response = await openmrsFetch(config.endpoints.billing, { - method: 'POST', - body: billingData - }); - return response.ok; - */ } catch (error) { - console.error("Error in billing submission:", error); - return false; + showToast({ + description: `An error occurred during billing submission: ${ + error.message || "Unknown error" + }`, + kind: "error", + }); + return true; } }, }; diff --git a/src/post-submission-handlers/billing.resource.ts b/src/post-submission-handlers/billing.resource.ts deleted file mode 100644 index 1c9719c..0000000 --- a/src/post-submission-handlers/billing.resource.ts +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @file src/types/billing.resource.ts - * @description Types and interfaces for billing functionality - */ - -/** - * Configuration interface for billing-related concepts and endpoints - */ -export interface BillingConfig { - concepts: { - diseaseType: string; - consultationType: string; - department: string; - isAdmitted: string; - }; - endpoints: { - billing: string; - }; -} - -/** - * Interface for billing data structure - */ -export interface BillingData { - patientUuid: string; - encounterUuid: string; - diseaseType: string; - consultationType: string; - department: string; - isAdmitted: boolean; - dateCreated: string; - location?: string; - provider?: string; -} - -/** - * Interface for billing API response - */ -export interface BillingResponse { - uuid: string; - status: "PENDING" | "COMPLETED" | "FAILED"; - billDetails: { - amount: number; - items: Array<{ - service: string; - cost: number; - }>; - }; -}