-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Abhi Markan <amarkan@ukexportfinance.gov.uk>
- Loading branch information
1 parent
1df56b9
commit ef51d86
Showing
12 changed files
with
778 additions
and
140 deletions.
There are no files selected for viewing
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
247 changes: 151 additions & 96 deletions
247
portal-api/api-tests/v1/deals/deals-status-facilities.api-test.js
Large diffs are not rendered by default.
Oops, something went wrong.
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
58 changes: 36 additions & 22 deletions
58
portal-api/src/v1/controllers/deal-status/update-facility-cover-start-dates.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 |
---|---|---|
@@ -1,42 +1,56 @@ | ||
const CONSTANTS = require('../../../constants'); | ||
const { getNowAsEpoch } = require('../../helpers/date'); | ||
const facilitiesController = require('../facilities.controller'); | ||
|
||
/** | ||
* Updates the cover start dates of facilities in a deal. | ||
* @param {object} user - The user object representing the user performing the update. | ||
* @param {object} deal - The deal object containing the facilities to be updated. | ||
* @returns {object} - The modified deal object with updated facility cover start dates. | ||
*/ | ||
const updateFacilityCoverStartDates = async (user, deal) => { | ||
const modifiedDeal = deal; | ||
try { | ||
const modifiedDeal = { ...deal }; | ||
|
||
if (modifiedDeal.facilities.length) { | ||
modifiedDeal.facilities.forEach(async (facilityId) => { | ||
const facility = await facilitiesController.findOne(facilityId); | ||
if (!modifiedDeal.facilities || !modifiedDeal.facilities?.length) { | ||
console.error('No facilities found in deal %s', modifiedDeal._id); | ||
throw new Error(`No facilities found in deal ${modifiedDeal._id}`); | ||
} | ||
|
||
for (const facilityId of modifiedDeal.facilities) { | ||
const facility = await facilitiesController.findOne(facilityId); | ||
const { facilityStage } = facility; | ||
const canUpdate = | ||
(facilityStage === CONSTANTS.FACILITIES.FACILITIES_STAGE.BOND.ISSUED || facilityStage === CONSTANTS.FACILITIES.FACILITIES_STAGE.LOAN.UNCONDITIONAL) && | ||
!facility.requestedCoverStartDate && | ||
!facility.coverDateConfirmed; | ||
|
||
const shouldUpdate = ((facilityStage === CONSTANTS.FACILITIES.FACILITIES_STAGE.BOND.ISSUED | ||
|| facilityStage === CONSTANTS.FACILITIES.FACILITIES_STAGE.LOAN.UNCONDITIONAL) | ||
&& !facility.requestedCoverStartDate); | ||
if (canUpdate) { | ||
console.info('⚡ Updating facility %s cover start date', facilityId); | ||
|
||
if (shouldUpdate) { | ||
const now = Date.now(); | ||
const today = new Date(); | ||
const nowAsEpoch = getNowAsEpoch(); | ||
|
||
facility.updatedAt = now; | ||
facility.requestedCoverStartDate = now; | ||
facility.updatedAt = nowAsEpoch; | ||
facility.coverDateConfirmed = true; | ||
facility.requestedCoverStartDate = nowAsEpoch; | ||
facility['requestedCoverStartDate-day'] = today.getDate(); | ||
facility['requestedCoverStartDate-month'] = today.getMonth() + 1; | ||
facility['requestedCoverStartDate-year'] = today.getFullYear(); | ||
|
||
const { data } = await facilitiesController.update( | ||
deal._id, | ||
facilityId, | ||
facility, | ||
user, | ||
); | ||
return data; | ||
const { data } = await facilitiesController.update(deal._id, facilityId, facility, user); | ||
|
||
if (!data) { | ||
console.error('Error updating facility cover start date for facility %s with response %o', facilityId, data); | ||
} | ||
} | ||
return facility; | ||
}); | ||
} | ||
} | ||
|
||
return deal; | ||
return modifiedDeal; | ||
} catch (error) { | ||
console.error("An error occurred while updating %s deal's facilities cover start date %o", deal._id, error); | ||
return deal; | ||
} | ||
}; | ||
|
||
module.exports = updateFacilityCoverStartDates; |
Oops, something went wrong.