Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ import { AuthGuard } from './auth.guard';
],
providers: [AuthService, UserService, AuthGuard],
controllers: [AuthController],
exports: [AuthService, AuthGuard, JwtModule],
exports: [AuthService, AuthGuard, JwtModule, UserService],
})
export class AuthModule {}
19 changes: 19 additions & 0 deletions src/products/dto/create-product.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsInt, IsOptional, IsString, IsUUID } from 'class-validator';

export class CreateProductDTO {
@IsString()
name: string;

@IsString()
genericName: string;

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

@IsInt()
priority: number;

@IsUUID()
manufacturer: string;
}
25 changes: 25 additions & 0 deletions src/products/products.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {
Body,
Controller,
DefaultValuePipe,
Get,
ParseIntPipe,
Post,
Query,
Req,
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import { ProductsService } from './products.service';
import {
Expand All @@ -18,6 +22,10 @@ import { Request } from 'express';
import { ConfigService } from '@nestjs/config';
import { getPaginationUrl } from 'src/utils/pagination-urls';
import { PaginationDTO } from 'src/utils/dto/pagination.dto';
import { CreateProductDTO } from './dto/create-product.dto';
import { Product } from './entities/product.entity';
import { AuthGuard, CustomRequest } from 'src/auth/auth.guard';
import { UserRole } from 'src/user/entities/user.entity';

@Controller('product')
@ApiExtraModels(PaginationDTO, ProductPresentationDTO)
Expand Down Expand Up @@ -60,4 +68,21 @@ export class ProductsController {
const products = await this.productsServices.getProducts(page, limit);
return { results: products, count, next, previous };
}

@Post()
@UseGuards(AuthGuard)
async createProduct(
@Body() createProductDto: CreateProductDTO,
@Req() request: CustomRequest,
): Promise<Product> {
if (![UserRole.ADMIN, UserRole.BRANCH_ADMIN].includes(request.user.role)) {
throw new UnauthorizedException();
}

const manufacturer = await this.productsServices.findManufacturer(
createProductDto.manufacturer,
);

return this.productsServices.createProduct(createProductDto, manufacturer);
}
}
7 changes: 6 additions & 1 deletion src/products/products.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { ProductsService } from './products.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Product } from './entities/product.entity';
import { ProductPresentation } from './entities/product-presentation.entity';
import { Manufacturer } from './entities/manufacturer.entity';
import { AuthModule } from 'src/auth/auth.module';

@Module({
imports: [TypeOrmModule.forFeature([Product, ProductPresentation])],
imports: [
TypeOrmModule.forFeature([Product, ProductPresentation, Manufacturer]),
AuthModule,
],
controllers: [ProductsController],
providers: [ProductsService],
})
Expand Down
32 changes: 31 additions & 1 deletion src/products/products.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Product } from './entities/product.entity';
import { Repository } from 'typeorm';
import { ProductPresentation } from './entities/product-presentation.entity';
import { CreateProductDTO } from './dto/create-product.dto';
import { Manufacturer } from './entities/manufacturer.entity';

@Injectable()
export class ProductsService {
Expand All @@ -12,6 +14,9 @@ export class ProductsService {

@InjectRepository(Product)
private productRepository: Repository<Product>,

@InjectRepository(Manufacturer)
private manufacturerRepository: Repository<Manufacturer>,
) {}

async countProducts(): Promise<number> {
Expand Down Expand Up @@ -43,4 +48,29 @@ export class ProductsService {

return products;
}

async findManufacturer(id: string): Promise<Manufacturer> {
const manufacturer = await this.manufacturerRepository.findOne({
where: { id },
});

if (!manufacturer) {
throw new NotFoundException('Manufacturer not found');
}

return manufacturer;
}

async createProduct(
createProductDto: CreateProductDTO,
manufacturer: Manufacturer,
): Promise<Product> {
const newProduct = this.productRepository.create({
...createProductDto,
manufacturer,
});

const savedProduct = await this.productRepository.save(newProduct);
return savedProduct;
}
}