Skip to content

Commit

Permalink
Try to free core-delivery but it’s hard
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 22, 2024
1 parent f704432 commit 4ade58e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 116 deletions.
1 change: 1 addition & 0 deletions packages/core-delivery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@unchainedshop/events": "^3.0.0-alpha4",
"@unchainedshop/logger": "^3.0.0-alpha4",
"@unchainedshop/mongodb": "^3.0.0-alpha4",
"@unchainedshop/utils": "^3.0.0-alpha4"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions packages/core-delivery/src/delivery-settings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { UnchainedCore } from '@unchainedshop/core';
import { DeliveryProvider, FilterProviders } from './types.js';
import { Order } from '@unchainedshop/core-orders';
import type { Order } from '@unchainedshop/core-orders';

export type DetermineDefaultProvider = (
params: {
providers: Array<DeliveryProvider>;
order: Order;
},
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<DeliveryProvider>;
export interface DeliverySettingsOptions {
sortProviders?: (a: DeliveryProvider, b: DeliveryProvider) => number;
Expand Down
7 changes: 3 additions & 4 deletions packages/core-delivery/src/director/DeliveryPricingAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
PricingCalculation,
} from '@unchainedshop/utils';
import { DeliveryProvider } from '../types.js';
import { OrderDelivery, OrderDiscount, Order } from '@unchainedshop/core-orders';
import { User } from '@unchainedshop/core-users';
import { UnchainedCore } from '@unchainedshop/core';
import type { OrderDelivery, OrderDiscount, Order } from '@unchainedshop/core-orders';
import type { User } from '@unchainedshop/core-users';

export interface DeliveryPricingCalculation extends PricingCalculation {
discountId?: string;
Expand All @@ -36,7 +35,7 @@ export type IDeliveryPricingSheet = IPricingSheet<DeliveryPricingCalculation> &
}) => void;
};

