Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing classes with modules and functions #46

Merged
merged 6 commits into from
Dec 9, 2022
Merged
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
30 changes: 15 additions & 15 deletions app/controllers/health/airbrake.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
* @module AirbrakeController
*/

class AirbrakeController {
static async index (request, _h) {
// First section tests connecting to Airbrake through a manual notification
request.server.app.airbrake.notify({
message: 'Airbrake manual health check',
error: new Error('Airbrake manual health check error'),
session: {
req: {
id: request.info.id
}
async function index (request, _h) {
// First section tests connecting to Airbrake through a manual notification
request.server.app.airbrake.notify({
message: 'Airbrake manual health check',
error: new Error('Airbrake manual health check error'),
session: {
req: {
id: request.info.id
}
})
}
})

// Second section throws an error and checks that we automatically capture it and then connect to Airbrake
throw new Error('Airbrake automatic health check error')
}
// Second section throws an error and checks that we automatically capture it and then connect to Airbrake
throw new Error('Airbrake automatic health check error')
}

module.exports = AirbrakeController
module.exports = {
index
}
12 changes: 6 additions & 6 deletions app/controllers/health/database.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

const DatabaseHealthCheckService = require('../../services/database-health-check.service.js')

class DatabaseController {
static async index (_request, h) {
const result = await DatabaseHealthCheckService.go()
async function index (_request, h) {
const result = await DatabaseHealthCheckService.go()

return h.response(result).code(200)
}
return h.response(result).code(200)
}

module.exports = DatabaseController
module.exports = {
index
}
33 changes: 13 additions & 20 deletions app/controllers/root.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@

const ServiceStatusService = require('../services/service-status.service.js')

class RootController {
static async index (_request, _h) {
return { status: 'alive' }
}

static helloWorld (_request, h) {
return h.view('home.njk', {
title: 'Hello',
message: 'World',
pageTitle: 'Hello World!'
})
}
async function index (_request, _h) {
return { status: 'alive' }
}

static async serviceStatus (_request, h) {
const pageData = await ServiceStatusService.go()
async function serviceStatus (_request, h) {
const pageData = await ServiceStatusService.go()

return h.view('service_status.njk', {
pageTitle: 'Service Status',
...pageData
})
}
return h.view('service_status.njk', {
pageTitle: 'Service Status',
...pageData
})
}

module.exports = RootController
module.exports = {
index,
serviceStatus
}
12 changes: 6 additions & 6 deletions app/controllers/test/supplementary.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

const SupplementaryService = require('../../services/supplementary-billing/supplementary.service.js')

class SupplementaryController {
static async index (request, h) {
const result = await SupplementaryService.go(request.query.region)
async function index (request, h) {
const result = await SupplementaryService.go(request.query.region)

return h.response(result).code(200)
}
return h.response(result).code(200)
}

module.exports = SupplementaryController
module.exports = {
index
}
54 changes: 23 additions & 31 deletions app/presenters/supplementary.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,31 @@
* @module SupplementaryPresenter
*/

class SupplementaryPresenter {
constructor (data) {
this._data = data
}

go () {
return this._presentation(this._data)
}

_presentation (data) {
const licences = data.licences.map((licence) => {
return {
licenceId: licence.licenceId,
licenceRef: licence.licenceRef
}
})
const chargeVersions = data.chargeVersions.map((chargeVersion) => {
return {
chargeVersionId: chargeVersion.chargeVersionId,
licenceRef: chargeVersion.licenceRef,
licenceId: chargeVersion.licenceId,
scheme: chargeVersion.scheme,
startDate: chargeVersion.startDate,
endDate: chargeVersion.endDate
}
})

function go (data) {
const licences = data.licences.map((licence) => {
return {
billingPeriods: data.billingPeriods,
licences,
chargeVersions
licenceId: licence.licenceId,
licenceRef: licence.licenceRef
}
})
const chargeVersions = data.chargeVersions.map((chargeVersion) => {
return {
chargeVersionId: chargeVersion.chargeVersionId,
licenceRef: chargeVersion.licenceRef,
licenceId: chargeVersion.licenceId,
scheme: chargeVersion.scheme,
startDate: chargeVersion.startDate,
endDate: chargeVersion.endDate
}
})

return {
billingPeriods: data.billingPeriods,
licences,
chargeVersions
}
}

module.exports = SupplementaryPresenter
module.exports = {
go
}
8 changes: 0 additions & 8 deletions app/routes/root.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ const routes = [
auth: false
}
},
{
method: 'GET',
path: '/hello-world',
handler: RootController.helloWorld,
options: {
auth: false
}
},
{
method: 'GET',
path: '/service-status',
Expand Down
16 changes: 9 additions & 7 deletions app/services/database-health-check.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
const { db } = require('../../db/db.js')

/**
* Generates an array of stats for each table in the database when `go()` is called
* Generates an array of stats for each table in the database
*
* This is a dump of running `SELECT * FROM pg_stat_user_tables` for the database. It's part of the database
* healthcheck and we use it for 2 reasons
*
* - confirm we can connect
* - get some basic stats, for example number of records, for each table without needing to connect to the db
*
* @returns an array of stats for each table found in the db
*/
class DatabaseHealthCheckService {
static async go () {
const stats = db.select().table('pg_stat_user_tables')
async function go () {
const stats = db.select().table('pg_stat_user_tables')

return stats
}
return stats
}

module.exports = DatabaseHealthCheckService
module.exports = {
go
}
Loading