@@ -9,6 +9,7 @@ import { cartItems, carts } from '../db';
99import {
1010 isCashierShoppingCartEvent ,
1111 ProductItemAddedToShoppingCart ,
12+ ProductItemRemovedFromShoppingCart ,
1213 ShoppingCartErrors ,
1314 ShoppingCartOpened ,
1415 ShoppingCartStatus ,
@@ -36,7 +37,11 @@ export const projectToShoppingCartItem = (
3637 case 'product-item-added-to-shopping-cart' :
3738 return projectProductItemAddedToShoppingCart ( db , event , streamRevision ) ;
3839 case 'product-item-removed-from-shopping-cart' :
39- return Promise . resolve ( ) ; // projectProductItemRemovedFromShoppingCart(event, streamRevision);
40+ return projectProductItemRemovedFromShoppingCart (
41+ db ,
42+ event ,
43+ streamRevision
44+ ) ;
4045 case 'shopping-cart-confirmed' :
4146 return Promise . resolve ( ) ; // projectShoppingCartConfirmed(event, streamRevision);
4247 default : {
@@ -62,7 +67,6 @@ export const projectShoppingCartOpened = async (
6267 // city?: (string) | null
6368 // content?: (string) | null
6469 // country?: (string) | null
65- // createdAt: Date
6670 // email?: (string) | null
6771 // firstName?: (string) | null
6872 // lastName?: (string) | null
@@ -72,7 +76,6 @@ export const projectShoppingCartOpened = async (
7276 // mobile?: (string) | null
7377 // province?: (string) | null
7478 // status?: number
75- // token: string
7679 // updatedAt?: (Date) | null
7780 // userId: number | null,
7881 } ) ;
@@ -92,12 +95,11 @@ export const projectProductItemAddedToShoppingCart = async (
9295 const { wasApplied, cartId } = await wasAlreadyApplied (
9396 db ,
9497 shoppingCartId ,
98+ new Date ( addedAt ) ,
9599 streamRevision
96100 ) ;
97101
98- if ( wasApplied ) {
99- return ;
100- }
102+ if ( wasApplied ) return ;
101103
102104 const shoppingCartsItems = cartItems ( db ) ;
103105
@@ -110,21 +112,57 @@ export const projectProductItemAddedToShoppingCart = async (
110112 sql `
111113 INSERT INTO ${ shoppingCartsItems . tableId } as ci ("cartId", "productId", "sku", "price", "discount", "quantity", "createdAt")
112114 VALUES (${ cartId } , ${ productId } , ${ sku } , ${ price } , ${ discount } , ${ quantity } , ${ addedAt } )
113- ON CONFLICT ("cartId", "productId") DO UPDATE SET "quantity" = EXCLUDED."quantity" + ci."quantity";
115+ ON CONFLICT ("cartId", "productId") DO UPDATE SET "quantity" = EXCLUDED."quantity" + ci."quantity", "updatedAt" = ${ addedAt } ;
116+ `
117+ ) ;
118+ } ;
119+
120+ export const projectProductItemRemovedFromShoppingCart = async (
121+ db : Transaction ,
122+ event : ProductItemRemovedFromShoppingCart ,
123+ streamRevision : number
124+ ) : Promise < void > => {
125+ const {
126+ shoppingCartId,
127+ productItem : { productId, quantity } ,
128+ removedAt,
129+ } = event . data ;
130+
131+ const { wasApplied, cartId } = await wasAlreadyApplied (
132+ db ,
133+ shoppingCartId ,
134+ new Date ( removedAt ) ,
135+ streamRevision
136+ ) ;
137+
138+ if ( wasApplied ) return ;
139+
140+ const shoppingCartsItems = cartItems ( db ) ;
141+
142+ await db . query (
143+ sql `
144+ UPDATE ${ shoppingCartsItems . tableId }
145+ SET "quantity" = "quantity" - ${ quantity } , "updatedAt" = ${ removedAt }
146+ WHERE "cartId" = ${ cartId } AND "productId" = ${ productId } ;
147+
148+ DELETE FROM ${ shoppingCartsItems . tableId }
149+ WHERE "cartId" = ${ cartId } AND "productId" = ${ productId } AND "quantity" = 0;
114150 `
115151 ) ;
116152} ;
117153
118154const wasAlreadyApplied = async (
119155 db : Transaction ,
120156 shoppingCartId : string ,
157+ updatedAt : Date ,
121158 streamRevision : number
122159) => {
123160 const shoppingCarts = carts ( db ) ;
124161 const result = await shoppingCarts . update (
125162 { sessionId : shoppingCartId , revision : streamRevision - 1 } ,
126163 {
127164 revision : streamRevision ,
165+ updatedAt,
128166 }
129167 ) ;
130168
@@ -134,45 +172,6 @@ const wasAlreadyApplied = async (
134172 } ;
135173} ;
136174
137- // export const projectProductItemRemovedFromShoppingCart = async (
138- // event: ProductItemRemovedFromShoppingCart,
139- // streamRevision: number
140- // ): Promise<void> => {
141- // const shoppingCarts = await getShoppingCarts();
142- // const lastRevision = streamRevision - 1;
143-
144- // const { productItems, revision } = await retryIfNotFound(() =>
145- // shoppingCarts.findOne(
146- // {
147- // _id: toObjectId(event.data.shoppingCartId),
148- // revision: { $gte: lastRevision },
149- // },
150- // {
151- // projection: { productItems: 1, revision: 1 },
152- // }
153- // )
154- // );
155- // if (revision > lastRevision) {
156- // return;
157- // }
158-
159- // await retryIfNotUpdated(() =>
160- // shoppingCarts.updateOne(
161- // {
162- // _id: toObjectId(event.data.shoppingCartId),
163- // revision: lastRevision,
164- // },
165- // {
166- // $set: {
167- // productItems: removeProductItem(productItems, event.data.productItem),
168- // revision: streamRevision,
169- // },
170- // },
171- // { upsert: false }
172- // )
173- // );
174- // };
175-
176175// export const projectShoppingCartConfirmed = async (
177176// event: ShoppingCartConfirmed,
178177// streamRevision: number
0 commit comments