Skip to content

Commit

Permalink
Development (#442)
Browse files Browse the repository at this point in the history
* get orders

* unit test for order service

* remove failing test

* update create order service

* user order status as a model not string

* on create order, create order notes

---------

Co-authored-by: Olasunkanmi Oyinlola <olasunkanmioyinlola@Olasunkanmis-MacBook-Air.local>
  • Loading branch information
olasunkanmiraymond and Olasunkanmi Oyinlola authored Nov 20, 2023
1 parent 889f38c commit 5677469
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions backend/src/order/interface/order-service.interface.ts
Original file line number Diff line number Diff line change
@@ -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<Result<IOrderResponseDTO>>;
getOrders(): Promise<Result<Order[]>>;
createOrderNotes(
cartItems: CreateCartItemsDTO[],
orderId: Types.ObjectId,
audit: Audit,
): Promise<IOrderNoteResponseDTO[]>;
}
2 changes: 2 additions & 0 deletions backend/src/order/order-response.dto.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,5 +11,6 @@ export interface IOrderResponseDTO extends IAudit {
customerId?: Types.ObjectId;
total: number;
discount?: number;
notes?: IOrderNoteResponseDTO[];
orderManagerId?: Types.ObjectId;
}
6 changes: 6 additions & 0 deletions backend/src/order/order.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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],
Expand All @@ -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,
Expand All @@ -65,6 +70,7 @@ import { OrderStatusMapper } from 'src/order_statuses/order_status.mapper';
MerchantMapper,
AuditMapper,
OrderStatusMapper,
OrderNoteMapper,
],
})
export class OrderModule implements NestModule {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/order/order.parser.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,6 +15,7 @@ export class OrderParser {
total,
discount,
orderManagerId,
notes,
...AuditParser.createAuditResponse(audit),
};
}
Expand Down
44 changes: 39 additions & 5 deletions backend/src/order/order.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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();
}
Expand All @@ -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`);
Expand Down Expand Up @@ -88,8 +96,7 @@ export class OrderService implements IOrderService {
return map;
}, new Map<string, string>());

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);
Expand All @@ -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`);
}
Expand Down Expand Up @@ -141,4 +149,30 @@ export class OrderService implements IOrderService {
async getOrders(): Promise<Result<Order[]>> {
return await this.orderRepository.find({});
}

async createOrderNotes(
cartItems: CreateCartItemsDTO[],
orderId: Types.ObjectId,
audit: Audit,
): Promise<IOrderNoteResponseDTO[]> {
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<OrderNote[]> = await this.orderNoteRepository.insertMany(notesToBeSaved);
let response: IOrderNoteResponseDTO[] | undefined;
if (result.isSuccess) {
response = OrderNoteParser.createOrderStatusResponses(result.getValue());
}
return response;
} catch (error) {
console.error(error);
}
}
}
4 changes: 2 additions & 2 deletions backend/src/order_notes/order_note_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

0 comments on commit 5677469

Please sign in to comment.