-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathutils.ts
More file actions
69 lines (57 loc) · 1.97 KB
/
utils.ts
File metadata and controls
69 lines (57 loc) · 1.97 KB
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
import type { DataSource } from 'typeorm/data-source';
import { DatasetLocation } from './DatasetLocation';
import { mapboxClient } from '../../integrations/mapbox/clients';
export const createLocationFromMapbox = async (
con: DataSource,
externalId: string,
): Promise<DatasetLocation | null> => {
const geocodingData = await mapboxClient.geocode(externalId);
const feature = geocodingData.features[0];
const properties = feature.properties;
return await con.manager.getRepository(DatasetLocation).save({
externalId: externalId,
country: properties.context?.country?.name,
city: properties.context?.place?.name,
subdivision: properties.context?.region?.name,
iso2: properties.context?.country?.country_code?.toUpperCase(),
iso3: properties.context?.country?.country_code_alpha_3?.toUpperCase(),
});
};
/**
* Find an existing location by externalId or create one from Mapbox.
*/
export const findOrCreateDatasetLocation = async (
con: DataSource,
externalLocationId: string | null | undefined,
): Promise<DatasetLocation | null> => {
if (!externalLocationId) {
return null;
}
let location = await con.getRepository(DatasetLocation).findOne({
where: { externalId: externalLocationId },
});
if (!location) {
location = await createLocationFromMapbox(con, externalLocationId);
}
return location;
};
/**
* Find an existing location in the dataset_location table based on iso2 country code.
*/
export const findDatasetLocation = async (
con: DataSource,
locationData: Partial<Pick<DatasetLocation, 'iso2' | 'city' | 'subdivision'>>,
): Promise<DatasetLocation | null> => {
const { iso2 } = locationData;
if (!iso2) {
return null;
}
const locationQuery = con.manager
.getRepository(DatasetLocation)
.createQueryBuilder()
.where('iso2 = :iso2Only', { iso2Only: iso2 })
.andWhere('city IS NULL')
.andWhere('subdivision IS NULL');
const location = await locationQuery.getOne();
return location;
};