-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalAuthority.js
48 lines (44 loc) · 1.22 KB
/
localAuthority.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
const pool = require('../helpers/database')
const tableFields = ['code', 'name', 'nice_name']
/**
* Gets the local library authorities in the database
* @returns {Array} Set of local authorities
*/
module.exports.getLocalAuthorities = async () => {
let services = []
try {
const query =
'select ' + tableFields.join(', ') + ' from schemas_local_authority'
const { rows } = await pool.query(query)
if (rows.length > 0) services = rows
services = rows
} catch (e) {}
return services
}
/**
* Gets the local library authorities in the database that match a set of codes
* @param {Array} codes An array of ONS codes
* @returns {Array} Set of local authorities
*/
module.exports.getLocalAuthoritiesByCodes = async codes => {
let services = []
try {
const query =
'select ' +
tableFields.join(', ') +
' from schemas_local_authority where code = ANY($1::text[])'
const { rows } = await pool.query(query, [codes])
if (rows.length > 0) services = rows
services = rows
} catch (e) {
console.log(e)
}
return services
}
module.exports.getLocalAuthoritySlugFromName = name => {
return name
.trim()
.replace(/[^\w\s]/gi, '')
.replace(/ /g, '_')
.toLowerCase()
}