Skip to content

Commit 20b8873

Browse files
Fix Oxfmt and spellcheck failures, skip duplicate reference rates, and test the latest-edited-rate rule
Co-authored-by: thelullabyy <thelullabyy@users.noreply.github.com>
1 parent e01722a commit 20b8873

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎src/libs/PolicyDistanceRatesUtils.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ function buildOnyxDataForGovernmentRateAutoUpdate(
510510
continue;
511511
}
512512

513+
copiedSourceRateIDs.add(governmentMileageRate.sourceRateID);
514+
513515
const enabled = getPreviousSeriesRateEnabled(governmentMileageRate.sourceRateID, governmentMileageRate.startDate) ?? true;
514516
addSeriesRate(governmentMileageRate.sourceRateID, governmentMileageRate.startDate, enabled);
515517

‎src/libs/actions/Policy/DistanceRate.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,13 @@ function clearPolicyRequireMapOrGPSErrors(policyID: string) {
11571157
* behavior are documented on buildOnyxDataForGovernmentRateAutoUpdate, which provides the Onyx data used here.
11581158
*/
11591159
function setWorkspaceDistanceAutoUpdate(policy: Policy, shouldAutoUpdateGovernmentDistanceRates: boolean, governmentMileageRates: GovernmentMileageRate[], countryCode?: string) {
1160-
const {optimisticRateIDs, onyxData} = buildOnyxDataForGovernmentRateAutoUpdate(policy, shouldAutoUpdateGovernmentDistanceRates, governmentMileageRates, policy.outputCurrency, countryCode);
1160+
const {optimisticRateIDs, onyxData} = buildOnyxDataForGovernmentRateAutoUpdate(
1161+
policy,
1162+
shouldAutoUpdateGovernmentDistanceRates,
1163+
governmentMileageRates,
1164+
policy.outputCurrency,
1165+
countryCode,
1166+
);
11611167

11621168
const parameters: SetWorkspaceDistanceAutoUpdateParams = {
11631169
policyID: policy.id,

‎tests/unit/DistanceRateTest.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ describe('DistanceRate', () => {
603603
it('should keep a disabled fuel band disabled for every later rate of its series in the same request', async () => {
604604
// Given an eligible GBP workspace whose earlier petrol band an admin disabled, and whose earlier diesel band an
605605
// admin edited to their own amount and disabled
606-
const editedDieselRateID = 'EDITEDDIESEL1';
606+
const editedDieselRateID = '5A1D3C8E9F0B2';
607607
const policy = buildUKPolicy(true, true, {
608608
[existingRateID]: {
609609
customUnitRateID: existingRateID,
@@ -644,6 +644,59 @@ describe('DistanceRate', () => {
644644
// so its disabled state doesn't opt the workspace out of the government rate
645645
expect(getRateBySourceID(rates, dieselBand.sourceRateID)?.enabled).toBe(true);
646646
});
647+
648+
it('should enable a copied fuel band when its latest previous rate was edited, even if an older one was disabled', async () => {
649+
// Given an eligible GBP workspace whose March diesel band is unmodified and disabled, and whose June diesel band an
650+
// admin edited to their own amount
651+
const marchDieselRateID = '3B7E0A9C4D1F6';
652+
const juneDieselRateID = '8C2F5E1A7B3D0';
653+
const policy = buildUKPolicy(true, true, {
654+
[marchDieselRateID]: {
655+
customUnitRateID: marchDieselRateID,
656+
name: 'Cars & Vans (Diesel) - Up to 1600cc',
657+
rate: 45,
658+
currency: 'GBP',
659+
enabled: false,
660+
startDate: '2026-03-01',
661+
endDate: '2026-05-31',
662+
attributes: {
663+
taxClaimablePercentage: 11 / 45,
664+
governmentRate: {sourceRateID: 'GB_DIESEL_UP_TO_1600_2026-03-01', rate: 45, startDate: '2026-03-01', endDate: '2026-05-31', taxClaimablePercentage: 11 / 45},
665+
},
666+
},
667+
[juneDieselRateID]: {
668+
customUnitRateID: juneDieselRateID,
669+
name: 'Cars & Vans (Diesel) - Up to 1600cc',
670+
rate: 50,
671+
currency: 'GBP',
672+
enabled: true,
673+
startDate: '2026-06-01',
674+
endDate: '2026-08-31',
675+
attributes: {
676+
taxClaimablePercentage: 11 / 45,
677+
governmentRate: {sourceRateID: 'GB_DIESEL_UP_TO_1600_2026-06-01', rate: 45, startDate: '2026-06-01', endDate: '2026-08-31', taxClaimablePercentage: 11 / 45},
678+
},
679+
},
680+
});
681+
682+
// When auto-update copies the September diesel band
683+
const rates = await enableAutoUpdate(policy, [dieselBand]);
684+
685+
// Then the September band is enabled, because only the latest previous rate counts and the admin edited it, so the
686+
// disabled March rate further back doesn't opt the workspace out
687+
expect(getRateBySourceID(rates, dieselBand.sourceRateID)?.enabled).toBe(true);
688+
});
689+
690+
it('should copy a reference rate listed twice only once', async () => {
691+
// Given an eligible GBP workspace and a reference list that contains the same fuel band twice
692+
const policy = buildUKPolicy(true, true);
693+
694+
// When auto-update is turned on
695+
const rates = await enableAutoUpdate(policy, [dieselBand, dieselBand]);
696+
697+
// Then the band is copied once, since the server de-dupes by sourceRateID and would leave a second copy orphaned
698+
expect(rates.filter((rate) => rate.attributes?.governmentRate?.sourceRateID === dieselBand.sourceRateID)).toHaveLength(1);
699+
});
647700
});
648701
});
649702

0 commit comments

Comments
 (0)