Skip to content

Commit

Permalink
The typescript problem with bi-directional type dependencies has been…
Browse files Browse the repository at this point in the history
… resolved only leaving out core-delivery, core-payment and core-products
  • Loading branch information
pozylon committed Nov 26, 2024
1 parent ce3dd9b commit 9b2b53c
Show file tree
Hide file tree
Showing 32 changed files with 337 additions and 325 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Unchained Engine vNEXT

- The order module function `initProviders` has been moved to order services renamed as `initCartProviders`
- The order module function `updateCalculation` has been moved to order services
- The order module function `invalidateProviders` has been removed, the caller now uses the new `findCartsToInvalidate` to get the list of carts and then calls the new updateCalculation service


## Removing the auth fat of unchained
We experienced feature creep in the authentication part of Unchained and suddenly woke up to homemade implementations of Two-Factor Auth via TOTP, WebAuthn, oAuth, Impersonator features etc. Many solutions like Zitadel, Keycloak, Auth0 etc. solve that just perfect and keep up with the ever increasing complexity of auth mechanisms. At the same time, core-accountsjs depends on a package called accountsjs which is unmaintained and uses a conflicting old mongodb driver.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function addCartDiscount(
{ orderId, code }: { orderId?: string; code: string },
context: Context,
) {
const { modules, user, userId } = context;
const { modules, services, user, userId } = context;

log(`mutation addCartDiscount ${code} ${orderId}`, { userId, orderId });

Expand All @@ -17,6 +17,6 @@ export default async function addCartDiscount(
if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });

