Skip to content

Commit

Permalink
delete events in program (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
deeonwuli committed Aug 22, 2024
1 parent dc0f541 commit d07aade
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.14.2
v18.19.0
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"classnames": "2.3.1",
"d2": "31.10.2",
"d2-manifest": "1.0.0",
"dotenv": "^16.4.5",
"font-awesome": "4.7.0",
"purify-ts": "1.2.0",
"purify-ts-extra-codec": "0.6.0",
Expand Down Expand Up @@ -110,7 +111,8 @@
"localize": "yarn update-po && d2-i18n-generate -n dhis2-skeleton-app -p ./i18n/ -o ./src/locales/",
"update-po": "yarn extract-pot && find i18n/ -name '*.po' -exec msgmerge --backup=off -U {} i18n/en.pot \\;",
"prepare": "husky install",
"script-example": "npx ts-node src/scripts/example.ts"
"script-example": "npx ts-node src/scripts/example.ts",
"script-map-outbreak-to-alerts": "npx ts-node -r dotenv/config src/scripts/mapDiseaseOutbreakToAlerts.ts"
},
"manifest.webapp": {
"name": "ZEBRA",
Expand Down
26 changes: 26 additions & 0 deletions src/scripts/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";

function getApiOptionsFromUrl(url: string): { baseUrl: string; auth: Auth } {
const urlObj = new URL(url);
const decode = decodeURIComponent;
const auth = { username: decode(urlObj.username), password: decode(urlObj.password) };
return { baseUrl: urlObj.origin + urlObj.pathname, auth };
}

type Auth = {
username: string;
password: string;
};

type D2ApiArgs = {
url: string;
auth?: Auth;
};

export function getD2ApiFromArgs(args: D2ApiArgs): D2Api {
const { baseUrl, auth } = args.auth
? { baseUrl: args.url, auth: args.auth }
: getApiOptionsFromUrl(args.url);

return new D2Api({ baseUrl, auth });
}
92 changes: 92 additions & 0 deletions src/scripts/mapDiseaseOutbreakToAlerts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { command, run } from "cmd-ts";
import path from "path";
import { getD2ApiFromArgs } from "./common";
// import {
// D2TrackerTrackedEntity,
// TrackedEntitiesGetResponse,
// } from "@eyeseetea/d2-api/api/trackerTrackedEntities";

// Get all TEIs for Zebra Alerts Program which do not have a National Disease Outbreak event Id.
// For each of these, get the disease/hazard type.
// Fetch TEI for Zebra RTSL program with above disease/hazard type (Ideally it should return only 1 TEI, because only 1 National Event per disease/hazard type is allowed) - do error handling if TEIs.length > 1
// Update Zebra Alerts TEI with above National TEI id and incident status.

function main() {
const cmd = command({
name: path.basename(__filename),
description: "Show DHIS2 instance info",
args: {},
handler: async () => {
if (!process.env.VITE_DHIS2_BASE_URL)
throw new Error("VITE_DHIS2_BASE_URL must be set in the .env file");

if (!process.env.VITE_DHIS2_AUTH)
throw new Error("VITE_DHIS2_AUTH must be set in the .env file");

const username = process.env.VITE_DHIS2_AUTH.split(":")[0] ?? "";
const password = process.env.VITE_DHIS2_AUTH.split(":")[1] ?? "";

if (username === "" || password === "") {
throw new Error("VITE_DHIS2_AUTH must be in the format 'username:password'");
}

const envVars = {
url: process.env.VITE_DHIS2_AUTH,
auth: {
username: username,
password: password,
},
};

const api = getD2ApiFromArgs(envVars);

// const d2TrackerTrackedEntities: D2TrackerTrackedEntity[] = [];

// const pageSize = 250;
// let page = 1;
// let result: TrackedEntitiesGetResponse;

// try {
// do {
// result = await api.tracker.trackedEntities
// .get({
// program: "qkOTdxkte8V",
// orgUnit: "PS5JpkoHHio",
// totalPages: true,
// page: page,
// pageSize: pageSize,
// fields: {
// attributes: true,
// orgUnit: true,
// trackedEntity: true,
// trackedEntityType: true,
// },
// })
// .getData();

// d2TrackerTrackedEntities.push(...result.instances);

// console.debug(d2TrackerTrackedEntities);

// page++;
// } while (result.page < Math.ceil((result.total as number) / pageSize));
// api.tracker.post(
// { importStrategy: "DELETE" },
// { trackedEntities: d2TrackerTrackedEntities }
// );
// } catch (error) {
// console.error({ error });
// return [];
// }

console.debug(api, username, password);

const info = await api.system.info.getData();
console.debug(info);
},
});

run(cmd, process.argv.slice(2));
}

main();
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6172,6 +6172,11 @@ dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dotenv@^16.4.5:
version "16.4.5"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==

dotenv@^8.0.0:
version "8.6.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b"
Expand Down

0 comments on commit d07aade

Please sign in to comment.