Skip to content

Commit

Permalink
Updating view fields for id
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveBathnes committed Oct 8, 2023
1 parent b793cff commit 510b973
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 106 deletions.
40 changes: 30 additions & 10 deletions helpers/airtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const Airtable = require('airtable')
* @returns {Array} An array of records
*/
module.exports.getAllRecordsInTable = async (baseName, table) => {
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(baseName)
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(
baseName
)
const allRecords = await base(table).select({}).all()
return allRecords.map(r => r.fields)
}
Expand All @@ -21,12 +23,21 @@ module.exports.getAllRecordsInTable = async (baseName, table) => {
* @param {string} filterFieldValue The value to filter the filter field by
* @returns {Array} An array of records
*/
module.exports.getSingleFieldArrayAllRecordsInTable = async (baseName, table, fieldName, filterFieldName, filterFieldValue) => {
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(baseName)
module.exports.getSingleFieldArrayAllRecordsInTable = async (
baseName,
table,
fieldName,
filterFieldName,
filterFieldValue
) => {
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(
baseName
)
let select = {}
if (filterFieldName) {
select = {
filterByFormula: '({' + filterFieldName + "} = '" + filterFieldValue + "')"
filterByFormula:
'({' + filterFieldName + "} = '" + filterFieldValue + "')"
}
}
try {
Expand All @@ -45,13 +56,22 @@ module.exports.getSingleFieldArrayAllRecordsInTable = async (baseName, table, fi
* @param {string} fieldValue The value to filter the filter field by
* @returns
*/
module.exports.getRecordInTable = async (baseName, table, fieldName, fieldValue) => {
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(baseName)
module.exports.getRecordInTable = async (
baseName,
table,
fieldName,
fieldValue
) => {
const base = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(
baseName
)

const records = await base(table).select({
maxRecords: 1,
filterByFormula: '({' + fieldName + "} = '" + fieldValue + "')"
}).all()
const records = await base(table)
.select({
maxRecords: 1,
filterByFormula: '({' + fieldName + "} = '" + fieldValue + "')"
})
.all()

if (records.length > 0) return records[0].fields
return null
Expand Down
41 changes: 23 additions & 18 deletions helpers/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Email = require('email-templates')
* @param {string} token A JSON web token
* @returns {object|null} A claims object
*/
module.exports.verifyToken = async (token) => {
module.exports.verifyToken = async token => {
let domain = null
try {
const decoded = jwt.verify(token, process.env.AUTHSECRET)
Expand All @@ -25,7 +25,7 @@ module.exports.verifyToken = async (token) => {
* @param {string} token
* @returns {string} Email domain
*/
module.exports.getTokenDomain = async (token) => {
module.exports.getTokenDomain = async token => {
let domain = null
try {
const decoded = jwt.verify(token, process.env.AUTHSECRET)
Expand All @@ -41,13 +41,14 @@ module.exports.getTokenDomain = async (token) => {
* @param {string} domain A top level domain
* @returns {object} A claims object
*/
module.exports.getDomainClaims = async (domain) => {
module.exports.getDomainClaims = async domain => {
let claims = { admin: false, codes: [] }
try {
const query = 'select * from authentication where domain = $1 limit 1'
const { rows } = await pool.query(query, [domain])
if (rows.length > 0) claims = { admin: rows[0].admin, codes: rows[0].authority_codes }
} catch (e) { }
if (rows.length > 0)
claims = { admin: rows[0].admin, codes: rows[0].authority_codes }
} catch (e) {}
return claims
}

Expand All @@ -59,7 +60,12 @@ module.exports.getDomainClaims = async (domain) => {
*/
module.exports.sendMagicLink = async (email, claims, website) => {
const domain = email.split('@').pop()
const token = jwt.sign(claims, process.env.AUTHSECRET, { audience: [website], expiresIn: '30d', issuer: 'api.librarydata.uk', subject: domain })
const token = jwt.sign(claims, process.env.AUTHSECRET, {
audience: [website],
expiresIn: '30d',
issuer: 'api.librarydata.uk',
subject: domain
})

const mailConfig = {
host: process.env.SMTPSERVER,
Expand All @@ -81,17 +87,16 @@ module.exports.sendMagicLink = async (email, claims, website) => {
})

try {
emailTemplate
.send({
template: website + '-login',
message: {
to: email
},
locals: {
website: website,
token: token
}
})
emailTemplate.send({
template: website + '-login',
message: {
to: email
},
locals: {
website: website,
token: token
}
})
} catch (e) {
return false
}
Expand All @@ -106,5 +111,5 @@ module.exports.sendMagicLink = async (email, claims, website) => {
* @returns {boolean} Access
*/
module.exports.verifyServiceCodeAccess = async (serviceCode, claims) => {
return (claims.codes.indexOf(serviceCode) === -1 && claims.admin === false)
return claims.codes.indexOf(serviceCode) === -1 && claims.admin === false
}
28 changes: 18 additions & 10 deletions helpers/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports.getFeedFromUrls = async (urls, title, customNamespaces) => {
* @param {string} id ID of the YouTube channel/playlist/user
* @returns {string} A feed URL
*/
module.exports.getYouTubeFeedUrlFromId = (id) => {
module.exports.getYouTubeFeedUrlFromId = id => {
const youTubeUrl = process.env.YOUTUBE_FEED_URL
const idTypes = {
UC: 'channel_id',
Expand All @@ -38,7 +38,7 @@ module.exports.getYouTubeFeedUrlFromId = (id) => {
* @param {string} id ID of the Flickr user
* @returns {string} A feed URL
*/
module.exports.getFlickrFeedUrlFromId = (id) => {
module.exports.getFlickrFeedUrlFromId = id => {
return process.env.FLICKR_FEED_URL + id
}

Expand All @@ -47,7 +47,7 @@ module.exports.getFlickrFeedUrlFromId = (id) => {
* @param {Array} ids An array of YouTube IDs
* @returns {Array} A set of feed URLs
*/
module.exports.getYouTubeFeedUrlArrayFromIds = (ids) => {
module.exports.getYouTubeFeedUrlArrayFromIds = ids => {
return ids.map(id => this.getYouTubeFeedUrlFromId(id))
}

Expand All @@ -56,12 +56,16 @@ module.exports.getYouTubeFeedUrlArrayFromIds = (ids) => {
* @param {Array} ids An array of YouTube IDs
* @returns {object} A combined feed
*/
module.exports.getFeedFromYouTubeIds = async (ids) => {
module.exports.getFeedFromYouTubeIds = async ids => {
const urls = this.getYouTubeFeedUrlArrayFromIds(ids)
const feed = await this.getFeedFromUrls(urls, 'YouTube libraries | Libraries at home', {
yt: 'http://www.youtube.com/xml/schemas/2015',
media: 'http://search.yahoo.com/mrss/'
})
const feed = await this.getFeedFromUrls(
urls,
'YouTube libraries | Libraries at home',
{
yt: 'http://www.youtube.com/xml/schemas/2015',
media: 'http://search.yahoo.com/mrss/'
}
)
return feed
}

Expand All @@ -70,7 +74,11 @@ module.exports.getFeedFromYouTubeIds = async (ids) => {
* @param {*} urls A set of Blog feed URLs
* @returns {object} A combined feed
*/
module.exports.getFeedFromBlogUrls = async (urls) => {
const feed = await this.getFeedFromUrls(urls, 'Library blogs | Libraries at home', {})
module.exports.getFeedFromBlogUrls = async urls => {
const feed = await this.getFeedFromUrls(
urls,
'Library blogs | Libraries at home',
{}
)
return feed
}
10 changes: 8 additions & 2 deletions helpers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ const { Base64 } = require('js-base64')
* @param {string} message A commit message that describes the change
* @returns {boolean} Whether the update was successful.
*/
module.exports.createOrUpdateFile = async (path, file, message, authorName, authorEmail) => {
module.exports.createOrUpdateFile = async (
path,
file,
message,
authorName,
authorEmail
) => {
const content = Base64.encode(file)

const octokit = new Octokit({
Expand Down Expand Up @@ -43,5 +49,5 @@ module.exports.createOrUpdateFile = async (path, file, message, authorName, auth
sha
})

return (result?.status === 200)
return result?.status === 200
}
2 changes: 1 addition & 1 deletion helpers/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const axios = require('axios')
* @param {string} url The file URL
* @returns {string} The file contents
*/
module.exports.getFileFromUrl = async (url) => {
module.exports.getFileFromUrl = async url => {
const response = await axios.get(url)
return response.data
}
4 changes: 2 additions & 2 deletions middleware/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const mcache = require('memory-cache')
* @param {int} duration
* @returns {null}
*/
const cache = (duration) => {
const cache = duration => {
return (req, res, next) => {
const key = '__express__' + req.originalUrl || req.url
const cachedBody = mcache.get(key)
if (cachedBody) {
res.send(cachedBody)
} else {
res.sendResponse = res.send
res.send = (body) => {
res.send = body => {
mcache.put(key, body, duration * 1000)
res.sendResponse(body)
}
Expand Down
3 changes: 2 additions & 1 deletion models/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const viewFieldsGeo = [
'rural_urban_classification',
'imd'
]

const viewFieldsSchema = [
'"Local authority"',
'"Library name"',
Expand Down Expand Up @@ -97,7 +98,7 @@ const viewFieldsSchemaExtended = [
'"Email address"',
'"Longitude"',
'"Latitude"',
'id'
"'id'"
]

/**
Expand Down
18 changes: 13 additions & 5 deletions models/localAuthority.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const tableFields = ['code', 'name', 'nice_name']
module.exports.getLocalAuthorities = async () => {
let services = []
try {
const query = 'select ' + tableFields.join(', ') + ' from schemas_local_authority'
const query =
'select ' + tableFields.join(', ') + ' from schemas_local_authority'
const { rows } = await pool.query(query)
if (rows.length > 0) services = rows
services = rows
Expand All @@ -22,10 +23,13 @@ module.exports.getLocalAuthorities = async () => {
* @param {Array} codes An array of ONS codes
* @returns {Array} Set of local authorities
*/
module.exports.getLocalAuthoritiesByCodes = async (codes) => {
module.exports.getLocalAuthoritiesByCodes = async codes => {
let services = []
try {
const query = 'select ' + tableFields.join(', ') + ' from schemas_local_authority where code = ANY($1::text[])'
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
Expand All @@ -35,6 +39,10 @@ module.exports.getLocalAuthoritiesByCodes = async (codes) => {
return services
}

module.exports.getLocalAuthoritySlugFromName = (name) => {
return name.trim().replace(/[^\w\s]/gi, '').replace(/ /g, '_').toLowerCase()
module.exports.getLocalAuthoritySlugFromName = name => {
return name
.trim()
.replace(/[^\w\s]/gi, '')
.replace(/ /g, '_')
.toLowerCase()
}
Loading

0 comments on commit 510b973

Please sign in to comment.