From 4ade58e271dd46049291bb41a82ba7446ad4be70 Mon Sep 17 00:00:00 2001 From: Pascal Kaufmann Date: Fri, 22 Nov 2024 20:38:25 +0100 Subject: [PATCH] =?UTF-8?q?Try=20to=20free=20core-delivery=20but=20it?= =?UTF-8?q?=E2=80=99s=20hard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core-delivery/package.json | 1 + .../core-delivery/src/delivery-settings.ts | 5 +- .../src/director/DeliveryPricingAdapter.ts | 7 +- .../src/director/DeliveryPricingDirector.ts | 10 +- .../src/module/configureDeliveryModule.ts | 143 +++++++----------- packages/core-delivery/src/types.ts | 22 +-- 6 files changed, 72 insertions(+), 116 deletions(-) diff --git a/packages/core-delivery/package.json b/packages/core-delivery/package.json index abf6cd37c..457f5358f 100644 --- a/packages/core-delivery/package.json +++ b/packages/core-delivery/package.json @@ -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": { diff --git a/packages/core-delivery/src/delivery-settings.ts b/packages/core-delivery/src/delivery-settings.ts index 8debfbb84..40080ff02 100644 --- a/packages/core-delivery/src/delivery-settings.ts +++ b/packages/core-delivery/src/delivery-settings.ts @@ -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; order: Order; }, - unchainedAPI: UnchainedCore, + unchainedAPI, ) => Promise; export interface DeliverySettingsOptions { sortProviders?: (a: DeliveryProvider, b: DeliveryProvider) => number; diff --git a/packages/core-delivery/src/director/DeliveryPricingAdapter.ts b/packages/core-delivery/src/director/DeliveryPricingAdapter.ts index 87b4e1c82..78b1d9944 100644 --- a/packages/core-delivery/src/director/DeliveryPricingAdapter.ts +++ b/packages/core-delivery/src/director/DeliveryPricingAdapter.ts @@ -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; @@ -36,7 +35,7 @@ export type IDeliveryPricingSheet = IPricingSheet & }) => void; }; -export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext, UnchainedCore { +export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext { country?: string; currency?: string; provider: DeliveryProvider; diff --git a/packages/core-delivery/src/director/DeliveryPricingDirector.ts b/packages/core-delivery/src/director/DeliveryPricingDirector.ts index e5456dd7c..31f046d37 100644 --- a/packages/core-delivery/src/director/DeliveryPricingDirector.ts +++ b/packages/core-delivery/src/director/DeliveryPricingDirector.ts @@ -1,9 +1,6 @@ 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, @@ -11,7 +8,9 @@ import { 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 = | { @@ -29,8 +28,7 @@ export type IDeliveryPricingDirector = IPricing DeliveryPricingCalculation, DeliveryPricingAdapterContext, IDeliveryPricingSheet, - IDeliveryPricingAdapter, - UnchainedCore + IDeliveryPricingAdapter >; const baseDirector = BasePricingDirector< diff --git a/packages/core-delivery/src/module/configureDeliveryModule.ts b/packages/core-delivery/src/module/configureDeliveryModule.ts index f1428353c..055c21ae9 100644 --- a/packages/core-delivery/src/module/configureDeliveryModule.ts +++ b/packages/core-delivery/src/module/configureDeliveryModule.ts @@ -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'; @@ -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) => Promise; - create: (doc: DeliveryProvider) => Promise; - update: (_id: string, doc: DeliveryProvider) => Promise; - delete: (_id: string) => Promise; - - findProvider: ( - query: { - deliveryProviderId: string; - } & mongodb.Filter, - options?: mongodb.FindOptions, - ) => Promise; - findProviders: ( - query: mongodb.Filter, - options?: mongodb.FindOptions, - ) => Promise>; - - providerExists: (query: { deliveryProviderId: string }) => Promise; - - pricingSheet: (params: { - calculation: Array; - currency: string; - }) => IDeliveryPricingSheet; - - // Delivery adapter - findInterface: (params: DeliveryProvider) => DeliveryInterface; - findInterfaces: (params: { type: DeliveryProviderType }) => Array; - findSupported: ( - params: { order: Order }, - unchainedAPI: UnchainedCore, - ) => Promise>; - determineDefault: ( - deliveryProviders: Array, - params: { order: Order }, - unchainedAPI: UnchainedCore, - ) => Promise; - - isActive: (deliveryProvider: DeliveryProvider, unchainedAPI: UnchainedCore) => Promise; - configurationError: ( - deliveryProvider: DeliveryProvider, - unchainedAPI: UnchainedCore, - ) => Promise; - - isAutoReleaseAllowed: ( - deliveryProvider: DeliveryProvider, - unchainedAPI: UnchainedCore, - ) => Promise; - calculate: ( - pricingContext: DeliveryPricingContext, - unchainedAPI: UnchainedCore, - ) => Promise>; - send: ( - deliveryProviderId: string, - deliveryContext: DeliveryContext, - unchainedAPI: UnchainedCore, - ) => Promise; -}; +import type { Order } from '@unchainedshop/core-orders'; const DELIVERY_PROVIDER_EVENTS: string[] = [ 'DELIVERY_PROVIDER_CREATE', @@ -93,7 +32,7 @@ export const buildFindSelector = ({ type }: mongodb.Filter = { export const configureDeliveryModule = async ({ db, options: deliveryOptions = {}, -}: ModuleInput): Promise => { +}: ModuleInput) => { registerEvents(DELIVERY_PROVIDER_EVENTS); deliverySettings.configureSettings(deliveryOptions); @@ -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), {}); @@ -112,24 +51,35 @@ export const configureDeliveryModule = async ({ return { // Queries - count: async (query) => { + count: async (query: mongodb.Filter): Promise => { const providerCount = await DeliveryProviders.countDocuments(buildFindSelector(query)); return providerCount; }, - findProvider: async ({ deliveryProviderId, ...query }, options) => { + findProvider: async ( + { + deliveryProviderId, + ...query + }: { + deliveryProviderId: string; + } & mongodb.Filter, + options?: mongodb.FindOptions, + ): Promise => { return DeliveryProviders.findOne( deliveryProviderId ? generateDbFilterById(deliveryProviderId) : query, options, ); }, - findProviders: async (query, options = { sort: { created: 1 } }) => { + findProviders: async ( + query: mongodb.Filter, + options: mongodb.FindOptions = { sort: { created: 1 } }, + ): Promise> => { const providers = DeliveryProviders.find(buildFindSelector(query), options); return providers.toArray(); }, - providerExists: async ({ deliveryProviderId }) => { + providerExists: async ({ deliveryProviderId }: { deliveryProviderId: string }): Promise => { const providerCount = await DeliveryProviders.countDocuments( generateDbFilterById(deliveryProviderId, { deleted: null }), { limit: 1 }, @@ -138,8 +88,7 @@ export const configureDeliveryModule = async ({ }, // Delivery Adapter - - findInterface: (paymentProvider) => { + findInterface: (paymentProvider: Pick): DeliveryInterface => { const Adapter = DeliveryDirector.getAdapter(paymentProvider.adapterKey); if (!Adapter) return null; return { @@ -149,7 +98,7 @@ export const configureDeliveryModule = async ({ }; }, - findInterfaces: ({ type }) => { + findInterfaces: ({ type }: { type: DeliveryProviderType }): Array => { return DeliveryDirector.getAdapters({ adapterFilter: (Adapter) => Adapter.typeSupported(type), }).map((Adapter) => ({ @@ -159,13 +108,13 @@ export const configureDeliveryModule = async ({ })); }, - findSupported: async (deliveryContext, unchainedAPI) => { + findSupported: async (params: { order: Order }, unchainedAPI): Promise> => { 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; @@ -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, + params: { order: Order }, + unchainedAPI, + ): Promise => { return deliverySettings.determineDefaultProvider( { providers: deliveryProviders, - ...deliveryContext, + ...params, }, unchainedAPI, ); }, - configurationError: async (deliveryProvider, unchainedAPI) => { + configurationError: async ( + deliveryProvider: DeliveryProvider, + unchainedAPI, + ): Promise => { const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI); return director.configurationError(); }, - isActive: async (deliveryProvider, unchainedAPI) => { + isActive: async (deliveryProvider: DeliveryProvider, unchainedAPI): Promise => { const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI); return Boolean(director.isActive()); }, - isAutoReleaseAllowed: async (deliveryProvider, unchainedAPI) => { + isAutoReleaseAllowed: async (deliveryProvider: DeliveryProvider, unchainedAPI): Promise => { const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI); return Boolean(director.isAutoReleaseAllowed()); }, - calculate: async (pricingContext, unchainedAPI) => { + calculate: async ( + pricingContext: DeliveryPricingContext, + unchainedAPI, + ): Promise> => { const pricing = await DeliveryPricingDirector.actions(pricingContext, unchainedAPI); return pricing.calculate(); }, - send: async (deliveryProviderId, deliveryContext, unchainedAPI) => { + send: async ( + deliveryProviderId: string, + deliveryContext: DeliveryContext, + unchainedAPI, + ): Promise => { const adapter = await getDeliveryAdapter(deliveryProviderId, deliveryContext, unchainedAPI); return adapter.send(); }, - pricingSheet: (params) => { + pricingSheet: (params: { + calculation: Array; + currency: string; + }): IDeliveryPricingSheet => { return DeliveryPricingSheet(params); }, // Mutations - create: async (doc) => { + create: async (doc: DeliveryProvider): Promise => { const Adapter = DeliveryDirector.getAdapter(doc.adapterKey); if (!Adapter) return null; @@ -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 => { const deliveryProvider = await DeliveryProviders.findOneAndUpdate( generateDbFilterById(_id), { @@ -255,7 +218,7 @@ export const configureDeliveryModule = async ({ return deliveryProvider; }, - delete: async (_id) => { + delete: async (_id: string): Promise => { const deliveryProvider = await DeliveryProviders.findOneAndUpdate( generateDbFilterById(_id), { @@ -270,3 +233,5 @@ export const configureDeliveryModule = async ({ }, }; }; + +export type DeliveryModule = Awaited>; diff --git a/packages/core-delivery/src/types.ts b/packages/core-delivery/src/types.ts index 537ba7e2d..03065c8b5 100644 --- a/packages/core-delivery/src/types.ts +++ b/packages/core-delivery/src/types.ts @@ -1,14 +1,10 @@ -import { Work } from '@unchainedshop/core-worker'; import { TimestampFields } from '@unchainedshop/mongodb'; -import { User } from '@unchainedshop/core-users'; - import { IBaseAdapter, IBaseDirector } from '@unchainedshop/utils'; -import { UnchainedCore } from '@unchainedshop/core'; -import { Order } from '@unchainedshop/core-orders'; -import { OrderDelivery } from '@unchainedshop/core-orders'; -import { OrderPosition } from '@unchainedshop/core-orders'; -import { Product } from '@unchainedshop/core-products'; -import { WarehousingProvider } from '@unchainedshop/core-warehousing'; +import type { Order, OrderPosition, OrderDelivery } from '@unchainedshop/core-orders'; +import type { Product } from '@unchainedshop/core-products'; +import type { WarehousingProvider } from '@unchainedshop/core-warehousing'; +import type { Work } from '@unchainedshop/core-worker'; +import type { User } from '@unchainedshop/core-users'; export enum DeliveryProviderType { SHIPPING = 'SHIPPING', @@ -53,8 +49,6 @@ export interface DeliveryContext { warehousingThroughputTime?: number; } -export type DeliveryAdapterContext = DeliveryContext & UnchainedCore; - export interface DeliveryLocation { _id: string; name: string; @@ -85,14 +79,14 @@ export type IDeliveryAdapter = IBaseAdapter & { typeSupported: (type: DeliveryProviderType) => boolean; - actions: (config: DeliveryConfiguration, context: DeliveryAdapterContext) => DeliveryAdapterActions; + actions: (config: DeliveryConfiguration, context: DeliveryContext) => DeliveryAdapterActions; }; export type IDeliveryDirector = IBaseDirector & { actions: ( deliveryProvider: DeliveryProvider, deliveryContext: DeliveryContext, - unchainedAPI: UnchainedCore, + unchainedAPI, ) => Promise; }; @@ -111,5 +105,5 @@ export type FilterProviders = ( providers: Array; order: Order; }, - unchainedAPI: UnchainedCore, + unchainedAPI, ) => Promise>;