Crear-editar productos, depuracion de archivos users

This commit is contained in:
2025-06-27 13:14:50 -04:00
parent ed2a1da038
commit 2840bbec11
23 changed files with 333 additions and 1193 deletions

View File

@@ -18,12 +18,19 @@ export class CreateProductDto {
price: string;
@ApiProperty()
@IsString({
@IsInt({
message: 'stock must be a number',
})
@IsOptional()
stock: number;
@ApiProperty()
@IsInt({
message: 'stock must be a number',
})
@IsOptional()
userId: number;
@ApiProperty()
@IsString({
message: 'urlImg must be a string',

View File

@@ -5,4 +5,15 @@ export class Product {
price: string;
stock: number;
urlImg: string;
UserId?: number;
}
export class CreateProduct {
id: number;
title: string;
description: string;
price: string;
stock: string;
urlImg: string;
UserId: number;
}

View File

@@ -3,7 +3,7 @@ import { InventoryService } from './inventory.service';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Roles } from '../../common/decorators/roles.decorator';
// import { Roles } from '../../common/decorators/roles.decorator';
import { PaginationDto } from '../../common/dto/pagination.dto';
@ApiTags('inventory')
@@ -42,22 +42,19 @@ export class UsersController {
@Body() createUserDto: CreateProductDto,
@Query('roleId') roleId?: string,
) {
const data = await this.inventoryService.create(
createUserDto,
roleId ? parseInt(roleId) : undefined,
);
const data = await this.inventoryService.create(createUserDto)
return { message: 'User created successfully', data };
}
// @Patch(':id')
@Patch(':id')
// @Roles('admin')
// @ApiOperation({ summary: 'Update a user' })
// @ApiResponse({ status: 200, description: 'User updated successfully.' })
// @ApiResponse({ status: 404, description: 'User not found.' })
// async update(@Param('id') id: string, @Body() UpdateProductDto: UpdateProductDto) {
// const data = await this.inventoryService.update(id, UpdateProductDto);
// return { message: 'User updated successfully', data };
// }
@ApiOperation({ summary: 'Update a product' })
@ApiResponse({ status: 200, description: 'Product updated successfully.' })
@ApiResponse({ status: 404, description: 'Product not found.' })
async update(@Param('id') id: string, @Body() UpdateProductDto: UpdateProductDto) {
const data = await this.inventoryService.update(id, UpdateProductDto);
return { message: 'User updated successfully', data };
}
// @Delete(':id')
// @Roles('admin')

View File

@@ -6,7 +6,7 @@ import { products } from 'src/database/index';
import { eq, like, or, SQL, sql, and, not } from 'drizzle-orm';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { Product } from './entities/inventory.entity';
import { Product, CreateProduct } from './entities/inventory.entity';
import { PaginationDto } from '../../common/dto/pagination.dto';
@Injectable()
@@ -54,13 +54,8 @@ export class InventoryService {
price: products.price,
urlImg: products.urlImg,
stock: products.stock,
// price: products.price,
// quantity: products.quantity,
// isActive: products.isActive
})
.from(products)
// .leftJoin(usersRole, eq(usersRole.userId, users.id))
// .leftJoin(roles, eq(roles.id, usersRole.roleId))
.where(searchCondition)
.orderBy(orderBy)
.limit(limit)
@@ -77,9 +72,6 @@ export class InventoryService {
nextPage: page < totalPages ? page + 1 : null,
previousPage: page > 1 ? page - 1 : null,
};
console.log(data);
return { data, meta };
}
@@ -94,16 +86,10 @@ export class InventoryService {
stock: products.stock
})
.from(products)
// .leftJoin(usersRole, eq(usersRole.userId, users.id))
// .leftJoin(roles, eq(roles.id, usersRole.roleId))
// .leftJoin(schema.states, eq(schema.states.id, users.state))
// .leftJoin(schema.municipalities, eq(schema.municipalities.id, users.municipality))
// .leftJoin(schema.parishes, eq(schema.parishes.id, users.parish))
.where(eq(products.id, parseInt(id)));
if (find.length === 0) {
throw new HttpException('User does not exist', HttpStatus.BAD_REQUEST);
throw new HttpException('Product does not exist', HttpStatus.BAD_REQUEST);
}
return find[0];
@@ -111,14 +97,13 @@ export class InventoryService {
// Rest of the service remains the same
async create(
createProductDto: CreateProductDto,
roleId: number = 2,
): Promise<Product> {
createProductDto: CreateProductDto
): Promise<any> {
// Start a transaction
return await this.drizzle.transaction(async (tx) => {
// Create the user
const [newProduct] = await tx
.insert(products)
.values({
@@ -126,72 +111,33 @@ export class InventoryService {
description: createProductDto.description,
price: createProductDto.price,
urlImg: createProductDto.urlImg,
stock: createProductDto.stock
stock: createProductDto.stock,
userId: createProductDto.userId
})
.returning();
// Assign role to user
// await tx.insert(usersRole).values({
// userId: newProduct.id,
// roleId: roleId,
// });
// Return the created user with role
// const [userWithRole] = await tx
// .select({
// id: users.id,
// username: users.username,
// email: users.email,
// fullname: users.fullname,
// phone: users.phone,
// isActive: users.isActive,
// role: roles.name,
// })
// .from(users)
// .leftJoin(usersRole, eq(usersRole.userId, users.id))
// .leftJoin(roles, eq(roles.id, usersRole.roleId))
// .where(eq(users.id, newProduct.id));
return this.findOne(String(newProduct.id));
return newProduct
});
}
// async update(id: string, updateUserDto: UpdateUserDto): Promise<User> {
// const userId = parseInt(id);
async update(id: string, updateProductDto: UpdateProductDto): Promise<Product> {
const productId = parseInt(id);
// // Check if user exists
// await this.findOne(id);
// Check if exists
await this.findOne(id);
// // Prepare update data
// const updateData: any = {};
// if (updateUserDto.username) updateData.username = updateUserDto.username;
// if (updateUserDto.email) updateData.email = updateUserDto.email;
// if (updateUserDto.fullname) updateData.fullname = updateUserDto.fullname;
// if (updateUserDto.password) {
// updateData.password = await bcrypt.hash(updateUserDto.password, 10);
// }
// if (updateUserDto.phone) updateData.phone = updateUserDto.phone;
// if (updateUserDto.isActive) updateData.isActive = updateUserDto.isActive;
// Prepare update data
const updateData: any = {};
if (updateProductDto.title) updateData.title = updateProductDto.title;
if (updateProductDto.description) updateData.description = updateProductDto.description;
if (updateProductDto.price) updateData.price = updateProductDto.price;
if (updateProductDto.stock) updateData.stock = updateProductDto.stock;
if (updateProductDto.urlImg) updateData.urlImg = updateProductDto.urlImg;
// const updateDataRole: any = {};
// if (updateUserDto.role) updateDataRole.roleId = updateUserDto.role;
// // Update user
// await this.drizzle
// .update(users)
// .set(updateData)
// .where(eq(users.id, userId));
// await this.drizzle
// .update(usersRole)
// .set(updateDataRole)
// .where(eq(usersRole.userId, userId));
// // Return updated user
// return this.findOne(id);
// }
const [updatedProduct] = await this.drizzle.update(products).set(updateData).where(eq(products.id, productId)).returning();
return updatedProduct
// Return updated user
// return this.findOne(id);
}
// async remove(id: string): Promise<{ message: string, data: User }> {