|
| 1 | +import memoize from "memoizee"; |
| 2 | + |
| 3 | +import { GetSoftwareExternalData, SoftwareExternalData } from "../../ports/GetSoftwareExternalData"; |
| 4 | +import { Source } from "../../usecases/readWriteSillData"; |
| 5 | +import { SchemaPerson } from "../dbApi/kysely/kysely.database"; |
| 6 | +import { identifersUtils } from "../../utils"; |
| 7 | +import { makeZenodoApi } from "./zenodoAPI"; |
| 8 | +import { Zenodo } from "./zenodoAPI/type"; |
| 9 | + |
| 10 | +export const getZenodoExternalData: GetSoftwareExternalData = memoize( |
| 11 | + async ({ |
| 12 | + externalId, |
| 13 | + source |
| 14 | + }: { |
| 15 | + externalId: string; |
| 16 | + source: Source; |
| 17 | + }): Promise<SoftwareExternalData | undefined> => { |
| 18 | + if (source.kind !== "Zenodo" && source.url !== "https://zenodo.org/") |
| 19 | + throw new Error(`Not a Zenodo source, was : ${source.kind}`); |
| 20 | + |
| 21 | + const zenodoApi = makeZenodoApi(); |
| 22 | + const record = await zenodoApi.records.get(Number(externalId)); |
| 23 | + |
| 24 | + if (!record) return undefined; |
| 25 | + if (record.metadata.resource_type.type !== "software") |
| 26 | + throw new TypeError(`The record corresponding at ${externalId} is not a software`); |
| 27 | + |
| 28 | + return formatRecordToExternalData(record, source); |
| 29 | + } |
| 30 | +); |
| 31 | + |
| 32 | +const creatorToPerson = (creator: Zenodo.Creator): SchemaPerson => { |
| 33 | + return { |
| 34 | + "@type": "Person" as const, |
| 35 | + name: creator.name, |
| 36 | + affiliations: creator.affiliation |
| 37 | + ? [ |
| 38 | + { |
| 39 | + "@type": "Organization", |
| 40 | + name: creator.affiliation |
| 41 | + } |
| 42 | + ] |
| 43 | + : [], |
| 44 | + identifiers: [...(creator.orcid ? [identifersUtils.makeOrcidIdentifer({ orcidId: creator.orcid })] : [])] |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +const formatRecordToExternalData = (recordSoftwareItem: Zenodo.Record, source: Source): SoftwareExternalData => { |
| 49 | + return { |
| 50 | + externalId: recordSoftwareItem.id.toString(), |
| 51 | + sourceSlug: source.slug, |
| 52 | + developers: recordSoftwareItem.metadata.creators.map(creatorToPerson), |
| 53 | + label: { "en": recordSoftwareItem.metadata.title }, |
| 54 | + description: { "en": recordSoftwareItem.metadata.description }, |
| 55 | + isLibreSoftware: recordSoftwareItem.metadata.access_right === "open", // Not sure |
| 56 | + logoUrl: undefined, |
| 57 | + websiteUrl: undefined, |
| 58 | + sourceUrl: |
| 59 | + recordSoftwareItem.metadata.related_identifiers?.filter( |
| 60 | + identifier => identifier.relation === "isSupplementTo" |
| 61 | + )?.[0]?.identifier ?? undefined, |
| 62 | + documentationUrl: undefined, |
| 63 | + license: recordSoftwareItem.metadata.license.id, |
| 64 | + softwareVersion: recordSoftwareItem.metadata.version, |
| 65 | + keywords: recordSoftwareItem.metadata.keywords ?? [], |
| 66 | + programmingLanguages: [], |
| 67 | + applicationCategories: recordSoftwareItem.metadata.communities?.map(commu => commu.id), |
| 68 | + publicationTime: recordSoftwareItem.metadata.publication_date, |
| 69 | + referencePublications: [], // TODO reliotated identifers // relation type // ?? |
| 70 | + identifiers: [ |
| 71 | + identifersUtils.makeZenodoIdentifer({ |
| 72 | + zenodoId: recordSoftwareItem.id.toString(), |
| 73 | + url: `htpps://zenodo.org/records/${recordSoftwareItem.id.toString()}`, |
| 74 | + additionalType: "Software" |
| 75 | + }), |
| 76 | + ...(recordSoftwareItem.metadata.doi |
| 77 | + ? [identifersUtils.makeDOIIdentifier({ doi: recordSoftwareItem.metadata.doi })] |
| 78 | + : []), |
| 79 | + ...(recordSoftwareItem.swh.swhid |
| 80 | + ? [identifersUtils.makeSWHIdentifier({ swhId: recordSoftwareItem.swh.swhid })] |
| 81 | + : []) |
| 82 | + ], |
| 83 | + providers: [] |
| 84 | + }; |
| 85 | +}; |
0 commit comments