Skip to content

Commit aea2153

Browse files
committed
feat(source): add CNLL source
refs: #360
1 parent 25f2ba5 commit aea2153

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import memoize from "memoizee";
2+
3+
import { GetSoftwareExternalData, SoftwareExternalData } from "../../ports/GetSoftwareExternalData";
4+
import { Source } from "../../usecases/readWriteSillData";
5+
import { SchemaOrganization } from "../dbApi/kysely/kysely.database";
6+
import { identifersUtils } from "../../utils";
7+
import { getCnllPrestatairesSill } from "../getCnllPrestatairesSill";
8+
import { CnllPrestatairesSill } from "../../ports/GetCnllPrestatairesSill";
9+
10+
export const getCNLLSoftwareExternalData: GetSoftwareExternalData = memoize(
11+
async ({
12+
externalId,
13+
source
14+
}: {
15+
externalId: string;
16+
source: Source;
17+
}): Promise<SoftwareExternalData | undefined> => {
18+
if (source.kind !== "CNLL") throw new Error("This source if not compatible with CNLL Adapter");
19+
20+
const cNLLProviders = await getCnllPrestatairesSill();
21+
22+
const providersForExternalId = cNLLProviders.find(element => element.sill_id.toString() === externalId);
23+
24+
if (!providersForExternalId) return undefined;
25+
26+
return formatCNLLProvidersToExternalData(providersForExternalId, source);
27+
}
28+
);
29+
30+
const cNLLproviderToCMProdivers = (provider: CnllPrestatairesSill.Prestataire): SchemaOrganization => {
31+
return {
32+
"@type": "Organization" as const,
33+
name: provider.nom,
34+
url: provider.url ?? undefined,
35+
identifiers: [
36+
identifersUtils.makeSIRENIdentifier({
37+
SIREN: provider.siren,
38+
additionalType: "Organization"
39+
})
40+
]
41+
};
42+
};
43+
44+
const formatCNLLProvidersToExternalData = (
45+
cNLLProdivers: CnllPrestatairesSill,
46+
source: Source
47+
): SoftwareExternalData => ({
48+
externalId: cNLLProdivers.sill_id.toString(),
49+
sourceSlug: source.slug,
50+
developers: [],
51+
label: { "fr": cNLLProdivers.nom },
52+
description: { "fr": "" },
53+
isLibreSoftware: true,
54+
logoUrl: undefined,
55+
websiteUrl: undefined,
56+
sourceUrl: undefined,
57+
documentationUrl: undefined,
58+
license: undefined,
59+
softwareVersion: undefined,
60+
keywords: [],
61+
programmingLanguages: [],
62+
applicationCategories: [],
63+
publicationTime: undefined,
64+
referencePublications: [],
65+
identifiers: [
66+
identifersUtils.makeCNLLIdentifier({
67+
cNNLId: cNLLProdivers.sill_id.toString()
68+
})
69+
],
70+
providers: cNLLProdivers.prestataires.map(prodiver => cNLLproviderToCMProdivers(prodiver))
71+
});

api/src/core/adapters/dbApi/kysely/kysely.database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ type InstancesTable = {
114114
};
115115

116116
type ExternalId = string;
117-
export type ExternalDataOriginKind = "wikidata" | "HAL" | "ComptoirDuLibre";
117+
export type ExternalDataOriginKind = "wikidata" | "HAL" | "ComptoirDuLibre" | "CNLL";
118118
type LocalizedString = Partial<Record<string, string>>;
119119

120120
type SimilarExternalSoftwareExternalDataTable = {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { sql, type Kysely } from "kysely";
2+
3+
export async function up(db: Kysely<any>): Promise<void> {
4+
await db.schema
5+
.alterTable("sources")
6+
.alterColumn("kind", col => col.setDataType("text"))
7+
.execute();
8+
9+
await db.schema.dropType("external_data_origin_type").execute();
10+
await db.schema.createType("external_data_origin_type").asEnum(["wikidata", "HAL", "ComptoirDuLibre", "CNLL"]).execute();
11+
12+
await db.schema
13+
.alterTable("sources")
14+
.alterColumn("kind", col =>
15+
col.setDataType(sql`external_data_origin_type USING kind::external_data_origin_type`)
16+
)
17+
.execute();
18+
}
19+
20+
export async function down(db: Kysely<any>): Promise<void> {
21+
await db.schema
22+
.alterTable("sources")
23+
.alterColumn("kind", col => col.setDataType("text"))
24+
.execute();
25+
26+
await db.schema.dropType("external_data_origin_type").execute();
27+
await db.schema.createType("external_data_origin_type").asEnum(["wikidata", "HAL", "ComptoirDuLibre"]).execute();
28+
29+
await db.schema
30+
.alterTable("sources")
31+
.alterColumn("kind", col =>
32+
col.setDataType(sql`external_data_origin_type USING kind::external_data_origin_type`)
33+
)
34+
.execute();
35+
}

api/src/tools/identifiersTools.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ const orcidSource: WebSite = {
5656
additionalType: "ORCID"
5757
};
5858

59+
const nationalSIREN: WebSite = {
60+
"@type": "Website" as const,
61+
name: "L’Annuaire des Entreprises",
62+
url: new URL("https://annuaire-entreprises.data.gouv.fr"),
63+
additionalType: "SIREN"
64+
};
65+
5966
export const identifersUtils = {
6067
makeGenericIdentifier: (params: { value: string; url?: string | URL }): SchemaIdentifier => {
6168
const { value, url } = params;
@@ -110,7 +117,7 @@ export const identifersUtils = {
110117
...(additionalType ? { additionalType: additionalType } : {})
111118
};
112119
},
113-
makeCNLLIdentifier: (params: { cNNLId: string; url: string; additionalType?: string }): SchemaIdentifier => {
120+
makeCNLLIdentifier: (params: { cNNLId: string; url?: string; additionalType?: string }): SchemaIdentifier => {
114121
const { cNNLId, url, additionalType } = params;
115122
return {
116123
"@type": "PropertyValue" as const,
@@ -166,5 +173,17 @@ export const identifersUtils = {
166173
subjectOf: orcidSource,
167174
...(additionalType ? { additionalType: additionalType } : {})
168175
};
176+
},
177+
makeSIRENIdentifier: (params: { SIREN: string; additionalType?: string; url?: string }) => {
178+
const { SIREN, additionalType, url } = params;
179+
return {
180+
...{
181+
"@type": "PropertyValue" as const,
182+
value: SIREN,
183+
url: url ?? undefined,
184+
subjectOf: nationalSIREN
185+
},
186+
...(additionalType ? { additionalType: additionalType } : {})
187+
};
169188
}
170189
};

0 commit comments

Comments
 (0)