const discount = await modules.orders.discounts.createManualOrderDiscount({ order, code }, context);
await modules.orders.updateCalculation(order._id, context);
await services.orders.updateCalculation(order._id, context);
return discount;
}
4 changes: 2 additions & 2 deletions packages/api/src/resolvers/mutations/orders/addCartProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function addCartProduct(
{ orderId, productId, quantity, configuration },
context: Context,
) {
const { modules, userId, user } = context;
const { modules, services, userId, user } = context;

log(
`mutation addCartProduct ${productId} ${quantity} ${
Expand All @@ -39,6 +39,6 @@ export default async function addCartProduct(
{ order, product },
context,
);
await modules.orders.updateCalculation(order._id, context);
await services.orders.updateCalculation(order._id, context);
return modules.orders.positions.findOrderPosition({ itemId: orderPosition._id });
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function addCartQuotation(
},
context: Context,
) {
const { modules, userId, user } = context;
const { modules, services, userId, user } = context;
const { orderId, quotationId, quantity, configuration } = params;

log(
Expand Down Expand Up @@ -66,6 +66,6 @@ export default async function addCartQuotation(
{ order, product },
context,
);
await modules.orders.updateCalculation(order._id, context);
await services.orders.updateCalculation(order._id, context);
return modules.orders.positions.findOrderPosition({ itemId: updatedOrderPosition._id });
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function addMultipleCartProducts(
},
context: Context,
) {
const { modules, userId, user } = context;
const { modules, services, userId, user } = context;
const { orderId, items } = params;

log(`mutation addMultipleCartProducts ${JSON.stringify(items)}`, {
Expand Down Expand Up @@ -64,5 +64,5 @@ export default async function addMultipleCartProducts(
return positions;
}, Promise.resolve([]));

return modules.orders.updateCalculation(order._id, context);
return services.orders.updateCalculation(order._id, context);
}
4 changes: 2 additions & 2 deletions packages/api/src/resolvers/mutations/orders/emptyCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function emptyCart(
{ orderId }: { orderId?: string },
context: Context,
) {
const { modules, userId, user } = context;
const { modules, services, userId, user } = context;

log('mutation emptyCart', { userId, orderId });

Expand All @@ -18,5 +18,5 @@ export default async function emptyCart(
if (!order) return null;

await modules.orders.positions.removePositions({ orderId: order._id });
return modules.orders.updateCalculation(order._id, context);
return services.orders.updateCalculation(order._id, context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function removeCartDiscount(
{ discountId }: { discountId: string },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;

log(`mutation removeCartDiscount ${discountId}`, { userId });

Expand All @@ -27,6 +27,6 @@ export default async function removeCartDiscount(
}

const deletedDiscount = await modules.orders.discounts.delete(discountId, context);
await modules.orders.updateCalculation(order._id, context);
await services.orders.updateCalculation(order._id, context);
return deletedDiscount;
}
4 changes: 2 additions & 2 deletions packages/api/src/resolvers/mutations/orders/removeCartItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function removeCartItem(
{ itemId }: { itemId: string },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;

log(`mutation removeCartItem ${itemId}`, { userId });

Expand All @@ -25,6 +25,6 @@ export default async function removeCartItem(
}

const removedOrderPosition = await modules.orders.positions.delete(itemId);
await modules.orders.updateCalculation(order._id, context);
await services.orders.updateCalculation(order._id, context);
return removedOrderPosition;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function setOrderDeliveryProvider(
params: { orderId: string; deliveryProviderId: string },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
const { orderId, deliveryProviderId } = params;

log(`mutation setOrderDeliveryProvider ${deliveryProviderId}`, {
Expand All @@ -21,5 +21,5 @@ export default async function setOrderDeliveryProvider(
if (!(await modules.orders.orderExists({ orderId }))) throw new OrderNotFoundError({ orderId });

await modules.orders.setDeliveryProvider(orderId, deliveryProviderId, context);
return modules.orders.updateCalculation(orderId, context);
return services.orders.updateCalculation(orderId, context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function setOrderPaymentProvider(
params: { orderId: string; paymentProviderId: string },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
const { orderId, paymentProviderId } = params;

log(`mutation setOrderPaymentProvider ${paymentProviderId}`, {
Expand All @@ -22,5 +22,5 @@ export default async function setOrderPaymentProvider(
if (!order) throw new OrderNotFoundError({ orderId });

await modules.orders.setPaymentProvider(orderId, paymentProviderId, context);
return modules.orders.updateCalculation(orderId, context);
return services.orders.updateCalculation(orderId, context);
}
4 changes: 2 additions & 2 deletions packages/api/src/resolvers/mutations/orders/updateCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface UpdateCartParams {
export default async function updateCart(root: never, params: UpdateCartParams, context: Context) {
const { orderId, billingAddress, contact, paymentProviderId, deliveryProviderId, meta } = params;

const { modules, userId, user } = context;
const { modules, services, userId, user } = context;

log('mutation updateCart', { userId });

Expand Down Expand Up @@ -44,5 +44,5 @@ export default async function updateCart(root: never, params: UpdateCartParams,
}

// Recalculate, then return
return modules.orders.updateCalculation(order._id, context);
return services.orders.updateCalculation(order._id, context);
}
4 changes: 2 additions & 2 deletions packages/api/src/resolvers/mutations/orders/updateCartItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function updateCartItem(
},
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
const { itemId, configuration = null, quantity = null } = params;

log(`mutation updateCartItem ${itemId} ${quantity} ${JSON.stringify(configuration)}`, { userId });
Expand Down Expand Up @@ -49,6 +49,6 @@ export default async function updateCartItem(
},
context,
);
await modules.orders.updateCalculation(order._id, context);
await services.orders.updateCalculation(order._id, context);
return modules.orders.positions.findOrderPosition({ itemId: updatedOrderPosition._id });
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function updateOrderDeliveryPickUp(
params: { orderDeliveryId: string; orderPickUpLocationId: string; meta: any },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
const { orderDeliveryId, orderPickUpLocationId, meta } = params;
log(`mutation updateOrderDeliveryPickUp ${orderDeliveryId} with location ${orderPickUpLocationId}`, {
userId,
Expand Down Expand Up @@ -37,7 +37,7 @@ export default async function updateOrderDeliveryPickUp(
orderPickUpLocationId,
meta,
});
await modules.orders.updateCalculation(orderDelivery.orderId, context);
await services.orders.updateCalculation(orderDelivery.orderId, context);
return modules.orders.deliveries.findDelivery({
orderDeliveryId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function updateOrderDeliveryShipping(
params: { orderDeliveryId: string; address: any; meta: any },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
const { orderDeliveryId, address, meta } = params;

log(`mutation updateOrderDeliveryShipping ${orderDeliveryId}`, { userId });
Expand Down Expand Up @@ -36,7 +36,7 @@ export default async function updateOrderDeliveryShipping(
address,
meta,
});
await modules.orders.updateCalculation(orderDelivery.orderId, context);
await services.orders.updateCalculation(orderDelivery.orderId, context);
return modules.orders.deliveries.findDelivery({
orderDeliveryId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function updateOrderPaymentCard(
{ orderPaymentId, meta }: { orderPaymentId: string; meta?: any },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;

log(`mutation updateOrderPaymentCard ${orderPaymentId} ${JSON.stringify(meta)}`, { userId });

Expand All @@ -32,6 +32,6 @@ export default async function updateOrderPaymentCard(
});

await modules.orders.payments.updateContext(orderPayment._id, { meta });
await modules.orders.updateCalculation(orderPayment.orderId, context);
await services.orders.updateCalculation(orderPayment.orderId, context);
return modules.orders.payments.findOrderPayment({ orderPaymentId });
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function updateOrderPaymentGeneric(
{ orderPaymentId, meta }: { orderPaymentId: string; meta?: any },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
log(`mutation updateOrderPaymentGeneric ${orderPaymentId} ${JSON.stringify(meta)}`, {
userId,
});
Expand All @@ -33,6 +33,6 @@ export default async function updateOrderPaymentGeneric(
});

await modules.orders.payments.updateContext(orderPayment._id, { meta });
await modules.orders.updateCalculation(orderPayment.orderId, context);
await services.orders.updateCalculation(orderPayment.orderId, context);
return modules.orders.payments.findOrderPayment({ orderPaymentId });
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function updateOrderPaymentInvoice(
{ orderPaymentId, meta }: { orderPaymentId: string; meta?: any },
context: Context,
) {
const { modules, userId } = context;
const { modules, services, userId } = context;
log(`mutation updateOrderPaymentInvoice ${orderPaymentId} ${JSON.stringify(meta)}`, {
userId,
});
Expand All @@ -33,6 +33,6 @@ export default async function updateOrderPaymentInvoice(
});

await modules.orders.payments.updateContext(orderPayment._id, { meta });
await modules.orders.updateCalculation(orderPayment.orderId, context);
await services.orders.updateCalculation(orderPayment.orderId, context);
return modules.orders.payments.findOrderPayment({ orderPaymentId });
}
3 changes: 1 addition & 2 deletions packages/core-delivery/src/delivery-settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DeliveryProvider, FilterProviders } from './types.js';
import type { Order } from '@unchainedshop/core-orders';

export type DetermineDefaultProvider = (
export type DetermineDefaultProvider<Order = unknown> = (
params: {
providers: Array<DeliveryProvider>;
order: Order;
Expand Down
14 changes: 0 additions & 14 deletions packages/core-delivery/src/module/configureDeliveryModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,6 @@ export const configureDeliveryModule = async ({
);
},

determineDefault: async (
deliveryProviders: Array<DeliveryProvider>,
params: { order: Order },
unchainedAPI,
): Promise<DeliveryProvider> => {
return deliverySettings.determineDefaultProvider(
{
providers: deliveryProviders,
...params,
},
unchainedAPI,
);
},

configurationError: async (
deliveryProvider: DeliveryProvider,
unchainedAPI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export interface OrderMutations {

updateBillingAddress: (orderId: string, billingAddress: Address) => Promise<Order>;
updateContact: (orderId: string, contact: Contact) => Promise<Order>;
updateContext: (orderId: string, context: any) => Promise<Order | null>;
updateContext: (orderId: string, context: any) => Promise<Order>;
updateCalculationSheet: (orderId: string, calculation) => Promise<Order>;
}

const ORDER_EVENTS: string[] = [
Expand Down Expand Up @@ -206,6 +207,23 @@ export const configureOrderModuleMutations = ({
return order;
},

updateCalculationSheet: async (orderId, calculation) => {
const selector = generateDbFilterById(orderId);
const order = await Orders.findOneAndUpdate(
selector,
{
$set: {
calculation,
updated: new Date(),
},
},
{ returnDocument: 'after' },
);

await emit('ORDER_UPDATE', { order, field: 'calculation' });
return order;
},

updateContext: async (orderId, context) => {
const selector = generateDbFilterById<Order>(orderId);
selector.status = { $in: [null, OrderStatus.PENDING] };
Expand Down
13 changes: 13 additions & 0 deletions packages/core-orders/src/module/configureOrdersModule-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Colle
return Orders.findOne(selector, options);
},

findCartsToInvalidate: async (maxAgeDays = 30) => {
const ONE_DAY_IN_MILLISECONDS = 86400000;

const minValidDate = new Date(new Date().getTime() - maxAgeDays * ONE_DAY_IN_MILLISECONDS);

const orders = await Orders.find({
status: { $eq: null },
updated: { $gte: minValidDate },
}).toArray();

return orders;
},

findOrders: async (
{
limit,
Expand Down
Loading

0 comments on commit 9b2b53c

Please sign in to comment.