Skip to content

Commit

Permalink
Remove _id from price, pricerange, dispatch, stock (heavy weight with…
Browse files Browse the repository at this point in the history
…out reason)
  • Loading branch information
pozylon committed Dec 20, 2024
1 parent ca193f4 commit 63b38a0
Show file tree
Hide file tree
Showing 33 changed files with 29 additions and 258 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Along the way we thought it would be nice to remove about 100 NPM module depende
## BREAKING API CHANGES
Behavioral Change: Cart total are now null if there is no item in the cart and needs to be defaulted to an amount of 0 by the frontend. The reason for this change is that a free position could still have delivery or payment fees based on the article. So in order to communicate that to the frontend, we can't price an order when we don't know what is beeing ordered. As orders without a price can't be checked out it makes that clear to the client, too.

- `Price._id` **removed** (this only caused problems with caching behavior and added weight to the code)
- `Stock._id` **removed** (this only caused problems with caching behavior and added weight to the code)
- `Dispatch._id` **removed** (this only caused problems with caching behavior and added weight to the code)
- `PriceRange._id` **removed** (this only caused problems with caching behavior and added weight to the code)
- `Mutation.loginWithOAuth` removed
- `Mutation.linkOAuthAccount` removed
- `Mutation.unlinkOAuthAccount` removed
Expand Down
3 changes: 0 additions & 3 deletions packages/api/src/resolvers/type/delivery-provider-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Context } from '../../context.js';
import { DeliveryProvider as DeliveryProviderType } from '@unchainedshop/core-delivery';
import { DeliveryDirector, DeliveryError, DeliveryPricingDirector } from '@unchainedshop/core';
import { sha256 } from '@unchainedshop/utils';

export type HelperType<P, T> = (provider: DeliveryProviderType, params: P, context: Context) => T;

