From da4fa963cb7499205a51f94a6232169b409295ce Mon Sep 17 00:00:00 2001 From: Manuel Schaechinger Date: Fri, 20 Aug 2021 11:33:31 +0200 Subject: [PATCH] feat: shopping cart item match product to receiver country --- TODO.md | 2 +- src/1c4a/Service.ts | 20 +++++++++++++++++--- src/1c4a/address.ts | 1 + test/1c4a/Service/Service.spec.ts | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index 31414eb..3758133 100644 --- a/TODO.md +++ b/TODO.md @@ -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] Private gallery walk through **Update:** Not in use according to Deutsche Post diff --git a/src/1c4a/Service.ts b/src/1c4a/Service.ts index 22bf164..3d15a84 100644 --- a/src/1c4a/Service.ts +++ b/src/1c4a/Service.ts @@ -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, @@ -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) { @@ -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) { diff --git a/src/1c4a/address.ts b/src/1c4a/address.ts index 5076f17..1ddc675 100644 --- a/src/1c4a/address.ts +++ b/src/1c4a/address.ts @@ -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), diff --git a/test/1c4a/Service/Service.spec.ts b/test/1c4a/Service/Service.spec.ts index b127c71..3e1343f 100644 --- a/test/1c4a/Service/Service.spec.ts +++ b/test/1c4a/Service/Service.spec.ts @@ -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'; @@ -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, {