Vista (intefaz y bd), esquema y endpoints de store agregados
This commit is contained in:
@@ -4,7 +4,8 @@ import {
|
||||
ApiResponseSchema,
|
||||
InventoryTable,
|
||||
productMutate,
|
||||
editInventory
|
||||
// editInventory,
|
||||
productApiResponseSchema
|
||||
} from '../schemas/inventory';
|
||||
|
||||
import { auth } from '@/lib/auth';
|
||||
@@ -27,8 +28,8 @@ export const getInventoryAction = async (params: {
|
||||
|
||||
const [error, response] = await safeFetchApi(
|
||||
ApiResponseSchema,
|
||||
`/inventory?${searchParams}`,
|
||||
'GET',
|
||||
`/products/inventory?${searchParams}`,
|
||||
'GET'
|
||||
);
|
||||
|
||||
if (error) {
|
||||
@@ -53,6 +54,52 @@ export const getInventoryAction = async (params: {
|
||||
};
|
||||
}
|
||||
|
||||
export const getAllProducts = async (params: {
|
||||
page?: number;
|
||||
limit?: number;
|
||||
search?: string;
|
||||
sortBy?: string;
|
||||
sortOrder?: 'asc' | 'desc';
|
||||
}) => {
|
||||
|
||||
const session = await auth()
|
||||
|
||||
const searchParams = new URLSearchParams({
|
||||
page: (params.page || 1).toString(),
|
||||
limit: (params.limit || 10).toString(),
|
||||
...(params.search && { search: params.search }),
|
||||
...(params.sortBy && { sortBy: params.sortBy }),
|
||||
...(params.sortOrder && { sortOrder: params.sortOrder }),
|
||||
})
|
||||
|
||||
const id = session?.user.id
|
||||
|
||||
const [error, response] = await safeFetchApi(
|
||||
productApiResponseSchema,
|
||||
`/products/store?${searchParams}`,
|
||||
'GET'
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.error('Error:', error);
|
||||
throw new Error(error.message);
|
||||
}
|
||||
|
||||
return {
|
||||
data: response?.data || [],
|
||||
meta: response?.meta || {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
totalCount: 0,
|
||||
totalPages: 1,
|
||||
hasNextPage: false,
|
||||
hasPreviousPage: false,
|
||||
nextPage: null,
|
||||
previousPage: null,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const createProductAction = async (payload: InventoryTable) => {
|
||||
const session = await auth()
|
||||
const userId = session?.user?.id
|
||||
@@ -62,14 +109,14 @@ export const createProductAction = async (payload: InventoryTable) => {
|
||||
|
||||
const [error, data] = await safeFetchApi(
|
||||
productMutate,
|
||||
'/inventory',
|
||||
'/products',
|
||||
'POST',
|
||||
payloadWithoutId,
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.error(error);
|
||||
throw new Error('Error al crear el usuario');
|
||||
throw new Error('Error al crear el producto');
|
||||
}
|
||||
|
||||
return payloadWithoutId;
|
||||
@@ -81,16 +128,14 @@ export const updateUserAction = async (payload: InventoryTable) => {
|
||||
|
||||
const [error, data] = await safeFetchApi(
|
||||
productMutate,
|
||||
`/inventory/${id}`,
|
||||
`/products/${id}`,
|
||||
'PATCH',
|
||||
payloadWithoutId,
|
||||
);
|
||||
|
||||
// console.log(data);
|
||||
if (error) {
|
||||
console.error(error);
|
||||
|
||||
throw new Error(error?.message || 'Error al actualizar el usuario');
|
||||
throw new Error(error?.message || 'Error al actualizar el producto');
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
@@ -98,17 +143,14 @@ export const updateUserAction = async (payload: InventoryTable) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteUserAction = async (id: Number) => {
|
||||
export const deleteProductAction = async (id: Number) => {
|
||||
const [error] = await safeFetchApi(
|
||||
productMutate,
|
||||
`/users/${id}`,
|
||||
`/products/${id}`,
|
||||
'DELETE'
|
||||
)
|
||||
|
||||
console.log(error);
|
||||
|
||||
|
||||
// if (error) throw new Error(error.message || 'Error al eliminar el usuario')
|
||||
if (error) throw new Error(error.message || 'Error al eliminar el usuario')
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user