Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/modules/inventory/controllers/product.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Controller,
Get,
Post,
Patch,
Delete,
Body,
Param,
Query,
UseGuards,
HttpCode,
HttpStatus,
Inject,
} from '@nestjs/common';
import { PRODUCT_SERVICE, IProductService } from '../services/product.service.interface';
import { Product, ProductWithStockSummary } from '../interfaces/product.interface';
import { CreateProductDto } from '../dto/create-product.dto';
import { UpdateProductDto } from '../dto/update-product.dto';
import { QueryProductDto } from '../dto/query-product.dto';
import { PaginatedResult } from '../../../shared/interfaces/index';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles } from '../../auth/decorators/roles.decorator';

@Controller('products')
@UseGuards(JwtAuthGuard)
export class ProductController {
constructor(
@Inject(PRODUCT_SERVICE) private readonly productService: IProductService,
) {}

@Get()
async findAll(@Query() query: QueryProductDto): Promise<PaginatedResult<Product>> {
return this.productService.findAll(query);
}

@Get(':id')
async findById(@Param('id') id: string): Promise<ProductWithStockSummary> {
return this.productService.findById(id);
}

@Get('sku/:sku')
async findBySku(@Param('sku') sku: string): Promise<ProductWithStockSummary> {
return this.productService.findBySku(sku);
}

@Post()
@UseGuards(RolesGuard)
@Roles('admin', 'manager')
async create(@Body() dto: CreateProductDto): Promise<Product> {
return this.productService.create(dto);
}

@Patch(':id')
@UseGuards(RolesGuard)
@Roles('admin', 'manager')
async update(
@Param('id') id: string,
@Body() dto: UpdateProductDto,
): Promise<Product> {
return this.productService.update(id, dto);
}

@Delete(':id')
@UseGuards(RolesGuard)
@Roles('admin')
@HttpCode(HttpStatus.NO_CONTENT)
async remove(@Param('id') id: string): Promise<void> {
return this.productService.softDelete(id);
}
}
39 changes: 39 additions & 0 deletions src/modules/inventory/dto/create-product.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IsString, IsNotEmpty, IsOptional, IsNumber, Min } from 'class-validator';
import { Type } from 'class-transformer';

export class CreateProductDto {
@IsString()
@IsNotEmpty()
sku!: string;

@IsString()
@IsNotEmpty()
name!: string;

@IsString()
@IsOptional()
description?: string;

@IsString()
@IsOptional()
category?: string;

@IsString()
@IsNotEmpty()
unitOfMeasure!: string;

@IsNumber()
@IsOptional()
@Type(() => Number)
unitPrice?: number;

@IsNumber()
@IsOptional()
@Type(() => Number)
@Min(0)
reorderPoint?: number;

@IsString()
@IsOptional()
imageUrl?: string;
}
38 changes: 38 additions & 0 deletions src/modules/inventory/dto/query-product.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { IsString, IsOptional, IsNumber, IsBoolean, Min, Max, IsIn } from 'class-validator';
import { Type } from 'class-transformer';

export class QueryProductDto {
@IsNumber()
@IsOptional()
@Type(() => Number)
@Min(1)
page?: number = 1;

@IsNumber()
@IsOptional()
@Type(() => Number)
@Min(1)
@Max(100)
limit?: number = 20;

@IsString()
@IsOptional()
search?: string;

@IsString()
@IsOptional()
category?: string;

@IsBoolean()
@IsOptional()
isActive?: boolean;

@IsString()
@IsOptional()
sortBy?: string;

@IsString()
@IsOptional()
@IsIn(['asc', 'desc'])
sortOrder?: 'asc' | 'desc';
}
39 changes: 39 additions & 0 deletions src/modules/inventory/dto/update-product.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IsString, IsOptional, IsNumber, IsBoolean, Min } from 'class-validator';
import { Type } from 'class-transformer';

export class UpdateProductDto {
@IsString()
@IsOptional()
name?: string;

@IsString()
@IsOptional()
description?: string;

@IsString()
@IsOptional()
category?: string;

@IsString()
@IsOptional()
unitOfMeasure?: string;

@IsNumber()
@IsOptional()
@Type(() => Number)
unitPrice?: number;

@IsNumber()
@IsOptional()
@Type(() => Number)
@Min(0)
reorderPoint?: number;

@IsBoolean()
@IsOptional()
isActive?: boolean;

@IsString()
@IsOptional()
imageUrl?: string;
}
6 changes: 5 additions & 1 deletion src/modules/inventory/inventory.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { ProductController } from './controllers/product.controller';
import { ProductService } from './services/product.service';
import { LocationService } from './services/location.service';
import { MovementService } from './services/movement.service';
Expand All @@ -9,15 +10,18 @@ import { LOCATION_SERVICE } from './services/location.service.interface';
import { MOVEMENT_SERVICE } from './services/movement.service.interface';
import { STOCK_SERVICE } from './services/stock.service.interface';
import { ALERT_SERVICE } from './services/alert.service.interface';
import { PRODUCT_REPOSITORY } from './repositories/product.repository.interface';
import { FirestoreProductRepository } from './repositories/product.repository';

@Module({
controllers: [], // Controllers will be added when implemented
controllers: [ProductController],
providers: [
{ provide: PRODUCT_SERVICE, useClass: ProductService },
{ provide: LOCATION_SERVICE, useClass: LocationService },
{ provide: MOVEMENT_SERVICE, useClass: MovementService },
{ provide: STOCK_SERVICE, useClass: StockService },
{ provide: ALERT_SERVICE, useClass: AlertService },
{ provide: PRODUCT_REPOSITORY, useClass: FirestoreProductRepository },
],
exports: [PRODUCT_SERVICE, LOCATION_SERVICE, MOVEMENT_SERVICE, STOCK_SERVICE, ALERT_SERVICE],
})
Expand Down
Loading