diff --git a/backend/src/infrastructure/data_access/repositories/cart-item.repository.ts b/backend/src/infrastructure/data_access/repositories/cart-item.repository.ts index c038bca3..adaf83ee 100644 --- a/backend/src/infrastructure/data_access/repositories/cart-item.repository.ts +++ b/backend/src/infrastructure/data_access/repositories/cart-item.repository.ts @@ -6,7 +6,6 @@ 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 { Result } from 'src/domain'; @Injectable() export class CartItemRepository diff --git a/backend/src/order/interface/order-service.interface.ts b/backend/src/order/interface/order-service.interface.ts index a68d1a74..6b28a038 100644 --- a/backend/src/order/interface/order-service.interface.ts +++ b/backend/src/order/interface/order-service.interface.ts @@ -1,9 +1,16 @@ -import { Result } from 'src/domain'; -import { CreateOrderDTO } from '../dto/create-order.dto'; +import { Audit, Result } from 'src/domain'; +import { CreateCartItemsDTO, CreateOrderDTO } from '../dto/create-order.dto'; import { IOrderResponseDTO } from '../order-response.dto'; import { Order } from '../order'; +import { Types } from 'mongoose'; +import { IOrderNoteResponseDTO } from 'src/order_notes/dto/order-note-response'; export interface IOrderService { createOrder(orderSummary: CreateOrderDTO): Promise>; getOrders(): Promise>; + createOrderNotes( + cartItems: CreateCartItemsDTO[], + orderId: Types.ObjectId, + audit: Audit, + ): Promise; } diff --git a/backend/src/order/order-response.dto.ts b/backend/src/order/order-response.dto.ts index 9b797e92..17ef6c99 100644 --- a/backend/src/order/order-response.dto.ts +++ b/backend/src/order/order-response.dto.ts @@ -1,5 +1,6 @@ import { Types } from 'mongoose'; import { IAudit } from 'src/infrastructure'; +import { IOrderNoteResponseDTO } from 'src/order_notes/dto/order-note-response'; import { OrderStatus } from 'src/order_statuses/order_status'; export interface IOrderResponseDTO extends IAudit { @@ -10,5 +11,6 @@ export interface IOrderResponseDTO extends IAudit { customerId?: Types.ObjectId; total: number; discount?: number; + notes?: IOrderNoteResponseDTO[]; orderManagerId?: Types.ObjectId; } diff --git a/backend/src/order/order.module.ts b/backend/src/order/order.module.ts index fe6137d1..119f4b13 100644 --- a/backend/src/order/order.module.ts +++ b/backend/src/order/order.module.ts @@ -34,6 +34,9 @@ import { OrderStatusSchema, } from 'src/infrastructure/data_access/repositories/schemas/order-status.schema'; import { OrderStatusMapper } from 'src/order_statuses/order_status.mapper'; +import { OrderNoteMapper } from 'src/order_notes/order_note.mapper'; +import { OrderNoteRepository } from 'src/infrastructure/data_access/repositories/order-note.repository'; +import { OrderNoteModel, OrderNoteSchema } from 'src/infrastructure/data_access/repositories/schemas/order-note.schema'; @Module({ imports: [ @@ -44,6 +47,7 @@ import { OrderStatusMapper } from 'src/order_statuses/order_status.mapper'; { name: CartItemDataModel.name, schema: CartItemSchema }, { name: SelectedCartItemDataModel.name, schema: SelectedCartItemSchema }, { name: OrderStatusModel.name, schema: OrderStatusSchema }, + { name: OrderNoteModel.name, schema: OrderNoteSchema }, ]), ], controllers: [OrderController], @@ -55,6 +59,7 @@ import { OrderStatusMapper } from 'src/order_statuses/order_status.mapper'; { provide: TYPES.IContextService, useClass: ContextService }, { provide: TYPES.IValidateUser, useClass: ValidateUser }, { provide: TYPES.IOrderStatusRepository, useClass: OrderStatusRepository }, + { provide: TYPES.IOrderNoteRepository, useClass: OrderNoteRepository }, MerchantRepository, CartItemRepository, SelectedCartItemRepository, @@ -65,6 +70,7 @@ import { OrderStatusMapper } from 'src/order_statuses/order_status.mapper'; MerchantMapper, AuditMapper, OrderStatusMapper, + OrderNoteMapper, ], }) export class OrderModule implements NestModule { diff --git a/backend/src/order/order.parser.ts b/backend/src/order/order.parser.ts index 416876a6..a4530a96 100644 --- a/backend/src/order/order.parser.ts +++ b/backend/src/order/order.parser.ts @@ -1,9 +1,10 @@ +import { IOrderNoteResponseDTO } from 'src/order_notes/dto/order-note-response'; import { AuditParser } from './../audit/audit.parser'; import { Order } from './order'; import { IOrderResponseDTO } from './order-response.dto'; export class OrderParser { - static createOrderResponse(order: Order): IOrderResponseDTO { + static createOrderResponse(order: Order, notes?: IOrderNoteResponseDTO[]): IOrderResponseDTO { const { id, state, type, merchantId, customerId, total, discount, orderManagerId, audit } = order; return { id, @@ -14,6 +15,7 @@ export class OrderParser { total, discount, orderManagerId, + notes, ...AuditParser.createAuditResponse(audit), }; } diff --git a/backend/src/order/order.service.ts b/backend/src/order/order.service.ts index 112a8dc8..551e14ed 100644 --- a/backend/src/order/order.service.ts +++ b/backend/src/order/order.service.ts @@ -1,3 +1,4 @@ +import { OrderNoteMapper } from 'src/order_notes/order_note.mapper'; import { HttpStatus, Inject } from '@nestjs/common'; import { TYPES } from 'src/application'; import { CartItem } from 'src/cart/cart-item'; @@ -13,13 +14,18 @@ import { throwApplicationError } from 'src/infrastructure/utilities/exception-in import { IMerchantService, Merchant } from 'src/merchant'; import { CartItemMapper } from './../cart/cart-item.mapper'; import { SelectedCartItemMapper } from './../cart/selectedItems/selected-cart-item.mapper'; -import { CreateOrderDTO } from './dto/create-order.dto'; +import { CreateCartItemsDTO, CreateOrderDTO } from './dto/create-order.dto'; import { IOrderService } from './interface/order-service.interface'; import { Order } from './order'; import { IOrderResponseDTO } from './order-response.dto'; import { OrderMapper } from './order.mapper'; import { OrderParser } from './order.parser'; import { IOrderStatusRespository } from 'src/infrastructure/data_access/repositories/interfaces/order-status.repository'; +import { OrderNote } from 'src/order_notes/order_note'; +import { IOrderNoteRespository } from 'src/infrastructure/data_access/repositories/interfaces/order-note.repository'; +import { OrderNoteParser } from 'src/order_notes/order_note_parser'; +import { IOrderNoteResponseDTO } from 'src/order_notes/dto/order-note-response'; +import { Types } from 'mongoose'; export class OrderService implements IOrderService { private context: Context; @@ -33,8 +39,10 @@ export class OrderService implements IOrderService { private readonly orderMapper: OrderMapper, private readonly selectedItemMapper: SelectedCartItemMapper, private readonly cartItemMapper: CartItemMapper, + private readonly orderNoteMapper: OrderNoteMapper, @Inject(TYPES.ICartItemRepository) private readonly cartItemRepository: ICartItemRepository, @Inject(TYPES.IOrderStatusRepository) private readonly orderStatusRespository: IOrderStatusRespository, + @Inject(TYPES.IOrderNoteRepository) private readonly orderNoteRepository: IOrderNoteRespository, ) { this.context = this.contextService.getContext(); } @@ -54,7 +62,7 @@ export class OrderService implements IOrderService { session.startTransaction(); try { const audit: Audit = Audit.createInsertContext(this.context); - const merchantObjId = await this.orderRepository.stringToObjectId(merchantId); + const merchantObjId = this.orderRepository.stringToObjectId(merchantId); const getOrderStatus = await this.orderStatusRespository.findOne({ code: state.toUpperCase() }); if (!getOrderStatus) { throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, `Order status not found`); @@ -88,8 +96,7 @@ export class OrderService implements IOrderService { return map; }, new Map()); - const cartSelectedItems = cartItems.map((item) => item.selectedItems); - const flattenedSelectedItems = cartSelectedItems.flat(); + const flattenedSelectedItems = cartItems.flatMap((item) => item.selectedItems); flattenedSelectedItems.forEach((item) => { if (cartItemMap.has(item.menuId)) { const cartItemId = cartItemMap.get(item.menuId); @@ -110,8 +117,9 @@ export class OrderService implements IOrderService { selectedCartItemsDataModel, ); let response: IOrderResponseDTO | undefined; + const notes = await this.createOrderNotes(cartItems, orderId, audit); if (insertedItems.isSuccess) { - response = OrderParser.createOrderResponse(savedOrder); + response = OrderParser.createOrderResponse(savedOrder, notes); } else { throwApplicationError(HttpStatus.INTERNAL_SERVER_ERROR, `Could not create an order`); } @@ -141,4 +149,30 @@ export class OrderService implements IOrderService { async getOrders(): Promise> { return await this.orderRepository.find({}); } + + async createOrderNotes( + cartItems: CreateCartItemsDTO[], + orderId: Types.ObjectId, + audit: Audit, + ): Promise { + try { + const orderNotes = cartItems.map((item) => { + return { + menuId: item.menuId, + note: item.note ? item.note : '', + orderId: orderId, + }; + }); + const createOrderNotes: OrderNote[] = orderNotes.map((note) => OrderNote.create({ ...note, audit })); + const notesToBeSaved = createOrderNotes.map((note) => this.orderNoteMapper.toPersistence(note)); + const result: Result = await this.orderNoteRepository.insertMany(notesToBeSaved); + let response: IOrderNoteResponseDTO[] | undefined; + if (result.isSuccess) { + response = OrderNoteParser.createOrderStatusResponses(result.getValue()); + } + return response; + } catch (error) { + console.error(error); + } + } } diff --git a/backend/src/order_notes/order_note_parser.ts b/backend/src/order_notes/order_note_parser.ts index ee62f71f..b1edde53 100644 --- a/backend/src/order_notes/order_note_parser.ts +++ b/backend/src/order_notes/order_note_parser.ts @@ -7,9 +7,9 @@ export class OrderNoteParser { return { id, note, orderId, ...AuditParser.createAuditResponse(audit) }; } - static createOrderStatusResponses(orderNote: OrderNote[]) { + static createOrderStatusResponses(orderNote: OrderNote[]): IOrderNoteResponseDTO[] { return orderNote.map((note) => { - this.createResponse(note); + return this.createResponse(note); }); } }