Skip to content

Commit

Permalink
Complete moving existing services, preparing for new services
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 22, 2024
1 parent c736d3d commit 239b4ed
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
1 change: 0 additions & 1 deletion packages/core-products/src/products-index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './types.js';
export * from './module/configureProductsModule.js';
export * from './service/productServices.js';
export * from './products-settings.js';

export { ProductPricingAdapter } from './director/ProductPricingAdapter.js';
Expand Down
7 changes: 0 additions & 7 deletions packages/core-products/src/service/productServices.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/core-products/src/service/removeProductService.ts

This file was deleted.

11 changes: 8 additions & 3 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { productServices, ProductServices } from '@unchainedshop/core-products';

import { migrateUserDataService } from './migrateUserDataService.js';
import { updateUserAvatarAfterUploadService } from './updateUserAvatarAfterUploadService.js';
import { linkFileService } from './linkFileService.js';
Expand All @@ -11,6 +9,7 @@ import { createDownloadStreamService } from './createDownloadStreamService.js';
import { migrateBookmarksService } from './migrateBookmarksService.js';
import { migrateOrderCartsService } from './migrateOrderCartService.js';
import { nextUserCartService } from './nextUserCartService.js';
import { removeProductService } from './removeProductService.js';

export interface UserServices {
updateUserAvatarAfterUpload: typeof updateUserAvatarAfterUploadService;
Expand All @@ -35,6 +34,10 @@ export interface OrderServices {
nextUserCart: typeof nextUserCartService;
}

export interface ProductServices {
removeProduct: typeof removeProductService;
}

export interface Services {
bookmarks: BookmarkServices;
files: FileServices;
Expand All @@ -59,7 +62,9 @@ export default {
migrateOrderCarts: migrateOrderCartsService,
nextUserCart: nextUserCartService,
},
products: productServices,
products: {
removeProduct: removeProductService,
},
users: {
migrateUserData: migrateUserDataService,
updateUserAvatarAfterUpload: updateUserAvatarAfterUploadService,
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/services/removeProductService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { AssortmentsModule } from '@unchainedshop/core-assortments';
import { BookmarksModule } from '@unchainedshop/core-bookmarks';
import { OrdersModule } from '@unchainedshop/core-orders';
import { ProductsModule, ProductStatus } from '@unchainedshop/core-products';

export type RemoveProductService = (
params: { productId: string },
unchainedAPI: {
modules: {
products: ProductsModule;
bookmarks: BookmarksModule;
assortments: AssortmentsModule;
orders: OrdersModule;
};
},
) => Promise<boolean>;

export const removeProductService: RemoveProductService = async ({ productId }, unchainedAPI) => {
const { modules } = unchainedAPI;
const product = await modules.products.findProduct({ productId });
switch (product.status) {
case ProductStatus.ACTIVE:
await modules.products.unpublish(product);
// falls through
case null:
case ProductStatus.DRAFT:
{
await modules.bookmarks.deleteByProductId(productId);
await modules.assortments.products.delete(productId);
const orderIdsToRecalculate =
await modules.orders.positions.removeProductByIdFromAllOpenPositions(productId);
await Promise.all(
[...new Set(orderIdsToRecalculate)].map(async (orderIdToRecalculate) => {
await modules.orders.updateCalculation(orderIdToRecalculate, unchainedAPI as any);
}),
);
await modules.products.delete(productId);
}
break;
default:
throw new Error(`Invalid status', ${product.status}`);
}

return true;
};

0 comments on commit 239b4ed

Please sign in to comment.