Skip to content

Commit dd5118e

Browse files
committed
feat(source): add CNLL source
refs: #360
1 parent 66b03bd commit dd5118e

File tree

4 files changed

+128
-3
lines changed

4 files changed

+128
-3
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/core/utils.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export const orcidSource: WebSite = {
5858
additionalType: "ORCID"
5959
};
6060

61+
export const nationalSIREN: WebSite = {
62+
"@type": "Website" as const,
63+
name: "L’Annuaire des Entreprises",
64+
url: new URL("https://annuaire-entreprises.data.gouv.fr"),
65+
additionalType: "SIREN"
66+
};
67+
6168
export const identifersUtils = {
6269
makeGenericIdentifier: (params: { value: string; url?: string | URL }): SchemaIdentifier => {
6370
const { value, url } = params;
@@ -112,12 +119,12 @@ export const identifersUtils = {
112119
...(additionalType ? { additionalType: additionalType } : {})
113120
};
114121
},
115-
makeCNLLIdentifier: (params: { cNNLId: string; url: string; additionalType?: string }): SchemaIdentifier => {
122+
makeCNLLIdentifier: (params: { cNNLId: string; url?: string; additionalType?: string }): SchemaIdentifier => {
116123
const { cNNLId, url, additionalType } = params;
117124
return {
118125
"@type": "PropertyValue" as const,
119126
value: cNNLId,
120-
url: url,
127+
url: url ?? undefined,
121128
subjectOf: cNNLSource,
122129
...(additionalType ? { additionalType: additionalType } : {})
123130
};
@@ -168,6 +175,18 @@ export const identifersUtils = {
168175
subjectOf: orcidSource,
169176
...(additionalType ? { additionalType: additionalType } : {})
170177
};
178+
},
179+
makeSIRENIdentifier: (params: { SIREN: string; additionalType?: string; url?: string }) => {
180+
const { SIREN, additionalType, url } = params;
181+
return {
182+
...{
183+
"@type": "PropertyValue" as const,
184+
value: SIREN,
185+
url: url ?? undefined,
186+
subjectOf: nationalSIREN
187+
},
188+
...(additionalType ? { additionalType: additionalType } : {})
189+
};
171190
}
172191
};
173192

0 commit comments

Comments
 (0)