Skip to content

Commit

Permalink
include restaurant information as part of the menu response (#471)
Browse files Browse the repository at this point in the history
* include restaurant information as part of the menu response

* fix build errors

* fix build errors

---------

Co-authored-by: Olasunkanmi Oyinlola <[email protected]>
  • Loading branch information
olasunkanmi-SE and Olasunkanmi Oyinlola authored Jan 7, 2024
1 parent 2ff14f0 commit a5e6e6a
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { RestaurantMapper } from './../../../restaurant/restaurant.mapper';
import { SingleClientRepository } from './singleclient.repository';
import { IRestaurantRepository } from './interfaces/restaurant-repository.interface';
import { RestaurantData, RestaurantDocument } from './schemas';
import { throwApplicationError } from '../../../infrastructure/utilities/exception-instance';
import { HttpStatus } from '@nestjs/common';

export class RestaurantRepository
extends GenericDocumentRepository<Restaurant, RestaurantDocument>
Expand All @@ -27,6 +29,9 @@ export class RestaurantRepository

async getRestaurant(restaurantId: Types.ObjectId): Promise<Restaurant> {
const restaurantDoc = await this.DocumentModel.findById(restaurantId).populate('singleclient').exec();
if (!restaurantDoc) {
throwApplicationError(HttpStatus.NOT_FOUND, `Restaurant with id ${restaurantId} does not exist`);
}
const restaurant: Restaurant = this.restaurantMapper.toDomain(restaurantDoc);
return restaurant;
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/menu/menu-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Types } from 'mongoose';
import { IRestaurantResponseDTO } from 'src/restaurant';
import { ICategoryResponseDTO } from './../category/category-response.dto';
import { IAudit } from './../infrastructure/database/mongoDB/base-document.interface';
import { ITemResponseDTO } from './../item/item-response.dto';
Expand All @@ -12,4 +13,5 @@ export interface IMenuResponseDTO extends IAudit {
restaurantId: Types.ObjectId;
category: ICategoryResponseDTO;
items?: ITemResponseDTO[];
restaurant?: IRestaurantResponseDTO;
}
1 change: 1 addition & 0 deletions backend/src/menu/menu-service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface IMenuService {
updateMenu(props: any, id: Types.ObjectId): Promise<Result<IMenuResponseDTO>>;
deleteMenu(id: Types.ObjectId): Promise<Result<boolean>>;
getMenuByRestaurantId(restaurantId: string): Promise<Result<IMenuResponseDTO[]>>;
getExtendedMenuByRestaurantId(restaurantId: string): Promise<Result<IMenuResponseDTO[]>>;
}
4 changes: 3 additions & 1 deletion backend/src/menu/menu.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export class MenuController {
return this.menuService.deleteMenu(menuId);
}

//Todo.
//This API should require both the merchantId and RestaurantId /singleclient/:singleClientId/:restaurantId
@Get('/singleclient/:restaurantId')
async getMenusByRestaurantId(@Param('restaurantId') restaurantId: string): Promise<Result<IMenuResponseDTO[]>> {
return this.menuService.getMenuByRestaurantId(restaurantId);
return this.menuService.getExtendedMenuByRestaurantId(restaurantId);
}
}
8 changes: 5 additions & 3 deletions backend/src/menu/menu.parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Restaurant, RestaurantParser } from '../restaurant';
import { AuditParser } from '../audit';
import { CategoryParser } from './../category/category.parser';
import { ITemResponseDTO } from './../item/item-response.dto';
Expand All @@ -6,7 +7,7 @@ import { Menu } from './menu';
import { IMenuResponseDTO } from './menu-response.dto';

