|
| 1 | +import { redis } from "../../libs/redis" |
| 2 | +import type { DatasetOrSnapshot } from "../../utils/datasetOrSnapshot" |
| 3 | +import { latestSnapshot } from "./snapshots" |
| 4 | +import { description } from "../../datalad/description" |
| 5 | +import Metadata from "../../models/metadata" |
| 6 | +import CacheItem, { CacheType } from "../../cache/item" |
| 7 | +import * as Sentry from "@sentry/node" |
| 8 | +import fundedAwards from "../../data/funded_awards.json" |
| 9 | + |
| 10 | +const brainInitiativeMatch = new RegExp("brain.initiative", "i") |
| 11 | + |
| 12 | +const brainInitiativeGrants = fundedAwards.map((award) => |
| 13 | + award.field_project_number.replace(/[^a-zA-Z0-9]/g, "") |
| 14 | +) |
| 15 | + |
| 16 | +/** |
| 17 | + * Check for any Brain Initiative metadata |
| 18 | + */ |
| 19 | +export const brainInitiative = async ( |
| 20 | + dataset: DatasetOrSnapshot, |
| 21 | + _, |
| 22 | + context, |
| 23 | +): Promise<boolean> => { |
| 24 | + const cache = new CacheItem( |
| 25 | + redis, |
| 26 | + CacheType.brainInitiative, |
| 27 | + [dataset.id], |
| 28 | + 86400, |
| 29 | + ) |
| 30 | + return await cache.get(async () => { |
| 31 | + try { |
| 32 | + const metadata = await Metadata.findOne({ datasetId: dataset.id }) |
| 33 | + if (metadata.grantFunderName.match(brainInitiativeMatch)) { |
| 34 | + return true |
| 35 | + } else { |
| 36 | + // Fetch snapshot if metadata didn't match |
| 37 | + const snapshot = await latestSnapshot(dataset, null, context) |
| 38 | + const snapshotDescription = await description(snapshot) |
| 39 | + for (const funding of snapshotDescription.Funding) { |
| 40 | + if (funding.match(brainInitiativeMatch)) { |
| 41 | + return true |
| 42 | + } |
| 43 | + } |
| 44 | + // Check for grant ids too - filter to only alphanumeric to improve matching across format differences |
| 45 | + const identifier = metadata.grantIdentifier.replace(/[^a-zA-Z0-9]/g, "") |
| 46 | + for (const grant of brainInitiativeGrants) { |
| 47 | + if ( |
| 48 | + identifier.includes(grant) |
| 49 | + ) { |
| 50 | + return true |
| 51 | + } |
| 52 | + for (const funding of snapshotDescription.Funding) { |
| 53 | + if (funding.replace(/[^a-zA-Z0-9]/g, "").includes(grant)) { |
| 54 | + return true |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return false |
| 60 | + } catch (_err) { |
| 61 | + Sentry.captureException(_err) |
| 62 | + return false |
| 63 | + } |
| 64 | + }) |
| 65 | +} |
0 commit comments