Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): added and updated the bulk-update-patients script #1805

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(utils): addressed comments
  • Loading branch information
RamilGaripov committed May 3, 2024
commit daa6b25b90bda084bbf6f1587ac6978abd90c284
43 changes: 22 additions & 21 deletions packages/utils/src/bulk-update-patients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import { chunk } from "lodash";
import { QueryTypes, Sequelize } from "sequelize";
import z from "zod";

/**
* This script looks at all of the patients created after a certain date and triggers the UPDATE PATIENT
* for those patients, who do not have a link in the cq_patient_data table.
*
* To run:
* 1. Set the env vars:
* -CX_ID
* -API_KEY
* -API_URL
* -DB_CREDS - Must use the read replica
* 2. Set the patientCreatedDate string in the format YYYY-MM-DD
* 3. Run the script with `ts-node src/bulk-update-patients.ts`
*/

dayjs.extend(duration);

const apiKey = getEnvVarOrFail("API_KEY");
Expand All @@ -19,7 +33,7 @@ const cxId = getEnvVarOrFail("CX_ID");
const CHUNK_DELAY_MAX_MS = dayjs.duration({ minutes: 1 }).asMilliseconds();
const PATIENT_CHUNK_SIZE = 5;

const dateString = ""; // YYYY-MM-DD format
const patientCreatedDate = ""; // YYYY-MM-DD format

const metriportAPI = new MetriportMedicalApi(apiKey, {
baseAddress: apiUrl,
Expand All @@ -37,26 +51,13 @@ const cqPatientDataResultSchema = z.array(
);
type CqPatientDataResult = z.infer<typeof cqPatientDataResultSchema>;

/**
* This script looks at all of the patients created after a certain date and triggers the UPDATE PATIENT
* for those patients, who do not have a link in the cq_patient_data table.
*
* To run:
* 1. Set the env vars:
* -CX_ID
* -API_KEY
* -API_URL
* -DB_CREDS - Must use the read replica
* 2. Set the date string in the format YYYY-MM-DD
* 3. Run the script with `ts-node src/bulk-update-patients.ts`
*/
async function main() {
const dbCreds = JSON.parse(sqlDBCreds);
if (!dateString) {
if (!patientCreatedDate) {
console.log("Please provide a date string in the format YYYY-MM-DD");
return;
}
const targetDate = new Date(dateString);
const targetDate = new Date(patientCreatedDate);

const sequelize = new Sequelize(dbCreds.dbname, dbCreds.username, dbCreds.password, {
host: dbCreds.host,
Expand Down Expand Up @@ -99,8 +100,8 @@ async function main() {
const patientChunks = chunk(patientsWithNoLinks, PATIENT_CHUNK_SIZE);
console.log(`Facility ${facility.id} has ${patientsWithNoLinks.length} patients`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...patients with no links - they'll be updated.


for (const [i, patients] of patientChunks.entries()) {
console.log(`Chunk ${i + 1} of ${patientChunks.length}`);
for (const [j, patients] of patientChunks.entries()) {
console.log(`Chunk ${j + 1} of ${patientChunks.length}`);
console.log(`# of patients ${patients.length}`);

for (const patient of patients) {
Expand All @@ -111,7 +112,7 @@ async function main() {
const address = addressObject as Address;
await updatePatient(patient, address, facility.id);
}
if (i < patientChunks.length - 1) {
if (j < patientChunks.length - 1) {
await sleep(CHUNK_DELAY_MAX_MS * patients.length);
}
}
Expand All @@ -130,8 +131,6 @@ async function main() {
}
}

main();

async function getPatientCqLinks(sequelize: Sequelize, cxId: string): Promise<CqPatientDataResult> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: put both helper functions at top of file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no definition for this, I personally prefer the main function at the top, that's how we read code, from the main logic to the detail anyways.

https://www.notion.so/metriport/Development-Best-Practices-bb3574d3c83d46dc9914a9e5c5542ca7?pvs=4#de6ccc0884ec4c8c976858ec476b34be

const query = `SELECT * FROM cq_patient_data WHERE cx_id=:cxId`;
const patientResults = await sequelize.query(query, {
Expand Down Expand Up @@ -165,3 +164,5 @@ async function updatePatient(patient: PatientDTO, address: Address, facilityId:
facilityId
);
}

main();
Loading