Skip to content

Commit

Permalink
fix: don't output false for shipment options that are disabled (#247)
Browse files Browse the repository at this point in the history
INT-656
  • Loading branch information
EdieLemoine authored Oct 2, 2024
1 parent 1f50edc commit 1cba349
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
KEY_CONFIG,
useCarrierRequest,
ConfigSetting,
type InputDeliveryOptionsConfig,
} from '@myparcel-do/shared';
import {CarrierName, DeliveryTypeName, PackageTypeName, ShipmentOptionName} from '@myparcel/constants';
import {
Expand All @@ -32,6 +33,7 @@ import {
import {useResolvedValues} from './useResolvedValues';

interface TestInput {
config?: Partial<InputDeliveryOptionsConfig>;
external: DeliveryOptionsOutput;
internal: InternalOutput;
name: string;
Expand Down Expand Up @@ -73,15 +75,28 @@ describe('useResolvedValues', () => {
name: 'default values',
internal: createInternalOutput(),
external: createExternalOutput({
shipmentOptions: {
[FIELD_SHIPMENT_OPTIONS]: {
onlyRecipient: false,
signature: false,
},
}),
},
{
name: 'default values with signature and only recipient disabled',
config: {
[CarrierSetting.AllowSignature]: false,
[CarrierSetting.AllowOnlyRecipient]: false,
},
internal: createInternalOutput(),
external: createExternalOutput(),
},

{
name: 'onlyRecipient',
name: 'onlyRecipient enabled but not selected',
config: {
[CarrierSetting.AllowOnlyRecipient]: true,
},

internal: createInternalOutput({
[FIELD_DELIVERY_DATE]: '2023-12-31',
[FIELD_DELIVERY_MOMENT]: {
Expand All @@ -106,6 +121,8 @@ describe('useResolvedValues', () => {

{
name: 'pickup',
config: {},

internal: createInternalOutput({
[FIELD_HOME_OR_PICKUP]: HOME_OR_PICKUP_PICKUP,
[FIELD_PICKUP_LOCATION]: '176688',
Expand All @@ -120,16 +137,20 @@ describe('useResolvedValues', () => {
}),
}),
},
] satisfies TestInput[])('converts internal output to external output with $name', async ({internal, external}) => {
expect.assertions(1);
] satisfies TestInput[])(
'converts internal output to external output with $name',
async ({internal, external, config}) => {
expect.assertions(1);

mockSelectedDeliveryOptions(internal);
await flushPromises();
mockDeliveryOptionsConfig({[KEY_CONFIG]: config});
mockSelectedDeliveryOptions(internal);
await flushPromises();

const resolvedValues = useResolvedValues();
const resolvedValues = useResolvedValues();

expect(resolvedValues.value).toEqual(external);
});
expect(resolvedValues.value).toEqual(external);
},
);

it('should not expose delivery date if it is disabled', async () => {
mockDeliveryOptionsConfig({[KEY_CONFIG]: {[ConfigSetting.ShowDeliveryDate]: true}});
Expand Down
42 changes: 31 additions & 11 deletions apps/delivery-options/src/composables/events/useResolvedValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,44 @@ import {
type DeliveryOutput,
type PickupOutput,
type SupportedDeliveryTypeName,
type DeliveryDeliveryType,
type SupportedShipmentOptionName,
getConfigKey,
type CarrierIdentifier,
} from '@myparcel-do/shared';
import {DeliveryTypeName, PackageTypeName, ShipmentOptionName} from '@myparcel/constants';
import {useSelectedValues} from '../useSelectedValues';
import {useSelectedPickupLocation} from '../useSelectedPickupLocation';
import {useResolvedDeliveryOptions} from '../useResolvedDeliveryOptions';
import {getResolvedValue, parseJson} from '../../utils';
import {type SelectedDeliveryMoment} from '../../types';
import {type SelectedDeliveryMomentDelivery} from '../../types';
import {FIELD_DELIVERY_MOMENT, FIELD_SHIPMENT_OPTIONS, HOME_OR_PICKUP_PICKUP} from '../../data';

const DELIVERY_DELIVERY_TYPES: readonly SupportedDeliveryTypeName[] = Object.freeze([
const DELIVERY_DELIVERY_TYPES = Object.freeze([
DeliveryTypeName.Morning,
DeliveryTypeName.Evening,
DeliveryTypeName.Standard,
]);
] satisfies SupportedDeliveryTypeName[]);

const SHIPMENT_OPTION_OUTPUT_MAP = Object.freeze({
[ShipmentOptionName.Signature]: 'signature',
[ShipmentOptionName.OnlyRecipient]: 'onlyRecipient',
} as Record<SupportedShipmentOptionName, keyof DeliveryOutput['shipmentOptions']>);

const createResolvedShipmentOptions = (
carrier: CarrierIdentifier,
shipmentOptions: string[],
): DeliveryOutput['shipmentOptions'] => {
return Object.entries(SHIPMENT_OPTION_OUTPUT_MAP).reduce((acc, [shipmentOption, objectKey]) => {
const enabledKey = getConfigKey(shipmentOption as SupportedShipmentOptionName);
const enabled = getResolvedValue(enabledKey, carrier, false);

if (enabled) {
acc[objectKey] = shipmentOptions.includes(shipmentOption);
}

return acc;
}, {} as DeliveryOutput['shipmentOptions']);
};

export const useResolvedValues = (): ComputedRef<PickupOutput | DeliveryOutput | undefined> => {
const selectedValues = useSelectedValues();
Expand Down Expand Up @@ -48,12 +71,12 @@ export const useResolvedValues = (): ComputedRef<PickupOutput | DeliveryOutput |
} satisfies PickupOutput;
}

const parsedMoment = parseJson<SelectedDeliveryMoment>(selectedValues[FIELD_DELIVERY_MOMENT].value);
const parsedMoment = parseJson<SelectedDeliveryMomentDelivery>(selectedValues[FIELD_DELIVERY_MOMENT].value);
const showDeliveryDate = getResolvedValue(ConfigSetting.ShowDeliveryDate);
const shipmentOptions = selectedValues[FIELD_SHIPMENT_OPTIONS].value ?? [];

const deliveryType: DeliveryDeliveryType = DELIVERY_DELIVERY_TYPES.includes(parsedMoment.deliveryType)
? (parsedMoment.deliveryType as DeliveryDeliveryType)
const deliveryType = DELIVERY_DELIVERY_TYPES.includes(parsedMoment.deliveryType)
? parsedMoment.deliveryType
: DeliveryTypeName.Standard;

return {
Expand All @@ -62,10 +85,7 @@ export const useResolvedValues = (): ComputedRef<PickupOutput | DeliveryOutput |
deliveryType,
isPickup: false,
packageType: parsedMoment.packageType,
shipmentOptions: {
signature: shipmentOptions.includes(ShipmentOptionName.Signature) ?? false,
onlyRecipient: shipmentOptions.includes(ShipmentOptionName.OnlyRecipient) ?? false,
},
shipmentOptions: createResolvedShipmentOptions(parsedMoment.carrier, shipmentOptions),
} satisfies DeliveryOutput;
});
};
9 changes: 7 additions & 2 deletions apps/delivery-options/src/types/form.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type OutputPickupLocation,
type SupportedDeliveryTypeName,
type AnyTranslatable,
type DeliveryDeliveryType,
} from '@myparcel-do/shared';
import {type Replace} from '@myparcel/ts-utils';
import {type DeliveryOption, type StartEndDate} from '@myparcel/sdk';
Expand All @@ -28,7 +29,11 @@ export interface ResolvedPickupLocation extends OutputPickupLocation {
openingHours: OpeningHoursEntry[];
}

export interface SelectedDeliveryMoment
extends Replace<Omit<ResolvedDeliveryOptions, 'carrier'>, 'deliveryType', SupportedDeliveryTypeName> {
export interface SelectedDeliveryMoment<T extends SupportedDeliveryTypeName = SupportedDeliveryTypeName>
extends Replace<Omit<ResolvedDeliveryOptions, 'carrier'>, 'deliveryType', T> {
carrier: CarrierIdentifier;
}

export type SelectedDeliveryMomentDelivery = SelectedDeliveryMoment<DeliveryDeliveryType>;

export type SelectedDeliveryMomentPickup = SelectedDeliveryMoment<DeliveryTypeName.Pickup>;
2 changes: 1 addition & 1 deletion libs/shared/src/types/config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,4 @@ export type ResolvedConfigOption<O extends ConfigKey | ConfigOption> = O extends

export type CarrierSettingsKey = CarrierSetting;

export type ConfigKey = ConfigSetting | CarrierSettingsKey | SupportedPackageTypeName;
export type ConfigKey = ConfigSetting | CarrierSettingsKey;

0 comments on commit 1cba349

Please sign in to comment.