Skip to content

Instantly share code, notes, and snippets.

@Taofiqq
Created December 31, 2024 22:24
Show Gist options
  • Save Taofiqq/2cf2ffcbef3368833be2cdea551cf75b to your computer and use it in GitHub Desktop.
Save Taofiqq/2cf2ffcbef3368833be2cdea551cf75b to your computer and use it in GitHub Desktop.
Orders Module
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);
}
}
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;
}
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;
}
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);
}
}
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