Skip to content

Commit

Permalink
feat: shopping cart item match product to receiver country
Browse files Browse the repository at this point in the history
  • Loading branch information
schaechinger committed Aug 20, 2021
1 parent e4bb762 commit da4fa96
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This document contains next steps and other todos for v1.0.0
- [ ] [feat] Improve error handling from soap requests
- [ ] [test] Service tests
- [ ] [test] Checkout tests
- [ ] [feat] Check shopping cart item address and match country to domestic
- [x] [feat] Check shopping cart item address and match country to domestic
flag of product
- [ ] [feat] <del>Private gallery walk through</del>
**Update:** Not in use according to Deutsche Post
Expand Down
20 changes: 17 additions & 3 deletions src/1c4a/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SoapService } from '../services/Soap';
import { User, UserCredentials, UserInfo } from '../User';
import { amountToCents, parseAmount } from '../utils/amount';
import { parseAddress, SimpleAddress } from './address';
import CountryCode from './countryCode';
import {
AddressError,
CheckoutError,
Expand Down Expand Up @@ -363,9 +364,7 @@ export class OneClickForAppService extends SoapService implements OneClickForApp
}
if (options.sender || options.receiver) {
if (!options.sender || !options.receiver) {
throw new AddressError(
'Address muss be available for sender and receiver if one is given.'
);
throw new AddressError('Address muss be available for sender and receiver if one is given');
}

if (VoucherLayout.FrankingZone === voucherLayout) {
Expand All @@ -376,6 +375,21 @@ export class OneClickForAppService extends SoapService implements OneClickForApp
const receiver = parseAddress(options.receiver);

position.address = { sender, receiver };

if (undefined !== product.domestic) {
// domestic product for abroad address
if (product.domestic && CountryCode.DEU !== receiver.address.country) {
throw new ProductError(
'Domestic products cannot be used for international receiver addresses'
);
}
// internatiomal product for national receiver address
else if (!product.domestic && CountryCode.DEU === receiver.address.country) {
throw new ProductError(
'International products should not be used for national receiver addresses'
);
}
}
}

if (product.ppl) {
Expand Down
1 change: 1 addition & 0 deletions src/1c4a/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const parseAddress = (data: SimpleAddress): NamedAddress => {
if (data.state) {
zip = `${data.state.toUpperCase()} ${zip}`.trim();
}

const address: Address = {
additional: (data.additional || '').substr(0, 50),
street: data.street?.substr(0, 50),
Expand Down
24 changes: 24 additions & 0 deletions test/1c4a/Service/Service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { OneClickForAppService } from '../../../src/1c4a/Service';
import { VoucherLayout } from '../../../src/1c4a/voucher';
import { UserError } from '../../../src/Error';
import { CountryCode } from '../../../src/Internetmarke';
import { ProductError } from '../../../src/prodWs/Error';
import { Product } from '../../../src/prodWs/product';
import { DataStore } from '../../../src/services/DataStore';
Expand Down Expand Up @@ -322,6 +323,29 @@ describe('1C4A Service', () => {
}).to.throw(AddressError);
});

it('should throw an error if a domestic product is used for an abroad receiver address', () => {
const receiverAddressAbroad = { ...receiverAddress };
receiverAddressAbroad.country = CountryCode.AUT;

expect(() => {
service.addItemToShoppingCart({ id: 1, price: 80, domestic: true } as Product, {
voucherLayout: VoucherLayout.AddressZone,
sender: senderAddress,
receiver: receiverAddressAbroad
});
}).to.throw(ProductError);
});

it('should throw an error if an internatioal product is used for a national receiver address', () => {
expect(() => {
service.addItemToShoppingCart({ id: 1, price: 80, domestic: false } as Product, {
voucherLayout: VoucherLayout.AddressZone,
sender: senderAddress,
receiver: receiverAddress
});
}).to.throw(ProductError);
});

it('should throw an error when passing addresses in franking layout', () => {
expect(() => {
service.addItemToShoppingCart({ id: 1, price: 80 } as Product, {
Expand Down

0 comments on commit da4fa96

Please sign in to comment.