Skip to content

Commit 04b44bc

Browse files
authored
feat: recalculate basket after currency change (#1005)
1 parent 120dac3 commit 04b44bc

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/app/core/store/customer/basket/basket.effects.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { anyString, anything, instance, mock, verify, when } from 'ts-mockito';
1111
import { Basket } from 'ish-core/models/basket/basket.model';
1212
import { BasketService } from 'ish-core/services/basket/basket.service';
1313
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
14+
import { loadServerConfigSuccess } from 'ish-core/store/core/server-config';
1415
import { CustomerStoreModule } from 'ish-core/store/customer/customer-store.module';
1516
import { resetOrderErrors } from 'ish-core/store/customer/orders';
1617
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
@@ -62,7 +63,7 @@ describe('Basket Effects', () => {
6263
TestBed.configureTestingModule({
6364
declarations: [DummyComponent],
6465
imports: [
65-
CoreStoreModule.forTesting(['router']),
66+
CoreStoreModule.forTesting(['router', 'serverConfig', 'configuration']),
6667
CustomerStoreModule.forTesting('user', 'basket'),
6768
RouterTestingModule.withRoutes([{ path: '**', component: DummyComponent }]),
6869
],
@@ -194,6 +195,37 @@ describe('Basket Effects', () => {
194195
});
195196
});
196197

198+
describe('recalculateBasketAfterCurrencyChange$', () => {
199+
beforeEach(() => {
200+
store$.dispatch(
201+
loadServerConfigSuccess({
202+
config: {
203+
general: {
204+
defaultLocale: 'de_DE',
205+
defaultCurrency: 'EUR',
206+
locales: ['en_US', 'de_DE', 'fr_BE', 'nl_BE'],
207+
currencies: ['USD', 'EUR'],
208+
},
209+
},
210+
})
211+
);
212+
});
213+
214+
it('should trigger a basket recalculation if the basket currency differs from current currency', done => {
215+
const id = 'BID';
216+
217+
actions$ = of(loadBasketSuccess({ basket: { id, purchaseCurrency: 'USD' } as Basket }));
218+
219+
effects.recalculateBasketAfterCurrencyChange$.subscribe(action => {
220+
expect(action).toMatchInlineSnapshot(`
221+
[Basket Internal] Update Basket:
222+
update: {"calculated":true}
223+
`);
224+
done();
225+
});
226+
});
227+
});
228+
197229
describe('createBasket$', () => {
198230
beforeEach(() => {
199231
when(basketServiceMock.createBasket()).thenCall(() => of({ id: 'BID' } as Basket));

src/app/core/store/customer/basket/basket.effects.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import {
1515
sample,
1616
startWith,
1717
switchMap,
18+
take,
1819
withLatestFrom,
1920
} from 'rxjs/operators';
2021

2122
import { Basket } from 'ish-core/models/basket/basket.model';
2223
import { BasketService } from 'ish-core/services/basket/basket.service';
24+
import { getCurrentCurrency } from 'ish-core/store/core/configuration';
2325
import { mapToRouterState } from 'ish-core/store/core/router';
2426
import { resetOrderErrors } from 'ish-core/store/customer/orders';
2527
import { createUser, loadUserByAPIToken, loginUser, loginUserSuccess } from 'ish-core/store/customer/user';
@@ -114,6 +116,20 @@ export class BasketEffects {
114116
)
115117
);
116118

119+
/**
120+
* Recalculate basket if the basket currency doesn't match the current currency.
121+
*/
122+
recalculateBasketAfterCurrencyChange$ = createEffect(() =>
123+
this.actions$.pipe(
124+
ofType(loadBasketSuccess),
125+
mapToPayloadProperty('basket'),
126+
withLatestFrom(this.store.select(getCurrentCurrency)),
127+
filter(([basket, currency]) => basket.purchaseCurrency !== currency),
128+
take(1),
129+
mapTo(updateBasket({ update: { calculated: true } }))
130+
)
131+
);
132+
117133
/**
118134
* Creates a basket that is used for all subsequent basket operations with a fixed basket id instead of 'current'.
119135
*/

0 commit comments

Comments
 (0)