export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext, UnchainedCore {
export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext {
country?: string;
currency?: string;
provider: DeliveryProvider;
Expand Down
10 changes: 4 additions & 6 deletions packages/core-delivery/src/director/DeliveryPricingDirector.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { BasePricingDirector } from '@unchainedshop/utils';
import { DeliveryPricingSheet } from './DeliveryPricingSheet.js';
import { DeliveryProvider } from '../types.js';
import { Order } from '@unchainedshop/core-orders';
import { User } from '@unchainedshop/core-users';
import { OrderDelivery } from '@unchainedshop/core-orders';
import { IPricingDirector } from '@unchainedshop/utils';
import {
DeliveryPricingAdapterContext,
DeliveryPricingCalculation,
IDeliveryPricingAdapter,
IDeliveryPricingSheet,
} from './DeliveryPricingAdapter.js';
import { UnchainedCore } from '@unchainedshop/core';
import type { Order } from '@unchainedshop/core-orders';
import type { User } from '@unchainedshop/core-users';
import type { OrderDelivery } from '@unchainedshop/core-orders';

export type DeliveryPricingContext =
| {
Expand All @@ -29,8 +28,7 @@ export type IDeliveryPricingDirector<DiscountConfiguration = unknown> = IPricing
DeliveryPricingCalculation,
DeliveryPricingAdapterContext,
IDeliveryPricingSheet,
IDeliveryPricingAdapter<DiscountConfiguration>,
UnchainedCore
IDeliveryPricingAdapter<DiscountConfiguration>
>;

const baseDirector = BasePricingDirector<
Expand Down
143 changes: 54 additions & 89 deletions packages/core-delivery/src/module/configureDeliveryModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UnchainedCore } from '@unchainedshop/core';
import { DeliveryContext, DeliveryInterface, DeliveryProvider, DeliveryProviderType } from '../types.js';
import { emit, registerEvents } from '@unchainedshop/events';
import { mongodb, generateDbFilterById, generateDbObjectId, ModuleInput } from '@unchainedshop/mongodb';
Expand All @@ -7,72 +6,12 @@ import { DeliveryProvidersCollection } from '../db/DeliveryProvidersCollection.j
import { deliverySettings, DeliverySettingsOptions } from '../delivery-settings.js';
import { DeliveryDirector } from '../director/DeliveryDirector.js';
import { DeliveryPricingContext, DeliveryPricingDirector } from '../director/DeliveryPricingDirector.js';

import { DeliveryError } from '../delivery-index.js';
import { Order } from '@unchainedshop/core-orders';
import {
DeliveryPricingCalculation,
IDeliveryPricingSheet,
} from '../director/DeliveryPricingAdapter.js';

export type DeliveryModule = {
// Queries
count: (query: mongodb.Filter<DeliveryProvider>) => Promise<number>;
create: (doc: DeliveryProvider) => Promise<DeliveryProvider>;
update: (_id: string, doc: DeliveryProvider) => Promise<DeliveryProvider>;
delete: (_id: string) => Promise<DeliveryProvider>;

findProvider: (
query: {
deliveryProviderId: string;
} & mongodb.Filter<DeliveryProvider>,
options?: mongodb.FindOptions,
) => Promise<DeliveryProvider>;
findProviders: (
query: mongodb.Filter<DeliveryProvider>,
options?: mongodb.FindOptions,
) => Promise<Array<DeliveryProvider>>;

providerExists: (query: { deliveryProviderId: string }) => Promise<boolean>;

pricingSheet: (params: {
calculation: Array<DeliveryPricingCalculation>;
currency: string;
}) => IDeliveryPricingSheet;

// Delivery adapter
findInterface: (params: DeliveryProvider) => DeliveryInterface;
findInterfaces: (params: { type: DeliveryProviderType }) => Array<DeliveryInterface>;
findSupported: (
params: { order: Order },
unchainedAPI: UnchainedCore,
) => Promise<Array<DeliveryProvider>>;
determineDefault: (
deliveryProviders: Array<DeliveryProvider>,
params: { order: Order },
unchainedAPI: UnchainedCore,
) => Promise<DeliveryProvider>;

isActive: (deliveryProvider: DeliveryProvider, unchainedAPI: UnchainedCore) => Promise<boolean>;
configurationError: (
deliveryProvider: DeliveryProvider,
unchainedAPI: UnchainedCore,
) => Promise<DeliveryError>;

isAutoReleaseAllowed: (
deliveryProvider: DeliveryProvider,
unchainedAPI: UnchainedCore,
) => Promise<boolean>;
calculate: (
pricingContext: DeliveryPricingContext,
unchainedAPI: UnchainedCore,
) => Promise<Array<DeliveryPricingCalculation>>;
send: (
deliveryProviderId: string,
deliveryContext: DeliveryContext,
unchainedAPI: UnchainedCore,
) => Promise<any>;
};
import type { Order } from '@unchainedshop/core-orders';

const DELIVERY_PROVIDER_EVENTS: string[] = [
'DELIVERY_PROVIDER_CREATE',
Expand All @@ -93,7 +32,7 @@ export const buildFindSelector = ({ type }: mongodb.Filter<DeliveryProvider> = {
export const configureDeliveryModule = async ({
db,
options: deliveryOptions = {},
}: ModuleInput<DeliverySettingsOptions>): Promise<DeliveryModule> => {
}: ModuleInput<DeliverySettingsOptions>) => {
registerEvents(DELIVERY_PROVIDER_EVENTS);

deliverySettings.configureSettings(deliveryOptions);
Expand All @@ -103,7 +42,7 @@ export const configureDeliveryModule = async ({
const getDeliveryAdapter = async (
deliveryProviderId: string,
deliveryContext: DeliveryContext,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const provider = await DeliveryProviders.findOne(generateDbFilterById(deliveryProviderId), {});

Expand All @@ -112,24 +51,35 @@ export const configureDeliveryModule = async ({

return {
// Queries
count: async (query) => {
count: async (query: mongodb.Filter<DeliveryProvider>): Promise<number> => {
const providerCount = await DeliveryProviders.countDocuments(buildFindSelector(query));
return providerCount;
},

findProvider: async ({ deliveryProviderId, ...query }, options) => {
findProvider: async (
{
deliveryProviderId,
...query
}: {
deliveryProviderId: string;
} & mongodb.Filter<DeliveryProvider>,
options?: mongodb.FindOptions<DeliveryProvider>,
): Promise<DeliveryProvider> => {
return DeliveryProviders.findOne(
deliveryProviderId ? generateDbFilterById(deliveryProviderId) : query,
options,
);
},

findProviders: async (query, options = { sort: { created: 1 } }) => {
findProviders: async (
query: mongodb.Filter<DeliveryProvider>,
options: mongodb.FindOptions<DeliveryProvider> = { sort: { created: 1 } },
): Promise<Array<DeliveryProvider>> => {
const providers = DeliveryProviders.find(buildFindSelector(query), options);
return providers.toArray();
},

providerExists: async ({ deliveryProviderId }) => {
providerExists: async ({ deliveryProviderId }: { deliveryProviderId: string }): Promise<boolean> => {
const providerCount = await DeliveryProviders.countDocuments(
generateDbFilterById(deliveryProviderId, { deleted: null }),
{ limit: 1 },
Expand All @@ -138,8 +88,7 @@ export const configureDeliveryModule = async ({
},

// Delivery Adapter

findInterface: (paymentProvider) => {
findInterface: (paymentProvider: Pick<DeliveryProvider, 'adapterKey'>): DeliveryInterface => {
const Adapter = DeliveryDirector.getAdapter(paymentProvider.adapterKey);
if (!Adapter) return null;
return {
Expand All @@ -149,7 +98,7 @@ export const configureDeliveryModule = async ({
};
},

findInterfaces: ({ type }) => {
findInterfaces: ({ type }: { type: DeliveryProviderType }): Array<DeliveryInterface> => {
return DeliveryDirector.getAdapters({
adapterFilter: (Adapter) => Adapter.typeSupported(type),
}).map((Adapter) => ({
Expand All @@ -159,13 +108,13 @@ export const configureDeliveryModule = async ({
}));
},

findSupported: async (deliveryContext, unchainedAPI) => {
findSupported: async (params: { order: Order }, unchainedAPI): Promise<Array<DeliveryProvider>> => {
const foundProviders = await DeliveryProviders.find(buildFindSelector({})).toArray();
const providers: DeliveryProvider[] = await asyncFilter(
foundProviders,
async (provider: DeliveryProvider) => {
try {
const director = await DeliveryDirector.actions(provider, deliveryContext, unchainedAPI);
const director = await DeliveryDirector.actions(provider, params, unchainedAPI);
return director.isActive();
} catch {
return false;
Expand All @@ -176,53 +125,70 @@ export const configureDeliveryModule = async ({
return deliverySettings.filterSupportedProviders(
{
providers,
order: deliveryContext.order,
order: params.order,
},
unchainedAPI,
);
},

determineDefault: async (deliveryProviders, deliveryContext, unchainedAPI) => {
determineDefault: async (
deliveryProviders: Array<DeliveryProvider>,
params: { order: Order },
unchainedAPI,
): Promise<DeliveryProvider> => {
return deliverySettings.determineDefaultProvider(
{
providers: deliveryProviders,
...deliveryContext,
...params,
},
unchainedAPI,
);
},

configurationError: async (deliveryProvider, unchainedAPI) => {
configurationError: async (
deliveryProvider: DeliveryProvider,
unchainedAPI,
): Promise<DeliveryError> => {
const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI);
return director.configurationError();
},

isActive: async (deliveryProvider, unchainedAPI) => {
isActive: async (deliveryProvider: DeliveryProvider, unchainedAPI): Promise<boolean> => {
const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI);
return Boolean(director.isActive());
},

isAutoReleaseAllowed: async (deliveryProvider, unchainedAPI) => {
isAutoReleaseAllowed: async (deliveryProvider: DeliveryProvider, unchainedAPI): Promise<boolean> => {
const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI);
return Boolean(director.isAutoReleaseAllowed());
},

calculate: async (pricingContext, unchainedAPI) => {
calculate: async (
pricingContext: DeliveryPricingContext,
unchainedAPI,
): Promise<Array<DeliveryPricingCalculation>> => {
const pricing = await DeliveryPricingDirector.actions(pricingContext, unchainedAPI);
return pricing.calculate();
},

send: async (deliveryProviderId, deliveryContext, unchainedAPI) => {
send: async (
deliveryProviderId: string,
deliveryContext: DeliveryContext,
unchainedAPI,
): Promise<any> => {
const adapter = await getDeliveryAdapter(deliveryProviderId, deliveryContext, unchainedAPI);
return adapter.send();
},

pricingSheet: (params) => {
pricingSheet: (params: {
calculation: Array<DeliveryPricingCalculation>;
currency: string;
}): IDeliveryPricingSheet => {
return DeliveryPricingSheet(params);
},

// Mutations
create: async (doc) => {
create: async (doc: DeliveryProvider): Promise<DeliveryProvider> => {
const Adapter = DeliveryDirector.getAdapter(doc.adapterKey);
if (!Adapter) return null;

Expand All @@ -232,15 +198,12 @@ export const configureDeliveryModule = async ({
configuration: Adapter.initialConfiguration,
...doc,
});
const deliveryProvider = await DeliveryProviders.findOne(
generateDbFilterById(deliveryProviderId),
{},
);
const deliveryProvider = await DeliveryProviders.findOne({ _id: deliveryProviderId }, {});
await emit('DELIVERY_PROVIDER_CREATE', { deliveryProvider });
return deliveryProvider;
},

update: async (_id: string, doc: DeliveryProvider) => {
update: async (_id: string, doc: DeliveryProvider): Promise<DeliveryProvider> => {
const deliveryProvider = await DeliveryProviders.findOneAndUpdate(
generateDbFilterById(_id),
{
Expand All @@ -255,7 +218,7 @@ export const configureDeliveryModule = async ({
return deliveryProvider;
},

delete: async (_id) => {
delete: async (_id: string): Promise<DeliveryProvider> => {
const deliveryProvider = await DeliveryProviders.findOneAndUpdate(
generateDbFilterById(_id),
{
Expand All @@ -270,3 +233,5 @@ export const configureDeliveryModule = async ({
},
};
};

export type DeliveryModule = Awaited<ReturnType<typeof configureDeliveryModule>>;
Loading

0 comments on commit 4ade58e

Please sign in to comment.