diff --git a/src/app/(dashboard)/stats/actions.ts b/src/app/(dashboard)/stats/actions.ts index 4eebca6..3c74959 100644 --- a/src/app/(dashboard)/stats/actions.ts +++ b/src/app/(dashboard)/stats/actions.ts @@ -5,6 +5,7 @@ import { LocationData, SummaryItem } from "@/lib/models/types"; import { FilterFormData } from "@/model/filters"; import { fhirR4 } from "@smile-cdr/fhirts"; import { format } from "date-fns"; +import { QueryParam } from "./model"; export async function fetchRequiredData() { const locationQuery = paramGenerator("/Location", { @@ -25,7 +26,7 @@ export async function fetchData(formData: FormData) { formData.getAll("data")[0] as string ) as FilterFormData; - console.log(JSON.stringify(data)); + // console.log(JSON.stringify(data)); const baseFilter = data.filters.map((filter) => { const temp: Record = {}; if (filter.template == "_tag_location") { @@ -38,20 +39,43 @@ export async function fetchData(formData: FormData) { } return temp; }); - const query: Record = { + + const allFinishVisitsQuery = new QueryParam({ _summary: "count", questionnaire: "patient-finish-visit", - }; - baseFilter.forEach((filter) => { - Object.assign(query, filter); }); - if (query["date"]) { - query["authored"] = format(query["date"], "yyyy-MM-dd"); - delete query["date"]; + const allNewlyRegisteredQuery = new QueryParam({ + _summary: "count", + }); + + allFinishVisitsQuery.fromArray(baseFilter); + allNewlyRegisteredQuery.fromArray(baseFilter); + + if (allFinishVisitsQuery.has("date")) { + allFinishVisitsQuery.set( + "authored", + format(allFinishVisitsQuery.get("date")!, "yyyy-MM-dd") + ); + allFinishVisitsQuery.remove("date"); } - const allVisits = paramGenerator("/QuestionnaireResponse", query); - const bundle = await fetchBundle([allVisits]); - const summary: string[] = ["Total visits"]; + + if (allNewlyRegisteredQuery.has("date")) { + allNewlyRegisteredQuery.set( + "_tag", + `https://d-tree.org/fhir/created-on-tag|${format( + allNewlyRegisteredQuery.get("date")!, + "dd/MM/yyyy" + )}` + ); + allNewlyRegisteredQuery.remove("date"); + } + + const allVisits = allFinishVisitsQuery.toUrl("/QuestionnaireResponse"); + + const allNewlyRegistered = allNewlyRegisteredQuery.toUrl("/Patient"); + + const bundle = await fetchBundle([allVisits, allNewlyRegistered]); + const summary: string[] = ["Total visits", "Newly registered patients"]; console.log(JSON.stringify(bundle)); return getResults(bundle, summary); @@ -90,7 +114,7 @@ const getResults = ( const paramGenerator = ( resources: string, - params: Record + params: Record ) => { return `${resources}?${Object.keys(params) .map((key) => `${key}=${params[key]}`) diff --git a/src/app/(dashboard)/stats/model.ts b/src/app/(dashboard)/stats/model.ts new file mode 100644 index 0000000..f9cd45f --- /dev/null +++ b/src/app/(dashboard)/stats/model.ts @@ -0,0 +1,56 @@ +import { randomInt } from "crypto"; + +export class QueryParam { + queries: Map = new Map(); + constructor(values: Record) { + this.from(values); + } + + add(key: string, value: any) { + if (this.queries.has(key)) { + this.queries.set(`${key}[${randomInt(100)}]`, value); + } else { + this.queries.set(key, value); + } + } + + get(key: string) { + return this.queries.get(key); + } + + set(key: string, value: any) { + this.queries.set(key, value); + } + + remove(key: string) { + this.queries.delete(key); + } + + from(values: Record) { + for (const key in values) { + this.add(key, values[key]); + } + } + + has(key: string) { + return this.queries.has(key); + } + + fromArray(values: Record[]) { + for (const key in values) { + this.from(values[key]); + } + } + + toUrl( resources: string,) { + const query = Array.from(this.queries) + .map(([key, value]) => { + if (key.includes("[")) { + return `${key.split("[")[0]}=${value}`; + } + return `${key}=${value}`; + }) + .join("&"); + return `${resources}?${query}`; + } +}