status añadido, cambios en el responsive al ver el producto
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
DROP VIEW "public"."v_product_store";--> statement-breakpoint
|
||||
ALTER TABLE "products" ALTER COLUMN "price" SET DEFAULT '0';--> statement-breakpoint
|
||||
ALTER TABLE "products" ALTER COLUMN "stock" SET DEFAULT 0;--> statement-breakpoint
|
||||
ALTER TABLE "products" ADD COLUMN "status" text DEFAULT 'BORRADOR' NOT NULL;--> statement-breakpoint
|
||||
CREATE VIEW "public"."v_product_store" AS (
|
||||
select p.id as product_id, p.title, p.description, p.price, p.stock, p.url_img, p.address, p.status, p.user_id, u.fullname, u.email, u.phone
|
||||
from products p
|
||||
left join auth.users as u on u.id = p.user_id);
|
||||
1537
apps/api/src/database/migrations/meta/0006_snapshot.json
Normal file
1537
apps/api/src/database/migrations/meta/0006_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -43,6 +43,13 @@
|
||||
"when": 1752500607554,
|
||||
"tag": "0005_little_bloodscream",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 6,
|
||||
"version": "7",
|
||||
"when": 1752507413748,
|
||||
"tag": "0006_real_tyger_tiger",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -9,10 +9,11 @@ export const products = t.pgTable(
|
||||
id: t.serial('id').primaryKey(),
|
||||
title: t.text('title').notNull(),
|
||||
description: t.text('description').notNull(),
|
||||
price: t.numeric('price').notNull(),
|
||||
stock: t.integer('stock').notNull(),
|
||||
price: t.numeric('price').notNull().default('0'),
|
||||
stock: t.integer('stock').notNull().default(0),
|
||||
address: t.text('address').notNull(),
|
||||
urlImg: t.text('url_img').notNull(),
|
||||
status: t.text('status').notNull().default('BORRADOR'),
|
||||
userId: t.integer('user_id').references(() => users.id, { onDelete: 'cascade' }).notNull(),
|
||||
...timestamps,
|
||||
}
|
||||
@@ -26,11 +27,12 @@ export const viewProductsStore = t.pgView('v_product_store', {
|
||||
stock: t.integer('stock'),
|
||||
urlImg: t.text('url_img'),
|
||||
address: t.text('address'),
|
||||
status: t.text('status'),
|
||||
userId: t.integer('user_id'),
|
||||
fullname: t.text('fullname'),
|
||||
email: t.text('email'),
|
||||
phone: t.text('phone')
|
||||
}).as(sql`
|
||||
select p.id as product_id, p.title, p.description, p.price, p.stock, p.url_img, p.address, p.user_id, u.fullname, u.email, u.phone
|
||||
select p.id as product_id, p.title, p.description, p.price, p.stock, p.url_img, p.address, p.status, p.user_id, u.fullname, u.email, u.phone
|
||||
from products p
|
||||
left join auth.users as u on u.id = p.user_id`);
|
||||
@@ -7,19 +7,31 @@ export async function seedProducts(db: NodePgDatabase<typeof schema>) {
|
||||
console.log('Seeding example product...');
|
||||
|
||||
// Insert inventory
|
||||
const array = [{title:'manzana',description:'fruta roja',price:'100',urlImg:'apple.avif',address:"Calle 1",userId:1,stock:0}];
|
||||
const array = [
|
||||
{
|
||||
title:'manzana',
|
||||
description:'Fruta pequeña y roja, extraída de los árboles de nuestra fundación, de increíble sabor',
|
||||
price:'100',
|
||||
stock:10,
|
||||
address:"Calle 1",
|
||||
status:'PUBLICADO', // PUBLICADO, AGOTADO, BORRADOR
|
||||
urlImg:'apple.avif',
|
||||
userId:1
|
||||
}
|
||||
];
|
||||
|
||||
for (const item of array) {
|
||||
try {
|
||||
await db.insert(products).values({
|
||||
title: item.title,
|
||||
description: item.description,
|
||||
price: item.price,
|
||||
stock: item.stock,
|
||||
address: item.address,
|
||||
urlImg: item.urlImg,
|
||||
userId: item.userId
|
||||
}).onConflictDoNothing();
|
||||
// await db.insert(products).values({
|
||||
// title: item.title,
|
||||
// description: item.description,
|
||||
// price: item.price,
|
||||
// stock: item.stock,
|
||||
// address: item.address,
|
||||
// urlImg: item.urlImg,
|
||||
// userId: item.userId
|
||||
// }).onConflictDoNothing();
|
||||
await db.insert(products).values(item).onConflictDoNothing();
|
||||
} catch (error) {
|
||||
console.error(`Error creating products '${item.title}':`, error);
|
||||
}
|
||||
|
||||
@@ -32,10 +32,17 @@ export class CreateProductDto {
|
||||
address: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsInt({
|
||||
message: 'stock must be a number',
|
||||
@IsString({
|
||||
message: 'address must be a string',
|
||||
})
|
||||
@IsOptional()
|
||||
status: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsInt({
|
||||
message: 'userID must be a number',
|
||||
})
|
||||
// @IsOptional()
|
||||
userId: number;
|
||||
|
||||
@ApiProperty()
|
||||
|
||||
@@ -20,6 +20,9 @@ export class UpdateProductDto extends PartialType(CreateProductDto) {
|
||||
@IsOptional()
|
||||
address: string;
|
||||
|
||||
@IsOptional()
|
||||
status: string;
|
||||
|
||||
@IsOptional()
|
||||
urlImg: string;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export class UsersController {
|
||||
@ApiOperation({ summary: 'Get all products with pagination and filters' })
|
||||
@ApiResponse({ status: 200, description: 'Return paginated products.' })
|
||||
async findAll(@Query() paginationDto: PaginationDto) {
|
||||
const result = await this.inventoryService.findAll(paginationDto);
|
||||
const result = await this.inventoryService.findAll(paginationDto,true);
|
||||
return {
|
||||
message: 'products fetched successfully',
|
||||
data: result.data,
|
||||
|
||||
@@ -56,6 +56,7 @@ export class InventoryService {
|
||||
address: products.address,
|
||||
price: products.price,
|
||||
stock: products.stock,
|
||||
status: products.status,
|
||||
urlImg: products.urlImg
|
||||
})
|
||||
.from(products)
|
||||
@@ -78,7 +79,7 @@ export class InventoryService {
|
||||
return { data, meta };
|
||||
}
|
||||
|
||||
async findAll(paginationDto?: PaginationDto): Promise<{ data: Store[], meta: any }> {
|
||||
async findAll(paginationDto?: PaginationDto, isStore: boolean = false): Promise<{ data: Store[], meta: any }> {
|
||||
const { page = 1, limit = 10, search = '', sortBy = 'id', sortOrder = 'asc' } = paginationDto || {};
|
||||
|
||||
// Calculate offset
|
||||
@@ -86,11 +87,21 @@ export class InventoryService {
|
||||
|
||||
// Build search condition
|
||||
let searchCondition: SQL<unknown> | undefined;
|
||||
if (search) {
|
||||
searchCondition = or(
|
||||
if (search && isStore) {
|
||||
searchCondition = and(
|
||||
or(
|
||||
like(viewProductsStore.title, `%${search}%`),
|
||||
like(viewProductsStore.description, `%${search}%`)
|
||||
),
|
||||
or(eq(viewProductsStore.status, 'PUBLICADO'), eq(viewProductsStore.status, 'AGOTADO'))
|
||||
)
|
||||
} else if(search){
|
||||
or(
|
||||
like(viewProductsStore.title, `%${search}%`),
|
||||
like(viewProductsStore.description, `%${search}%`),
|
||||
);
|
||||
like(viewProductsStore.description, `%${search}%`)
|
||||
)
|
||||
} else if(isStore){
|
||||
searchCondition = or(eq(viewProductsStore.status, 'PUBLICADO'), eq(viewProductsStore.status, 'AGOTADO'))
|
||||
}
|
||||
|
||||
// Build sort condition
|
||||
@@ -117,6 +128,7 @@ export class InventoryService {
|
||||
address: viewProductsStore.address,
|
||||
urlImg: viewProductsStore.urlImg,
|
||||
stock: viewProductsStore.stock,
|
||||
status: viewProductsStore.status,
|
||||
userId: viewProductsStore.userId,
|
||||
fullname: viewProductsStore.fullname,
|
||||
email: viewProductsStore.email,
|
||||
@@ -152,6 +164,7 @@ export class InventoryService {
|
||||
address: viewProductsStore.address,
|
||||
urlImg: viewProductsStore.urlImg,
|
||||
stock: viewProductsStore.stock,
|
||||
status: viewProductsStore.status,
|
||||
userId: viewProductsStore.userId,
|
||||
fullname: viewProductsStore.fullname,
|
||||
email: viewProductsStore.email,
|
||||
@@ -185,6 +198,7 @@ export class InventoryService {
|
||||
address: createProductDto.address,
|
||||
urlImg: createProductDto.urlImg,
|
||||
stock: createProductDto.stock,
|
||||
status: createProductDto.status,
|
||||
userId: createProductDto.userId
|
||||
})
|
||||
.returning();
|
||||
@@ -194,7 +208,8 @@ export class InventoryService {
|
||||
|
||||
async update(id: string, updateProductDto: UpdateProductDto): Promise<Product> {
|
||||
const productId = parseInt(id);
|
||||
|
||||
console.log(updateProductDto);
|
||||
|
||||
// Check if exists
|
||||
await this.findOne(id);
|
||||
|
||||
@@ -204,6 +219,7 @@ export class InventoryService {
|
||||
if (updateProductDto.description) updateData.description = updateProductDto.description;
|
||||
if (updateProductDto.price) updateData.price = updateProductDto.price;
|
||||
if (updateProductDto.address) updateData.address = updateProductDto.address;
|
||||
if (updateProductDto.status) updateData.status = updateProductDto.status;
|
||||
if (updateProductDto.stock) updateData.stock = updateProductDto.stock;
|
||||
if (updateProductDto.urlImg) updateData.urlImg = updateProductDto.urlImg;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user