-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lieux with duplicates from api are paginated (#190)
- Loading branch information
1 parent
d4f17e6
commit ef560f1
Showing
11 changed files
with
154 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import axios from 'axios'; | ||
|
||
export type Pagination<T> = { | ||
data: T[]; | ||
links: { | ||
self: string; | ||
first: string; | ||
last: string; | ||
next?: string; | ||
previous?: string; | ||
}; | ||
}; | ||
|
||
export const paginate = async <T>(url: string | undefined, query: string = '', data: T[] = []): Promise<T[]> => { | ||
if (url == null) return data; | ||
|
||
const nextResult: Pagination<T> = (await axios.get<Pagination<T>>(query === '' ? url : `${url}&${query}`)).data; | ||
|
||
return paginate(nextResult.links.next, query, [...data, ...nextResult.data]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/extract/cli/action/build-api-url/build-api-url.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/extract/cli/action/build-api-url/extract-query-string.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { extractQueryString } from './extract-query-string'; | ||
|
||
describe('build api url', (): void => { | ||
it('should build url with filter on code insee', (): void => { | ||
const url: string = extractQueryString({ | ||
departements: '01,03,07,15,26,38,42,43,63,69,73,74', | ||
duplicates: true | ||
}); | ||
|
||
expect(url).toBe('and[mergedIds][exists]=false&adresse[beginsWith][code_insee]=01,03,07,15,26,38,42,43,63,69,73,74'); | ||
}); | ||
|
||
it('should build url without departements', (): void => { | ||
const url: string = extractQueryString({ | ||
duplicates: true | ||
}); | ||
|
||
expect(url).toBe('and[mergedIds][exists]=false'); | ||
}); | ||
|
||
it('should build url without duplicates', (): void => { | ||
const url: string = extractQueryString({ | ||
duplicates: false | ||
}); | ||
|
||
expect(url).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ExtractOptions } from '../../extract-options'; | ||
|
||
export const extractQueryString = ({ | ||
departements, | ||
duplicates | ||
}: Pick<ExtractOptions, 'departements' | 'duplicates'>): string => { | ||
const mergedIds: string = duplicates ? 'and[mergedIds][exists]=false' : ''; | ||
return departements == null ? mergedIds : `${mergedIds}&adresse[beginsWith][code_insee]=${departements}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './build-api-url'; | ||
export * from './extract-query-string'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import axios from 'axios'; | ||
import { | ||
fromSchemaLieuDeMediationNumerique, | ||
LieuMediationNumerique, | ||
SchemaLieuMediationNumerique | ||
} from '@gouvfr-anct/lieux-de-mediation-numerique'; | ||
import { paginate } from '../../../common'; | ||
import { saveOutputsInFiles } from '../../../transformer/data'; | ||
import { ExtractOptions } from '../extract-options'; | ||
import { buildApiUrl } from './build-api-url'; | ||
import { extractQueryString } from './build-api-url'; | ||
|
||
export const extractAction = async (extractOptions: ExtractOptions): Promise<void> => { | ||
const lieuxToPublish: LieuMediationNumerique[] = ( | ||
await axios.get<SchemaLieuMediationNumerique[]>(buildApiUrl(extractOptions)) | ||
).data.map(fromSchemaLieuDeMediationNumerique); | ||
await paginate<SchemaLieuMediationNumerique>( | ||
`${extractOptions.cartographieNationaleApiUrl}/lieux-inclusion-numerique/with-duplicates?page[number]=0&page[size]=10000`, | ||
extractQueryString(extractOptions) | ||
) | ||
).map(fromSchemaLieuDeMediationNumerique); | ||
|
||
await saveOutputsInFiles(extractOptions)(lieuxToPublish, extractOptions.duplicates ? undefined : 'sans-doublons'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters