From e297ace50ef39072e3a31de895b9813e1269a838 Mon Sep 17 00:00:00 2001 From: Oyinlola Olasunkanmi Raymond <60177090+olasunkanmi-SE@users.noreply.github.com> Date: Tue, 24 Oct 2023 11:21:17 +0800 Subject: [PATCH] Development (#397) * implement the menu search * fix code smell * calculate service charge * update sonar project config * update total order calculation * add cart items to order * move email header check to the backend * remove quantity from order * update selected items to include itemId * make cartitem and selectedCart Items optional in parent models * create the order service * create order controller * checkout and fix (#391) * Test (#393) * checkout and fix * fix errors and remove env from application (#392) Co-authored-by: Olasunkanmi Oyinlola * Offshore dev (#394) * fix errors and remove env from application * fix build errors Co-authored-by: Olasunkanmi Oyinlola <143487325+olasunkanmiraymond@users.noreply.github.com> Co-authored-by: Olasunkanmi Oyinlola * Test (#396) * checkout and fix * fix errors and remove env from application (#392) Co-authored-by: Olasunkanmi Oyinlola * Offshore dev (#394) * fix errors and remove env from application * fix build errors --------- Co-authored-by: Olasunkanmi Oyinlola * Offshore dev (#395) * fix errors and remove env from application * fix build errors * remove .env file from backend --------- Co-authored-by: Olasunkanmi Oyinlola --------- Co-authored-by: Olasunkanmi Oyinlola <143487325+olasunkanmiraymond@users.noreply.github.com> Co-authored-by: Olasunkanmi Oyinlola --------- Co-authored-by: Olasunkanmi Oyinlola <143487325+olasunkanmiraymond@users.noreply.github.com> Co-authored-by: Olasunkanmi Oyinlola --- backend/.env | 8 --- backend/.gitignore | 5 +- .../repositories/order.repository.ts | 4 +- .../middlewares/context.middleware.ts | 8 +-- frontend/src/apis/menusApi.tsx | 2 +- frontend/src/apis/orderApi.ts | 49 +++++++++++++++++++ frontend/src/contexts/shoppingCartContext.tsx | 1 + frontend/src/models/order.model.ts | 21 ++++++++ 8 files changed, 82 insertions(+), 16 deletions(-) delete mode 100644 backend/.env create mode 100644 frontend/src/apis/orderApi.ts create mode 100644 frontend/src/models/order.model.ts diff --git a/backend/.env b/backend/.env deleted file mode 100644 index 613345f8..00000000 --- a/backend/.env +++ /dev/null @@ -1,8 +0,0 @@ -DATABASE_URL=mongodb+srv://kosemani:omowunmi888@cluster0.4i82g.mongodb.net/learn?retryWrites=true -mockHash=23rt565regf3454t -JWT_ACCESS_TOKEN_SECRET='98h34jfew%i&o3298hin2p9#kn2u80@#$' -JWT_REFRESH_TOKEN_SECRET='few%i&o3298hin2p9#kn2u80@#$749' -JWT_ACCESS_TOKEN_EXPIRATION_TIME=60m -JWT_REFRESH_TOKEN_EXPIRATION_TIME=420m -APPSESSION=RestaurantApp -GUEST_EMAIL=guest@application.com diff --git a/backend/.gitignore b/backend/.gitignore index 7ce7d985..65fe4734 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,4 @@ +.env # compiled output /dist /node_modules @@ -33,4 +34,6 @@ lerna-debug.log* !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json -.vscode/launch.json \ No newline at end of file +.vscode/launch.json + +.env \ No newline at end of file diff --git a/backend/src/infrastructure/data_access/repositories/order.repository.ts b/backend/src/infrastructure/data_access/repositories/order.repository.ts index 64ca93ee..34f52c56 100644 --- a/backend/src/infrastructure/data_access/repositories/order.repository.ts +++ b/backend/src/infrastructure/data_access/repositories/order.repository.ts @@ -1,11 +1,11 @@ import { Injectable } from '@nestjs/common'; import { InjectConnection, InjectModel } from '@nestjs/mongoose'; -import { Connection, Model, Types } from 'mongoose'; +import { Connection, Model } from 'mongoose'; import { GenericDocumentRepository } from 'src/infrastructure/database'; import { Order } from 'src/order/order'; import { OrderMapper } from './../../../order/order.mapper'; -import { OrderDataModel, OrderDocument } from './schemas/order.schema'; import { IOrderRepository } from './interfaces/order-repository.interface'; +import { OrderDataModel, OrderDocument } from './schemas/order.schema'; @Injectable() export class OrderRepository extends GenericDocumentRepository implements IOrderRepository { diff --git a/backend/src/infrastructure/middlewares/context.middleware.ts b/backend/src/infrastructure/middlewares/context.middleware.ts index 34ff54e8..63251c08 100644 --- a/backend/src/infrastructure/middlewares/context.middleware.ts +++ b/backend/src/infrastructure/middlewares/context.middleware.ts @@ -18,10 +18,10 @@ export class ContextMiddleWare implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { const headers = req.headers; const errors = new Object() as any; - if (!Object.hasOwn(headers, APIResponseMessage.emailHeader)) { - errors.email = APIResponseMessage.emailHeaderError; - } - if (!Object.hasOwn(headers, APIResponseMessage.correlationIdHeader)) { + // if (!Object.hasOwn(headers, APIResponseMessage.emailHeader)) { + // errors.email = APIResponseMessage.emailHeaderError; + // } + if (!Object.hasOwnProperty.call(headers, APIResponseMessage.correlationIdHeader)) { errors.correlationId = APIResponseMessage.correlationIdHeaderError; } for (const [key, value] of Object.entries(headers)) { diff --git a/frontend/src/apis/menusApi.tsx b/frontend/src/apis/menusApi.tsx index 4036e200..7549116c 100644 --- a/frontend/src/apis/menusApi.tsx +++ b/frontend/src/apis/menusApi.tsx @@ -1,4 +1,4 @@ -import { IMenu, IMenus } from "../models/menu.model"; +import { IMenu } from "../models/menu.model"; import { ICreateMenu } from "../interfaces/menu.interface"; import { QueryObserverResult, useQuery } from "react-query"; import { menuApi } from "./axios"; diff --git a/frontend/src/apis/orderApi.ts b/frontend/src/apis/orderApi.ts new file mode 100644 index 00000000..be372538 --- /dev/null +++ b/frontend/src/apis/orderApi.ts @@ -0,0 +1,49 @@ +import { SelectedItem } from "./../reducers/cartReducer"; +import { useShoppingCart } from "../hooks/UseShoppingCart"; + +export const createOrder = async (order: any) => {}; + +const getOrderSummary = () => { + const { GetOrderSummary } = useShoppingCart(); + return GetOrderSummary(); +}; + +const reduceSelectedItems = () => { + 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 selectedItems; +}; + +// const getCartItems = () => { +// const orderSummary = getOrderSummary(); +// if (orderSummary?.length) { +// const selectedItemsMap = new Map(); +// reduceSelectedItems.forEach((item) => {}); +// orderSummary.map((summary) => { +// const cartItem = summary.menus; +// }); +// } +// }; diff --git a/frontend/src/contexts/shoppingCartContext.tsx b/frontend/src/contexts/shoppingCartContext.tsx index 263f4c86..a8ac2697 100644 --- a/frontend/src/contexts/shoppingCartContext.tsx +++ b/frontend/src/contexts/shoppingCartContext.tsx @@ -399,6 +399,7 @@ export const ShoppingCartProvider = ({ children }: shoppingCartProviderProps) => const updateCartItems = (orderSummary: OrderSummary[]) => { state.orderSummary = orderSummary; + console.log(state.orderSummary); setLocalStorageData("cart", JSON.stringify(state), true); dispatch({ type: CartActionsType.UPDATE_CART_ITEMS, diff --git a/frontend/src/models/order.model.ts b/frontend/src/models/order.model.ts new file mode 100644 index 00000000..ae40de81 --- /dev/null +++ b/frontend/src/models/order.model.ts @@ -0,0 +1,21 @@ +export interface IOrder { + state: string; + type: string; + merchantId: string; + total: number; + cartItems: IcartItems[]; +} + +export interface IcartItems { + menuId: string; + total: number; + quantity: number; + selectedItems: IselectedItems[]; +} + +export interface IselectedItems { + itemId: string; + menuId: string; + price: number; + quantity: number; +}