-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
map the order summary to order request
- Loading branch information
Olasunkanmi Oyinlola
authored and
Olasunkanmi Oyinlola
committed
Jan 4, 2024
1 parent
eb04fdf
commit 2ff14f0
Showing
2 changed files
with
55 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,60 @@ | ||
import { SelectedItem } from "./../reducers/cartReducer"; | ||
import { OrderSummary, SelectedItem } from "./../reducers/cartReducer"; | ||
import { useShoppingCart } from "../hooks/UseShoppingCart"; | ||
|
||
export const createOrder = async (order: any) => {}; | ||
import useAxiosPrivate from "../hooks/useAxiosPrivate"; | ||
import { IcartItems } from "../models/order.model"; | ||
|
||
const getOrderSummary = () => { | ||
const { GetOrderSummary } = useShoppingCart(); | ||
return GetOrderSummary(); | ||
}; | ||
|
||
const reduceSelectedItems = () => { | ||
export const createOrder = async () => { | ||
return useAxiosPrivate(); | ||
}; | ||
|
||
const mapOrderSummaryToOrderRequest = () => { | ||
const orderSummary = getOrderSummary(); | ||
let selectedItems: SelectedItem[] = []; | ||
if (orderSummary?.length) { | ||
selectedItems = orderSummary.reduce((result: SelectedItem[], item) => { | ||
if (item.menus?.length) { | ||
item.menus.forEach((menu) => { | ||
if (menu.selectedItems) { | ||
menu.selectedItems.forEach((selectedItem) => { | ||
const itemId = selectedItem.id; | ||
const existingItem = result.find( | ||
(item: any) => item.id === itemId | ||
); | ||
if (existingItem) { | ||
existingItem.price += selectedItem.price; | ||
existingItem.quantity! += selectedItem.quantity!; | ||
} else { | ||
result.push({ ...selectedItem }); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
return result; | ||
}, []); | ||
return { | ||
state: "CREATED", | ||
type: "DINE_IN", | ||
singleclientId: "63d792433b857e1697fe7017", | ||
total: calculateOrderTotalPrice(orderSummary) ?? 0, | ||
cartItems: cartItemsMapper(orderSummary), | ||
}; | ||
} | ||
return selectedItems; | ||
}; | ||
|
||
// const getCartItems = () => { | ||
// const orderSummary = getOrderSummary(); | ||
// if (orderSummary?.length) { | ||
// const selectedItemsMap = new Map<string, SelectedItem>(); | ||
// reduceSelectedItems.forEach((item) => {}); | ||
// orderSummary.map((summary) => { | ||
// const cartItem = summary.menus; | ||
// }); | ||
// } | ||
// }; | ||
const calculateOrderTotalPrice = (orderSummary: OrderSummary[]) => { | ||
return orderSummary.reduce((acc, item) => { | ||
return acc + (item.menus[0]?.menuTotalPrice ?? 0); | ||
}, 0); | ||
}; | ||
|
||
const calculateCartItemsTotalPrice = (selectedItems: SelectedItem[]) => { | ||
let totalSelectedItemsPrice = 0; | ||
if (selectedItems?.length) { | ||
totalSelectedItemsPrice = selectedItems.reduce((acc, item) => { | ||
return acc + Number(item?.price ?? 0); | ||
}, 0); | ||
} | ||
return totalSelectedItemsPrice; | ||
}; | ||
|
||
const cartItemsMapper = (orderSummary: OrderSummary[]): IcartItems[] => { | ||
const menus = orderSummary.flatMap((summary) => summary.menus); | ||
const cartItems = menus.map((menu) => { | ||
return { | ||
menuId: menu.id, | ||
total: | ||
menu.menuTotalPrice ?? | ||
0 - calculateCartItemsTotalPrice(menu.selectedItems ?? []), | ||
note: menu.note, | ||
selectedItems: menu.selectedItems?.map((item) => { | ||
return { ...item, itemId: item.id }; | ||
}), | ||
quantity: menu.quantity, | ||
}; | ||
}); | ||
return cartItems; | ||
}; |
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