Skip to content

Commit

Permalink
feat: add company contacts model
Browse files Browse the repository at this point in the history
add company model tests
  • Loading branch information
jonathangoulding committed May 14, 2024
1 parent b94f254 commit 2a8aa0c
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/models/company-contact.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

/**
* Model for company contact (crm_v2.company_contacts)
* @module CompanyContactModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class CompanyContactModel extends BaseModel {
static get tableName () {
return 'company_contacts'
}

static get relationMappings () {
return {
companies: {
relation: Model.HasManyRelation,
modelClass: 'company.model',
join: {
from: 'company_contacts.companyId',
to: 'companies.id'
}
},
contacts: {
relation: Model.HasManyRelation,
modelClass: 'contact.model',
join: {
from: 'company_contacts.contactId',
to: 'contacts.id'
}
}
}
}
}

module.exports = CompanyContactModel
103 changes: 103 additions & 0 deletions test/models/company-contacts.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'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 CompanyContactHelper = require('../support/helpers/company-contact.helper.js')
const CompanyHelper = require('../support/helpers/company.helper.js')
const CompanyModel = require('../../app/models/company.model.js')
const ContactsHelper = require('../support/helpers/contact.helper.js')
const ContactsModel = require('../../app/models/contact.model.js')
const DatabaseSupport = require('../support/database.js')

// Thing under test
const CompanyContactModel = require('../../app/models/company-contact.model.js')

describe('Company Contacts model', () => {
let testRecord

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

describe('Basic query', () => {
beforeEach(async () => {
testRecord = await CompanyContactHelper.add()
})

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

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

describe('Relationships', () => {
describe('when linking to companies', () => {
let testCompany
beforeEach(async () => {
testRecord = await CompanyContactHelper.add()

testCompany = await CompanyHelper.add({
id: testRecord.companyId
})
})

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

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

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

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

expect(result.companies).to.be.an.array()
expect(result.companies[0]).to.be.an.instanceOf(CompanyModel)
expect(result.companies).to.include(testCompany)
})
})
describe('when linking to contacts', () => {
let testContact
beforeEach(async () => {
testRecord = await CompanyContactHelper.add()

testContact = await ContactsHelper.add({
id: testRecord.contactId
})
})

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

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

it('can eager load the company contacts', async () => {
const result = await CompanyContactModel.query()
.findById(testRecord.id)
.withGraphFetched('contacts')

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

expect(result.contacts).to.be.an.array()
expect(result.contacts[0]).to.be.an.instanceOf(ContactsModel)
expect(result.contacts).to.include(testContact)
})
})
})
})
56 changes: 56 additions & 0 deletions test/support/helpers/company-contact.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

/**
* @module CompanyContactHelper
*/

const CompanyContactModel = require('../../../app/models/company-contact.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]
* - `companyId` - [random UUID]
* - `contactId` - [random UUID]
* - `roleId` - [random UUID]
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {Promise<module:CompanyContactModel>} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

return CompanyContactModel.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 = {
id: generateUUID(),
companyId: generateUUID(),
contactId: generateUUID(),
roleId: generateUUID()
}

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

module.exports = {
add,
defaults
}

0 comments on commit 2a8aa0c

Please sign in to comment.