Skip to content

Commit 7f766d5

Browse files
committed
feat: add notion of primary and secondary for source gateway
refs: #365 # Conflicts: # api/src/core/usecases/importFromSource.ts
1 parent 4b0793b commit 7f766d5

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

api/src/core/adapters/comptoirDuLibre/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { getCDLSoftwareOptions } from "./getCDLSoftwareOptions";
22
import { getCDLSoftwareExternalData } from "./getCDLExternalData";
33
import { getCDLFormData } from "./getCDLFormData";
4-
import { SourceGateway } from "../../ports/SourceGateway";
4+
import { PrimarySourceGateway } from "../../ports/SourceGateway";
55

6-
export const comptoirDuLibreSourceGateway: SourceGateway = {
6+
export const comptoirDuLibreSourceGateway: PrimarySourceGateway = {
77
sourceType: "ComptoirDuLibre",
8+
sourceProfile: "Primary",
89
softwareExternalData: {
910
getById: getCDLSoftwareExternalData
1011
},

api/src/core/adapters/hal/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { getHalSoftwareOptions } from "./getHalSoftwareOptions";
22
import { getHalSoftwareExternalData } from "./getHalSoftwareExternalData";
33
import { getHalSoftwareForm } from "./getSoftwareForm";
4-
import { SourceGateway } from "../../ports/SourceGateway";
4+
import { PrimarySourceGateway } from "../../ports/SourceGateway";
55

6-
export const halSourceGateway: SourceGateway = {
6+
export const halSourceGateway: PrimarySourceGateway = {
77
sourceType: "HAL",
8+
sourceProfile: "Primary",
89
softwareExternalData: {
910
getById: getHalSoftwareExternalData
1011
},

api/src/core/adapters/resolveAdapter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { SourceGateway } from "../ports/SourceGateway";
1+
import { PrimarySourceGateway, SecondarySourceGateway } from "../ports/SourceGateway";
22
import { DatabaseDataType } from "../ports/DbApiV2";
33
import { halSourceGateway } from "./hal";
44
import { wikidataSourceGateway } from "./wikidata";
55
import { comptoirDuLibreSourceGateway } from "./comptoirDuLibre";
66

7-
export const resolveAdapterFromSource = (source: DatabaseDataType.SourceRow): SourceGateway => {
7+
export const resolveAdapterFromSource = (
8+
source: DatabaseDataType.SourceRow
9+
): PrimarySourceGateway | SecondarySourceGateway => {
810
switch (source.kind) {
911
case "HAL":
1012
return halSourceGateway;

api/src/core/adapters/wikidata/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { SourceGateway } from "../../ports/SourceGateway";
1+
import { PrimarySourceGateway } from "../../ports/SourceGateway";
22
import { getWikidataForm } from "./getSoftwareForm";
33
import { getWikidataSoftware } from "./getWikidataSoftware";
44
import { getWikidataSoftwareOptions } from "./getWikidataSoftwareOptions";
55

6-
export const wikidataSourceGateway: SourceGateway = {
6+
export const wikidataSourceGateway: PrimarySourceGateway = {
77
sourceType: "wikidata",
8+
sourceProfile: "Primary",
89
softwareExternalData: {
910
getById: getWikidataSoftware
1011
},

api/src/core/ports/SourceGateway.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GetSoftwareFormData } from "./GetSoftwareFormData";
55

66
export type SourceGateway = {
77
sourceType: ExternalDataOriginKind;
8+
sourceProfile: "Primary" | "Secondary";
89
softwareExternalData: {
910
getById: GetSoftwareExternalData;
1011
};
@@ -15,3 +16,11 @@ export type SourceGateway = {
1516
getById: GetSoftwareFormData;
1617
};
1718
};
19+
20+
export type PrimarySourceGateway = SourceGateway & {
21+
sourceProfile: "Primary";
22+
};
23+
24+
export type SecondarySourceGateway = Pick<SourceGateway, "sourceType" | "sourceProfile" | "softwareExternalData"> & {
25+
sourceProfile: "Secondary";
26+
};

api/src/core/usecases/importFromSource.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export type ImportFromSource = (params: {
1313

1414
export const importFromSource: (dbApi: DbApiV2) => ImportFromSource = (dbApi: DbApiV2) => {
1515
return async ({ agentEmail, source, softwareIdOnSource }) => {
16+
const sourceGateway = resolveAdapterFromSource(source);
17+
18+
if (sourceGateway.sourceProfile !== "Primary")
19+
throw new Error("Import if not possbile from a secondary source");
20+
1621
const agent = await dbApi.agent.getByEmail(agentEmail);
1722
const agentId = agent
1823
? agent.id
@@ -23,7 +28,7 @@ export const importFromSource: (dbApi: DbApiV2) => ImportFromSource = (dbApi: Db
2328
about: "This is a bot user created to import data."
2429
});
2530

26-
const getSoftwareForm = resolveAdapterFromSource(source).softwareForm.getById;
31+
const getSoftwareForm = sourceGateway.softwareForm.getById;
2732
let result = [];
2833

2934
switch (source.kind) {

api/src/rpc/router.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ export function createRouter(params: {
102102
const mainSource = await dbApi.source.getMainSource();
103103
const sourceGateway = resolveAdapterFromSource(mainSource);
104104

105+
if (sourceGateway.sourceProfile !== "Primary")
106+
throw new Error("Getting option if not possbile from a secondary source");
107+
105108
const [queryResults, softwareExternalDataIds] = await Promise.all([
106109
sourceGateway.softwareOptions.getById({ queryString, language, source: mainSource }),
107110
dbApi.software.getAllSillSoftwareExternalIds(mainSource.slug)
@@ -481,21 +484,21 @@ export function createRouter(params: {
481484

482485
// prettier-ignore
483486
languages
484-
.map(lang => createResolveLocalizedString({
485-
"currentLanguage": lang,
486-
"fallbackLanguage": "en"
487-
}))
488-
.map(({resolveLocalizedString}) => [termsOfServiceUrl].map(resolveLocalizedString))
489-
.flat()
490-
.forEach(async function callee(url) {
487+
.map(lang => createResolveLocalizedString({
488+
"currentLanguage": lang,
489+
"fallbackLanguage": "en"
490+
}))
491+
.map(({ resolveLocalizedString }) => [termsOfServiceUrl].map(resolveLocalizedString))
492+
.flat()
493+
.forEach(async function callee(url) {
491494

492-
memoizedFetch(url);
495+
memoizedFetch(url);
493496

494-
await new Promise(resolve => setTimeout(resolve, maxAge - 10_000));
497+
await new Promise(resolve => setTimeout(resolve, maxAge - 10_000));
495498

496-
callee(url);
499+
callee(url);
497500

498-
});
501+
});
499502

500503
return async ({ input }) => {
501504
const { language, name } = input;

0 commit comments

Comments
 (0)