|
| 1 | +import { executeOnMongoDB } from '#core/mongoDB'; |
| 2 | +import { assertUnreachable, Result, success } from '#core/primitives'; |
| 3 | +import { Event, StreamEvent } from '#core/events'; |
| 4 | +import { |
| 5 | + ProductItemAddedToShoppingCart, |
| 6 | + ProductItemRemovedFromShoppingCart, |
| 7 | + ShoppingCartConfirmed, |
| 8 | + ShoppingCartOpened, |
| 9 | + ShoppingCartStatus, |
| 10 | +} from '..'; |
| 11 | +import { CurrentShoppingCartDetails, CURRENT_SHOPPING_CART_DETAILS } from '.'; |
| 12 | +import { addProductItem, removeProductItem } from '../productItems'; |
| 13 | + |
| 14 | +export async function projectShoppingCartOpened( |
| 15 | + event: ShoppingCartOpened, |
| 16 | + streamRevision: bigint |
| 17 | +): Promise<Result<true>> { |
| 18 | + await executeOnMongoDB<CurrentShoppingCartDetails>( |
| 19 | + { collectionName: CURRENT_SHOPPING_CART_DETAILS }, |
| 20 | + async (collection) => { |
| 21 | + await collection.insertOne({ |
| 22 | + shoppingCartId: event.data.shoppingCartId, |
| 23 | + clientId: event.data.clientId, |
| 24 | + status: ShoppingCartStatus.Opened.toString(), |
| 25 | + productItems: [], |
| 26 | + openedAt: event.data.openedAt, |
| 27 | + revision: streamRevision.toString(), |
| 28 | + }); |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + return success(true); |
| 33 | +} |
| 34 | + |
| 35 | +export async function projectProductItemAddedToShoppingCart( |
| 36 | + event: ProductItemAddedToShoppingCart, |
| 37 | + streamRevision: bigint |
| 38 | +): Promise<Result<true>> { |
| 39 | + await executeOnMongoDB<CurrentShoppingCartDetails>( |
| 40 | + { collectionName: CURRENT_SHOPPING_CART_DETAILS }, |
| 41 | + async (collection) => { |
| 42 | + const { productItems } = (await collection.findOne( |
| 43 | + { |
| 44 | + shoppingCartId: event.data.shoppingCartId, |
| 45 | + }, |
| 46 | + { projection: { productItems: 1 } } |
| 47 | + ))!; |
| 48 | + |
| 49 | + await collection.findOneAndUpdate( |
| 50 | + { |
| 51 | + shoppingCartId: event.data.shoppingCartId, |
| 52 | + }, |
| 53 | + { |
| 54 | + $set: { |
| 55 | + productItems: addProductItem(productItems, event.data.productItem), |
| 56 | + revision: streamRevision.toString(), |
| 57 | + }, |
| 58 | + } |
| 59 | + ); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + return success(true); |
| 64 | +} |
| 65 | + |
| 66 | +export async function projectProductItemRemovedFromShoppingCart( |
| 67 | + event: ProductItemRemovedFromShoppingCart, |
| 68 | + streamRevision: bigint |
| 69 | +): Promise<Result<true>> { |
| 70 | + await executeOnMongoDB<CurrentShoppingCartDetails>( |
| 71 | + { collectionName: CURRENT_SHOPPING_CART_DETAILS }, |
| 72 | + async (collection) => { |
| 73 | + const { productItems } = (await collection.findOne( |
| 74 | + { |
| 75 | + shoppingCartId: event.data.shoppingCartId, |
| 76 | + }, |
| 77 | + { projection: { productItems: 1 } } |
| 78 | + ))!; |
| 79 | + |
| 80 | + await collection.findOneAndUpdate( |
| 81 | + { |
| 82 | + shoppingCartId: event.data.shoppingCartId, |
| 83 | + }, |
| 84 | + { |
| 85 | + $set: { |
| 86 | + productItems: removeProductItem( |
| 87 | + productItems, |
| 88 | + event.data.productItem |
| 89 | + ), |
| 90 | + revision: streamRevision.toString(), |
| 91 | + }, |
| 92 | + } |
| 93 | + ); |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + return success(true); |
| 98 | +} |
| 99 | + |
| 100 | +export async function projectShoppingCartConfirmed( |
| 101 | + event: ShoppingCartConfirmed, |
| 102 | + streamRevision: bigint |
| 103 | +): Promise<Result<true>> { |
| 104 | + await executeOnMongoDB<CurrentShoppingCartDetails>( |
| 105 | + { collectionName: CURRENT_SHOPPING_CART_DETAILS }, |
| 106 | + async (collection) => { |
| 107 | + await collection.updateOne( |
| 108 | + { |
| 109 | + shoppingCartId: event.data.shoppingCartId, |
| 110 | + }, |
| 111 | + { |
| 112 | + $set: { |
| 113 | + confirmedAt: event.data.confirmedAt, |
| 114 | + status: ShoppingCartStatus.Confirmed.toString(), |
| 115 | + revision: streamRevision.toString(), |
| 116 | + }, |
| 117 | + } |
| 118 | + ); |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + return success(true); |
| 123 | +} |
| 124 | + |
| 125 | +type CurrentShoppingCartDetailsEvent = |
| 126 | + | ShoppingCartOpened |
| 127 | + | ProductItemAddedToShoppingCart |
| 128 | + | ProductItemRemovedFromShoppingCart |
| 129 | + | ShoppingCartConfirmed; |
| 130 | + |
| 131 | +function isCashierShoppingCartDetailsEvent( |
| 132 | + event: Event |
| 133 | +): event is CurrentShoppingCartDetailsEvent { |
| 134 | + const eventType = (event as CurrentShoppingCartDetailsEvent).type; |
| 135 | + |
| 136 | + return ( |
| 137 | + eventType === 'shopping-cart-opened' || |
| 138 | + eventType === 'product-item-added-to-shopping-cart' || |
| 139 | + eventType === 'product-item-removed-from-shopping-cart' || |
| 140 | + eventType === 'shopping-cart-confirmed' |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +export async function projectToCurrentShoppingCartDetails( |
| 145 | + streamEvent: StreamEvent |
| 146 | +): Promise<Result<boolean>> { |
| 147 | + const { event, streamRevision } = streamEvent; |
| 148 | + |
| 149 | + if (!isCashierShoppingCartDetailsEvent(event)) { |
| 150 | + return success(false); |
| 151 | + } |
| 152 | + |
| 153 | + switch (event.type) { |
| 154 | + case 'shopping-cart-opened': |
| 155 | + return projectShoppingCartOpened(event, streamRevision); |
| 156 | + case 'product-item-added-to-shopping-cart': |
| 157 | + return projectProductItemAddedToShoppingCart(event, streamRevision); |
| 158 | + case 'product-item-removed-from-shopping-cart': |
| 159 | + return projectProductItemRemovedFromShoppingCart(event, streamRevision); |
| 160 | + case 'shopping-cart-confirmed': |
| 161 | + return projectShoppingCartConfirmed(event, streamRevision); |
| 162 | + default: |
| 163 | + assertUnreachable(event); |
| 164 | + } |
| 165 | +} |
0 commit comments