Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #482

Merged
merged 12 commits into from
Jan 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface IOrderDataModel {
readonly orderManagerId?: Types.ObjectId;
readonly cartItems?: CartItemDataModel[];
readonly orderStatusId: Types.ObjectId;
readonly summary: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class OrderDataModel extends BaseDocument implements IOrderDataModel {
@Prop({ type: String, required: true })
type: dinningType;

@Prop({ type: String, required: true })
summary: string;

@Prop({ type: mongoose.Schema.Types.ObjectId })
@Type(() => SingleClientDataModel)
singleclientId: Types.ObjectId;
Expand Down
4 changes: 4 additions & 0 deletions backend/src/order/dto/create-order.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class CreateOrderDTO {
@IsNotEmpty()
total: number;

@IsString()
@IsNotEmpty()
summary: string;

@IsOptional()
@IsArray()
cartItems: CreateCartItemsDTO[];
Expand Down
1 change: 1 addition & 0 deletions backend/src/order/order-entity.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IOrder {
orderManagerId?: Types.ObjectId;
audit: Audit;
cartItems?: CartItem[];
summary: string;
}

//tableNumber
Expand Down
2 changes: 2 additions & 0 deletions backend/src/order/order-mock-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const orderMockData: IOrder = {
total: 1,
audit: Audit.create(auditMockData).getValue(),
cartItems: [cartItemMock],
summary: '',
};

export const orderMock: CreateOrderDTO = {
Expand All @@ -26,4 +27,5 @@ export const orderMock: CreateOrderDTO = {
singleClientId: id.toString(),
total: 1,
cartItems: [createItem],
summary: '',
};
20 changes: 17 additions & 3 deletions backend/src/order/order.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ export class OrderMapper implements IMapper<Order, OrderDataModel> {
private readonly orderStatusMapper: OrderStatusMapper,
) {}
toPersistence(entity: Order): OrderDataModel {
const { id, state, type, singleclientId, total, discount, orderManagerId, audit, cartItems, orderStatusId } =
entity;
const {
id,
state,
type,
singleclientId,
total,
discount,
orderManagerId,
audit,
cartItems,
orderStatusId,
summary,
} = entity;
const {
auditCreatedBy,
auditCreatedDateTime,
Expand All @@ -34,6 +45,7 @@ export class OrderMapper implements IMapper<Order, OrderDataModel> {
discount,
cartItems: cartItems?.length ? cartItems.map((cartItem) => this.cartItemMapper.toPersistence(cartItem)) : [],
orderManagerId,
summary,
auditCreatedBy,
auditCreatedDateTime,
auditModifiedBy,
Expand All @@ -45,7 +57,8 @@ export class OrderMapper implements IMapper<Order, OrderDataModel> {
}

toDomain(model: OrderDataModel): Order {
const { state, type, singleclientId, total, discount, orderManagerId, _id, cartItems, orderStatusId } = model;
const { state, type, singleclientId, total, discount, orderManagerId, _id, cartItems, orderStatusId, summary } =
model;
const entity: Order = Order.create(
{
state: state ? this.orderStatusMapper.toDomain(state) : undefined,
Expand All @@ -55,6 +68,7 @@ export class OrderMapper implements IMapper<Order, OrderDataModel> {
total,
discount,
orderManagerId,
summary,
audit: this.auditMapper.toDomain(model),
cartItems: cartItems?.length ? cartItems.map((cartItem) => this.cartItemMapper.toDomain(cartItem)) : [],
},
Expand Down
3 changes: 2 additions & 1 deletion backend/src/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class OrderService implements IOrderService {

async createOrder(orderSummary: CreateOrderDTO): Promise<Result<IOrderResponseDTO>> {
await this.singleclientService.validateContext();
const { state, type, singleClientId, total, cartItems } = orderSummary;
const { state, type, singleClientId, total, cartItems, summary } = orderSummary;
const orderDuplicate = await this.orderRepository.getDuplicateOrder(type, singleClientId, cartItems);
if (orderDuplicate) {
throwApplicationError(HttpStatus.NOT_FOUND, 'Duplicate order detected. Please confirm.');
Expand All @@ -74,6 +74,7 @@ export class OrderService implements IOrderService {
type,
total,
singleclientId: singleclientObjId,
summary,
audit,
});
const orderModel: OrderDataModel = this.orderMapper.toPersistence(order);
Expand Down
7 changes: 7 additions & 0 deletions backend/src/order/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Order extends Entity<IOrder> implements IOrder {
_orderManagerId: Types.ObjectId;
_audit: Audit;
_cartItems: CartItem[] | undefined;
_summary: string;

constructor(
id: Types.ObjectId,
Expand All @@ -30,6 +31,7 @@ export class Order extends Entity<IOrder> implements IOrder {
audit,
cartItems,
orderStatusId,
summary,
}: IOrder,
) {
super(id);
Expand All @@ -43,6 +45,7 @@ export class Order extends Entity<IOrder> implements IOrder {
this._audit = audit;
this._cartItems = cartItems;
this._orderStatusId = orderStatusId;
this._summary = summary;
}

get state(): OrderStatus {
Expand All @@ -53,6 +56,10 @@ export class Order extends Entity<IOrder> implements IOrder {
this._state = state;
}

get summary(): string {
return this._summary;
}

get type(): dinningType {
return this._type;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/apis/orderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const OrderApi = () => {
singleClientId: "63d78441a6fda119c09b1930",
total: calculateOrderTotalPrice(orderSummary) + serviceCharge,
cartItems: cartItemsMapper(orderSummary),
summary: JSON.stringify(orderSummary),
};
}
return order;
Expand All @@ -43,7 +44,6 @@ export const OrderApi = () => {
return totalSelectedItemsPrice;
};


const cartItemsMapper = (orderSummary: OrderSummary[]): IcartItems[] => {
const menus = orderSummary.flatMap((summary) => summary.menus);
const cartItems = menus.map((menu) => {
Expand Down
32 changes: 25 additions & 7 deletions frontend/src/components/Cart/ShoppinCartDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { useNavigate } from "react-router-dom";
import { CONSTANTS } from "../../constants/constant";
import { useShoppingCart } from "../../hooks/UseShoppingCart";
import { OrderSummary } from "../../reducers";
import { calculateServiceCharge, calculateTotalOrderAmount, setLocalStorageData, wordWrap } from "../../utility/utils";
import {
calculateServiceCharge,
calculateTotalOrderAmount,
clearStorage,
setLocalStorageData,
wordWrap,
} from "../../utility/utils";
import { QtyButton } from "../MenuItems/addItemButton";
import { CallToAction } from "../Utilities/modal";
import { CartSelectedItems } from "./CartSelectedItems";
Expand All @@ -14,9 +20,6 @@ import { OrderApi } from "../../apis/orderApi";
import useAxiosPrivate from "../../hooks/useAxiosPrivate";
import { useMutation } from "react-query";
import { ICreateOrderDTO } from "../../dto/order";
import Lottie from "lottie-react";
import groovyWalkAnimation from "../../assets/animations/1704611321528.json";
import success from "../../assets/animations/1704612008454.json";

export const ShoppingCartDetails = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -88,8 +91,14 @@ export const ShoppingCartDetails = () => {
mutationFn: async (order: ICreateOrderDTO) => {
return await axios.post("orders/create", order);
},
onSuccess: (data, variables, context) => {},
onError: (data, variables, context) => {},
onSuccess: (data) => {
if (data.data.isSuccess) {
resetCart();
clearStorage();
closeCart();
navigate("/");
}
},
});

return (
Expand Down Expand Up @@ -214,7 +223,16 @@ export const ShoppingCartDetails = () => {
{handleCreateOrder.isLoading ? (
"Creating Order..."
) : (
<>{handleCreateOrder.isError ? <div>An error occurred: {handleCreateOrder.error.message}</div> : null}</>
<>
{handleCreateOrder.isError ? (
<div>
An error occurred:{" "}
{handleCreateOrder.error.response.data.message.message?.length
? handleCreateOrder.error.response.data.message.message.join(",")
: handleCreateOrder.error.response.data.message.error}
</div>
) : null}
</>
)}
{handleCreateOrder.isSuccess ? <div>Order processed successfully</div> : null}
</div>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/contexts/shoppingCartContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,12 @@ export const ShoppingCartProvider = ({ children }: shoppingCartProviderProps) =>
};

const addMenuToCart = (menu: IMenuData, instructions?: string) => {
//State.totalPrice is not correct
if (!state.menus.length) {
state.menus = menuToMenuStateMapper(menu);
state.quantity = 1;
}
let { menus, quantity, orderSummary } = state;
let { menus, quantity, orderSummary, totalPrice } = state;
if (instructions?.length) {
menus[0].note = instructions;
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/dto/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface ICreateOrderDTO {
singleClientId: string;
total: number;
cartItems: IcartItems[];
summary: string;
}
1 change: 0 additions & 1 deletion frontend/src/utility/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,5 @@ export const cartExpiry = (date: string): boolean => {
const specifiedDate: Date = new Date(date);
const timeDifferenceMs: number = new Date().getTime() - specifiedDate.getTime();
const hoursDifference: number = timeDifferenceMs / (1000 * 60 * 60);
console.log(hoursDifference);
return hoursDifference > 1;
};
Loading