-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathapi.ts
141 lines (131 loc) · 3.94 KB
/
api.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { omit } from 'lodash';
import { v4 as uuid } from 'uuid';
import { compare } from 'fast-json-patch';
import { FeatureCollection } from 'geojson';
import { get, post } from '../../../common/requests';
import {
ApiInfrastructure,
CreateEntityOperation,
DeleteEntityOperation,
EditorEntity,
EditorSchema,
EntityOperation,
SwitchType,
UpdateEntityOperation,
Zone,
} from '../../../types';
import { zoneToBBox } from '../../../utils/mapboxHelper';
import { getObjectTypeForLayer } from './utils';
/**
* Call the API to get an infra
*/
export async function getInfrastructure(id: number): Promise<ApiInfrastructure> {
return get(`/editoast/infra/${id}/`);
}
/**
* Call the API to get the list of infra
*/
export async function getInfrastructures(): Promise<Array<ApiInfrastructure>> {
return get(`/editoast/infra/`);
}
/**
* Call the API to get the definition of entities by layer.
*/
export async function getEditorSchema(): Promise<EditorSchema> {
const schemaResponse = await get('/infra/schema/');
const fieldToOmit = ['id', 'geo', 'sch'];
return Object.keys(schemaResponse.properties || {})
.filter((e: string) => schemaResponse.properties[e].type === 'array')
.map((e: string) => {
// we assume here, that the definition of the object is ref and not inline
const ref = schemaResponse.properties[e].items.$ref.split('/');
const refTarget = schemaResponse[ref[1]][ref[2]];
refTarget.properties = omit(refTarget.properties, fieldToOmit);
refTarget.required = (refTarget.required || []).filter(
(field: string) => !fieldToOmit.includes(field)
);
return {
layer: e,
objType: ref[2],
schema: {
...refTarget,
[ref[1]]: schemaResponse[ref[1]],
},
} as EditorSchema[0];
});
}
/**
* Call the API for the list of switch types in a given infra.
*/
export async function getSwitchTypes(infra: number): Promise<SwitchType[]> {
return get(`/editoast/infra/${infra}/switch_types`);
}
/**
* Call the API for geojson.
*/
export async function getEditorData(
schema: EditorSchema,
infra: number,
layers: Set<string>,
zone: Zone
): Promise<Record<string, EditorEntity[]>> {
const bbox = zoneToBBox(zone);
const layersArray = Array.from(layers);
const responses = await Promise.all(
layersArray.map(async (layer) => {
const objType = getObjectTypeForLayer(schema, layer);
const result = await get<FeatureCollection>(
`/layer/${layer}/objects/geo/${bbox[0]}/${bbox[1]}/${bbox[2]}/${bbox[3]}/?infra=${infra}`,
{}
);
return result.features.map((f) => ({ ...f, objType }));
})
);
return layersArray.reduce(
(iter, layer, i) => ({
...iter,
[layer]: responses[i],
}),
{}
);
}
/**
* Call the API to update the database.
*/
export async function editorSave(
infra: number,
operations: {
create?: Array<EditorEntity>;
update?: Array<{ source: EditorEntity; target: EditorEntity }>;
delete?: Array<EditorEntity>;
}
): Promise<Array<EditorEntity>> {
const payload: EntityOperation[] = [
...(operations.create || []).map(
(feature): CreateEntityOperation => ({
operation_type: 'CREATE',
obj_type: feature.objType,
railjson: {
...feature.properties,
id: uuid(),
},
})
),
...(operations.update || []).map(
(features): UpdateEntityOperation => ({
operation_type: 'UPDATE',
obj_id: features.source.properties.id,
obj_type: features.source.objType,
railjson_patch: compare(features.source.properties || {}, features.target.properties || {}),
})
),
...(operations.delete || []).map(
(feature): DeleteEntityOperation => ({
operation_type: 'DELETE',
obj_id: feature.properties.id,
obj_type: feature.objType,
})
),
];
return post<EntityOperation[], EditorEntity[]>(`/editoast/infra/${infra}`, payload, {});
}