Skip to content

Commit 7a8dbf4

Browse files
committed
Add lookupFromPdc command
In order to run `updateAll`, the ingester needs permission to update all the changemakers. But not all changemakers that are present in PDC have EINs that can be found in Charity Navigator. So before running a modify command (`updateAll`) it is useful to run this new read-only command (`lookupFromPdc`) to list the exact changemakers on which the ingest user needs permissions.
1 parent 5d896d3 commit 7a8dbf4

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

src/charityNavigator.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ interface LookupCommandArgs {
160160
outputFile?: string;
161161
}
162162

163+
interface LookupFromPdcCommandArgs {
164+
'charity-navigator-api-key'?: string;
165+
'pdc-api-base-url': string;
166+
outputFile?: string;
167+
}
168+
163169
interface UpdateAllCommandArgs {
164170
'charity-navigator-api-key'?: string;
165171
'oidc-base-url': string,
@@ -237,6 +243,66 @@ const getChangemakerByEin = (ein: string, changemakers: ChangemakerBundle): Chan
237243
throw new Error('How could this have happened?');
238244
};
239245

246+
const lookupFromPdcCommand: CommandModule<unknown, LookupFromPdcCommandArgs> = {
247+
command: 'lookupFromPdc',
248+
describe: 'Fetch and display information about organizations present in PDC',
249+
builder: (y) => (y
250+
.option('charity-navigator-api-key', {
251+
describe: 'CharityNavigator API key; get from account management at https://developer.charitynavigator.org/ (can also be set via DS_CHARITY_NAVIGATOR_API_KEY env var)',
252+
demandOption: false,
253+
type: 'string',
254+
})
255+
.check((argv) => {
256+
if (!argv.charityNavigatorApiKey) {
257+
throw new Error('Missing required argument: charity-navigator-api-key (set via CLI or DS_CHARITY_NAVIGATOR_API_KEY env var)');
258+
}
259+
return true;
260+
})
261+
.option('output-file', {
262+
alias: 'write',
263+
describe: 'Write organization information to the specified JSON file',
264+
normalize: true,
265+
type: 'string',
266+
})
267+
.option('pdc-api-base-url', {
268+
describe: 'Location of PDC API',
269+
demandOption: true,
270+
type: 'string',
271+
})
272+
),
273+
handler: async (args) => {
274+
const { charityNavigatorApiKey: apiKey, pdcApiBaseUrl } = args;
275+
if (!apiKey) {
276+
throw new Error('Missing required argument: charity-navigator-api-key');
277+
}
278+
if (!pdcApiBaseUrl) {
279+
throw new Error('Missing required argument: pdc-api-base-url');
280+
}
281+
const changemakers = await getChangemakers(args.pdcApiBaseUrl);
282+
const eins = changemakers.entries.flatMap((c) => c.taxId);
283+
// Charity Navigator expects no hyphens, strip them from EINs after validation.
284+
const validEins = eins.filter(isValidEin).flatMap((e) => e.replace('-', ''));
285+
const invalidEins = eins.filter((e) => !isValidEin(e));
286+
if (invalidEins.length > 0) {
287+
logger.warn(invalidEins, 'These EINs in PDC are invalid and will not be queried');
288+
}
289+
logger.info(validEins, 'Found these valid EINs which will be requested from Charity Navigator');
290+
const charityNavResponse = await getCharityNavigatorProfiles(
291+
apiKey,
292+
validEins,
293+
);
294+
if (args.outputFile) {
295+
await writeFile(
296+
args.outputFile,
297+
JSON.stringify(charityNavResponse, null, 2),
298+
);
299+
logger.info(`Wrote CharityNavigator data for ${JSON.stringify(args.ein)} to ${JSON.stringify(args.outputFile)}`);
300+
} else {
301+
logger.info({ charityNavResponse }, 'CharityNavigator result');
302+
}
303+
},
304+
};
305+
240306
const getOrCreateSource = async (baseUrl: string, token: AccessTokenSet): Promise<Source> => {
241307
const sources = await getSources(baseUrl, token);
242308
const filteredSources = sources.entries.filter((s) => s.dataProviderShortCode === CN_SHORT_CODE);
@@ -336,6 +402,7 @@ const charityNavigator: CommandModule = {
336402
describe: 'Interact with the CharityNavigator Premier API',
337403
builder: (y) => (y
338404
.command(lookupCommand)
405+
.command(lookupFromPdcCommand)
339406
.command(updateAllCommand)
340407
.demandCommand(1)
341408
),

0 commit comments

Comments
 (0)