Expand All @@ -24,7 +23,6 @@ export interface DeliveryProviderHelperTypes {
context: any;
},
Promise<{
_id: string;
amount: number;
currencyCode: string;
countryCode: string;
Expand Down Expand Up @@ -84,7 +82,6 @@ export const DeliveryProvider: DeliveryProviderHelperTypes = {
};

return {
_id: await sha256([deliveryProvider._id, country, useNetPrice, order ? order._id : ''].join('')),
amount: orderPrice.amount,
currencyCode: orderPrice.currency,
countryCode: country,
Expand Down
27 changes: 0 additions & 27 deletions packages/api/src/resolvers/type/dispatch-types.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/api/src/resolvers/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Country } from './country-types.js';
import { Currency } from './currency-types.js';
import { DeliveryProvider } from './delivery-provider-types.js';
import { Dimensions } from './dimensions-types.js';
import { Dispatch } from './dispatch-types.js';
import { Enrollment } from './enrollment/enrollment-types.js';
import { EnrollmentDelivery } from './enrollment/enrollment-delivery-types.js';
import { EnrollmentPayment } from './enrollment/enrollment-payment-types.js';
Expand Down Expand Up @@ -57,7 +56,6 @@ import { Quotation } from './quotation-types.js';
import { Shop } from './shop-types.js';
import { SimpleProduct } from './product/product-simple-types.js';
import { TokenizedProduct } from './product/product-tokenized-types.js';
import { Stock } from './stock-types.js';
import { User } from './user-types.js';
import { WebAuthnCredentials } from './webauthn-credentials-types.js';
import { WarehousingProvider } from './warehousing-provider-types.js';
Expand All @@ -84,7 +82,6 @@ const types = {
Currency,
DeliveryProvider,
Dimensions,
Dispatch,
Enrollment,
EnrollmentDelivery,
EnrollmentPayment,
Expand Down Expand Up @@ -131,7 +128,6 @@ const types = {
SimpleProduct,
TokenizedProduct,
Token,
Stock,
User,
WebAuthnCredentials,
WarehousingProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Context } from '../../../context.js';
import { OrderDeliveryDiscount as OrderDeliveryDiscountType } from '@unchainedshop/core-orders';
import { sha256 } from '@unchainedshop/utils';

export const OrderDeliveryDiscount = {
_id(obj: OrderDeliveryDiscountType) {
Expand All @@ -13,9 +12,8 @@ export const OrderDeliveryDiscount = {
});
},

async total(obj: OrderDeliveryDiscountType) {
total(obj: OrderDeliveryDiscountType) {
return {
_id: await sha256([`${obj.item._id}:${obj.discountId}`, obj.amount, obj.currency].join('')),
amount: obj.amount,
currency: obj.currency,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Order } from '@unchainedshop/core-orders';
import { Context } from '../../../context.js';
import { Price, sha256 } from '@unchainedshop/utils';
import { Price } from '@unchainedshop/utils';

export const OrderGlobalDiscount = {
_id(
Expand All @@ -25,14 +25,13 @@ export const OrderGlobalDiscount = {
});
},

async total(
total(
obj: Price & {
order: Order;
discountId: string;
},
) {
return {
_id: await sha256([`${obj.order._id}:${obj.discountId}`, obj.amount, obj.currency].join('')),
amount: obj.amount,
currency: obj.currency,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sha256 } from '@unchainedshop/utils';
import { Context } from '../../../context.js';
import { OrderPositionDiscount } from '@unchainedshop/core-orders';

Expand All @@ -13,9 +12,8 @@ export const OrderItemDiscount = {
});
},

async total(obj: OrderPositionDiscount) {
total(obj: OrderPositionDiscount) {
return {
_id: await sha256([`${obj.item._id}:${obj.discountId}`, obj.amount, obj.currency].join('')),
amount: obj.amount,
currency: obj.currency,
};
Expand Down
14 changes: 3 additions & 11 deletions packages/api/src/resolvers/type/order/order-item-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OrderPosition, OrderPositionDiscount } from '@unchainedshop/core-orders
import { Product } from '@unchainedshop/core-products';
import { Quotation } from '@unchainedshop/core-quotations';
import { TokenSurrogate, WarehousingProvider } from '@unchainedshop/core-warehousing';
import { Price, sha256 } from '@unchainedshop/utils';
import { Price } from '@unchainedshop/utils';
import { ProductPricingSheet } from '@unchainedshop/core';

const getPricingSheet = async (orderPosition: OrderPosition, context: Context) => {
Expand Down Expand Up @@ -120,11 +120,7 @@ export const OrderItem = {
const pricing = await getPricingSheet(orderPosition, context);

if (pricing.isValid()) {
const price = pricing.total(params);
return {
_id: await sha256([orderPosition._id, JSON.stringify(params), JSON.stringify(price)].join('')),
...price,
};
return pricing.total(params);
}
return null;
},
Expand All @@ -137,11 +133,7 @@ export const OrderItem = {
const pricing = await getPricingSheet(orderPosition, context);

if (pricing.isValid()) {
const price = pricing.unitPrice(params);
return {
_id: await sha256([`${orderPosition._id}-unit`, price.amount, pricing.currency].join('')),
...price,
};
return pricing.unitPrice(params);
}
return null;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from '../../../context.js';
import { OrderPayment } from '@unchainedshop/core-orders';
import { Price, sha256 } from '@unchainedshop/utils';
import { Price } from '@unchainedshop/utils';

export const OrderPaymentDiscount = {
_id: (orderDelivery: Price & { discountId: string; item: OrderPayment }) =>
Expand All @@ -15,15 +15,8 @@ export const OrderPaymentDiscount = {
discountId: orderDelivery.discountId,
}),

async total(orderDelivery: Price & { discountId: string; item: OrderPayment }) {
total(orderDelivery: Price & { discountId: string; item: OrderPayment }) {
return {
_id: await sha256(
[
`${orderDelivery.item._id}-${orderDelivery.discountId}`,
orderDelivery.amount,
orderDelivery.currency,
].join(''),
),
amount: orderDelivery.amount,
currency: orderDelivery.currency,
};
Expand Down
10 changes: 3 additions & 7 deletions packages/api/src/resolvers/type/order/order-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
OrderDelivery,
} from '@unchainedshop/core-orders';
import { User } from '@unchainedshop/core-users';
import { Price, sha256 } from '@unchainedshop/utils';
import { Price } from '@unchainedshop/utils';
import { OrderPricingSheet } from '@unchainedshop/core';

export const Order = {
Expand Down Expand Up @@ -74,18 +74,14 @@ export const Order = {
return order.status;
},

async total(order: OrderType, params: { category: string; useNetPrice: boolean }): Promise<Price> {
total(order: OrderType, params: { category: string; useNetPrice: boolean }): Price {
const pricing = OrderPricingSheet({
calculation: order.calculation,
currency: order.currency,
});

if (pricing.isValid()) {
const price = pricing.total(params);
return {
_id: await sha256([order._id, JSON.stringify(params), JSON.stringify(price)].join('')),
...price,
};
return pricing.total(params);
}
return null;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Context } from '../../../context.js';
import { PaymentProvider as PaymentProviderType } from '@unchainedshop/core-payment';
import { PaymentDirector, PaymentPricingDirector } from '@unchainedshop/core';
import { sha256 } from '@unchainedshop/utils';

export const PaymentProvider = {
interface(provider: PaymentProviderType) {
Expand Down Expand Up @@ -53,7 +52,6 @@ export const PaymentProvider = {
};

return {
_id: await sha256([paymentProvider._id, country, useNetPrice, order ? order._id : ''].join('')),
amount: orderPrice.amount,
currencyCode: orderPrice.currency,
countryCode: country,
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/resolvers/type/price-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import accounting from 'accounting';

type PriceType = {
_id?: string;
isTaxable?: boolean;
isNetPrice?: boolean;
countryCode?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@unchainedshop/core-products';
import { Context } from '../../../context.js';
import { Product } from './product-types.js';
import { sha256 } from '@unchainedshop/utils';

export const ConfigurableProduct = {
...Product,
Expand Down Expand Up @@ -127,15 +126,6 @@ export const ConfigurableProduct = {
const unitPrice = pricing.unitPrice({ useNetPrice });

return {
_id: await sha256(
[
proxyProduct._id,
countryContext,
quantity,
useNetPrice,
requestContext.userId || 'ANONYMOUS',
].join(''),
),
...unitPrice,
isNetPrice: useNetPrice,
isTaxable: pricing.taxSum() > 0,
Expand All @@ -152,36 +142,9 @@ export const ConfigurableProduct = {
prices: filteredPrices,
});

const minPriceHash = await sha256(
[
product._id,
minPrice?.isTaxable,
minPrice?.isNetPrice,
minPrice?.amount,
minPrice?.currencyCode,
].join(''),
);

const maxPriceHash = await sha256(
[
product._id,
maxPrice?.isTaxable,
maxPrice?.isNetPrice,
maxPrice?.amount,
maxPrice?.currencyCode,
].join(''),
);

return {
_id: await sha256([product._id, minPriceHash, maxPriceHash].join('')),
minPrice: {
_id: minPriceHash,
...minPrice,
},
maxPrice: {
_id: maxPriceHash,
...maxPrice,
},
minPrice,
maxPrice,
};
},
};
6 changes: 2 additions & 4 deletions packages/api/src/resolvers/type/product/product-discount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ProductDiscount as ProductDiscountType } from '@unchainedshop/core-products';
import { ProductDiscountDirector } from '@unchainedshop/core';
import { sha256 } from '@unchainedshop/utils';

export const ProductDiscount = {
interface: async (productDiscount: ProductDiscountType) => {
Expand All @@ -15,11 +14,10 @@ export const ProductDiscount = {
};
},

async total(productDiscount: ProductDiscountType) {
const { total, _id } = productDiscount;
total(productDiscount: ProductDiscountType) {
const { total } = productDiscount;
if (productDiscount.total) {
return {
_id: await sha256([`${_id}`, total.amount, total.currency].join('')),
amount: total.amount,
currency: total.currency,
};
Expand Down
4 changes: 0 additions & 4 deletions packages/api/src/resolvers/type/product/product-plan-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from '@unchainedshop/core-products';
import { Context } from '../../../context.js';
import { Product } from './product-types.js';
import { sha256 } from '@unchainedshop/utils';

export const PlanProduct = {
...Product,
Expand Down Expand Up @@ -56,9 +55,6 @@ export const PlanProduct = {
const unitPrice = pricing.unitPrice({ useNetPrice });

return {
_id: await sha256(
[obj._id, countryContext, quantity, useNetPrice, user ? user._id : 'ANONYMOUS'].join(''),
),
...unitPrice,
isNetPrice: useNetPrice,
isTaxable: pricing.taxSum() > 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Product, ProductSupply } from '@unchainedshop/core-products';
import { WarehousingContext, WarehousingDirector } from '@unchainedshop/core';
import { Context } from '../../../context.js';
import { DeliveryProviderType } from '@unchainedshop/core-delivery';
import { DeliveryProvider } from '@unchainedshop/core-delivery';
Expand Down
Loading

0 comments on commit 63b38a0

Please sign in to comment.