-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
336 lines (313 loc) · 9.31 KB
/
library.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const pool = require('../helpers/database')
const viewFieldsGeo = [
'local_authority',
'local_authority_code',
'library_name',
'address_1',
'address_2',
'address_3',
'postcode',
'library_type',
'year_closed',
'unique_property_reference_number',
'colocated',
'colocated_with',
'notes',
'url',
'email_address',
'longitude',
'latitude',
'easting',
'northing',
'oa_code',
'county_code',
'ward_code',
'region_code',
'country_code',
'rural_urban_classification',
'imd'
]
const viewFieldsSchema = [
'"Local authority"',
'"Library name"',
'"Address 1"',
'"Address 2"',
'"Address 3"',
'"Postcode"',
'"Unique property reference number"',
'"Statutory"',
'"Type of library"',
'"Year opened"',
'"Year closed"',
'"Monday staffed hours"',
'"Tuesday staffed hours"',
'"Wednesday staffed hours"',
'"Thursday staffed hours"',
'"Friday staffed hours"',
'"Saturday staffed hours"',
'"Sunday staffed hours"',
'"Monday unstaffed hours"',
'"Tuesday unstaffed hours"',
'"Wednesday unstaffed hours"',
'"Thursday unstaffed hours"',
'"Friday unstaffed hours"',
'"Saturday unstaffed hours"',
'"Sunday unstaffed hours"',
'"Special hours"',
'"Co-located"',
'"Co-located with"',
'"Notes"',
'"URL"',
'"Email address"'
]
const viewFieldsSchemaExtended = [
'"Local authority"',
'"Local authority code"',
'"Library name"',
'"Address 1"',
'"Address 2"',
'"Address 3"',
'"Postcode"',
'"Unique property reference number"',
'"Unique property reference number longitude"',
'"Unique property reference number latitude"',
'"Statutory"',
'"Type of library"',
'"Year opened"',
'"Year closed"',
'"Monday staffed hours"',
'"Tuesday staffed hours"',
'"Wednesday staffed hours"',
'"Thursday staffed hours"',
'"Friday staffed hours"',
'"Saturday staffed hours"',
'"Sunday staffed hours"',
'"Monday unstaffed hours"',
'"Tuesday unstaffed hours"',
'"Wednesday unstaffed hours"',
'"Thursday unstaffed hours"',
'"Friday unstaffed hours"',
'"Saturday unstaffed hours"',
'"Sunday unstaffed hours"',
'"Co-located"',
'"Co-located with"',
'"Notes"',
'"URL"',
'"Email address"',
'"Longitude"',
'"Latitude"',
'"id"'
]
/**
* Returns the field names in the library view schema
* @returns {Array} A list of field names
*/
module.exports.getSchemaFields = () =>
viewFieldsSchema.map(f => f.replace(/\"/g, ''))
/**
* Gets a list of libraries
* @param {Array} serviceCodes An array of GSS codes
* @param {numeric} longitude A longitude coordinate value
* @param {numeric} latitude A latitude coordinate value
* @param {numeric} distance A distance in metres to limit the libraries to
* @param {numeric} limit The number of libraries to limit to
* @param {int} page The page number of the results
* @param {sting} sortDirection The field to sort by
* @param {string} sortDirection ASC or DESC results
* @param {boolean} closed Whether to include closed libraries
* @returns {Array} A list of libraries
*/
module.exports.getLibraries = async (
serviceCodes,
longitude,
latitude,
distance,
limit,
page,
sort,
sortDirection,
closed
) => {
const services = serviceCodes ? serviceCodes.split('|') : []
let params = [
['limit', limit],
['page', page]
].filter(x => x[1] !== null)
let libraries = []
try {
const whereQueries = []
let limitQuery = ''
let offsetQuery = ''
let orderQuery = ''
let selectFields = [...viewFieldsSchemaExtended]
params.forEach((param, i) => {
const idx = i + 1
if (param[0] === 'limit') limitQuery = `limit $${idx}`
if (param[0] === 'page') {
params[i][1] = limit * (page - 1) // Calculate offset from the page and limit
offsetQuery = `offset $${idx}`
}
})
const sortDirectionText = sortDirection === 'desc' ? 'desc' : 'asc'
if (viewFieldsSchemaExtended.indexOf('"' + sort + '"') !== -1) {
orderQuery = `order by "${sort}" ${sortDirectionText}`
}
params = params.map(p => p[1]) // Change params array just to values.
if (services.length > 0) {
const servicesText = services
.map((o, oidx) => '$' + (oidx + 1 + params.length))
.join(',')
whereQueries.push(`"Local authority code" in (${servicesText})`)
params = params.concat(services)
}
if (longitude && latitude && distance) {
const longitudeParam = params.length + 1
const latitudeParam = params.length + 2
const distanceParam = params.length + 3
whereQueries.push(
`st_dwithin(st_transform(st_setsrid(st_makepoint($${longitudeParam}, $${latitudeParam}), 4326), 27700), st_transform(geom, 27700), $${distanceParam})`
)
params = params.concat([longitude, latitude, distance])
selectFields.push(
`round(st_distance(st_transform(st_setsrid(st_makepoint($${longitudeParam}, $${latitudeParam}), 4326), 27700), st_transform(geom, 27700))) as distance`
)
if (sort === 'distance')
orderQuery = `order by distance ${sortDirectionText}`
}
if (!closed) whereQueries.push('"Year closed" is null')
const selectFieldsText = selectFields.join(', ')
const whereQueriesText =
whereQueries.length > 0 ? `where ${whereQueries.join(' and ')}` : ''
const query = `select ${selectFieldsText}, count(*) OVER() AS total from vw_schemas_libraries_extended ${whereQueriesText} ${orderQuery} ${limitQuery} ${offsetQuery}`
const { rows } = await pool.query(query, params)
libraries = rows
} catch (e) {}
return libraries
}
/**
* Gets a list of libraries in the strict schema definition
* @param {Array} serviceCodes An array of ONS codes
* @returns {Array} A list of libraries
*/
module.exports.getLibrariesSchema = async serviceCodes => {
const services = serviceCodes ? serviceCodes.split('|') : []
let libraries = []
try {
let params = []
const whereQueries = []
if (services.length > 0) {
whereQueries.push(
'"Local authority" in (select name from schemas_local_authority where code in (' +
services
.map((o, oidx) => '$' + (oidx + 1 + params.length))
.join(',') +
'))'
)
params = params.concat(services)
}
const query =
'select ' +
viewFieldsSchema.join(', ') +
' from vw_schemas_libraries ' +
(whereQueries.length > 0
? 'where ' + whereQueries.join(' and ') + ' '
: '')
const { rows } = await pool.query(query, params)
libraries = rows
} catch (e) {}
return libraries
}
/**
* Gets nearest libraries from a point
* @param {numeric} longitude A longitude coordinate value
* @param {numeric} latitude A latitude coordinate value
* @param {int} limit The number of libraries to limit to
* @returns {Array} A list of libraries
*/
module.exports.getNearestLibraries = async (longitude, latitude, limit) => {
let libraries = []
try {
const query =
'select ' +
viewFieldsGeo.join(', ') +
', st_distance(st_transform(st_setsrid(st_makepoint($1, $2), 4326), 27700), st_setsrid(st_makepoint(easting, northing), 27700)) as distance from vw_libraries_geo where year_closed is null order by distance asc limit $3'
const { rows } = await pool.query(query, [longitude, latitude, limit])
if (rows.length > 0) libraries = rows
} catch (e) {}
return libraries
}
/**
* Get library details by the internal ID
* @param {int} id
* @returns {object} A library object
*/
module.exports.getLibraryById = async id => {
let library = null
try {
const query =
'select ' +
viewFieldsSchemaExtended.join(', ') +
' ' +
'from vw_schemas_libraries_extended where id = $1'
const { rows } = await pool.query(query, [id])
if (rows.length > 0) library = rows[0]
} catch (e) {}
return library
}
/**
* Get library details by the system name (slug)
* @param {serviceSystemName}
* @param {librarySystemName}
* @returns {object} A library object
*/
module.exports.getLibraryBySystemName = async (
serviceSystemName,
librarySystemName
) => {
let library = null
try {
const query = `select ${viewFieldsSchemaExtended.join(
', '
)} from vw_schemas_libraries_extended where lower(regexp_replace("Local authority", '[. ,:-]+', '-', 'g')) = $1 and lower(regexp_replace("Library name", '[. ,:-]+', '-', 'g')) = $2`
const { rows } = await pool.query(query, [
serviceSystemName,
librarySystemName
])
if (rows.length > 0) library = rows[0]
} catch (e) {}
return library
}
/**
* Get a library points tile
* @param {int} x
* @param {*} y
* @param {*} z
* @returns {encoded} The tile
*/
module.exports.getTileData = async (x, y, z) => {
const query = 'select fn_libraries_mvt($1, $2, $3)'
let tile = null
try {
const { rows } = await pool.query(query, [x, y, z])
if (rows && rows.length > 0 && rows[0].fn_libraries_mvt)
tile = rows[0].fn_libraries_mvt
} catch (e) {}
return tile
}
/**
* Get a building polygons tile
* @param {int} x
* @param {*} y
* @param {*} z
* @returns {encoded} The tile
*/
module.exports.getBuildingsTileData = async (x, y, z) => {
const query = 'select fn_libraries_buildings_mvt($1, $2, $3)'
let tile = null
try {
const { rows } = await pool.query(query, [x, y, z])
if (rows && rows.length > 0 && rows[0].fn_libraries_buildings_mvt)
tile = rows[0].fn_libraries_buildings_mvt
} catch (e) {}
return tile
}