Skip to content

Commit

Permalink
Free utils from core dependencies, starting to move services to core
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 22, 2024
1 parent 19ae1dc commit b79819d
Show file tree
Hide file tree
Showing 30 changed files with 110 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"dev:watch-packages": "workspaces-run --ignore '@unchainedshop/shared' --only-fs 'packages/**' --parallel -- npm run watch",
"update-version": "npm version --no-git-tag-version --workspaces --include-workspace-root",
"clean": "npm run clean --workspaces",
"build": "npm run build --workspaces",
"build": "npm run prepublishOnly --workspaces --if-present",
"test:unit": "node --experimental-vm-modules node_modules/jest/bin/jest.js --ci --forceExit --config ./packages/unit-jest.config.js",
"test:unit-watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --detectOpenHandles --watch --config ./packages/unit-jest.config.js"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { DeliveryProvider } from '../types.js';
import { OrderDelivery, OrderDiscount, Order } from '@unchainedshop/core-orders';
import { User } from '@unchainedshop/core-users';
import { UnchainedCore } from '@unchainedshop/core';

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

export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext {
export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext, UnchainedCore {
country?: string;
currency?: string;
provider: DeliveryProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IDeliveryPricingAdapter,
IDeliveryPricingSheet,
} from './DeliveryPricingAdapter.js';
import { UnchainedCore } from '@unchainedshop/core';

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

const baseDirector = BasePricingDirector<
Expand Down
3 changes: 2 additions & 1 deletion packages/core-orders/src/director/OrderDiscountAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BaseDiscountAdapter, IDiscountAdapter } from '@unchainedshop/utils';
import { OrderDiscountConfiguration } from './OrderDiscountConfiguration.js';
import { UnchainedCore } from '@unchainedshop/core';

export const OrderDiscountAdapter: Omit<
IDiscountAdapter<OrderDiscountConfiguration>,
IDiscountAdapter<OrderDiscountConfiguration, UnchainedCore>,
'key' | 'label' | 'version'
> = BaseDiscountAdapter;
6 changes: 4 additions & 2 deletions packages/core-orders/src/director/OrderDiscountDirector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { UnchainedCore } from '@unchainedshop/core';
import { BaseDiscountDirector } from '@unchainedshop/utils';
import { OrderDiscountConfiguration } from './OrderDiscountConfiguration.js';

export const OrderDiscountDirector =
BaseDiscountDirector<OrderDiscountConfiguration>('OrderDiscountDirector');
export const OrderDiscountDirector = BaseDiscountDirector<OrderDiscountConfiguration, UnchainedCore>(
'OrderDiscountDirector',
);
3 changes: 2 additions & 1 deletion packages/core-orders/src/director/OrderPricingAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { UnchainedCore } from '@unchainedshop/core';
import { BasePricingAdapter, IPricingAdapter, BasePricingAdapterContext } from '@unchainedshop/utils';
import { IOrderPricingSheet, OrderPricingCalculation, OrderPricingSheet } from './OrderPricingSheet.js';
import { Order, OrderDelivery, OrderDiscount, OrderPayment, OrderPosition } from '../types.js';
import { User } from '@unchainedshop/core-users';

