Skip to content

Commit

Permalink
Refactor existing code to use new models
Browse files Browse the repository at this point in the history
we also deal with a number of uses of the old naming for variables within the code.
  • Loading branch information
Cruikshanks committed Dec 5, 2023
1 parent 5207e29 commit 415f3aa
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 245 deletions.
12 changes: 6 additions & 6 deletions app/presenters/bills/view-bill.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function go (bill, billingAccount) {

const formattedBill = {
accountName: _accountName(billingAccount),
accountNumber: billingAccount.invoiceAccountNumber,
accountNumber: billingAccount.accountNumber,
addressLines: _addressLines(billingAccount),
billId: bill.billingInvoiceId,
billingAccountId: billingAccount.invoiceAccountId,
billingAccountId: billingAccount.id,
billNumber: bill.invoiceNumber,
billRunId: billRun.billingBatchId,
billRunNumber: billRun.billRunNumber,
Expand All @@ -46,8 +46,8 @@ function go (bill, billingAccount) {
function _accountName (billingAccount) {
const accountAddress = billingAccount.billingAccountAddresses[0]

if (accountAddress.agentCompany) {
return accountAddress.agentCompany.name
if (accountAddress.company) {
return accountAddress.company.name
}

return billingAccount.company.name
Expand All @@ -61,8 +61,8 @@ function _addressLines (billingAccount) {
address.address2,
address.address3,
address.address4,
address.town,
address.county,
address.address5,
address.address6,
address.postcode,
address.country
]
Expand Down
20 changes: 10 additions & 10 deletions app/presenters/charging-module/create-customer-change.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* @returns {Object} the request data needed in the format required by the Charging Module
*/
function go (billingAccount, address, company, contact) {
const { invoiceAccountNumber: customerReference } = billingAccount
const { accountNumber: customerReference } = billingAccount

const region = customerReference.charAt(0)
const customerName = _customerName(billingAccount, company)
Expand All @@ -69,17 +69,17 @@ function go (billingAccount, address, company, contact) {
}
}

function _addressLine6 (county, country) {
if (!county && !country) {
function _addressLine6 (address6, country) {
if (!address6 && !country) {
return ''
}

if (county && country) {
return `${county}, ${country}`
if (address6 && country) {
return `${address6}, ${country}`
}

if (county) {
return county
if (address6) {
return address6
}

return country
Expand All @@ -96,7 +96,7 @@ function _customerName (billingAccount, company) {
function _formattedAddress (address, contact) {
const addressLines = []

const { address1, address2, address3, address4, town, county, country, postcode } = address
const { address1, address2, address3, address4, address5, address6, country, postcode } = address
const contactName = contact.$name()

if (address1) {
Expand Down Expand Up @@ -133,8 +133,8 @@ function _formattedAddress (address, contact) {
addressLine3: addressLines[2] ? addressLines[2] : null,
addressLine4: addressLines[3] ? addressLines[3] : null,
// We have encountered instances of ton being null so we confirm we have something to truncate to avoid an error
addressLine5: town ? _truncate(town, 60) : null,
addressLine6: _truncate(_addressLine6(county, country), 60),
addressLine5: address5 ? _truncate(address5, 60) : null,
addressLine6: _truncate(_addressLine6(address6, country), 60),
postcode: _truncate(postcode, 60)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module FetchBillingAccountsService
*/

const BillingAccountModel = require('../../../models/crm-v2/billing-account.model.js')
const BillingAccountModel = require('../../../models/billing-account.model.js')

/**
* Fetch all billing accounts for the supplied charge versions
Expand All @@ -15,8 +15,8 @@ const BillingAccountModel = require('../../../models/crm-v2/billing-account.mode
* @returns {Object[]} Array of objects in the format { invoiceAccountId: '...', invoiceAccountNumber: '...' }
*/
async function go (chargeVersions) {
const uniqueInvoiceAccountIds = _extractUniqueInvoiceAccountIds(chargeVersions)
const billingAccountModels = await _fetch(uniqueInvoiceAccountIds)
const uniqueBillingAccountIds = _extractUniqueBillingAccountIds(chargeVersions)
const billingAccountModels = await _fetch(uniqueBillingAccountIds)

// The results come back from Objection as BillingAccountModels. Since we want to be clear that these are not
// full-blown models, we turn them into plain objects using Objection's .toJSON() method
Expand All @@ -25,20 +25,20 @@ async function go (chargeVersions) {
return billingAccountObjects
}

function _extractUniqueInvoiceAccountIds (chargeVersions) {
const allInvoiceAccountIds = chargeVersions.map((chargeVersion) => {
function _extractUniqueBillingAccountIds (chargeVersions) {
const allBillingAccountIds = chargeVersions.map((chargeVersion) => {
return chargeVersion.invoiceAccountId
})

// Creating a new set from allInvoiceAccountIds gives us just the unique ids. Objection does not accept sets in
// Creating a new set from allBillingAccountIds gives us just the unique ids. Objection does not accept sets in
// .findByIds() so we spread it into an array
return [...new Set(allInvoiceAccountIds)]
return [...new Set(allBillingAccountIds)]
}

function _fetch (uniqueInvoiceAccountIds) {
function _fetch (uniqueBillingAccountIds) {
return BillingAccountModel.query()
.select('invoiceAccountId', 'invoiceAccountNumber')
.findByIds([...uniqueInvoiceAccountIds])
.select('id', 'accountNumber')
.findByIds([...uniqueBillingAccountIds])
}

function _makeObjects (models) {
Expand Down
4 changes: 2 additions & 2 deletions app/services/bill-runs/supplementary/generate-bill.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function go (billingAccount, billRunId, financialYearEnding) {
const bill = {
billingBatchId: billRunId,
financialYearEnding,
invoiceAccountId: billingAccount.invoiceAccountId,
invoiceAccountId: billingAccount.id,
billingInvoiceId: generateUUID(),
address: {}, // Address is set to an empty object for SROC billing invoices
invoiceAccountNumber: billingAccount.invoiceAccountNumber,
invoiceAccountNumber: billingAccount.accountNumber,
isCredit: false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ function _billLicenceKey (billId, licenceId) {
/**
* We pre-generate bills for every billing account so that we don't need to fetch any data from the db during the main
* charge version processing loop. This function generates the required bill licences and returns an object where
* each key is the invoice account id, and each value is the bill, ie:
* each key is the billing account id, and each value is the bill, ie:
*
* {
* 'uuid-1': { invoiceAccountId: 'uuid-1', ... },
* 'uuid-2': { invoiceAccountId: 'uuid-2', ... }
* 'uuid-1': { id: 'uuid-1', ... },
* 'uuid-2': { id: 'uuid-2', ... }
* }
*/
function _preGenerateBills (billingAccounts, billRunId, billingPeriod) {
Expand All @@ -83,7 +83,7 @@ function _preGenerateBills (billingAccounts, billRunId, billingPeriod) {
// bill licence already exists in the object before generating one
return {
...acc,
[billingAccount.invoiceAccountId]: GenerateBillService.go(
[billingAccount.id]: GenerateBillService.go(
billingAccount,
billRunId,
billingPeriod.endDate.getFullYear()
Expand Down
Loading

0 comments on commit 415f3aa

Please sign in to comment.