-
Notifications
You must be signed in to change notification settings - Fork 2
/
municipality-list.ts
117 lines (100 loc) · 3.32 KB
/
municipality-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Store from '@ember-data/store';
import Service, { service } from '@ember/service';
import { QueryParameterKeys } from 'frontend-burgernabije-besluitendatabank/constants/query-parameter-keys';
import LocationModel from 'frontend-burgernabije-besluitendatabank/models/location';
import { deserializeArray } from 'frontend-burgernabije-besluitendatabank/utils/query-params';
type Municipality = { label: string; id: string };
export default class MunicipalityListService extends Service {
@service declare store: Store;
private _municipalities?: Array<Municipality>;
private _municipalityCleanedLabels?: Array<{ label: string }>;
/**
* Get all municipalities
*
* - If possible, return it from local variable/cache
* - If that is empty, populate it with & return from
*
* @returns A promise for an array of municipalities
**/
async municipalities() {
if (!this._municipalities) {
this._municipalities = await this._loadMunicipalities();
}
return this._municipalities;
}
/**
* Get all municipalities, filtered on the following criteria:
* - no duplicate labels
* - remove 'Kruishoutem' (because it's not a municipality anymore) #BNB-402
*
* Filtering is managed frontend as a temporary solution ^^
* TODO: move this to the backend
*
* @returns A promise for an array of municipality labels
**/
async municipalityLabels() {
if (!this._municipalityCleanedLabels) {
this._municipalityCleanedLabels = this._filteredMunicipalities(
await this.municipalities()
);
}
return this._municipalityCleanedLabels;
}
private _filteredMunicipalities(municipalities?: Array<Municipality>) {
if (!municipalities) {
return [];
}
const uniqueLabels = [
...new Set(
municipalities
.filter(
(municipality) =>
municipality.label && municipality.label !== 'Kruishoutem'
)
.map(({ label }) => label)
),
].map((label) => ({ label, type: QueryParameterKeys.municipalities }));
return uniqueLabels;
}
/**
* Requests & parses municipalities from Ember-Data
*
* @returns An promise for an array of municipalities, parsed into objects with an id & label property
*/
private async _loadMunicipalities() {
const municipalities = await this.store.query('location', {
page: { size: 600 },
filter: {
niveau: 'Gemeente',
},
sort: ':no-case:label',
});
return municipalities.map((location: LocationModel) => ({
id: location.id,
label: location.label,
// look at the environment APP.municipalities for the type
type: QueryParameterKeys.municipalities,
}));
}
/**
*
* @param labels an array of location labels
* Alternatively, a string of location labels
* @returns a Promise for an array of location ids
*/
async getLocationIdsFromLabels(
labels?: Array<string> | string
): Promise<Array<string>> {
const municipalities = await this.municipalities();
if (typeof labels === 'string') {
labels = deserializeArray(labels);
}
if (!labels || !municipalities) {
return [];
}
const locationIds: Array<string> = municipalities
.filter(({ label }) => labels?.includes(label))
.map(({ id }) => id);
return locationIds;
}
}