Skip to content

Commit

Permalink
Move deleteUserCartService to orders service following new pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikearaya committed Nov 27, 2024
1 parent f62d434 commit 02e5e1d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export interface OrderMutations {
updateContact: (orderId: string, contact: Contact) => Promise<Order>;
updateContext: (orderId: string, context: any) => Promise<Order>;
updateCalculationSheet: (orderId: string, calculation) => Promise<Order>;
deleteUserOrders: (userId: string) => Promise<number>;
}

const ORDER_EVENTS: string[] = [
Expand Down
20 changes: 0 additions & 20 deletions packages/core-orders/src/module/configureOrdersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ export type OrdersModule = OrderQueries &
OrderTransformations &
OrderProcessing &
OrderMutations & {
// Order context recalculations
initProviders: (order: Order, unchainedAPI) => Promise<Order>;
updateCalculation: (orderId: string, unchainedAPI) => Promise<Order>;
invalidateProviders: (unchainedAPI, maxAgeDays: number) => Promise<void>;
deleteUserCart: (userId: string) => Promise<boolean>;

// Sub entities
deliveries: OrderDeliveriesModule;
discounts: OrderDiscountsModule;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-users/src/module/configureUsersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ export const configureUsersModule = async ({
params: { userId: string; removeUserReviews?: boolean },
context: Context,
): Promise<User> => {
const { modules } = context as UnchainedCore;
const { services, modules } = context as UnchainedCore;
const { userId, removeUserReviews = false } = params;
const userFilter = generateDbFilterById(userId);

Expand All @@ -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 });
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/services/deleteUserCartService.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -30,6 +31,7 @@ const services = {
nextUserCart: nextUserCartService,
initCartProviders: initCartProvidersService,
updateCalculation: updateCalculationService,
deleteUserCart: deleteUserCartService,
},
products: {
removeProduct: removeProductService,
Expand Down

0 comments on commit 02e5e1d

Please sign in to comment.