In the Order
logic, what is similar to its respective part in the Core module :
Copy export class OrderItemDto {
@ IsEntity ()
readonly product : IdDto ;
@ IsCardinal ()
readonly quantity : number ;
}
Implement the CreateOrderDto
Copy export class CreateOrderDto {
@ IsEntity ()
readonly customer : IdDto ;
@ ArrayNotEmpty ()
@ ValidateNested ()
@ Type (() => OrderItemDto)
readonly items : OrderItemDto [];
}
Delete the UpdateOrderDto
, and the update()
method in the controller and service
Now what's new: in the service , we should, both in findAll()
and findOne()
, fetch the relations. Note that we need to use include
whenever fetching an entity with relations.
Copy include : {
items : {
include : {
product : true ,
} ,
} ,
customer : true ,
payment : true ,
} ,
And the aux method to create items with price . What should be noticed is, when creating the orderItem
, the Prisma dynamic type , which allows for robust type safety.
Copy private async createOrderItemWithPrice (orderItemDto: OrderItemDto) {
const { id } = orderItemDto .product;
const { price } = await this . prisma . product .findUniqueOrThrow ({
where : { id } ,
});
const orderItem : Prisma . OrderItemCreateManyOrderInput = {
... orderItemDto ,
productId : id ,
price ,
};
return orderItem;
}
And finally the create()
method. Note that we connect
the order
with an existing user
but also create orderItems
when creating the order
.
Copy async create (createOrderDto: CreateOrderDto) {
const { customer , items } = createOrderDto;
const itemsWithPrice = await Promise .all (
items .map ((item) => this .createOrderItemWithPrice (item)) ,
);
return this . prisma . order .create ({
data : {
customer : { connect : customer } ,
items : { createMany : { data : itemsWithPrice } } ,
} ,
});
}
Commit - Implementing order logic
Last updated 4 months ago