Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions endpoints/tracking/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable no-param-reassign */

const request = require('request')
const cheerio = require('cheerio')
const app = require('../../server')

app.get('/tracking/:trackingNumber', (req, res) => {
const url = 'http://www.postur.is/' +
'einstaklingar/senda-pakka-innanlands/finna-sendingu/' +
`?TrackingNumber=${req.params.trackingNumber}&Language=IS`

request.get(url, (err, response, body) => {
const $ = cheerio.load(body)

const history = $('table').eq(1).find('tr').map((index, row) => {
return {
date: $(row).children().eq(0).text()
.replace(/\s/g, ''),
action: $(row).children().eq(1).text()
.replace(/\s/g, ''),
}
})
.get()
.slice(1)
.reduce((sum, curr) => {
sum[curr.date] = curr.action
return sum
}, {})

if (Object.keys(history).length === 0) {
return res.status(404).json({})
}

return res.cache(60).json({
history,
})
})
})
27 changes: 27 additions & 0 deletions endpoints/tracking/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const request = require('request')
const assert = require('assert')
const nock = require('nock')

describe.only('tracking', () => {
const trackingNumber = 'TRACKING_NUMBER'
before(() => {
const path = '/einstaklingar/senda-pakka-innanlands/finna-sendingu/' +
`?TrackingNumber=${trackingNumber}&Language=IS`
const scope = nock('http://www.postur.is')
.get(path)
.reply(200, 'domain matched')
//const secondScope = nock('http://localhost:3101')
// .get(`/tracking/${trackingNumber}`)
// .reply(200, 'domain matched')
})

it('should return a 404 when the tracking number can\'t be found', (done) => {
request.get(`http://localhost:3101/tracking/${trackingNumber}`, (error, response) => {
console.log(error)
console.log(response)
assert.equal(404, response.statusCode)

done()
})
})
})
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ fileModule.walkSync('./endpoints', (dirPath, dirs, endpoints) => {
if (!(endpoint.includes('.DS_Store') && !dirPath.includes('graphql') && !endpoint.includes('graphql'))) {
try {
require(`./${dirPath}/${endpoint}`)
} catch (e) {
console.error('Error loading file', e)
} catch (error) {
console.error('Error loading file', error)
}
}
}
Expand Down