From 02e5e1d73b1ca27ca3b2ff4ed65d700dd15a5160 Mon Sep 17 00:00:00 2001 From: Mikael Araya Date: Wed, 27 Nov 2024 22:52:37 +0300 Subject: [PATCH] Move deleteUserCartService to orders service following new pattern --- .../module/configureOrdersModule-mutations.ts | 1 - .../src/module/configureOrdersModule.ts | 20 ------------------- .../src/module/configureUsersModule.ts | 4 ++-- .../src/services/deleteUserCartService.ts | 17 ++++++++++++++++ packages/core/src/services/index.ts | 2 ++ 5 files changed, 21 insertions(+), 23 deletions(-) create mode 100644 packages/core/src/services/deleteUserCartService.ts diff --git a/packages/core-orders/src/module/configureOrdersModule-mutations.ts b/packages/core-orders/src/module/configureOrdersModule-mutations.ts index d0d248ff23..8f86b46361 100644 --- a/packages/core-orders/src/module/configureOrdersModule-mutations.ts +++ b/packages/core-orders/src/module/configureOrdersModule-mutations.ts @@ -31,7 +31,6 @@ export interface OrderMutations { updateContact: (orderId: string, contact: Contact) => Promise; updateContext: (orderId: string, context: any) => Promise; updateCalculationSheet: (orderId: string, calculation) => Promise; - deleteUserOrders: (userId: string) => Promise; } const ORDER_EVENTS: string[] = [ diff --git a/packages/core-orders/src/module/configureOrdersModule.ts b/packages/core-orders/src/module/configureOrdersModule.ts index c0aadc19ad..024a184147 100644 --- a/packages/core-orders/src/module/configureOrdersModule.ts +++ b/packages/core-orders/src/module/configureOrdersModule.ts @@ -25,12 +25,6 @@ export type OrdersModule = OrderQueries & OrderTransformations & OrderProcessing & OrderMutations & { - // Order context recalculations - initProviders: (order: Order, unchainedAPI) => Promise; - updateCalculation: (orderId: string, unchainedAPI) => Promise; - invalidateProviders: (unchainedAPI, maxAgeDays: number) => Promise; - deleteUserCart: (userId: string) => Promise; - // Sub entities deliveries: OrderDeliveriesModule; discounts: OrderDiscountsModule; @@ -97,20 +91,6 @@ export const configureOrdersModule = async ({ OrderDeliveries, }); - const deleteUserCart = async (userId: string) => { - try { - const userCart = await Orders.findOne({ status: null, userId }); - await orderMutations.delete(userCart?._id); - await orderPaymentsModule.deleteOrderPayment(userCart?._id); - await orderDeliveriesModule.deleteOrderDelivery(userCart?._id); - await orderDiscountsModule.deleteOrderDiscounts(userCart?._id); - return true; - } catch (e) { - console.error(e); - return false; - } - }; - return { ...orderQueries, ...orderTransformations, diff --git a/packages/core-users/src/module/configureUsersModule.ts b/packages/core-users/src/module/configureUsersModule.ts index 36534b647e..bc0ff2efbf 100644 --- a/packages/core-users/src/module/configureUsersModule.ts +++ b/packages/core-users/src/module/configureUsersModule.ts @@ -628,7 +628,7 @@ export const configureUsersModule = async ({ params: { userId: string; removeUserReviews?: boolean }, context: Context, ): Promise => { - const { modules } = context as UnchainedCore; + const { services, modules } = context as UnchainedCore; const { userId, removeUserReviews = false } = params; const userFilter = generateDbFilterById(userId); @@ -640,7 +640,7 @@ export const configureUsersModule = async ({ await updateUser({ _id: userId }, { $set: { ...maskedUserData, deleted: new Date() } }, {}); await modules.bookmarks.deleteByUserId(userId); - await modules.orders.deleteUserCart(userId); + await services.orders.deleteUserCart(userId, context as UnchainedCore); await modules.quotations.deleteRequestedUserQuotations(userId); await modules.enrollments.deleteOpenUserEnrollments(userId); if (removeUserReviews) await modules.products.reviews.deleteMany({ authorId: userId }); diff --git a/packages/core/src/services/deleteUserCartService.ts b/packages/core/src/services/deleteUserCartService.ts new file mode 100644 index 0000000000..bfc1caddd5 --- /dev/null +++ b/packages/core/src/services/deleteUserCartService.ts @@ -0,0 +1,17 @@ +import { UnchainedCore } from '../core-index.js'; + +const deleteUserCartService = async (userId: string, unchainedAPI: UnchainedCore) => { + try { + const userCart = await unchainedAPI.modules.orders.cart({ userId }); + await unchainedAPI.modules.orders.delete(userCart?._id); + await unchainedAPI.modules.orders.payments.deleteOrderPayment(userCart?._id); + await unchainedAPI.modules.orders.deliveries.deleteOrderDelivery(userCart?._id); + await unchainedAPI.modules.orders.discounts.deleteOrderDiscounts(userCart?._id); + return true; + } catch (e) { + console.error(e); + return false; + } +}; + +export default deleteUserCartService; diff --git a/packages/core/src/services/index.ts b/packages/core/src/services/index.ts index 657e1fa1c9..7d267c47ee 100644 --- a/packages/core/src/services/index.ts +++ b/packages/core/src/services/index.ts @@ -12,6 +12,7 @@ import { nextUserCartService } from './nextUserCartService.js'; import { removeProductService } from './removeProductService.js'; import { initCartProvidersService } from './initCartProviders.js'; import { updateCalculationService } from './updateCalculationService.js'; +import deleteUserCartService from './deleteUserCartService.js'; const services = { bookmarks: { @@ -30,6 +31,7 @@ const services = { nextUserCart: nextUserCartService, initCartProviders: initCartProvidersService, updateCalculation: updateCalculationService, + deleteUserCart: deleteUserCartService, }, products: { removeProduct: removeProductService,