Skip to content

Commit

Permalink
Reduce logging and export new platform function to print the plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Jan 13, 2025
1 parent 2239fb7 commit 1a81a8d
Show file tree
Hide file tree
Showing 27 changed files with 129 additions and 132 deletions.
3 changes: 0 additions & 3 deletions docs/docs/advanced/write-plugins/payment-pricing.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ export const ShopCommission: IPaymentPricingAdapter = {
isActivatedFor: (context: PaymentPricingAdapterContext): boolean => {
return true;
},
log(message: string, options?: LogOptions): void {
console.log(message);
},

actions: (params: {
calculationSheet: IPaymentPricingSheet;
Expand Down
4 changes: 0 additions & 4 deletions docs/docs/advanced/write-plugins/quotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ export const ManualOffering: IQuotationAdapter = {
},
};
},

log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
},
};

```
Expand Down
6 changes: 0 additions & 6 deletions packages/api/src/api-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export type UnchainedServerOptions = {
adminUiConfig?: AdminUiConfig;
unchainedAPI: UnchainedCore;
context?: (defaultResolver: UnchainedContextResolver) => UnchainedContextResolver;
events: Array<string>;
workTypes: Array<string>;
} & GraphQLServerOptions;

export const startAPIServer = async (options: UnchainedServerOptions) => {
Expand All @@ -35,8 +33,6 @@ export const startAPIServer = async (options: UnchainedServerOptions) => {
context: customContext,
roles,
adminUiConfig = {},
events,
workTypes,
typeDefs: additionalTypeDefs = [],
resolvers: additionalResolvers = [],
...serverOptions
Expand All @@ -56,8 +52,6 @@ export const startAPIServer = async (options: UnchainedServerOptions) => {
typeDefs: [
...buildDefaultTypeDefs({
actions: Object.keys(actions),
events,
workTypes,
}),
...additionalTypeDefs,
],
Expand Down
15 changes: 14 additions & 1 deletion packages/api/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ import types from './types/index.js';
import inputTypes from './inputTypes.js';
import query from './query.js';
import mutation from './mutation.js';
import { getRegisteredEvents } from '@unchainedshop/events';
import { WorkerDirector } from '@unchainedshop/core';
import { createLogger } from '@unchainedshop/logger';

const logger = createLogger('unchained:api');

export const buildDefaultTypeDefs = ({ actions = [] }) => {
logger.debug(`${actions.length} role actions added to GraphQL schema`, { actions });

const events = getRegisteredEvents() || [];
logger.debug(`${events.length} event types added to GraphQL schema`, { events });

const workTypes = WorkerDirector.getActivePluginTypes() || [];
logger.debug(`${workTypes.length} work types added to GraphQL schema`, { workTypes });

export const buildDefaultTypeDefs = ({ actions = [], events = [], workTypes = [] }) => {
const dynamicTypeDefs = [
actions?.length && `extend enum RoleAction { ${actions.join(',')} }`,
events?.length && `extend enum EventType { ${events.join(',')} }`,
Expand Down
6 changes: 3 additions & 3 deletions packages/core-worker/src/module/configureWorkerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const configureWorkerModule = async ({

const duration = new Date(work.finished).getTime() - new Date(work.started).getTime();
if (work.success) {
logger.info(`${work.type} finished with success (${duration}ms)`, {
logger.debug(`${work.type} finished with success (${duration}ms)`, {
workId,
worker,
});
Expand Down Expand Up @@ -422,7 +422,7 @@ export const configureWorkerModule = async ({
worker,
});

logger.info(`${type} scheduled @ ${new Date(scheduled || created).toISOString()}`, {
logger.debug(`${type} scheduled @ ${new Date(scheduled || created).toISOString()}`, {
workId,
});

Expand Down Expand Up @@ -532,7 +532,7 @@ export const configureWorkerModule = async ({
);

if (!result.lastErrorObject.updatedExisting) {
logger.info(`${type} auto-scheduled @ ${new Date(scheduled).toISOString()}`, {
logger.debug(`${type} auto-scheduled @ ${new Date(scheduled).toISOString()}`, {
workId,
});
emit(WorkerEventTypes.ADDED, removePrivateFields(result.value));
Expand Down
48 changes: 48 additions & 0 deletions packages/core/src/core-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import { mongodb, MigrationRepository, ModuleInput } from '@unchainedshop/mongod
import initServices, { CustomServices, Services } from './services/index.js';
import initModules, { Modules, ModuleOptions } from './modules.js';

import {
WorkerDirector,
DeliveryDirector,
DeliveryPricingDirector,
EnrollmentDirector,
FilterDirector,
OrderDiscountDirector,
OrderPricingDirector,
PaymentDirector,
PaymentPricingDirector,
ProductDiscountDirector,
ProductPricingDirector,
QuotationDirector,
WarehousingDirector,
} from './directors/index.js';

export * from './services/index.js';
export * from './directors/index.js';

Expand Down Expand Up @@ -50,3 +66,35 @@ export const initCore = async ({
options,
};
};

export const getAllAdapters = () => {
const worker = WorkerDirector.getAdapters();
const delivery = DeliveryDirector.getAdapters();
const deliveryPricing = DeliveryPricingDirector.getAdapters();
const enrollment = EnrollmentDirector.getAdapters();
const filter = FilterDirector.getAdapters();
const orderDiscount = OrderDiscountDirector.getAdapters();
const orderPricing = OrderPricingDirector.getAdapters();
const payment = PaymentDirector.getAdapters();
const paymentPricing = PaymentPricingDirector.getAdapters();
const productDiscount = ProductDiscountDirector.getAdapters();
const productPricing = ProductPricingDirector.getAdapters();
const quotation = QuotationDirector.getAdapters();
const warehousing = WarehousingDirector.getAdapters();

return [].concat(
worker,
delivery,
deliveryPricing,
enrollment,
filter,
orderDiscount,
orderPricing,
payment,
paymentPricing,
productDiscount,
productPricing,
quotation,
warehousing,
);
};
8 changes: 2 additions & 6 deletions packages/core/src/directors/BaseDiscountAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { IBaseAdapter } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';
import { IPricingSheet } from './BasePricingSheet.js';
import { PricingCalculation } from '@unchainedshop/utils';
import { Order, OrderDiscount } from '@unchainedshop/core-orders';
Expand Down Expand Up @@ -35,6 +34,7 @@ export interface DiscountAdapterActions<DiscountConfiguration> {
}

export const BaseDiscountAdapter: Omit<IDiscountAdapter<unknown>, 'key' | 'label' | 'version'> = {
...BaseAdapter,
orderIndex: 0,

isManualAdditionAllowed: async () => {
Expand Down Expand Up @@ -83,8 +83,4 @@ export const BaseDiscountAdapter: Omit<IDiscountAdapter<unknown>, 'key' | 'label
return null;
},
}),

log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
},
};
10 changes: 2 additions & 8 deletions packages/core/src/directors/BasePricingAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { createLogger } from '@unchainedshop/logger';
import { IPricingSheet } from './BasePricingSheet.js';
import { IBaseAdapter, PricingCalculation } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter, PricingCalculation } from '@unchainedshop/utils';
import { Discount } from './BasePricingDirector.js';
import { Order, OrderDiscount } from '@unchainedshop/core-orders';

const logger = createLogger('unchained:core');

interface IDiscountableItem {
_id?: string;
orderId?: string;
Expand Down Expand Up @@ -54,6 +51,7 @@ export const BasePricingAdapter = <
PricingAdapterContext extends BasePricingAdapterContext,
Calculation extends PricingCalculation,
>(): IPricingAdapter<PricingAdapterContext, Calculation, IPricingSheet<Calculation>> => ({
...BaseAdapter,
key: '',
label: '',
version: '',
Expand All @@ -73,8 +71,4 @@ export const BasePricingAdapter = <
}, // abstract
};
},

log(message: string, options) {
return logger.debug(message, options);
},
});
8 changes: 2 additions & 6 deletions packages/core/src/directors/DeliveryAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { IBaseAdapter } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';
import type { Order, OrderPosition, OrderDelivery } from '@unchainedshop/core-orders';
import type { Product } from '@unchainedshop/core-products';
import type { WarehousingProvider } from '@unchainedshop/core-warehousing';
Expand Down Expand Up @@ -57,6 +56,7 @@ export type IDeliveryAdapter = IBaseAdapter & {
};

export const DeliveryAdapter: Omit<IDeliveryAdapter, 'key' | 'label' | 'version'> = {
...BaseAdapter,
initialConfiguration: [],

typeSupported: () => {
Expand Down Expand Up @@ -103,8 +103,4 @@ export const DeliveryAdapter: Omit<IDeliveryAdapter, 'key' | 'label' | 'version'
},
};
},

log: (message: string, { level = LogLevel.Debug, ...options } = {}) => {
return log(message, { level, ...options });
},
};
9 changes: 2 additions & 7 deletions packages/core/src/directors/EnrollmentAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IBaseAdapter } from '@unchainedshop/utils';
import { log, LogLevel } from '@unchainedshop/logger';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';
import {
Enrollment,
EnrollmentPeriod,
Expand Down Expand Up @@ -52,6 +51,7 @@ export const periodForReferenceDate = (referenceDate: Date, intervalCount = 1, i
};

export const EnrollmentAdapter: Omit<IEnrollmentAdapter, 'key' | 'label' | 'version'> = {
...BaseAdapter,
isActivatedFor: () => {
return false;
},
Expand Down Expand Up @@ -105,9 +105,4 @@ export const EnrollmentAdapter: Omit<IEnrollmentAdapter, 'key' | 'label' | 'vers
},
};
},

// eslint-disable-next-line
log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
},
};
8 changes: 2 additions & 6 deletions packages/core/src/directors/FilterAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { Assortment } from '@unchainedshop/core-assortments';
import { mongodb } from '@unchainedshop/mongodb';
import { IBaseAdapter } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';
import { Product } from '@unchainedshop/core-products';
import { Filter, SearchQuery } from '@unchainedshop/core-filters';
import { Modules } from '../modules.js';
Expand Down Expand Up @@ -63,6 +62,7 @@ export type IFilterAdapter = IBaseAdapter & {
};

export const FilterAdapter: Omit<IFilterAdapter, 'key' | 'label' | 'version'> = {
...BaseAdapter,
orderIndex: 0,

actions: () => {
Expand Down Expand Up @@ -96,8 +96,4 @@ export const FilterAdapter: Omit<IFilterAdapter, 'key' | 'label' | 'version'> =
},
};
},

log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
},
};
17 changes: 5 additions & 12 deletions packages/core/src/directors/MessagingDirector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { log } from '@unchainedshop/logger';

export type EmailTemplateType = {
type: 'EMAIL';
input: {
Expand Down Expand Up @@ -41,19 +39,14 @@ export type TemplateResolver<T = { [x: string]: any }> = (
unchainedAPI,
) => Promise<Array<EmailTemplateType | SMSTemplateType | ArbitraryTemplateType>>;

export type IMessagingDirector = {
registerTemplate: (templateName: string, templateResolver: TemplateResolver) => void;
getTemplate: (templateName: string) => TemplateResolver;
};

const TemplateResolvers = new Map<string, TemplateResolver>();

export const MessagingDirector: IMessagingDirector = {
registerTemplate: (templateName, templateResolver) => {
log(`MessagingDirector -> Registered template resolver for ${templateName}`);

export const MessagingDirector = {
registerTemplate: (templateName: string, templateResolver: TemplateResolver) => {
TemplateResolvers.set(templateName, templateResolver);
},

getTemplate: (templateName) => TemplateResolvers.get(templateName),
getTemplate: (templateName: string) => TemplateResolvers.get(templateName),

getRegisteredTemplates: () => Array.from(TemplateResolvers.keys()),
};
8 changes: 2 additions & 6 deletions packages/core/src/directors/PaymentAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { IBaseAdapter } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';
import { Order, OrderPayment } from '@unchainedshop/core-orders';
import { PaymentConfiguration, PaymentProvider, PaymentProviderType } from '@unchainedshop/core-payment';
import { Modules } from '../modules.js';
Expand Down Expand Up @@ -58,6 +57,7 @@ export type IPaymentAdapter = IBaseAdapter & {
};

export const PaymentAdapter: Omit<IPaymentAdapter, 'key' | 'label' | 'version'> = {
...BaseAdapter,
initialConfiguration: [],

typeSupported: () => {
Expand Down Expand Up @@ -111,8 +111,4 @@ export const PaymentAdapter: Omit<IPaymentAdapter, 'key' | 'label' | 'version'>
},
};
},

log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
},
};
8 changes: 2 additions & 6 deletions packages/core/src/directors/QuotationAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
QuotationItemConfiguration,
QuotationProposal,
} from '@unchainedshop/core-quotations';
import { log, LogLevel } from '@unchainedshop/logger';
import { IBaseAdapter } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';

export type QuotationContext = {
quotation?: Quotation;
Expand Down Expand Up @@ -38,6 +37,7 @@ export enum QuotationError {
}

export const QuotationAdapter: Omit<IQuotationAdapter, 'key' | 'label' | 'version'> = {
...BaseAdapter,
orderIndex: 0,

isActivatedFor: () => {
Expand Down Expand Up @@ -79,8 +79,4 @@ export const QuotationAdapter: Omit<IQuotationAdapter, 'key' | 'label' | 'versio
},
};
},

log(message: string, { level = LogLevel.Debug, ...options } = {}) {
return log(message, { level, ...options });
},
};
9 changes: 2 additions & 7 deletions packages/core/src/directors/WarehousingAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { log, LogLevel } from '@unchainedshop/logger';
import { IBaseAdapter } from '@unchainedshop/utils';
import { BaseAdapter, IBaseAdapter } from '@unchainedshop/utils';
import { DeliveryProvider } from '@unchainedshop/core-delivery';
import { Product } from '@unchainedshop/core-products';
import { Order, OrderPosition } from '@unchainedshop/core-orders';
Expand Down Expand Up @@ -48,6 +47,7 @@ export type IWarehousingAdapter = IBaseAdapter & {
};

export const WarehousingAdapter: Omit<IWarehousingAdapter, 'key' | 'label' | 'version'> = {
...BaseAdapter,
orderIndex: 0,

typeSupported: () => {
Expand Down Expand Up @@ -75,9 +75,4 @@ export const WarehousingAdapter: Omit<IWarehousingAdapter, 'key' | 'label' | 've
isInvalidateable: async () => true,
};
},

log(message, { level = LogLevel.Debug, ...options } = {}) {
// eslint-disable-line
return log(message, { level, ...options });
},
};
Loading

0 comments on commit 1a81a8d

Please sign in to comment.