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

Add Scheduled Notifications model #1018

Merged
merged 8 commits into from
May 15, 2024
30 changes: 30 additions & 0 deletions app/models/scheduled-notification.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

/**
* Model for scheduled notifications (water.scheduled_notification)
* @module ScheduledNotificationModel
*/

const BaseModel = require('./base.model.js')
const { Model } = require('objection')

class ScheduledNotificationModel extends BaseModel {
static get tableName () {
return 'scheduled_notification'
}

static get relationMappings () {
return {
event: {
relation: Model.HasOneRelation,
modelClass: 'event.model',
join: {
from: 'scheduled_notification.eventId',
to: 'events.id'
}
}
}
}
}

module.exports = ScheduledNotificationModel
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

const tableName = 'scheduled_notification'

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex
.schema
.withSchema('water')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.bigint('status_checks')
table.date('send_after')
table.integer('notification_type')
table.jsonb('licences')
table.jsonb('metadata')
table.jsonb('personalisation')
table.string('company_entity_id')
table.string('individual_entity_id')
table.string('job_id').unique()
table.string('log')
table.string('medium')
table.string('message_ref')
table.string('message_type')
table.string('notify_id')
table.string('notify_status')
table.string('plaintext')
table.string('recipient')
table.string('status')
table.timestamp('next_status_check')
table.uuid('event_id')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
.raw(`
CREATE INDEX idx_scheduled_notification_statuses ON water.scheduled_notification USING btree (status, notify_status);
CREATE INDEX scheduled_notification_idx_send_after ON water.scheduled_notification USING btree (send_after);
`)
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const viewName = 'scheduled_notification'

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('scheduled_notification').withSchema('water').select([
'scheduled_notification.date_created AS created_at',
'scheduled_notification.event_id',
'scheduled_notification.id',
'scheduled_notification.licences',
'scheduled_notification.message_type',
'scheduled_notification.notify_status',
'scheduled_notification.send_after',
'scheduled_notification.status'

// 'scheduled_notification.company_entity_id',
// 'scheduled_notification.individual_entity_id',
// 'scheduled_notification.job_id',
// 'scheduled_notification.log',
// 'scheduled_notification.medium',
// 'scheduled_notification.message_ref',
// 'scheduled_notification.metadata',
// 'scheduled_notification.next_status_check',
// 'scheduled_notification.notification_type',
// 'scheduled_notification.notify_id',
// 'scheduled_notification.personalisation',
// 'scheduled_notification.plaintext',
// 'scheduled_notification.recipient',
// 'scheduled_notification.status_checks'
]))
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
}
68 changes: 68 additions & 0 deletions test/models/scheduled-notification.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const DatabaseSupport = require('../support/database.js')
const EventHelper = require('../support/helpers/event.helper.js')
const EventModel = require('../../app/models/event.model.js')
const ScheduledNotificationHelper = require('../support/helpers/scheduled-notification.helper.js')

// Thing under test
const ScheduledNotificationModel = require('../../app/models/scheduled-notification.model.js')

describe('Scheduled Notification model', () => {
let testRecord

beforeEach(async () => {
await DatabaseSupport.clean()

testRecord = await ScheduledNotificationHelper.add()
})

describe('Basic query', () => {
it('can successfully run a basic query', async () => {
const result = await ScheduledNotificationModel.query().findById(testRecord.id)

expect(result).to.be.an.instanceOf(ScheduledNotificationModel)
expect(result.id).to.equal(testRecord.id)
})
})

describe('Relationships', () => {
describe('when linking to events', () => {
let testEvent
beforeEach(async () => {
testRecord = await ScheduledNotificationHelper.add()

testEvent = await EventHelper.add({
id: testRecord.eventId
})
})

it('can successfully run a related query', async () => {
const query = await ScheduledNotificationModel.query()
.innerJoinRelated('event')

expect(query).to.exist()
})

it('can eager load the events', async () => {
const result = await ScheduledNotificationModel.query()
.findById(testRecord.id)
.withGraphFetched('event')

expect(result).to.be.instanceOf(ScheduledNotificationModel)
expect(result.id).to.equal(testRecord.id)

expect(result.event).to.be.an.instanceOf(EventModel)
expect(result.event).to.include(testEvent)
})
})
})
})
52 changes: 52 additions & 0 deletions test/support/helpers/scheduled-notification.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

/**
* @module ScheduledNotificationHelper
*/

const ScheduledNotificationModel = require('../../../app/models/scheduled-notification.model.js')
const { generateUUID } = require('../../../app/lib/general.lib.js')

/**
* Add a new company contact
*
* If no `data` is provided, default values will be used. These are
*
* - `id` - [random UUID]
* - `event_id` - [random UUID]
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {Promise<module:ScheduledNotificationModel>} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

return ScheduledNotificationModel.query()
.insert({ ...insertData })
.returning('*')
}

/**
* Returns the defaults used
*
* It will override or append to them any data provided. Mainly used by the `add()` method, we make it available
* for use in tests to avoid having to duplicate values.
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*/
function defaults (data = {}) {
const defaults = {
eventId: generateUUID()
}

return {
...defaults,
...data
}
}

module.exports = {
add,
defaults
}
Loading