export interface OrderPricingAdapterContext extends BasePricingAdapterContext {
export interface OrderPricingAdapterContext extends BasePricingAdapterContext, UnchainedCore {
currency?: string;
discounts: Array<OrderDiscount>;
order: Order;
Expand Down
4 changes: 3 additions & 1 deletion packages/core-orders/src/director/OrderPricingDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OrderPricingAdapterContext,
OrderPricingContext,
} from './OrderPricingAdapter.js';
import { UnchainedCore } from '@unchainedshop/core';

export type OrderPrice = { _id?: string; amount: number; currency: string };

Expand All @@ -21,7 +22,8 @@ export type IOrderPricingDirector<DiscountConfiguration = unknown> = IPricingDir
OrderPricingCalculation,
OrderPricingAdapterContext,
IOrderPricingSheet,
IOrderPricingAdapter<DiscountConfiguration>
IOrderPricingAdapter<DiscountConfiguration>,
UnchainedCore
>;

const baseDirector = BasePricingDirector<
Expand Down
3 changes: 2 additions & 1 deletion packages/core-payment/src/director/PaymentPricingAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
IPricingSheet,
PricingCalculation,
} from '@unchainedshop/utils';
import { UnchainedCore } from '@unchainedshop/core';
import { BasePricingAdapter } from '@unchainedshop/utils';
import { PaymentPricingSheet } from './PaymentPricingSheet.js';
import { User } from '@unchainedshop/core-users';
Expand All @@ -18,7 +19,7 @@ export interface PaymentPricingCalculation extends PricingCalculation {
isNetPrice: boolean;
rate?: number;
}
export interface PaymentPricingAdapterContext extends BasePricingAdapterContext {
export interface PaymentPricingAdapterContext extends BasePricingAdapterContext, UnchainedCore {
country?: string;
currency?: string;
user: User;
Expand Down
4 changes: 3 additions & 1 deletion packages/core-payment/src/director/PaymentPricingDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Order } from '@unchainedshop/core-orders';
import { PaymentProvider } from '../types.js';
import { OrderPayment } from '@unchainedshop/core-orders';
import { IPricingDirector } from '@unchainedshop/utils';
import { UnchainedCore } from '@unchainedshop/core';

export type PaymentPricingContext =
| {
Expand All @@ -30,7 +31,8 @@ export type IPaymentPricingDirector<DiscountConfiguration = unknown> = IPricingD
PaymentPricingCalculation,
PaymentPricingAdapterContext,
IPaymentPricingSheet,
IPaymentPricingAdapter<DiscountConfiguration>
IPaymentPricingAdapter<DiscountConfiguration>,
UnchainedCore
>;

const baseDirector = BasePricingDirector<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BaseDiscountAdapter, IDiscountAdapter } from '@unchainedshop/utils';
import { ProductDiscountConfiguration } from './ProductDiscountConfiguration.js';
import { UnchainedCore } from '@unchainedshop/core';

export const ProductDiscountAdapter: Omit<
IDiscountAdapter<ProductDiscountConfiguration>,
IDiscountAdapter<ProductDiscountConfiguration, UnchainedCore>,
'key' | 'label' | 'version'
> = BaseDiscountAdapter;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { UnchainedCore } from '@unchainedshop/core';
import { BaseDiscountDirector } from '@unchainedshop/utils';
import { ProductDiscountConfiguration } from './ProductDiscountConfiguration.js';

export const ProductDiscountDirector =
BaseDiscountDirector<ProductDiscountConfiguration>('ProductDiscountDirector');
export const ProductDiscountDirector = BaseDiscountDirector<ProductDiscountConfiguration, UnchainedCore>(
'ProductDiscountDirector',
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../types.js';
import { BasePricingDirector } from '@unchainedshop/utils';
import { ProductPricingSheet } from './ProductPricingSheet.js';
import { UnchainedCore } from '@unchainedshop/core';

const baseDirector = BasePricingDirector<
ProductPricingContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export type ProductsModule = {
productExists: (params: { productId?: string; slug?: string }) => Promise<boolean>;

// Transformations
interface: (productDiscount: ProductDiscount) => IDiscountAdapter<unknown>;
interface: (productDiscount: ProductDiscount) => IDiscountAdapter<unknown, UnchainedCore>;

isActive: (product: Product) => boolean;
isDraft: (product: Product) => boolean;
Expand Down
6 changes: 4 additions & 2 deletions packages/core-products/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TimestampFields, mongodb } from '@unchainedshop/mongodb';
import { OrderDiscount } from '@unchainedshop/core-orders';
import { OrderPosition } from '@unchainedshop/core-orders';
import { OrderPrice } from '@unchainedshop/core-orders';
import { UnchainedCore } from '@unchainedshop/core';
import {
BasePricingAdapterContext,
IPricingAdapter,
Expand Down Expand Up @@ -234,7 +235,7 @@ export interface ProductPricingCalculation extends PricingCalculation {
rate?: number;
}

export interface ProductPricingAdapterContext extends BasePricingAdapterContext {
export interface ProductPricingAdapterContext extends BasePricingAdapterContext, UnchainedCore {
country: string;
currency: string;
product: Product;
Expand Down Expand Up @@ -295,7 +296,8 @@ export type IProductPricingDirector<DiscountConfiguration = unknown> = IPricingD
ProductPricingCalculation,
ProductPricingAdapterContext,
IProductPricingSheet,
IProductPricingAdapter<DiscountConfiguration>
IProductPricingAdapter<DiscountConfiguration>,
UnchainedCore
>;

export type ProductPriceRate = {
Expand Down
1 change: 1 addition & 0 deletions packages/core-users/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@unchainedshop/logger": "^3.0.0-alpha4",
"@unchainedshop/roles": "^3.0.0-alpha4",
"@unchainedshop/utils": "^3.0.0-alpha4",
"@unchainedshop/mongodb": "^3.0.0-alpha4",
"bcryptjs": "^2.4.3",
"fido2-lib": "^3.5.3"
},
Expand Down
20 changes: 1 addition & 19 deletions packages/core-users/src/module/configureUsersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import {
mongodb,
generateDbObjectId,
} from '@unchainedshop/mongodb';
import { systemLocale } from '@unchainedshop/utils';
import { FileDirector } from '@unchainedshop/file-upload';
import { SortDirection, SortOption } from '@unchainedshop/utils';
import { systemLocale, SortDirection, SortOption } from '@unchainedshop/utils';
import { UsersCollection } from '../db/UsersCollection.js';
import addMigrations from './addMigrations.js';
import { userSettings, UserSettingsOptions } from '../users-settings.js';
import { configureUsersWebAuthnModule, UsersWebAuthnModule } from './configureUsersWebAuthnModule.js';
import * as pbkdf2 from './pbkdf2.js';
import * as sha256 from './sha256.js';
import { UserServices } from '../services/userServices.js';
import { FileServices, FilesModule } from '@unchainedshop/core-files';

export type UsersModule = {
// Submodules
Expand Down Expand Up @@ -126,20 +122,6 @@ export const buildFindSelector = ({ includeGuests, queryString, ...rest }: UserQ
return selector;
};

FileDirector.registerFileUploadCallback<{
services: {
users: UserServices;
files: FileServices;
};
modules: {
users: UsersModule;
files: FilesModule;
};
}>('user-avatars', async (file, context) => {
const { services } = context;
return services.users.updateUserAvatarAfterUpload({ file }, context);
});

export const configureUsersModule = async ({
db,
options,
Expand Down
15 changes: 0 additions & 15 deletions packages/core-users/src/services/userServices.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core-users/src/users-index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './types.js';
export * from './module/configureUsersModule.js';
export * from './services/userServices.js';
export * from './users-settings.js';
20 changes: 12 additions & 8 deletions packages/core/src/core-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,12 @@ import {
QuotationsModule,
QuotationsSettingsOptions,
} from '@unchainedshop/core-quotations';
import {
configureUsersModule,
UserServices,
userServices,
UserSettingsOptions,
UsersModule,
} from '@unchainedshop/core-users';
import { configureUsersModule, UserSettingsOptions, UsersModule } from '@unchainedshop/core-users';
import { configureWarehousingModule, WarehousingModule } from '@unchainedshop/core-warehousing';
import { configureWorkerModule, WorkerModule, WorkerSettingsOptions } from '@unchainedshop/core-worker';
import { mongodb, MigrationRepository, ModuleInput } from '@unchainedshop/mongodb';
import { migrateUserDataService } from './services/migrateUserDataService.js';
import { updateUserAvatarAfterUploadService } from './services/updateUserAvatarAfterUploadService.js';

export * from './types.js';

Expand Down Expand Up @@ -123,6 +119,11 @@ export interface Modules {
worker: WorkerModule;
}

export interface UserServices {
updateUserAvatarAfterUpload: typeof updateUserAvatarAfterUploadService;
migrateUserData: typeof migrateUserDataService;
}

export interface Services {
bookmarks: BookmarkServices;
files: FileServices;
Expand Down Expand Up @@ -270,7 +271,10 @@ export const initCore = async ({
files: fileServices,
orders: orderServices,
products: productServices,
users: userServices,
users: {
migrateUserData: migrateUserDataService,
updateUserAvatarAfterUpload: updateUserAvatarAfterUploadService,
},
...services,
},
bulkImporter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { UnchainedCore } from '@unchainedshop/core';
import { userSettings } from '../users-settings.js';
import { userSettings, UsersModule } from '@unchainedshop/core-users';

export type MigrateUserDataService = (
userIdBeforeLogin,
userId,
unchainedAPI: UnchainedCore,
unchainedAPI: {
modules: {
users: UsersModule;
};
services: {
orders: {
migrateOrderCarts: any;
};
bookmarks: {
migrateBookmarks: any;
};
};
},
) => Promise<void>;

export const migrateUserDataService: MigrateUserDataService = async (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { File, FileServices, FilesModule } from '@unchainedshop/core-files';
import { log, LogLevel } from '@unchainedshop/logger';
import { UsersModule } from '../users-index.js';
import { UsersModule } from '@unchainedshop/core-users/lib/users-index.js';

export type UpdateUserAvatarAfterUploadService = (
params: { file: File },
Expand Down
9 changes: 9 additions & 0 deletions packages/platform/src/setup/setupUploadHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { UnchainedCore } from '@unchainedshop/core';
import { FileDirector } from '../../../file-upload/lib/file-upload-index.js';

export const setupUploadHandlers = () => {
FileDirector.registerFileUploadCallback<UnchainedCore>('user-avatars', async (file, context) => {
const { services } = context;
return services.users.updateUserAvatarAfterUpload({ file }, context);
});
};
4 changes: 4 additions & 0 deletions packages/platform/src/startPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { WorkerDirector } from '@unchainedshop/core-worker';
import { BulkImportHandler, createBulkImporterFactory } from './bulk-importer/createBulkImporter.js';
import { runMigrations } from './migrations/runMigrations.js';
import { setupAccounts } from './setup/setupAccounts.js';
import { setupUploadHandlers } from './setup/setupUploadHandlers.js';
import { SetupCartsOptions, setupCarts } from './setup/setupCarts.js';
import { setupTemplates, MessageTypes } from './setup/setupTemplates.js';
import { SetupWorkqueueOptions, setupWorkqueue } from './setup/setupWorkqueue.js';
Expand Down Expand Up @@ -99,6 +100,9 @@ export const startPlatform = async ({
// Setup email templates
setupTemplates(unchainedAPI);

// Setup file upload handlers
setupUploadHandlers();

// Start the graphQL server
const graphqlHandler = await startAPIServer({
unchainedAPI,
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/src/pricing/discount-100-off.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
OrderDiscountAdapter,
OrderDiscountConfiguration,
} from '@unchainedshop/core-orders';
import { UnchainedCore } from '@unchainedshop/core';

export const HundredOff: IDiscountAdapter<OrderDiscountConfiguration> = {
export const HundredOff: IDiscountAdapter<OrderDiscountConfiguration, UnchainedCore> = {
...OrderDiscountAdapter,

key: 'shop.unchained.discount.100-off',
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/src/pricing/discount-half-price-manual.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UnchainedCore } from '@unchainedshop/core';
import { IDiscountAdapter } from '@unchainedshop/utils';
import { OrderDiscountDirector, OrderDiscountAdapter } from '@unchainedshop/core-orders';
import { ProductDiscountConfiguration } from '@unchainedshop/core-products';

export const HalfPriceManual: IDiscountAdapter<ProductDiscountConfiguration> = {
export const HalfPriceManual: IDiscountAdapter<ProductDiscountConfiguration, UnchainedCore> = {
...OrderDiscountAdapter,

key: 'shop.unchained.discount.half-price-manual',
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/src/pricing/discount-half-price.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UnchainedCore } from '@unchainedshop/core';
import { IDiscountAdapter } from '@unchainedshop/utils';
import { OrderDiscountDirector, OrderDiscountAdapter } from '@unchainedshop/core-orders';
import { ProductDiscountConfiguration } from '@unchainedshop/core-products';

export const HalfPrice: IDiscountAdapter<ProductDiscountConfiguration> = {
export const HalfPrice: IDiscountAdapter<ProductDiscountConfiguration, UnchainedCore> = {
...OrderDiscountAdapter,

key: 'shop.unchained.discount.half-price',
Expand Down
Loading

0 comments on commit b79819d

Please sign in to comment.