Created
December 31, 2024 22:24
-
-
Save Taofiqq/2cf2ffcbef3368833be2cdea551cf75b to your computer and use it in GitHub Desktop.
Orders Module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Post, Get, Param, Body } from '@nestjs/common'; | |
import { OrderService } from './order.service'; | |
import { CreateOrderDto } from './order.dto'; | |
import { CurrentTenant } from '../tenants/tenant.decorator'; | |
import { Tenant } from '../tenants/tenant.entity'; | |
@Controller('orders') | |
export class OrderController { | |
constructor(private readonly orderService: OrderService) {} | |
@Post() | |
create(@Body() createOrderDto: CreateOrderDto, @CurrentTenant() tenant: Tenant) { | |
return this.orderService.create(createOrderDto, tenant.id); | |
} | |
@Get() | |
findAll(@CurrentTenant() tenant: Tenant) { | |
return this.orderService.findAll(tenant.id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IsString, IsArray, ValidateNested } from 'class-validator'; | |
import { Type } from 'class-transformer'; | |
export class CreateOrderDto { | |
@IsString() | |
customerName: string; | |
@IsArray() | |
@ValidateNested({ each: true }) | |
@Type(() => OrderItemDto) | |
items: OrderItemDto[]; | |
} | |
export class OrderItemDto { | |
@IsString() | |
productId: string; | |
@IsString() | |
quantity: number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany, JoinColumn } from 'typeorm'; | |
import { TenantAwareEntity } from '../tenants/tenant-aware.entity'; | |
import { Product } from '../products/product.entity'; | |
@Entity('orders') | |
export class Order extends TenantAwareEntity { | |
@PrimaryGeneratedColumn('uuid') | |
id: string; | |
@Column() | |
customerName: string; | |
@Column() | |
totalPrice: number; | |
@Column() | |
orderDate: Date; | |
@OneToMany(() => OrderItem, (item) => item.order, { cascade: true }) | |
items: OrderItem[]; | |
} | |
@Entity('order_items') | |
export class OrderItem { | |
@PrimaryGeneratedColumn('uuid') | |
id: string; | |
@ManyToOne(() => Product) | |
@JoinColumn() | |
product: Product; | |
@ManyToOne(() => Order, (order) => order.items) | |
order: Order; | |
@Column() | |
quantity: number; | |
@Column({ type: 'decimal', precision: 10, scale: 2 }) | |
unitPrice: number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Post, Get, Param, Body } from '@nestjs/common'; | |
import { OrderService } from './order.service'; | |
import { CreateOrderDto } from './order.dto'; | |
import { CurrentTenant } from '../tenants/tenant.decorator'; | |
import { Tenant } from '../tenants/tenant.entity'; | |
@Controller('orders') | |
export class OrderController { | |
constructor(private readonly orderService: OrderService) {} | |
@Post() | |
create(@Body() createOrderDto: CreateOrderDto, @CurrentTenant() tenant: Tenant) { | |
return this.orderService.create(createOrderDto, tenant.id); | |
} | |
@Get() | |
findAll(@CurrentTenant() tenant: Tenant) { | |
return this.orderService.findAll(tenant.id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NotFoundException } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { Repository } from 'typeorm'; | |
import { Order, OrderItem } from './order.entity'; | |
import { Product } from '../products/product.entity'; | |
import { CreateOrderDto } from './order.dto'; | |
@Injectable() | |
export class OrderService { | |
constructor( | |
@InjectRepository(Order) private orderRepository: Repository<Order>, | |
@InjectRepository(OrderItem) private orderItemRepository: Repository<OrderItem>, | |
@InjectRepository(Product) private productRepository: Repository<Product>, | |
) {} | |
async create(createOrderDto: CreateOrderDto, tenantId: string) { | |
const items = await Promise.all( | |
createOrderDto.items.map(async (item) => { | |
const product = await this.productRepository.findOne({ | |
where: { id: item.productId, tenantId }, | |
}); | |
if (!product) { | |
throw new NotFoundException(`Product ${item.productId} not found`); | |
} | |
return this.orderItemRepository.create({ | |
product, | |
quantity: item.quantity, | |
unitPrice: product.price, | |
}); | |
}), | |
); | |
const totalPrice = items.reduce( | |
(sum, item) => sum + item.quantity * item.unitPrice, | |
0, | |
); | |
const order = this.orderRepository.create({ | |
customerName: createOrderDto.customerName, | |
totalPrice, | |
orderDate: new Date(), | |
items, | |
tenantId, | |
}); | |
return this.orderRepository.save(order); | |
} | |
findAll(tenantId: string) { | |
return this.orderRepository.find({ where: { tenantId }, relations: ['items', 'items.product'] }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment