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

Update new CRM_V2 models with relationships #385

Merged
merged 8 commits into from
Aug 29, 2023
15 changes: 15 additions & 0 deletions app/models/crm-v2/address.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module AddressModel
*/

const { Model } = require('objection')

const CrmV2BaseModel = require('./crm-v2-base.model.js')

class AddressModel extends CrmV2BaseModel {
Expand All @@ -22,6 +24,19 @@ class AddressModel extends CrmV2BaseModel {
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
invoiceAccountAddresses: {
relation: Model.HasManyRelation,
modelClass: 'invoice-account-address.model',
join: {
from: 'addresses.addressId',
to: 'invoiceAccountAddresses.addressId'
}
}
}
}
}

module.exports = AddressModel
23 changes: 23 additions & 0 deletions app/models/crm-v2/company.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module CompanyModel
*/

const { Model } = require('objection')

const CrmV2BaseModel = require('./crm-v2-base.model.js')

class CompanyModel extends CrmV2BaseModel {
Expand All @@ -22,6 +24,27 @@ class CompanyModel extends CrmV2BaseModel {
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
invoiceAccounts: {
relation: Model.HasManyRelation,
modelClass: 'invoice-account.model',
join: {
from: 'companies.companyId',
to: 'invoiceAccounts.companyId'
}
},
invoiceAccountAddresses: {
relation: Model.HasManyRelation,
modelClass: 'invoice-account-address.model',
join: {
from: 'companies.companyId',
to: 'invoiceAccountAddresses.agentCompanyId'
}
}
}
}
}

module.exports = CompanyModel
15 changes: 15 additions & 0 deletions app/models/crm-v2/contact.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module ContactModel
*/

const { Model } = require('objection')

const CrmV2BaseModel = require('./crm-v2-base.model.js')

class ContactModel extends CrmV2BaseModel {
Expand All @@ -23,6 +25,19 @@ class ContactModel extends CrmV2BaseModel {
]
}

static get relationMappings () {
return {
invoiceAccountAddresses: {
relation: Model.HasManyRelation,
modelClass: 'invoice-account-address.model',
join: {
from: 'contacts.contactId',
to: 'invoiceAccountAddresses.contactId'
}
}
}
}

/**
* Returns the name for the contact derived from its various parts
*
Expand Down
39 changes: 39 additions & 0 deletions app/models/crm-v2/invoice-account-address.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module InvoiceAccountAddressModel
*/

const { Model } = require('objection')

const CrmV2BaseModel = require('./crm-v2-base.model.js')

class InvoiceAccountAddressModel extends CrmV2BaseModel {
Expand All @@ -22,6 +24,43 @@ class InvoiceAccountAddressModel extends CrmV2BaseModel {
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
address: {
relation: Model.BelongsToOneRelation,
modelClass: 'address.model',
join: {
from: 'invoiceAccountAddresses.addressId',
to: 'addresses.addressId'
}
},
company: {
relation: Model.BelongsToOneRelation,
modelClass: 'company.model',
join: {
from: 'invoiceAccountAddresses.agentCompanyId',
to: 'companies.companyId'
}
},
contact: {
relation: Model.BelongsToOneRelation,
modelClass: 'contact.model',
join: {
from: 'invoiceAccountAddresses.contactId',
to: 'contacts.contactId'
}
},
invoiceAccount: {
relation: Model.BelongsToOneRelation,
modelClass: 'invoice-account.model',
join: {
from: 'invoiceAccountAddresses.invoiceAccountId',
to: 'invoiceAccounts.invoiceAccountId'
}
}
}
}
}

module.exports = InvoiceAccountAddressModel
23 changes: 23 additions & 0 deletions app/models/crm-v2/invoice-account.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module InvoiceAccountModel
*/

const { Model } = require('objection')

const CrmV2BaseModel = require('./crm-v2-base.model.js')

class InvoiceAccountModel extends CrmV2BaseModel {
Expand All @@ -22,6 +24,27 @@ class InvoiceAccountModel extends CrmV2BaseModel {
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
company: {
relation: Model.BelongsToOneRelation,
modelClass: 'company.model',
join: {
from: 'invoiceAccounts.companyId',
to: 'companies.companyId'
}
},
invoiceAccountAddresses: {
relation: Model.HasManyRelation,
modelClass: 'invoice-account-address.model',
join: {
from: 'invoiceAccounts.invoiceAccountId',
to: 'invoiceAccountAddresses.invoiceAccountId'
}
}
}
}
}

module.exports = InvoiceAccountModel
40 changes: 40 additions & 0 deletions test/models/crm-v2/address.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { expect } = Code
// Test helpers
const AddressHelper = require('../../support/helpers/crm-v2/address.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const InvoiceAccountAddressHelper = require('../../support/helpers/crm-v2/invoice-account-address.helper.js')
const InvoiceAccountAddressModel = require('../../../app/models/crm-v2/invoice-account-address.model.js')

// Thing under test
const AddressModel = require('../../../app/models/crm-v2/address.model.js')
Expand All @@ -31,4 +33,42 @@ describe('Address model', () => {
expect(result.addressId).to.equal(testRecord.addressId)
})
})

describe('Relationships', () => {
describe('when linking to invoice account addresses', () => {
let testInvoiceAccountAddresses

beforeEach(async () => {
testRecord = await AddressHelper.add()
const { addressId } = testRecord

testInvoiceAccountAddresses = []
for (let i = 0; i < 2; i++) {
const invoiceAccountAddress = await InvoiceAccountAddressHelper.add({ addressId })
testInvoiceAccountAddresses.push(invoiceAccountAddress)
}
})

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

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

it('can eager load the invoice account addresses', async () => {
const result = await AddressModel.query()
.findById(testRecord.addressId)
.withGraphFetched('invoiceAccountAddresses')

expect(result).to.be.instanceOf(AddressModel)
expect(result.addressId).to.equal(testRecord.addressId)

expect(result.invoiceAccountAddresses).to.be.an.array()
expect(result.invoiceAccountAddresses[0]).to.be.an.instanceOf(InvoiceAccountAddressModel)
expect(result.invoiceAccountAddresses).to.include(testInvoiceAccountAddresses[0])
expect(result.invoiceAccountAddresses).to.include(testInvoiceAccountAddresses[1])
})
})
})
})
40 changes: 40 additions & 0 deletions test/models/crm-v2/company.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { expect } = Code
// Test helpers
const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const InvoiceAccountAddressHelper = require('../../support/helpers/crm-v2/invoice-account-address.helper.js')
const InvoiceAccountAddressModel = require('../../../app/models/crm-v2/invoice-account-address.model.js')

// Thing under test
const CompanyModel = require('../../../app/models/crm-v2/company.model.js')
Expand All @@ -31,4 +33,42 @@ describe('Company model', () => {
expect(result.companyId).to.equal(testRecord.companyId)
})
})

describe('Relationships', () => {
describe('when linking to invoice account addresses', () => {
let testInvoiceAccountAddresses

beforeEach(async () => {
testRecord = await CompanyHelper.add()
const { companyId: agentCompanyId } = testRecord

testInvoiceAccountAddresses = []
for (let i = 0; i < 2; i++) {
const invoiceAccountAddress = await InvoiceAccountAddressHelper.add({ agentCompanyId })
testInvoiceAccountAddresses.push(invoiceAccountAddress)
}
})

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

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

it('can eager load the invoice account addresses', async () => {
const result = await CompanyModel.query()
.findById(testRecord.companyId)
.withGraphFetched('invoiceAccountAddresses')

expect(result).to.be.instanceOf(CompanyModel)
expect(result.companyId).to.equal(testRecord.companyId)

expect(result.invoiceAccountAddresses).to.be.an.array()
expect(result.invoiceAccountAddresses[0]).to.be.an.instanceOf(InvoiceAccountAddressModel)
expect(result.invoiceAccountAddresses).to.include(testInvoiceAccountAddresses[0])
expect(result.invoiceAccountAddresses).to.include(testInvoiceAccountAddresses[1])
})
})
})
})
40 changes: 40 additions & 0 deletions test/models/crm-v2/contact.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { expect } = Code
// Test helpers
const ContactHelper = require('../../support/helpers/crm-v2/contact.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const InvoiceAccountAddressHelper = require('../../support/helpers/crm-v2/invoice-account-address.helper.js')
const InvoiceAccountAddressModel = require('../../../app/models/crm-v2/invoice-account-address.model.js')

// Thing under test
const ContactModel = require('../../../app/models/crm-v2/contact.model.js')
Expand All @@ -32,6 +34,44 @@ describe('Contact model', () => {
})
})

describe('Relationships', () => {
describe('when linking to invoice account addresses', () => {
let testInvoiceAccountAddresses

beforeEach(async () => {
testRecord = await ContactHelper.add()
const { contactId } = testRecord

testInvoiceAccountAddresses = []
for (let i = 0; i < 2; i++) {
const invoiceAccountAddress = await InvoiceAccountAddressHelper.add({ contactId })
testInvoiceAccountAddresses.push(invoiceAccountAddress)
}
})

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

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

it('can eager load the invoice account addresses', async () => {
const result = await ContactModel.query()
.findById(testRecord.contactId)
.withGraphFetched('invoiceAccountAddresses')

expect(result).to.be.instanceOf(ContactModel)
expect(result.contactId).to.equal(testRecord.contactId)

expect(result.invoiceAccountAddresses).to.be.an.array()
expect(result.invoiceAccountAddresses[0]).to.be.an.instanceOf(InvoiceAccountAddressModel)
expect(result.invoiceAccountAddresses).to.include(testInvoiceAccountAddresses[0])
expect(result.invoiceAccountAddresses).to.include(testInvoiceAccountAddresses[1])
})
})
})

// NOTE: The test data we generate in these tests is in accordance with how a contact record could be populated. For
// example, if the data source is 'wrls' and the contact type is 'person', then first name and last name is always
// populated. We don't test what would happen if last name wasn't because analysis of the data confirms this would
Expand Down
Loading