export class MenuParser {
static createMenuResponse(menu: Menu): IMenuResponseDTO {
static createMenuResponse(menu: Menu, restaurant?: Restaurant): IMenuResponseDTO {
const { id, name, description, items, audit, discount, imageUrl, basePrice, category, restaurantId } = menu;
let itemsResponse: ITemResponseDTO[] = [];
if (items?.length) {
Expand All @@ -20,13 +21,14 @@ export class MenuParser {
imageUrl,
basePrice,
restaurantId,
restaurant: restaurant ? RestaurantParser.createRestaurantResponse(restaurant) : undefined,
category: category ? CategoryParser.createCategoryResponse(category) : undefined,
items: itemsResponse,
...AuditParser.createAuditResponse(audit),
};
}

static createMenusResponse(menus: Menu[]): IMenuResponseDTO[] {
return menus.map((menu) => this.createMenuResponse(menu));
static createMenusResponse(menus: Menu[], restaurant?: Restaurant): IMenuResponseDTO[] {
return menus.map((menu) => this.createMenuResponse(menu, restaurant));
}
}
17 changes: 16 additions & 1 deletion backend/src/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { IMenuResponseDTO } from './menu-response.dto';
import { IMenuService } from './menu-service.interface';
import { MenuParser } from './menu.parser';
import { UpdateMenuDTO } from './update-menu.schema';
import { Restaurant } from 'src/restaurant';
@Injectable()
export class MenuService implements IMenuService {
private context: Context;
Expand Down Expand Up @@ -126,6 +127,20 @@ export class MenuService implements IMenuService {
if (result.getValue()) {
menus = result.getValue();
}
return Result.ok(menus && menus.length ? MenuParser.createMenusResponse(menus) : []);
return Result.ok(menus?.length ? MenuParser.createMenusResponse(menus) : []);
}

async getExtendedMenuByRestaurantId(restaurantId: string): Promise<Result<IMenuResponseDTO[]>> {
const result = await this.menuRepository.getMenuByRestaurantId(restaurantId);
let menus: Menu[] | [];
if (result.getValue()) {
menus = result.getValue();
}
const id = menus[0].restaurantId;
let restaurant: Restaurant | undefined;
if (id) {
restaurant = await this.restaurantRepository.getRestaurant(id);
}
return Result.ok(menus?.length ? MenuParser.createMenusResponse(menus, restaurant) : []);
}
}
11 changes: 7 additions & 4 deletions backend/src/restaurant/restaurant.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RestaurantData } from './../infrastructure/data_access/repositories/sch
import { LocationMapper } from './../location/location.mapper';
import { SingleClientMapper } from './../singleclient/singleclient.mapper';
import { Restaurant } from './restaurant';
import { SingleClient } from 'src/singleclient';

@Injectable()
export class RestaurantMapper implements IMapper<Restaurant, RestaurantData> {
Expand Down Expand Up @@ -48,7 +49,7 @@ export class RestaurantMapper implements IMapper<Restaurant, RestaurantData> {
paymentMethod,
openingHour,
closingHour,
menus: menus && menus.length ? menus.map((menu) => this.menuMapper.toPersistence(menu)) : [],
menus: menus?.length ? menus.map((menu) => this.menuMapper.toPersistence(menu)) : [],
location: this.locationMapper.toPersistence(entity.location),
singleclientId,
singleclient: this.singleclientMapper.toPersistence(entity.singleclient),
Expand All @@ -62,7 +63,7 @@ export class RestaurantMapper implements IMapper<Restaurant, RestaurantData> {
return document;
}

toDomain(document: RestaurantData): Restaurant {
toDomain(document: any): Restaurant {
const {
name,
email,
Expand All @@ -79,6 +80,8 @@ export class RestaurantMapper implements IMapper<Restaurant, RestaurantData> {
closingHour,
menus,
singleclientId,
singleclient,
location,
} = document;
const entity: Restaurant = Restaurant.create(
{
Expand All @@ -96,8 +99,8 @@ export class RestaurantMapper implements IMapper<Restaurant, RestaurantData> {
closingHour,
singleclientId,
menus: menus.length ? menus.map((menu) => this.menuMapper.toDomain(menu)) : [],
location: this.locationMapper.toDomain(document.location),
singleclient: this.singleclientMapper.toDomain(document.singleclient),
location: this.locationMapper.toDomain(location),
singleclient: singleclient ? this.singleclientMapper.toDomain(singleclient) : undefined,
audit: this.auditMapper.toDomain(document),
},
_id,
Expand Down
5 changes: 3 additions & 2 deletions backend/src/restaurant/restaurant.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IRestaurantResponseDTO } from './restaurant-response.dto';
export class RestaurantParser {
static createRestaurantResponse(restaurant: Restaurant): IRestaurantResponseDTO {
const { audit, location, singleclient, menus } = restaurant;
const auditResponse = audit ? { ...AuditParser.createAuditResponse(audit) } : undefined;
const restaurantResponse: IRestaurantResponseDTO = {
id: restaurant.id,
name: restaurant.name,
Expand All @@ -17,8 +18,8 @@ export class RestaurantParser {
timeZone: restaurant.timeZone,
menus: MenuParser.createMenusResponse(menus),
location: LocationParser.createLocationResponse(location),
...AuditParser.createAuditResponse(audit),
singleclient: SingleClientParser.createSingleClientResponse(singleclient),
...auditResponse,
singleclient: singleclient ? SingleClientParser.createSingleClientResponse(singleclient) : undefined,
};
return restaurantResponse;
}
Expand Down
1 change: 0 additions & 1 deletion backend/src/singleclient/singleclient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export class SingleClientService extends AuthService implements ISingleClientSer
}
return Result.ok(SingleClientParser.createSingleClientResponse(singleclient));
}
ß;

async getSingleClients(): Promise<Result<ISingleClientResponseDTO[]>> {
const isValidUser = await this.validateContext();
Expand Down
13 changes: 7 additions & 6 deletions frontend/src/apis/orderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OrderSummary, SelectedItem } from "./../reducers/cartReducer";
import { useShoppingCart } from "../hooks/UseShoppingCart";
import useAxiosPrivate from "../hooks/useAxiosPrivate";
import { IcartItems } from "../models/order.model";
import { ICreateOrderDTO } from "../dto/order";

const getOrderSummary = () => {
const { GetOrderSummary } = useShoppingCart();
Expand All @@ -12,17 +13,19 @@ export const createOrder = async () => {
return useAxiosPrivate();
};

const mapOrderSummaryToOrderRequest = () => {
const order = (): ICreateOrderDTO | undefined => {
let order: ICreateOrderDTO | undefined;
const orderSummary = getOrderSummary();
if (orderSummary?.length) {
return {
order = {
state: "CREATED",
type: "DINE_IN",
singleclientId: "63d792433b857e1697fe7017",
singleClientId: "63d792433b857e1697fe7017",
total: calculateOrderTotalPrice(orderSummary) ?? 0,
cartItems: cartItemsMapper(orderSummary),
};
}
return order;
};

const calculateOrderTotalPrice = (orderSummary: OrderSummary[]) => {
Expand All @@ -46,9 +49,7 @@ const cartItemsMapper = (orderSummary: OrderSummary[]): IcartItems[] => {
const cartItems = menus.map((menu) => {
return {
menuId: menu.id,
total:
menu.menuTotalPrice ??
0 - calculateCartItemsTotalPrice(menu.selectedItems ?? []),
total: menu.menuTotalPrice ?? 0 - calculateCartItemsTotalPrice(menu.selectedItems ?? []),
note: menu.note,
selectedItems: menu.selectedItems?.map((item) => {
return { ...item, itemId: item.id };
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Menu/MenuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const MenuList = () => {
const axiosPrivate = useAxiosPrivate();

const getMenus = async (): Promise<IMenus> => {
//Todo
//remove the add coded value and make it dynamic
const response = await axiosPrivate.get("/menus/singleclient/63d792433b857e1697fe7017");
return response.data;
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/dto/order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IcartItems } from "../models/order.model";

export interface ICreateOrderDTO {
state: string;
type: string;
singleClientId: string;
total: number;
cartItems: IcartItems[];
}
24 changes: 12 additions & 12 deletions frontend/src/utility/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@ export const calculateTotalOrderAmount = (): number => {
};

export const setLocalStorageData = (key: string, value: string, encrypt: boolean) => {
if (encrypt) {
try {
try {
if (encrypt) {
const encryptedText = cryptoJs.AES.encrypt(value, import.meta.env.VITE_SECRET);
if (encryptedText) {
localStorage.setItem(key, encryptedText.toString());
}
} catch (error) {
console.log("Error while saving user Data", error);
} else {
localStorage.setItem(key, value);
}
} else {
localStorage.setItem(key, value);
} catch (error) {
console.log("Error while saving user Data", error);
}
};

export const getLocalStorageData = (key: string, decrypt: boolean) => {
let value = localStorage.getItem(key);
if (value && decrypt) {
try {
try {
let value = localStorage.getItem(key);
if (value && decrypt) {
const decryptedText = cryptoJs.AES.decrypt(value, import.meta.env.VITE_SECRET);
return decryptedText.toString(cryptoJs.enc.Utf8);
} catch (error) {
console.log("Error while getting user data", error);
}
return value;
} catch (error) {
console.log("Error while getting user data", error);
}
return value;
};

export const clearStorage = () => {
Expand Down

0 comments on commit a5e6e6a

Please sign in to comment.