114 lines
2.4 KiB
TypeScript
114 lines
2.4 KiB
TypeScript
'use server';
|
|
import { safeFetchApi } from '@/lib/fetch.api';
|
|
import {
|
|
ApiResponseSchema,
|
|
InventoryTable,
|
|
productMutate,
|
|
editInventory
|
|
} from '../schemas/inventory';
|
|
|
|
import { auth } from '@/lib/auth';
|
|
|
|
export const getInventoryAction = async (params: {
|
|
page?: number;
|
|
limit?: number;
|
|
search?: string;
|
|
sortBy?: string;
|
|
sortOrder?: 'asc' | 'desc';
|
|
}) => {
|
|
|
|
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 [error, response] = await safeFetchApi(
|
|
ApiResponseSchema,
|
|
`/inventory?${searchParams}`,
|
|
'GET',
|
|
);
|
|
|
|
if (error) {
|
|
console.error(error);
|
|
|
|
throw new Error(error.message);
|
|
}
|
|
// const transformedData = response?.data ? transformSurvey(response?.data) : undefined;
|
|
|
|
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
|
|
const { id, ...payloadWithoutId } = payload;
|
|
|
|
payloadWithoutId.userId = userId
|
|
|
|
const [error, data] = await safeFetchApi(
|
|
productMutate,
|
|
'/inventory',
|
|
'POST',
|
|
payloadWithoutId,
|
|
);
|
|
|
|
if (error) {
|
|
console.error(error);
|
|
throw new Error('Error al crear el usuario');
|
|
}
|
|
|
|
return payloadWithoutId;
|
|
};
|
|
|
|
export const updateUserAction = async (payload: InventoryTable) => {
|
|
try {
|
|
const { id, ...payloadWithoutId } = payload;
|
|
|
|
const [error, data] = await safeFetchApi(
|
|
productMutate,
|
|
`/inventory/${id}`,
|
|
'PATCH',
|
|
payloadWithoutId,
|
|
);
|
|
|
|
// console.log(data);
|
|
if (error) {
|
|
console.error(error);
|
|
|
|
throw new Error(error?.message || 'Error al actualizar el usuario');
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
export const deleteUserAction = async (id: Number) => {
|
|
const [error] = await safeFetchApi(
|
|
productMutate,
|
|
`/users/${id}`,
|
|
'DELETE'
|
|
)
|
|
|
|
console.log(error);
|
|
|
|
|
|
// if (error) throw new Error(error.message || 'Error al eliminar el usuario')
|
|
|
|
return true;
|
|
} |