Show history selection screen instead of guessing

Removes all the guesswork for requisition renewals, instead opting for
the manual approach of simply redirecting to the form and updating its
handling such that it jumps straight to history selection when needed.

When the institution is preloaded (so we skip country/bank selection)
the cloned element is a country and not the bank so we need to replace
athe flag with its logo, but otherwise the code was all moved as-is.
This commit is contained in:
Dave Shoreman 2024-12-24 02:24:10 +00:00
parent 691478d3e7
commit bea4ea3387
No known key found for this signature in database
GPG Key ID: C920D1D63709F443
2 changed files with 59 additions and 55 deletions

View File

@ -61,12 +61,13 @@ class NordigenController extends BaseController
$institutions = $nordigen->getInstitutions();
// show bank_selection_screen, when institution_id is not present
if (!array_key_exists('institution_id', $data)) {
if (!isset($data['institution_id'], $data['tx_days'])) {
return view('bank.nordigen.handler', [
'lang' => $lang,
'company' => $company,
'account' => $company->account,
'institutions' => $institutions,
'institutionId' => $data['institution_id'] ?? null,
'redirectUrl' => $context['redirect'] . '?action=nordigen_connect&status=user-aborted'
]);
}
@ -75,23 +76,6 @@ class NordigenController extends BaseController
return $institution['id'] == $data['institution_id'];
}))[0];
// Renewals have an Institution ID, but bypass the history selection screen
// and thus lack the history setting, so we can find the Agreement ID here.
if (!isset($data['tx_days'])) {
// Query the integration so we get the correct Account ID
$integration = $this->findIntegrationBy('institution', $institution, $company);
// Extract the EUA ID from the expired account error
$match = '/End User Agreement \(EUA\) ([0-9a-f-]+) has expired/';
$nordigenAccount = $nordigen->getAccount($integration->nordigen_account_id);
$euaId = preg_replace($match, '${1}', $nordigenAccount['error']);
// Fetch the old agreement and maintain its access/history settings
$agreement = $nordigen->getAgreement($euaId);
$data['access_days'] = $agreement['max_access_valid_for_days'];
$data['tx_days'] = $agreement['max_historical_days'];
}
try {
$txDays = $data['tx_days'] ?? 0;
@ -272,7 +256,7 @@ class NordigenController extends BaseController
Company $company,
): BankIntegration {
return BankIntegration::withTrashed()
->where("nordigen_{$key}_id", $accountOrInstitution['id'])
->where($key == 'id' ? 'id' : "nordigen_{$key}_id", $accountOrInstitution['id'])
->where('company_id', $company->id)
->where('is_deleted', 0)
->firstOrFail();

View File

@ -48,14 +48,66 @@
buttonColor: '#3A53EE',
buttonTextColor: '#fff'
}
}, createSelectionUI = (donor, institution, skippedSelect = false) => {
const clone = donor.cloneNode(true),
container = document.querySelector('.institution-container'),
max_history = parseInt(institution.transaction_total_days),
url = new URL(window.location.href);
container.innerHTML = '';
_changeHeading('Select your transaction history');
clone.classList.replace('ob-list-institution', 'ob-history-option');
clone.querySelector('.ob-span-text').innerText = `${max_history} days`;
url.searchParams.set('institution_id', institutionId);
// When we come from the renew button we need to replace the country flag
if (skippedSelect) {
const logo = document.createElement('img');
logo.setAttribute('src', institution.logo)
logo.setAttribute('class', 'ob-institution-logo');
clone.querySelector('span.fi').replaceWith(logo)
}
for (let i = 30, next = 30; i <= max_history; i += next) {
// If we're close to max, just use the real value
if (max_history - i < 15) {
continue;
}
const option = clone.cloneNode(true);
url.searchParams.set('tx_days', i == 360 ? 365 : i);
option.querySelector('.ob-span-text').innerText = `${i == 360 ? 365 : i} days`;
option.querySelector('a').href = url.href;
container.append(option);
// 1, 2, 3, 4, 6, 9, 12, 14, 18, 24 months--as of 24/12/24, no bank exceeds 730 days of history
next = i >= 500 ? 180 : i >= 400 ? 120 : i >= 360 ? 60 : i >= 180 ? 90 : i >= 120 ? 60 : 30;
}
url.searchParams.set('tx_days', max_history);
clone.querySelector('a').href = url.href;
container.append(clone);
};
const failedReason = "{{ $failed_reason ?? '' }}".trim(),
institutions = @json($institutions ?? []);
let institutionId = "{{ $institutionId ?? '' }}";
new institutionSelector(institutions, 'institution-modal-content', config);
if (!failedReason) {
if (null !== institutionId && !failedReason) {
createSelectionUI(
document.querySelector('.ob-institution'),
institutions.find(i => i.id == institutionId),
true
);
} else if (!failedReason) {
const observer = new MutationObserver((event) => {
const institutionButtons = document.querySelectorAll('.ob-list-institution > a');
@ -63,41 +115,9 @@
button.addEventListener('click', (e) => {
e.preventDefault();
const clone = button.parentElement.cloneNode(true),
container = document.querySelector('.institution-container'),
institutionId = button.getAttribute('data-institution'),
institution = institutions.find(i => i.id == institutionId),
max_history = parseInt(institution.transaction_total_days),
url = new URL(window.location.href);
container.innerHTML = '';
_changeHeading('Select your transaction history');
clone.classList.replace('ob-list-institution', 'ob-history-option');
clone.querySelector('span').innerText = `${max_history} days`;
url.searchParams.set('institution_id', institutionId);
for (let i = 30, next = 30; i <= max_history; i += next) {
// If we're close to max, just use the real value
if (max_history - i < 15) {
continue;
}
const option = clone.cloneNode(true);
url.searchParams.set('tx_days', i == 360 ? 365 : i);
option.querySelector('span').innerText = `${i == 360 ? 365 : i} days`;
option.querySelector('a').href = url.href;
container.append(option);
// 1, 2, 3, 4, 6, 9, 12, 14, 18, 24 months--as of 20/12/24, no bank exceeds 730 days of history
next = i >= 500 ? 180 : i >= 400 ? 120 : i >= 360 ? 60 : i >= 180 ? 90 : i >= 120 ? 60 : 30;
}
url.searchParams.set('tx_days', max_history);
clone.querySelector('a').href = url.href;
container.append(clone);
createSelectionUI(button.parentElement, institutions.find(
i => i.id == button.getAttribute('data-institution')
));
});
});
});