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 #408

Merged
merged 29 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0a6668e
implement the menu search
olasunkanmi-SE Jul 26, 2023
ff364d1
fix code smell
olasunkanmi-SE Jul 26, 2023
65b6009
calculate service charge
olasunkanmi-SE Sep 11, 2023
c6c683f
update sonar project config
olasunkanmi-SE Sep 11, 2023
cc60885
update total order calculation
olasunkanmi-SE Sep 11, 2023
d2dfe38
add cart items to order
olasunkanmi-SE Sep 11, 2023
541a0a3
move email header check to the backend
olasunkanmi-SE Sep 11, 2023
55765a0
remove quantity from order
olasunkanmi-SE Sep 23, 2023
5c3e4c7
Merge branch 'release' into development
olasunkanmi-SE Sep 23, 2023
f0dfa7b
update selected items to include itemId
olasunkanmi-SE Sep 23, 2023
faa996d
Merge branch 'development' of github.com:olasunkanmi-SE/restaurant in…
olasunkanmi-SE Sep 23, 2023
dead621
make cartitem and selectedCart Items optional in parent models
olasunkanmi-SE Sep 23, 2023
0dcffa7
Merge branch 'release' into development
olasunkanmi-SE Sep 23, 2023
ad8f925
create the order service
olasunkanmi-SE Sep 24, 2023
ddb05dd
Merge branch 'development' of github.com:olasunkanmi-SE/restaurant in…
olasunkanmi-SE Sep 24, 2023
e28d2ef
Merge branch 'release' into development
olasunkanmi-SE Sep 24, 2023
a83f120
create order controller
olasunkanmi-SE Sep 24, 2023
d90a534
checkout and fix (#391)
olasunkanmi-SE Oct 24, 2023
a998d1e
Test (#393)
olasunkanmi-SE Oct 24, 2023
599e37c
Test (#396)
olasunkanmi-SE Oct 24, 2023
3ab65de
Merge branch 'release' into development
olasunkanmi-SE Oct 24, 2023
35a3d9d
Test (#400)
olasunkanmi-SE Oct 25, 2023
b6dd7f1
Test (#403)
olasunkanmi-SE Oct 26, 2023
c094940
Merge branch 'release' into development
olasunkanmi-SE Oct 26, 2023
2bd6071
create the create order service
olasunkanmi-SE Oct 29, 2023
1d3abea
Merge branch 'release' into development
olasunkanmi-SE Oct 29, 2023
b4f6733
create order
olasunkanmi-SE Oct 29, 2023
f118a7b
Merge branch 'development' of github.com:olasunkanmi-SE/restaurant in…
olasunkanmi-SE Oct 29, 2023
420f2a9
Merge branch 'release' into development
olasunkanmi-SE Oct 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: ChatGPT comment
uses: kxxt/chatgpt-action@v0.3
id: chatgpr
with:
number: ${{ github.event.pull_request.number }}
sessionToken: ${{ secrets.CHATGPT_SESSION_TOKEN }}
split: "yolo"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions backend/src/application/constants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export const TYPES = {
IAddonRepository: Symbol('IAddonRepository'),
IOrderRepository: Symbol('IOrderRepository'),
IOrderService: Symbol('IOrderService'),
ICartItemRepository: Symbol('ICartItemRepository'),
};
2 changes: 1 addition & 1 deletion backend/src/cart/cart-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CartItem extends Entity<ICartItem> implements ICartItem {
return this._selectedItems;
}

set setSelectedItems(selectedItems: SelectedCartItem[] | undefined) {
set selectedItems(selectedItems: SelectedCartItem[] | undefined) {
this._selectedItems = selectedItems;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import { CartItemMapper } from './../../../cart/cart-item.mapper';
import { Injectable } from '@nestjs/common';
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
import { Connection, Model } from 'mongoose';
import { CartItem } from 'src/cart/cart-item';
import { GenericDocumentRepository } from 'src/infrastructure/database';
import { CartItemMapper } from './../../../cart/cart-item.mapper';
import { ICartItemRepository } from './interfaces/cart-item-repository.interface';
import { CartItemDataModel, CartItemDocument } from './schemas/cartItem.schema';
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
import { Connection, Model } from 'mongoose';
import { Result } from 'src/domain';

@Injectable()
export class CartItemRepository extends GenericDocumentRepository<CartItem, CartItemDocument> {
export class CartItemRepository
extends GenericDocumentRepository<CartItem, CartItemDocument>
implements ICartItemRepository
{
cartItemMapper: CartItemMapper;
constructor(
@InjectModel(CartItemDataModel.name) cartItemDataModel: Model<CartItemDocument>,
@InjectConnection() readonly connection: Connection,
cartItemMapper: CartItemMapper,
) {
super(cartItemDataModel, connection, cartItemMapper);
this.cartItemMapper = cartItemMapper;
}

async updateCartItemSelectedItems(cartItems: CartItem[]): Promise<void> {
const document = cartItems.map((doc) => this.cartItemMapper.toPersistence(doc));
document.forEach((item) => {
this.updateOne({ _id: item._id }, { selectedItems: item.selectedItems });
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { CartItem } from 'src/cart/cart-item';
import { IGenericDocument } from 'src/infrastructure/database';
import { CartItemDocument } from '../schemas/cartItem.schema';

export interface ICartItemRepository extends IGenericDocument<CartItem, CartItemDocument> {
updateCartItemSelectedItems(cartItems: CartItem[]): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface IGenericDocument<TEntity, T> {

findOneAndUpdate(filterQuery: FilterQuery<T>, update: UpdateQuery<T>): Promise<Result<TEntity | null>>;

upsert(filterQuery: FilterQuery<T>, document: Partial<T>): Promise<TEntity | unknown>;
upsert(filterQuery: FilterQuery<T>, document: Partial<T>): Promise<Result<TEntity | null>>;

deleteMany(filterQuery: FilterQuery<T>): Promise<boolean>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,20 @@ export abstract class GenericDocumentRepository<TEntity, T extends Document> imp
select?: ProjectionType<T | null>,
options?: QueryOptions<T>,
): Promise<Result<TEntity[] | []>> {
const documents = await this.DocumentModel.find(query, select, options)
.skip(options ? options.skip : null)
.limit(options ? options.limit : null)
.lean()
.exec();
const entities = documents?.length ? documents.map((document) => this.mapper.toDomain(document)) : [];
return Result.ok(entities);
try {
const documents = await this.DocumentModel.find(query, select, options)
.skip(options ? options.skip : null)
.limit(options ? options.limit : null)
.lean()
.exec();
if (!documents?.length) {
throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, 'documents does not exist');
}
const entities = documents?.length ? documents.map((document) => this.mapper.toDomain(document)) : [];
return Result.ok(entities);
} catch (error) {
console.error(error);
}
}

async pagination(query: FilterQuery<T>, select: ProjectionType<T | null>, options: QueryOptions<T>) {
Expand Down Expand Up @@ -116,7 +123,7 @@ export abstract class GenericDocumentRepository<TEntity, T extends Document> imp
return Result.ok(entity);
}

async upsert(filterQuery: FilterQuery<T>, document: Partial<T>): Promise<unknown> {
async upsert(filterQuery: FilterQuery<T>, document: Partial<T>): Promise<Result<TEntity | null>> {
const result = await this.DocumentModel.findOneAndUpdate(filterQuery, document, {
lean: true,
upsert: true,
Expand All @@ -127,7 +134,7 @@ export abstract class GenericDocumentRepository<TEntity, T extends Document> imp
return Result.fail('Unable to update the database', HttpStatus.INTERNAL_SERVER_ERROR);
}
const entity = this.mapper.toDomain(result);
return entity;
return Result.ok(entity);
}

async deleteMany(filterQuery: FilterQuery<T>): Promise<boolean> {
Expand Down Expand Up @@ -159,9 +166,32 @@ export abstract class GenericDocumentRepository<TEntity, T extends Document> imp
}

async updateOne(filter: any, query: any): Promise<Result<TEntity>> {
const document = await this.DocumentModel.updateOne(filter, { $set: query });
const entity: TEntity = this.mapper.toDomain(document as any);
return Result.ok(entity);
try {
const document = await this.DocumentModel.updateOne(filter, { $set: query });
if (!document) {
throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, 'Unable to update document in the database');
}
const entity: TEntity = this.mapper.toDomain(document as any);
return Result.ok(entity);
} catch (error) {
console.error(error);
}
}

async updateMany(query: FilterQuery<T>, updateBody: UpdateQuery<T>): Promise<Result<TEntity[]>> {
try {
const saved = await this.DocumentModel.updateMany(query, updateBody, {
multi: true,
});
if (saved.matchedCount < 1) {
throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, 'Unable to update documents into the database');
}
const updatedDocuments = await this.DocumentModel.find(query);
const entities: TEntity[] = updatedDocuments.map((doc) => this.mapper.toDomain(doc));
return Result.ok(entities);
} catch (error) {
console.error(error);
}
}

createDocument(document: any) {
Expand Down
1 change: 1 addition & 0 deletions backend/src/order/order.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { ContextMiddleWare } from 'src/infrastructure/middlewares';
controllers: [OrderController],
providers: [
{ provide: TYPES.IOrderService, useClass: OrderService },
{ provide: TYPES.ICartItemRepository, useClass: CartItemRepository },
{ provide: TYPES.IOrderRepository, useClass: OrderRepository },
{ provide: TYPES.IMerchantService, useClass: MerchantService },
{ provide: TYPES.IContextService, useClass: ContextService },
Expand Down
33 changes: 27 additions & 6 deletions backend/src/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Order } from './order';
import { IOrderResponseDTO } from './order-response.dto';
import { OrderMapper } from './order.mapper';
import { OrderParser } from './order.parser';
import { ICartItemRepository } from 'src/infrastructure/data_access/repositories/interfaces/cart-item-repository.interface';

export class OrderService implements IOrderService {
private context: Context;
Expand All @@ -29,11 +30,11 @@ export class OrderService implements IOrderService {
@Inject(TYPES.IContextService)
private readonly contextService: IContextService,
private readonly merchantRepository: MerchantRepository,
private readonly cartItemRepository: CartItemRepository,
private readonly selectedCartItemRepository: SelectedCartItemRepository,
private readonly orderMapper: OrderMapper,
private readonly selectedItemMapper: SelectedCartItemMapper,
private readonly cartItemMapper: CartItemMapper,
@Inject(TYPES.ICartItemRepository) private readonly cartItemRepository: ICartItemRepository,
) {
this.context = this.contextService.getContext();
}
Expand Down Expand Up @@ -64,6 +65,14 @@ export class OrderService implements IOrderService {
const cartItemDataModels: CartItemDataModel[] = items.map((item) => this.cartItemMapper.toPersistence(item));
const savedCartItems: Result<CartItem[]> = await this.cartItemRepository.insertMany(cartItemDataModels);
const savedItems = savedCartItems.getValue();
savedOrder.cartItems = savedItems;
const orderWithCartItems = await this.orderRepository.upsert(
{ _id: orderId },
this.orderMapper.toPersistence(savedOrder),
);
if (orderWithCartItems.isSuccess === false) {
throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, `Error while creating order`);
}

const cartItemMap = savedItems.reduce((map, item) => {
map.set(this.orderRepository.objectIdToString(item.menuId), this.orderRepository.objectIdToString(item.id));
Expand All @@ -73,13 +82,12 @@ export class OrderService implements IOrderService {
const cartSelectedItems = cartItems.map((item) => item.selectedItems);
const flattenedSelectedItems = cartSelectedItems.flat();
flattenedSelectedItems.forEach((item) => {
console.log(item.menuId);
if (cartItemMap.has(this.orderRepository.objectIdToString(item.menuId))) {
item.cartItemId = this.orderRepository.stringToObjectId(
cartItemMap.get(this.orderRepository.objectIdToString(item.menuId)),
);
const cartItemId = cartItemMap.get(this.orderRepository.objectIdToString(item.menuId));
item.cartItemId = this.orderRepository.stringToObjectId(cartItemId);
}
});

const selectedItems = flattenedSelectedItems.map((item) => SelectedCartItem.create({ ...item, audit }));
const selectedCartItemsDataModel = selectedItems.map((item) => this.selectedItemMapper.toPersistence(item));
const insertedItems: Result<SelectedCartItem[]> = await this.selectedCartItemRepository.insertMany(
Expand All @@ -91,12 +99,25 @@ export class OrderService implements IOrderService {
} else {
throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, `Could not create an order`);
}

const savedSelectedItems = insertedItems.getValue();
const savedItemsMap = savedSelectedItems.reduce((map, item) => {
const cartItemIdToString = this.cartItemRepository.objectIdToString(item.cartItemId);
!map.has(cartItemIdToString) ? map.set(cartItemIdToString, [item]) : map.get(cartItemIdToString).push(item);
return map;
}, new Map<string, SelectedCartItem[]>());
savedItems.forEach((item) => {
if (savedItemsMap.has(this.cartItemRepository.objectIdToString(item.id))) {
item.selectedItems = savedItemsMap.get(this.cartItemRepository.objectIdToString(item.id));
}
});
await this.cartItemRepository.updateCartItemSelectedItems(savedItems);
await session.commitTransaction();
return Result.ok(response);
}
} catch (error) {
session.abortTransaction();
} finally {
await session.commitTransaction();
await session.endSession();
}
}
Expand Down
Loading