-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cart): compute cart prices for in-memory driver (#2614)
- Loading branch information
Showing
11 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
libs/cart/driver/in-memory/src/helpers/compute-cart-item-prices.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { faker } from '@faker-js/faker'; | ||
|
||
import { DaffCartItem } from '@daffodil/cart'; | ||
import { daffMultiply } from '@daffodil/core'; | ||
import { DaffProduct } from '@daffodil/product'; | ||
|
||
export function daffCartInMemoryComputeCartItemPrices(cartItem: DaffCartItem, products: DaffProduct[]): DaffCartItem { | ||
cartItem.price = products.find(({ id }) => id === cartItem.product_id)?.price || 0; | ||
cartItem.row_total = daffMultiply(cartItem.price, cartItem.qty); | ||
|
||
return cartItem; | ||
} |
55 changes: 55 additions & 0 deletions
55
libs/cart/driver/in-memory/src/helpers/compute-cart-totals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { faker } from '@faker-js/faker'; | ||
|
||
import { | ||
DaffCart, | ||
DaffCartTotalTypeEnum, | ||
} from '@daffodil/cart'; | ||
import { DaffProduct } from '@daffodil/product'; | ||
|
||
export function daffCartInMemoryComputeCartTotals(cart: DaffCart, products: DaffProduct[]): DaffCart { | ||
const shipping = cart.shipping_information ? faker.datatype.number({ min: 0, max: 99 }) : 0; | ||
const subtotalExcludingTax = cart.items?.reduce((acc, { product_id }) => | ||
acc + (products.find(({ id }) => id === product_id)?.price || 0), | ||
0, | ||
) || 0; | ||
const discount = faker.datatype.number({ min: 0, max: subtotalExcludingTax }); | ||
const subtotalIncludingTax = subtotalExcludingTax + cart.totals[DaffCartTotalTypeEnum.tax].value; | ||
const subtotalWithDiscountExcludingTax = subtotalExcludingTax - discount; | ||
const subtotalWithDiscountIncludingTax = subtotalIncludingTax - discount; | ||
const grandTotal = subtotalWithDiscountIncludingTax + shipping; | ||
|
||
return { | ||
...cart, | ||
totals: { | ||
...cart.totals, | ||
[DaffCartTotalTypeEnum.shipping]: { | ||
...cart.totals[DaffCartTotalTypeEnum.shipping], | ||
value: shipping, | ||
}, | ||
[DaffCartTotalTypeEnum.subtotalExcludingTax]: { | ||
...cart.totals[DaffCartTotalTypeEnum.subtotalExcludingTax], | ||
value: subtotalExcludingTax, | ||
}, | ||
[DaffCartTotalTypeEnum.discount]: { | ||
...cart.totals[DaffCartTotalTypeEnum.discount], | ||
value: discount, | ||
}, | ||
[DaffCartTotalTypeEnum.subtotalIncludingTax]: { | ||
...cart.totals[DaffCartTotalTypeEnum.subtotalIncludingTax], | ||
value: subtotalIncludingTax, | ||
}, | ||
[DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax]: { | ||
...cart.totals[DaffCartTotalTypeEnum.subtotalWithDiscountExcludingTax], | ||
value: subtotalWithDiscountExcludingTax, | ||
}, | ||
[DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax]: { | ||
...cart.totals[DaffCartTotalTypeEnum.subtotalWithDiscountIncludingTax], | ||
value: subtotalWithDiscountIncludingTax, | ||
}, | ||
[DaffCartTotalTypeEnum.grandTotal]: { | ||
...cart.totals[DaffCartTotalTypeEnum.grandTotal], | ||
value: grandTotal, | ||
}, | ||
}, | ||
}; | ||
} |