|
| 1 | +import memoize from "memoizee"; |
| 2 | + |
| 3 | +import { GetSoftwareExternalData, SoftwareExternalData } from "../../ports/GetSoftwareExternalData"; |
| 4 | +import { Source } from "../../usecases/readWriteSillData"; |
| 5 | +import { comptoirDuLibreApi } from "../comptoirDuLibreApi"; |
| 6 | +import { ComptoirDuLibre } from "../../ports/ComptoirDuLibreApi"; |
| 7 | +import { SchemaOrganization } from "../dbApi/kysely/kysely.database"; |
| 8 | +import { identifersUtils } from "../../utils"; |
| 9 | + |
| 10 | +export const getCDLSoftwareExternalData: GetSoftwareExternalData = memoize( |
| 11 | + async ({ |
| 12 | + externalId, |
| 13 | + source |
| 14 | + }: { |
| 15 | + externalId: string; |
| 16 | + source: Source; |
| 17 | + }): Promise<SoftwareExternalData | undefined> => { |
| 18 | + const comptoirAPi = await comptoirDuLibreApi.getComptoirDuLibre(); |
| 19 | + |
| 20 | + const comptoirSoftware = comptoirAPi.softwares.find(softwareItem => softwareItem.id.toString() === externalId); |
| 21 | + |
| 22 | + if (!comptoirSoftware) return undefined; |
| 23 | + |
| 24 | + return formatCDLSoftwareToExternalData(comptoirSoftware, source); |
| 25 | + } |
| 26 | +); |
| 27 | + |
| 28 | +const cDLproviderToCMProdivers = (provider: ComptoirDuLibre.Provider): SchemaOrganization => { |
| 29 | + return { |
| 30 | + "@type": "Organization" as const, |
| 31 | + name: provider.name, |
| 32 | + url: provider.external_resources.website ?? undefined, |
| 33 | + identifiers: [ |
| 34 | + identifersUtils.makeCDLIdentifier({ |
| 35 | + cdlId: provider.id.toString(), |
| 36 | + url: provider.url, |
| 37 | + additionalType: "Organization" |
| 38 | + }) |
| 39 | + ] |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +const formatCDLSoftwareToExternalData = ( |
| 44 | + cDLSoftwareItem: ComptoirDuLibre.Software, |
| 45 | + source: Source |
| 46 | +): SoftwareExternalData => { |
| 47 | + const splittedCNLLUrl = !Array.isArray(cDLSoftwareItem.external_resources.cnll) |
| 48 | + ? cDLSoftwareItem.external_resources.cnll.url.split("/") |
| 49 | + : undefined; |
| 50 | + |
| 51 | + return { |
| 52 | + externalId: cDLSoftwareItem.id.toString(), |
| 53 | + sourceSlug: source.slug, |
| 54 | + developers: [], |
| 55 | + label: { "fr": cDLSoftwareItem.name }, |
| 56 | + description: { "fr": "" }, |
| 57 | + isLibreSoftware: true, |
| 58 | + // |
| 59 | + logoUrl: undefined, // Use scrapper ? |
| 60 | + websiteUrl: cDLSoftwareItem.external_resources.website ?? undefined, |
| 61 | + sourceUrl: cDLSoftwareItem.external_resources.repository ?? undefined, |
| 62 | + documentationUrl: undefined, |
| 63 | + license: cDLSoftwareItem.licence, |
| 64 | + softwareVersion: undefined, |
| 65 | + keywords: [], |
| 66 | + programmingLanguages: [], |
| 67 | + applicationCategories: [], |
| 68 | + publicationTime: undefined, |
| 69 | + referencePublications: [], |
| 70 | + identifiers: [ |
| 71 | + identifersUtils.makeCDLIdentifier({ |
| 72 | + cdlId: cDLSoftwareItem.id.toString(), |
| 73 | + url: cDLSoftwareItem.url, |
| 74 | + additionalType: "Software" |
| 75 | + }), |
| 76 | + ...(!Array.isArray(cDLSoftwareItem.external_resources.cnll) && splittedCNLLUrl |
| 77 | + ? [ |
| 78 | + identifersUtils.makeCNLLIdentifier({ |
| 79 | + cNNLId: splittedCNLLUrl[splittedCNLLUrl.length - 1], |
| 80 | + url: cDLSoftwareItem.external_resources.cnll.url |
| 81 | + }) |
| 82 | + ] |
| 83 | + : []), |
| 84 | + ...(!Array.isArray(cDLSoftwareItem.external_resources.framalibre) |
| 85 | + ? [ |
| 86 | + identifersUtils.makeFramaIndentifier({ |
| 87 | + framaLibreId: cDLSoftwareItem.external_resources.framalibre.slug, |
| 88 | + url: cDLSoftwareItem.external_resources.framalibre.url, |
| 89 | + additionalType: "Software" |
| 90 | + }) |
| 91 | + ] |
| 92 | + : []), |
| 93 | + ...(!Array.isArray(cDLSoftwareItem.external_resources.wikidata) |
| 94 | + ? [ |
| 95 | + identifersUtils.makeWikidataIdentifier({ |
| 96 | + wikidataId: cDLSoftwareItem.external_resources.wikidata.id, |
| 97 | + url: cDLSoftwareItem.external_resources.wikidata.url, |
| 98 | + additionalType: "Software" |
| 99 | + }) |
| 100 | + ] |
| 101 | + : []) |
| 102 | + ], |
| 103 | + providers: cDLSoftwareItem.providers.map(prodiver => cDLproviderToCMProdivers(prodiver)) |
| 104 | + }; |
| 105 | +}; |
0 commit comments