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

OpEx: Practitioners' email address, as defined in each of their cases #5574

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Changes from all commits
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
114 changes: 114 additions & 0 deletions scripts/reports/practitioners-email-in-cases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// usage:
// npx ts-node --transpile-only scripts/reports/practitioners-email-in-cases.ts 84c865f0-3867-40c8-aa31-f35c7e45998f

import {
ServerApplicationContext,
createApplicationContext,
} from '@web-api/applicationContext';
import { search } from '@web-api/persistence/elasticsearch/searchClient';

const practitionerId = process.argv[2];
if (!practitionerId) {
console.log(
'Usage: npx ts-node --transpile-only scripts/reports/practitioners-email-in-cases.ts <USER ID>',
);
process.exit(1);
}

const getUsersRole = async ({
applicationContext,
userId,
}: {
applicationContext: ServerApplicationContext;
userId: string;
}): Promise<string | undefined> => {
const { results } = await search({
applicationContext,
searchParameters: {
body: {
from: 0,
query: {
bool: {
must: [
{
term: {
'pk.S': `user|${userId}`,
},
},
{
term: {
'sk.S': `user|${userId}`,
},
},
],
},
},
size: 1,
},
index: 'efcms-user',
},
});
return results[0]?.role;
};

const getPractitionersCases = async ({
applicationContext,
role,
userId,
}: {
applicationContext: ServerApplicationContext;
role: string;
userId: string;
}): Promise<RawCase[]> => {
const { results } = await search({
applicationContext,
searchParameters: {
body: {
from: 0,
query: {
bool: {
must: [
{
term: {
[`${role}s.L.M.userId.S`]: userId,
},
},
],
},
},
size: 10000,
},
index: 'efcms-case',
},
});
return results;
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
const applicationContext = createApplicationContext({});
const role = await getUsersRole({
applicationContext,
userId: practitionerId,
});
if (role !== 'irsPractitioner' && role !== 'privatePractitioner') {
console.log(`Error: user is not a practitioner! User's role: ${role}`);
return;
}
const practitionersCases: RawCase[] = await getPractitionersCases({
applicationContext,
role,
userId: practitionerId,
});
const practitionersEmailInCases = {};
for (const practitionersCase of practitionersCases) {
const practitionerObj = practitionersCase[`${role}s`]?.find(
pract => pract.userId === practitionerId,
);
if (practitionerObj && practitionerObj.email) {
practitionersEmailInCases[practitionersCase.docketNumber] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to have a duplicate docketNumber (e.g., maybe missing distinguishing suffixes)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Even for docket numbers that have suffixes, the docketNumber.S field will not contain the suffix. The suffix is stored separately in the docketNumberSuffix.S field and a composite of the docket number and suffix is stored as docketNumberWithSuffix.S for good measure.

Because the docket number (without suffix) is used as the pk/sk for a case, there won't be duplicates.

If, however, it happens that the same practitioner can somehow be in a given case's [irs|private]Practitioners array twice, the .find() a few lines above will settle for the first match it comes across.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it--great explanation, thank you!

practitionerObj.email;
}
}
console.log(practitionersEmailInCases);
})();
Loading