Skip to content

Commit

Permalink
first push
Browse files Browse the repository at this point in the history
  • Loading branch information
nikiwycherley committed Oct 11, 2023
1 parent 4f5dcf1 commit 1c17bac
Show file tree
Hide file tree
Showing 5 changed files with 4,745 additions and 1,280 deletions.
2 changes: 1 addition & 1 deletion lib/functions/getMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports.getMessage = async (event) => {

const ret = await service.getMessage(event.pathParameters.id)

if (!ret || !ret.rows || !Array.isArray(ret.rows) || ret.rows.length < 1 || !ret.rows[0].getmessage) {
if (!ret?.rows?.[0]?.getmessage) {
console.log('No message found for ' + event.pathParameters.id)
throw new Error('No message found')
}
Expand Down
13 changes: 10 additions & 3 deletions lib/functions/processMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const xml2js = require('xml2js')
const moment = require('moment')
const AWSConfig = require('../../config/config.json').aws
const service = require('../helpers/service')
Expand All @@ -20,15 +19,23 @@ module.exports.processMessage = async (event) => {

// Add in the references field
const xmlMessage = event.bodyXml.replace('</scope>', '</scope>\n<references></references>')
const parseString = require('xml2js').parseString

const xmlResult = await new Promise((resolve, reject) => {
xml2js.parseString(xmlMessage, (err, value) => {
parseString(xmlMessage, function (err, value) {
if (err) return reject(err)
resolve(value)
})
})

const dbResult = await service.getLastMessage(xmlResult.alert.info[0].area[0].geocode[0].value[0])
let dbResult

try {
dbResult = await service.getLastMessage(xmlResult.alert.info[0].area[0].geocode[0].value[0])
} catch (error) {
console.error('Error while getting last message:', error)
throw error
}

const lastMessage = (!!dbResult && dbResult.rows.length > 0) ? dbResult.rows[0] : undefined

Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ const processMessageEventSchema = Joi.object({
})

module.exports = {
getMessageEventSchema: getMessageEventSchema,
processMessageEventSchema: processMessageEventSchema
getMessageEventSchema,
processMessageEventSchema
}
Loading

0 comments on commit 1c17bac

Please sign in to comment.