-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15665e2
commit a390d06
Showing
3 changed files
with
75 additions
and
14 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
app/services/supplementary-billing/generate-billing-invoice-licence.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
/** | ||
* Generates a billing invoice licence record ready for persisting | ||
* @module CreateBillingInvoiceLicenceService | ||
*/ | ||
|
||
const { randomUUID } = require('crypto') | ||
|
||
/** | ||
* Create a billing invoice licence record for the provided billing invoice and licence | ||
* | ||
* @param {module:BillingInvoiceModel} billingInvoice An instance of `BillingInvoiceModel` | ||
* @param {module:licenceModel} licence An instance of `LicenceModel` | ||
* | ||
* @returns {Object} The newly-created billing invoice licence record | ||
*/ | ||
async function go (generatedBillingInvoiceLicences, billingInvoiceId, licence) { | ||
let billingInvoiceLicence = _existing(generatedBillingInvoiceLicences, billingInvoiceId) | ||
|
||
if (billingInvoiceLicence) { | ||
return { | ||
billingInvoiceLicence, | ||
billingInvoiceLicences: generatedBillingInvoiceLicences | ||
} | ||
} | ||
|
||
billingInvoiceLicence = { | ||
billingInvoiceId, | ||
billingInvoiceLicenceId: randomUUID({ disableEntropyCache: true }), | ||
licenceRef: licence.licenceRef, | ||
licenceId: licence.licenceId | ||
} | ||
const updatedBillingInvoiceLicences = [...generatedBillingInvoiceLicences, billingInvoiceLicence] | ||
|
||
return { | ||
billingInvoiceLicence, | ||
billingInvoiceLicences: updatedBillingInvoiceLicences | ||
} | ||
} | ||
|
||
function _existing (generatedBillingInvoiceLicences, billingInvoiceId) { | ||
return generatedBillingInvoiceLicences.filter((invoiceLicence) => { | ||
return billingInvoiceId === invoiceLicence.billingInvoiceId | ||
})[0] | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters