Skip to content

Commit

Permalink
Extend typing with dynamic UnchainedAPI in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 22, 2024
1 parent 4ade58e commit 857ac85
Show file tree
Hide file tree
Showing 76 changed files with 311 additions and 444 deletions.
2 changes: 1 addition & 1 deletion docs/docs/advanced/write-plugins/payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
PaymentError,
} from '@unchainedshop/core-payment';

const ShopPayment: IPaymentAdapter = {
const ShopPayment: IPaymentAdapter<UnchainedCore> = {
key: 'ch.Shop.payment',
label: 'Shop Payment',
version: '1.0.0',
Expand Down
7 changes: 5 additions & 2 deletions packages/core-delivery/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ export interface DeliveryAdapterActions {
pickUpLocations: () => Promise<Array<DeliveryLocation>>;
send: () => Promise<boolean | Work>;
}
export type IDeliveryAdapter = IBaseAdapter & {
export type IDeliveryAdapter<UnchainedAPI = any> = IBaseAdapter & {
initialConfiguration: DeliveryConfiguration;

typeSupported: (type: DeliveryProviderType) => boolean;

actions: (config: DeliveryConfiguration, context: DeliveryContext) => DeliveryAdapterActions;
actions: (
config: DeliveryConfiguration,
context: DeliveryContext & UnchainedAPI,
) => DeliveryAdapterActions;
};

export type IDeliveryDirector = IBaseDirector<IDeliveryAdapter> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const EnrollmentAdapter: Omit<IEnrollmentAdapter, 'key' | 'label' | 'vers

nextPeriod: async () => {
const { enrollment } = context;
const product = await context.modules.products.findProduct({
const product = await (context as any).modules.products.findProduct({
productId: enrollment.productId,
});
const plan = product?.plan;
Expand Down
3 changes: 2 additions & 1 deletion packages/core-enrollments/src/enrollments-settings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import later from '@breejs/later';
import { WorkerSchedule } from '@unchainedshop/core-worker';
import { generateRandomHash } from '@unchainedshop/utils';
import { Enrollment } from './types.js';

import type { WorkerSchedule } from '@unchainedshop/core-worker';

const everyHourSchedule = later.parse.text('every 59 minutes');

export interface EnrollmentsSettingsOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnrollmentStatus } from '@unchainedshop/core-enrollments';
import { EnrollmentStatus } from '../types.js';
import { buildFindSelector } from './configureEnrollmentsModule.js';

describe('buildFindSelector', () => {
Expand Down
29 changes: 9 additions & 20 deletions packages/core-enrollments/src/module/configureEnrollmentsModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SortDirection, SortOption } from '@unchainedshop/utils';
import { UnchainedCore } from '@unchainedshop/core';
import {
Enrollment,
EnrollmentData,
Expand Down Expand Up @@ -53,10 +52,7 @@ export interface EnrollmentTransformations {

// Processing

export type EnrollmentContextParams = (
enrollment: Enrollment,
unchainedAPI: UnchainedCore,
) => Promise<Enrollment>;
export type EnrollmentContextParams = (enrollment: Enrollment, unchainedAPI) => Promise<Enrollment>;

export interface EnrollmentProcessing {
terminateEnrollment: EnrollmentContextParams;
Expand All @@ -66,7 +62,7 @@ export interface EnrollmentProcessing {
export interface EnrollmentMutations {
addEnrollmentPeriod: (enrollmentId: string, period: EnrollmentPeriod) => Promise<Enrollment>;

create: (doc: EnrollmentData, unchainedAPI: UnchainedCore) => Promise<Enrollment>;
create: (doc: EnrollmentData, unchainedAPI) => Promise<Enrollment>;

createFromCheckout: (
order: Order,
Expand All @@ -80,7 +76,7 @@ export interface EnrollmentMutations {
deliveryContext?: any;
};
},
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<void>;

delete: (enrollmentId: string) => Promise<number>;
Expand All @@ -97,16 +93,12 @@ export interface EnrollmentMutations {

updatePayment: (enrollmentId: string, payment: Enrollment['payment']) => Promise<Enrollment>;

updatePlan: (
enrollmentId: string,
plan: EnrollmentPlan,
unchainedAPI: UnchainedCore,
) => Promise<Enrollment>;
updatePlan: (enrollmentId: string, plan: EnrollmentPlan, unchainedAPI) => Promise<Enrollment>;

updateStatus: (
enrollmentId: string,
params: { status: EnrollmentStatus; info?: string },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<Enrollment>;
}

Expand Down Expand Up @@ -157,10 +149,7 @@ export const configureEnrollmentsModule = async ({
return findNewEnrollmentNumber(enrollment, index + 1);
};

const findNextStatus = async (
enrollment: Enrollment,
unchainedAPI: UnchainedCore,
): Promise<EnrollmentStatus> => {
const findNextStatus = async (enrollment: Enrollment, unchainedAPI): Promise<EnrollmentStatus> => {
let status = enrollment.status as EnrollmentStatus;
const director = await EnrollmentDirector.actions({ enrollment }, unchainedAPI);

Expand Down Expand Up @@ -227,7 +216,7 @@ export const configureEnrollmentsModule = async ({
return enrollment;
};

const processEnrollment = async (enrollment: Enrollment, unchainedAPI: UnchainedCore) => {
const processEnrollment = async (enrollment: Enrollment, unchainedAPI) => {
let status = await findNextStatus(enrollment, unchainedAPI);

if (status === EnrollmentStatus.ACTIVE) {
Expand All @@ -241,7 +230,7 @@ export const configureEnrollmentsModule = async ({
const initializeEnrollment = async (
enrollment: Enrollment,
params: { orderIdForFirstPeriod?: string; reason: string },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const { modules } = unchainedAPI;

Expand All @@ -263,7 +252,7 @@ export const configureEnrollmentsModule = async ({
const sendStatusToCustomer = async (
enrollment: Enrollment,
params: { locale?: Intl.Locale; reason?: string },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const { modules } = unchainedAPI;

Expand Down
12 changes: 4 additions & 8 deletions packages/core-enrollments/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IBaseAdapter, IBaseDirector } from '@unchainedshop/utils';
import { TimestampFields, LogFields, Address, Contact } from '@unchainedshop/mongodb';
import { Product, ProductPlan } from '@unchainedshop/core-products';
import { OrderPosition } from '@unchainedshop/core-orders';
import { UnchainedCore } from '@unchainedshop/core';

export enum EnrollmentStatus {
INITIAL = 'INITIAL',
Expand Down Expand Up @@ -78,10 +77,10 @@ export type IEnrollmentAdapter = IBaseAdapter & {

transformOrderItemToEnrollmentPlan: (
orderPosition: OrderPosition,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<EnrollmentPlan>;

actions: (params: EnrollmentContext & UnchainedCore) => EnrollmentAdapterActions;
actions: (params: EnrollmentContext) => EnrollmentAdapterActions;
};

export interface EnrollmentData {
Expand All @@ -103,11 +102,8 @@ export type IEnrollmentDirector = IBaseDirector<IEnrollmentAdapter> & {
transformOrderItemToEnrollment: (
item: { orderPosition: OrderPosition; product: Product },
doc: Omit<EnrollmentData, 'configuration' | 'productId' | 'quantity'>,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<EnrollmentData>;

actions: (
enrollmentContext: EnrollmentContext,
unchainedAPI: UnchainedCore,
) => Promise<EnrollmentAdapterActions>;
actions: (enrollmentContext: EnrollmentContext, unchainedAPI) => Promise<EnrollmentAdapterActions>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
SearchProductConfiguration,
} from '../search/search.js';
import { SearchQuery, Filter } from '../types.js';
import { UnchainedCore } from '@unchainedshop/core';
import { Assortment } from '@unchainedshop/core-assortments';

export type SearchProducts = {
Expand All @@ -34,13 +33,13 @@ export type FilterSearchModule = {
searchProducts: (
searchQuery: SearchQuery,
params: { forceLiveCollection?: boolean },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<SearchProducts>;

searchAssortments: (
searchQuery: SearchQuery,
params: { forceLiveCollection?: boolean },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<SearchAssortments>;
};

Expand Down
25 changes: 10 additions & 15 deletions packages/core-filters/src/module/configureFiltersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
generateDbObjectId,
ModuleInput,
} from '@unchainedshop/mongodb';
import { UnchainedCore } from '@unchainedshop/core';
import { FilterType } from '../db/FilterType.js';
import { FilterDirector } from '../director/FilterDirector.js';
import { FiltersCollection } from '../db/FiltersCollection.js';
Expand Down Expand Up @@ -43,25 +42,21 @@ export type FiltersModule = {

filterExists: (params: { filterId: string }) => Promise<boolean>;

invalidateCache: (query: mongodb.Filter<Filter>, unchainedAPI: UnchainedCore) => Promise<void>;
invalidateCache: (query: mongodb.Filter<Filter>, unchainedAPI) => Promise<void>;

// Mutations
create: (
doc: Filter & { title: string; locale: string },
unchainedAPI: UnchainedCore,
unchainedAPI,
options?: { skipInvalidation?: boolean },
) => Promise<Filter>;

createFilterOption: (
filterId: string,
option: { value: string },
unchainedAPI: UnchainedCore,
) => Promise<Filter>;
createFilterOption: (filterId: string, option: { value: string }, unchainedAPI) => Promise<Filter>;

update: (
filterId: string,
doc: Filter,
unchainedAPI: UnchainedCore,
unchainedAPI,
options?: { skipInvalidation?: boolean },
) => Promise<Filter>;

Expand All @@ -72,7 +67,7 @@ export type FiltersModule = {
filterId: string;
filterOptionValue?: string;
},
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<Filter>;

/*
Expand Down Expand Up @@ -117,7 +112,7 @@ export const configureFiltersModule = async ({
const findProductIds = async (
filter: Filter,
{ value }: { value?: boolean | string },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const { modules } = unchainedAPI;
const director = await FilterDirector.actions({ filter, searchQuery: {} }, unchainedAPI);
Expand All @@ -138,7 +133,7 @@ export const configureFiltersModule = async ({

const buildProductIdMap = async (
filter: Filter,
unchainedAPI: UnchainedCore,
unchainedAPI,
): Promise<[Array<string>, Record<string, Array<string>>]> => {
const allProductIds = await findProductIds(filter, {}, unchainedAPI);
const productIdsMap =
Expand All @@ -162,7 +157,7 @@ export const configureFiltersModule = async ({
async function filterProductIdsRaw(
filter: Filter,
{ values, forceLiveCollection }: { values: Array<string>; forceLiveCollection?: boolean },
unchainedAPI: UnchainedCore,
unchainedAPI,
) {
const getProductIds =
(!forceLiveCollection && (await filtersSettings.getCachedProductIds(filter._id))) ||
Expand All @@ -186,7 +181,7 @@ export const configureFiltersModule = async ({
},
);

const invalidateProductIdCache = async (filter: Filter, unchainedAPI: UnchainedCore) => {
const invalidateProductIdCache = async (filter: Filter, unchainedAPI) => {
if (!filter) return;

log(`Filters: Rebuilding ${filter.key}`, { level: LogLevel.Verbose });
Expand All @@ -195,7 +190,7 @@ export const configureFiltersModule = async ({
await filtersSettings.setCachedProductIds(filter._id, productIds, productIdMap);
};

const invalidateCache = async (selector: mongodb.Filter<Filter>, unchainedAPI: UnchainedCore) => {
const invalidateCache = async (selector: mongodb.Filter<Filter>, unchainedAPI) => {
log('Filters: Start invalidating filter caches', {
level: LogLevel.Verbose,
});
Expand Down
5 changes: 2 additions & 3 deletions packages/core-filters/src/search/loadFilter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UnchainedCore } from '@unchainedshop/core';
import { FilterType } from '../db/FilterType.js';
import { intersectSet } from '../utils/intersectSet.js';
import { FilterProductIds } from './search.js';
Expand All @@ -14,7 +13,7 @@ const findLoadedOptions = async (
},
filterProductIds: FilterProductIds,
filterActions: FilterAdapterActions,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const { values, forceLiveCollection, productIdSet } = params;
const parse = createFilterValueParser(filter.type);
Expand Down Expand Up @@ -62,7 +61,7 @@ export const loadFilter = async (
},
filterProductIds: FilterProductIds,
filterActions: FilterAdapterActions,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const { allProductIds, filterQuery, forceLiveCollection, otherFilters } = params;

Expand Down
3 changes: 1 addition & 2 deletions packages/core-filters/src/search/productFacetedSearch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UnchainedCore } from '@unchainedshop/core';
import { mongodb } from '@unchainedshop/mongodb';
import { intersectSet } from '../utils/intersectSet.js';
import { FilterProductIds, SearchConfiguration } from './search.js';
Expand All @@ -8,7 +7,7 @@ export const productFacetedSearch = (
Filters: mongodb.Collection<Filter>,
filterProductIds: FilterProductIds,
searchConfiguration: SearchConfiguration,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const { query, filterSelector, forceLiveCollection } = searchConfiguration;

Expand Down
5 changes: 2 additions & 3 deletions packages/core-filters/src/search/resolveProductSelector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { UnchainedCore } from '@unchainedshop/core';
import { FilterAdapterActions, SearchQuery } from '../types.js';

const defaultSelector = ({ includeInactive }: SearchQuery, { modules }: UnchainedCore) => {
const defaultSelector = ({ includeInactive }: SearchQuery, { modules }) => {
const selector = !includeInactive
? modules.products.search.buildActiveStatusFilter()
: modules.products.search.buildActiveDraftStatusFilter();
Expand All @@ -11,7 +10,7 @@ const defaultSelector = ({ includeInactive }: SearchQuery, { modules }: Unchaine
export const resolveProductSelector = async (
searchQuery: SearchQuery,
filterActions: FilterAdapterActions,
unchainedAPI: UnchainedCore,
unchainedAPI,
) => {
const selector = defaultSelector(searchQuery, unchainedAPI);
return filterActions.transformProductSelector(selector, {});
Expand Down
3 changes: 1 addition & 2 deletions packages/core-filters/src/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UnchainedCore } from '@unchainedshop/core';
import { Filter, SearchQuery } from '../types.js';
import { mongodb } from '@unchainedshop/mongodb';
import { Product } from '@unchainedshop/core-products';
Expand Down Expand Up @@ -26,5 +25,5 @@ export interface SearchAssortmentConfiguration extends SearchConfiguration {
export type FilterProductIds = (
filter: Filter,
params: { values: Array<string>; forceLiveCollection?: boolean },
unchainedAPI: UnchainedCore,
unchainedAPI,
) => Promise<Array<string>>;
7 changes: 3 additions & 4 deletions packages/core-filters/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Assortment } from '@unchainedshop/core-assortments';
import { TimestampFields, mongodb } from '@unchainedshop/mongodb';
import { IBaseAdapter, IBaseDirector } from '@unchainedshop/utils';
import { Product } from '@unchainedshop/core-products';
import { UnchainedCore } from '@unchainedshop/core';

export enum FilterType {
SWITCH = 'SWITCH',
Expand Down Expand Up @@ -106,12 +105,12 @@ export interface FilterAdapterActions {
) => Promise<mongodb.FindOptions['sort']>;
}

export type IFilterAdapter = IBaseAdapter & {
export type IFilterAdapter<UnchainedAPI = any> = IBaseAdapter & {
orderIndex: number;

actions: (params: FilterContext & UnchainedCore) => FilterAdapterActions;
actions: (params: FilterContext & UnchainedAPI) => FilterAdapterActions;
};

export type IFilterDirector = IBaseDirector<IFilterAdapter> & {
actions: (filterContext: FilterContext, unchainedAPI: UnchainedCore) => Promise<FilterAdapterActions>;
actions: (filterContext: FilterContext, unchainedAPI) => Promise<FilterAdapterActions>;
};
Loading

0 comments on commit 857ac85

Please sign in to comment.