Skip to content

Commit

Permalink
feat: add UPS express delivery
Browse files Browse the repository at this point in the history
draft commit - translations missing

fixes INT-659
  • Loading branch information
FreekVR committed Nov 29, 2024
1 parent b4af2ad commit eef7147
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 28 deletions.
2 changes: 2 additions & 0 deletions apps/delivery-options/src/__tests__/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum TestValue {
DeliveryTypeSaturday = 1 << 6,
DeliveryTypeSameDay = 1 << 7,
DeliveryTypePickup = 1 << 8,
DeliveryTypeExpress = 1 << 9,

// Carriers
CarrierPostNl = 1 << 20,
Expand Down Expand Up @@ -76,4 +77,5 @@ export const TEST_VALUE_MAP_PRICES = Object.freeze({
[CarrierSetting.PriceSaturdayDelivery]: TestValue.DeliveryTypeSaturday,
[CarrierSetting.PriceSignature]: TestValue.ShipmentOptionSignature,
[CarrierSetting.PriceStandardDelivery]: TestValue.Default,
[CarrierSetting.PriceExpressDelivery]: TestValue.DeliveryTypeExpress,
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
"config": {
"allowDeliveryOptions": true,
"allowEveningDelivery": true,
"allowExpressDelivery": true,
"allowMondayDelivery": true,
"allowMorningDelivery": true,
"allowOnlyRecipient": true,
Expand All @@ -22,6 +23,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
"carrierSettings": {
"bpost": {
"priceEveningDelivery": 2097168,
"priceExpressDelivery": 2097664,
"priceMondayDelivery": 2097184,
"priceMorningDelivery": 2097160,
"priceOnlyRecipient": 2097664,
Expand All @@ -36,6 +38,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"bpost:1": {
"priceEveningDelivery": 2359312,
"priceExpressDelivery": 2359808,
"priceMondayDelivery": 2359328,
"priceMorningDelivery": 2359304,
"priceOnlyRecipient": 2359808,
Expand All @@ -50,6 +53,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"bpost:2": {
"priceEveningDelivery": 2621456,
"priceExpressDelivery": 2621952,
"priceMondayDelivery": 2621472,
"priceMorningDelivery": 2621448,
"priceOnlyRecipient": 2621952,
Expand All @@ -64,6 +68,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhl": {
"priceEveningDelivery": 33554448,
"priceExpressDelivery": 33554944,
"priceMondayDelivery": 33554464,
"priceMorningDelivery": 33554440,
"priceOnlyRecipient": 33554944,
Expand All @@ -78,6 +83,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhl:1": {
"priceEveningDelivery": 33816592,
"priceExpressDelivery": 33817088,
"priceMondayDelivery": 33816608,
"priceMorningDelivery": 33816584,
"priceOnlyRecipient": 33817088,
Expand All @@ -92,6 +98,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhl:2": {
"priceEveningDelivery": 34078736,
"priceExpressDelivery": 34079232,
"priceMondayDelivery": 34078752,
"priceMorningDelivery": 34078728,
"priceOnlyRecipient": 34079232,
Expand All @@ -106,6 +113,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhleuroplus": {
"priceEveningDelivery": 1073741840,
"priceExpressDelivery": 1073742336,
"priceMondayDelivery": 1073741856,
"priceMorningDelivery": 1073741832,
"priceOnlyRecipient": 1073742336,
Expand All @@ -120,6 +128,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhleuroplus:1": {
"priceEveningDelivery": 1074003984,
"priceExpressDelivery": 1074004480,
"priceMondayDelivery": 1074004000,
"priceMorningDelivery": 1074003976,
"priceOnlyRecipient": 1074004480,
Expand All @@ -134,6 +143,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhleuroplus:2": {
"priceEveningDelivery": 1074266128,
"priceExpressDelivery": 1074266624,
"priceMondayDelivery": 1074266144,
"priceMorningDelivery": 1074266120,
"priceOnlyRecipient": 1074266624,
Expand All @@ -148,6 +158,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhlforyou": {
"priceEveningDelivery": 268435472,
"priceExpressDelivery": 268435968,
"priceMondayDelivery": 268435488,
"priceMorningDelivery": 268435464,
"priceOnlyRecipient": 268435968,
Expand All @@ -162,6 +173,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhlforyou:1": {
"priceEveningDelivery": 268697616,
"priceExpressDelivery": 268698112,
"priceMondayDelivery": 268697632,
"priceMorningDelivery": 268697608,
"priceOnlyRecipient": 268698112,
Expand All @@ -176,6 +188,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhlforyou:2": {
"priceEveningDelivery": 268959760,
"priceExpressDelivery": 268960256,
"priceMondayDelivery": 268959776,
"priceMorningDelivery": 268959752,
"priceOnlyRecipient": 268960256,
Expand All @@ -190,6 +203,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhlparcelconnect": {
"priceEveningDelivery": 536870928,
"priceExpressDelivery": 536871424,
"priceMondayDelivery": 536870944,
"priceMorningDelivery": 536870920,
"priceOnlyRecipient": 536871424,
Expand All @@ -204,6 +218,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhlparcelconnect:1": {
"priceEveningDelivery": 537133072,
"priceExpressDelivery": 537133568,
"priceMondayDelivery": 537133088,
"priceMorningDelivery": 537133064,
"priceOnlyRecipient": 537133568,
Expand All @@ -218,6 +233,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dhlparcelconnect:2": {
"priceEveningDelivery": 537395216,
"priceExpressDelivery": 537395712,
"priceMondayDelivery": 537395232,
"priceMorningDelivery": 537395208,
"priceOnlyRecipient": 537395712,
Expand All @@ -232,6 +248,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dpd": {
"priceEveningDelivery": 8388624,
"priceExpressDelivery": 8389120,
"priceMondayDelivery": 8388640,
"priceMorningDelivery": 8388616,
"priceOnlyRecipient": 8389120,
Expand All @@ -246,6 +263,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dpd:1": {
"priceEveningDelivery": 8650768,
"priceExpressDelivery": 8651264,
"priceMondayDelivery": 8650784,
"priceMorningDelivery": 8650760,
"priceOnlyRecipient": 8651264,
Expand All @@ -260,6 +278,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"dpd:2": {
"priceEveningDelivery": 8912912,
"priceExpressDelivery": 8913408,
"priceMondayDelivery": 8912928,
"priceMorningDelivery": 8912904,
"priceOnlyRecipient": 8913408,
Expand All @@ -274,6 +293,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"postnl": {
"priceEveningDelivery": 1048592,
"priceExpressDelivery": 1049088,
"priceMondayDelivery": 1048608,
"priceMorningDelivery": 1048584,
"priceOnlyRecipient": 1049088,
Expand All @@ -288,6 +308,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"postnl:1": {
"priceEveningDelivery": 1310736,
"priceExpressDelivery": 1311232,
"priceMondayDelivery": 1310752,
"priceMorningDelivery": 1310728,
"priceOnlyRecipient": 1311232,
Expand All @@ -302,6 +323,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"postnl:2": {
"priceEveningDelivery": 1572880,
"priceExpressDelivery": 1573376,
"priceMondayDelivery": 1572896,
"priceMorningDelivery": 1572872,
"priceOnlyRecipient": 1573376,
Expand All @@ -316,6 +338,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"ups": {
"priceEveningDelivery": 134217744,
"priceExpressDelivery": 134218240,
"priceMondayDelivery": 134217760,
"priceMorningDelivery": 134217736,
"priceOnlyRecipient": 134218240,
Expand All @@ -330,6 +353,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"ups:1": {
"priceEveningDelivery": 134479888,
"priceExpressDelivery": 134480384,
"priceMondayDelivery": 134479904,
"priceMorningDelivery": 134479880,
"priceOnlyRecipient": 134480384,
Expand All @@ -344,6 +368,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
"ups:2": {
"priceEveningDelivery": 134742032,
"priceExpressDelivery": 134742528,
"priceMondayDelivery": 134742048,
"priceMorningDelivery": 134742024,
"priceOnlyRecipient": 134742528,
Expand All @@ -358,6 +383,7 @@ exports[`createTestConfiguration > creates a default test configuration with uni
},
},
"priceEveningDelivery": 16,
"priceExpressDelivery": 512,
"priceMondayDelivery": 32,
"priceMorningDelivery": 8,
"priceOnlyRecipient": 512,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const createTestConfiguration = (): InputDeliveryOptionsConfiguration =>
[CarrierSetting.AllowSaturdayDelivery]: true,
[CarrierSetting.AllowSignature]: true,
[CarrierSetting.AllowStandardDelivery]: true,
[CarrierSetting.AllowExpressDelivery]: true,
...Object.entries(TEST_VALUE_MAP_PRICES).reduce((acc, [optionKey, priceValue]) => {
return {...acc, [optionKey]: priceValue};
}, {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {useResolvedDeliveryOptions} from './useResolvedDeliveryOptions';

const CARRIER_IDENTIFIER_WITH_CONTRACT = `${CarrierName.PostNl}:1234`;

const doTestSetup = async (config: RecursivePartial<InputDeliveryOptionsConfiguration> = {}): Promise<void> => {
const setupPostNl = async (config: RecursivePartial<InputDeliveryOptionsConfiguration> = {}): Promise<void> => {
const morning = normalizeDate('2022-01-01T09:00:00');
const standard = normalizeDate('2022-01-01T15:00:00');
const evening = normalizeDate('2022-01-01T20:00:00');
Expand Down Expand Up @@ -63,18 +63,57 @@ const doTestSetup = async (config: RecursivePartial<InputDeliveryOptionsConfigur
),
),
);

await waitForDeliveryOptions();
};

const setupUps = async (config: RecursivePartial<InputDeliveryOptionsConfiguration> = {}): Promise<void> => {
const today = normalizeDate('2022-01-01T09:00:00');
const tomorrow = normalizeDate('2022-01-02T09:00:00');
const inTwoDays = normalizeDate('2022-01-03T09:00:00');

mockGetDeliveryOptions.mockReturnValueOnce(
Promise.resolve([
{
date: createTimestamp(today),
possibilities: [
createDeliveryPossibility(tomorrow, {type: DeliveryTypeName.Express}),
createDeliveryPossibility(inTwoDays, {type: DeliveryTypeName.Express}),
createDeliveryPossibility(inTwoDays, {type: DeliveryTypeName.Standard}),
],
},
]),
);

mockDeliveryOptionsConfig(
getMockDeliveryOptionsConfiguration(
assign(
{
[KEY_CONFIG]: {
[KEY_CARRIER_SETTINGS]: {
[CarrierName.Ups]: {
[CarrierSetting.AllowDeliveryOptions]: true,
[CarrierSetting.AllowStandardDelivery]: true,
[CarrierSetting.AllowExpressDelivery]: true,
},
},
},
},
config,
),
),
);

await waitForDeliveryOptions(CarrierName.Ups);
};

describe('useResolvedDeliveryOptions', () => {
beforeEach(() => {
useResolvedDeliveryOptions.clear();
setActivePinia(createPinia());
});

it('sorts options by time', async () => {
await doTestSetup();
await setupPostNl();

const options = useResolvedDeliveryOptions();

Expand All @@ -90,9 +129,22 @@ describe('useResolvedDeliveryOptions', () => {
]);
});

it('prefers standard delivery over express in the same time window', async () => {
await setupUps();

const options = useResolvedDeliveryOptions();

const resolvedOptions = options.value.map(({carrier, deliveryType}) => ({carrier, deliveryType}));

expect(resolvedOptions).toEqual([
{carrier: CarrierName.Ups, deliveryType: DeliveryTypeName.Express},
{carrier: CarrierName.Ups, deliveryType: DeliveryTypeName.Standard},
]);
});

it('handles fake delivery', async () => {
// DE is not a delivery country for PostNL.
await doTestSetup({[KEY_ADDRESS]: {cc: 'DE'}});
await setupPostNl({[KEY_ADDRESS]: {cc: 'DE'}});

const options = useResolvedDeliveryOptions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@myparcel-do/shared';
import {type Replace} from '@myparcel/ts-utils';
import {type Timestamp, type DeliveryOption, type DeliveryPossibility, type DeliveryTimeFrame} from '@myparcel/sdk';
import {DeliveryTypeName} from '@myparcel/constants';
import {createGetDeliveryOptionsParameters, getResolvedDeliveryType, createDeliveryTypeTranslatable} from '../utils';
import {type SelectedDeliveryMoment} from '../types';
import {useTimeRange} from './useTimeRange';
Expand Down Expand Up @@ -87,6 +88,19 @@ const callback = (): UseResolvedDeliveryOptions => {
? createUntranslatable(useTimeRange(start.date_time.date, end.date_time.date).value)
: createDeliveryTypeTranslatable(datePossibility.type);

// Given a possibility with the same start/end timeFrame, don't add the express option if standard is already present.
if (
datePossibility.type === DeliveryTypeName.Express &&
possibilities.some(
(possibility) =>
possibility.type === DeliveryTypeName.Standard &&
possibility.delivery_time_frames[0]?.date_time.date === start.date_time.date &&
possibility.delivery_time_frames[1]?.date_time.date === end.date_time.date,
)
) {
return;
}

acc.push({
carrier: carrier.carrier.value.identifier,
date: dateOption.date?.date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`getDefaultConfigForPlatform > should return the correct default config
{
"allowDeliveryOptions": true,
"allowEveningDelivery": true,
"allowExpressDelivery": false,
"allowMondayDelivery": true,
"allowMorningDelivery": true,
"allowOnlyRecipient": true,
Expand Down Expand Up @@ -42,6 +43,7 @@ exports[`getDefaultConfigForPlatform > should return the correct default config
"pickupShowDistance": false,
"platform": "belgie",
"priceEveningDelivery": 0,
"priceExpressDelivery": 0,
"priceMondayDelivery": 0,
"priceMorningDelivery": 0,
"priceOnlyRecipient": 0,
Expand All @@ -63,6 +65,7 @@ exports[`getDefaultConfigForPlatform > should return the correct default config
{
"allowDeliveryOptions": true,
"allowEveningDelivery": true,
"allowExpressDelivery": false,
"allowMondayDelivery": true,
"allowMorningDelivery": true,
"allowOnlyRecipient": true,
Expand Down Expand Up @@ -101,6 +104,7 @@ exports[`getDefaultConfigForPlatform > should return the correct default config
"pickupShowDistance": true,
"platform": "myparcel",
"priceEveningDelivery": 0,
"priceExpressDelivery": 0,
"priceMondayDelivery": 0,
"priceMorningDelivery": 0,
"priceOnlyRecipient": 0,
Expand Down
5 changes: 3 additions & 2 deletions apps/delivery-options/src/config/handleDeprecatedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ export const handleDeprecatedOptions = <Input extends InputDeliveryOptionsConfig

if (
isDefined(resolvedConfig[CarrierSetting.AllowDeliveryOptions]) &&
!isDefined(resolvedConfig[CarrierSetting.AllowStandardDelivery])
!isDefined(resolvedConfig[CarrierSetting.AllowStandardDelivery]) &&
!isDefined(resolvedConfig[CarrierSetting.AllowExpressDelivery])
) {
logger.deprecated(
`Passing only ${CarrierSetting.AllowDeliveryOptions} without ${CarrierSetting.AllowStandardDelivery}`,
`${CarrierSetting.AllowDeliveryOptions}: true and ${CarrierSetting.AllowStandardDelivery}: true`,
`${CarrierSetting.AllowDeliveryOptions}: true and ${CarrierSetting.AllowStandardDelivery}: true and/or and ${CarrierSetting.AllowExpressDelivery}: true`,
);

resolvedConfig[CarrierSetting.AllowStandardDelivery] = restConfig[CarrierSetting.AllowDeliveryOptions];
Expand Down
Loading

0 comments on commit eef7147

Please sign in to comment.