Skip to content

Commit

Permalink
fix: access id form nested fields (#145)
Browse files Browse the repository at this point in the history
* fix: access id form nested fields

* feat: do not save merge groups when ther is nothing to save
  • Loading branch information
marc-gavanier authored Nov 22, 2023
1 parent 2542ce9 commit 6a3ff0c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 160 deletions.
156 changes: 0 additions & 156 deletions assets/input/les-assembleurs/services.francil-in.md

This file was deleted.

8 changes: 7 additions & 1 deletion src/dedupliquer/cli/action/dedupliquer.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ const onlyMoreThanDuplicationScoreThreshold =

const noCache = (): string => Math.random().toString(16).slice(2, -1);

/* eslint-disable-next-line max-statements */
export const dedupliquerAction = async (dedupliquerOptions: DedupliquerOptions): Promise<void> => {
const repository: DeduplicationRepository = await deduplicationRepository(dedupliquerOptions);
const repository: DeduplicationRepository = deduplicationRepository(dedupliquerOptions);

const lieux: AxiosResponse<SchemaLieuMediationNumerique[]> = await axios.get(
dedupliquerOptions.baseSource.replace('$cache', noCache())
Expand All @@ -43,5 +44,10 @@ export const dedupliquerAction = async (dedupliquerOptions: DedupliquerOptions):
const groups: Groups = groupDuplicates(duplicationComparisonsToGroup);
const merged: MergedLieuxByGroupMap = mergeDuplicates(new Date())(lieux.data, groups);

/* eslint-disable-next-line no-console */
console.log('Nouveaux lieux concernés par une fusion :', groups.itemGroupMap.size);
/* eslint-disable-next-line no-console */
console.log('Nouveaux lieux fusionnés à enregistrer :', merged.size);

await repository.save(groups, merged, lieux.data);
};
9 changes: 7 additions & 2 deletions src/dedupliquer/cli/data/save/save-with-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ const toSourceFromId = (id: string): string | undefined => id.split('@').at(0);
export const shouldMarkAsDeduplicated = (mergeGroupsMap: Map<string, string[]>): boolean =>
Array.from(new Set(Array.from(mergeGroupsMap.values()).flat().map(toSourceFromId))).length > 1;

const nothingToUpdate = (groups: Groups, merged: MergedLieuxByGroupMap): boolean =>
merged.size === 0 && groups.itemGroupMap.size === 0 && groups.mergeGroupsMap.size === 0;

export const saveWithApi =
(dedupliquerOptions: DedupliquerOptions) =>
async (groups: Groups, merged: MergedLieuxByGroupMap): Promise<AxiosResponse<void>> => {
async (groups: Groups, merged: MergedLieuxByGroupMap): Promise<void> => {
if (nothingToUpdate(groups, merged)) return;

const previousMergeGroup: MergeGroup[] = (
await axios.get<MergeGroup[]>(`${dedupliquerOptions.cartographieNationaleApiUrl}/lieux-inclusion-numerique/merge-groups`)
).data;

return axios.patch<unknown, AxiosResponse, MergeGroupTransfer>(
await axios.patch<unknown, AxiosResponse, MergeGroupTransfer>(
`${
dedupliquerOptions.cartographieNationaleApiUrl
}/lieux-inclusion-numerique/merge-groups?markAsDeduplicated=${shouldMarkAsDeduplicated(groups.mergeGroupsMap)}`,
Expand Down
38 changes: 38 additions & 0 deletions src/transformer/cli/diff-since-last-transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const UATL_FINGERPRINT: Fingerprint = {
hash: 'e7d7002d1df0c66f3c0ab706f6511dc534baeae83c0221607696532932af4751'
};

const UATL_NESTED_FINGERPRINT: Fingerprint = {
sourceId: '14',
hash: '6e4220f91ae29edacd6ae9f3bfc2525b8e4344755746c04e5dbe912d56f51c32'
};

const UATL_FINGERPRINT_UPDATED: Fingerprint = {
sourceId: '14',
hash: '0a93e9d31109b1c7a33342eca2c853c8f0ca71afeb7102e880e211db7f8d8faf'
Expand All @@ -39,6 +44,28 @@ const TRAIT_D_UNION_FINGERPRINT: Fingerprint = {

const FINGERPRINTS: Fingerprint[] = [UATL_FINGERPRINT, IREPS_FINGERPRINT, FILALIGNE_FINGERPRINT];

const UATL_NESTED: DataSource = {
properties: {
id_source: 14,
USER_NOM: 'Université Angevine du Temps Libre (UATL)',
USER_ADRES: '14, rue Pocquet de Livonnières',
USER_QUART: 'Centre Ville',
LAT: 47.47303152,
LNG: -0.54897495,
DATE_MAJ: '2022-09-26 19:18:28',
PUBLIC_: 'Senior (plus de 45 ans)',
ACCES_ORDI: 'Utilisation ordinateur et/ou tablette',
WIFI: 'Accès WIFI',
HORAIRE: '9h à 12h, 14h à 17h hors vacances scolaires',
TRANSPORT: 'Tramway (Ligne A) - arrêt Hôtel de Ville',
TELEPHONE: '02 41 88 96 41',
MAIL: 'uatl@uatl-eca.fr',
SITE_INTER: 'https://uatl-eca.fr/',
code_postal: '49000',
commune: 'Angers'
}
};

const UATL: DataSource = {
TYPO_UID: 14,
USER_NOM: 'Université Angevine du Temps Libre (UATL)',
Expand Down Expand Up @@ -133,6 +160,17 @@ const SOURCE_WITH_CREATE: DataSource[] = [UATL, IREPS, FILALIGNE, TRAIT_D_UNION]

describe('should transform', (): void => {
it('should detect that an item has not changed between two transformations', (): void => {
const itemsToTransform: DiffSinceLastTransform = diffSinceLastTransform('properties.id_source', [UATL_NESTED_FINGERPRINT])([
UATL_NESTED
]);

expect(itemsToTransform).toStrictEqual({
toUpsert: [],
toDelete: []
});
});

it('should detect that an item has not changed between two transformations with nested key', (): void => {
const itemsToTransform: DiffSinceLastTransform = diffSinceLastTransform(ID_KEY, FINGERPRINTS)(SOURCE);

expect(itemsToTransform).toStrictEqual({
Expand Down
6 changes: 5 additions & 1 deletion src/transformer/cli/diff-since-last-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const toIdsToDeleteFrom =
const findDeletedIds = (previousIds: Fingerprint[], newIds: string[]): FingerprintToDelete[] =>
previousIds.reduce(toIdsToDeleteFrom(newIds), []);

const getId = (idKey: string, item?: DataSource): string => (item?.[idKey] as string).toString();
const toInnerProperty = (source: DataSource | string, key: string): DataSource | string =>
typeof source === 'string' ? source : (source[key] as DataSource | string);

/* eslint-disable-next-line @typescript-eslint/no-base-to-string */
const getId = (idKey: string, item: DataSource): string => idKey.split('.').reduce(toInnerProperty, item).toString();

const onlyMatchingItemIds =
(idKey: string, currentItem: DataSource) =>
Expand Down

0 comments on commit 6a3ff0c

Please sign in to comment.