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

@@ -1,49 +1,14 @@
'use server';
import { safeFetchApi } from '@/lib/fetch.api';
import {
surveysApiResponseSchema,
CreateUser,
ApiResponseSchema,
InventoryTable,
productMutate,
UpdateUser
editInventory
} from '../schemas/inventory';
import { auth } from '@/lib/auth';
export const getProfileAction = async () => {
const session = await auth()
const id = session?.user?.id
const [error, response] = await safeFetchApi(
productMutate,
`/users/${id}`,
'GET'
);
if (error) throw new Error(error.message);
return response;
};
export const updateProfileAction = async (payload: UpdateUser) => {
const { id, ...payloadWithoutId } = payload;
const [error, data] = await safeFetchApi(
productMutate,
`/users/profile/${id}`,
'PATCH',
payloadWithoutId,
);
console.log(payload);
if (error) {
if (error.message === 'Email already exists') {
throw new Error('Ese correo ya está en uso');
}
// console.error('Error:', error);
throw new Error('Error al crear el usuario');
}
return data;
};
export const getInventoryAction = async (params: {
page?: number;
limit?: number;
@@ -61,7 +26,7 @@ export const getInventoryAction = async (params: {
});
const [error, response] = await safeFetchApi(
surveysApiResponseSchema,
ApiResponseSchema,
`/inventory?${searchParams}`,
'GET',
);
@@ -71,8 +36,6 @@ export const getInventoryAction = async (params: {
throw new Error(error.message);
}
// const transformedData = response?.data ? transformSurvey(response?.data) : undefined;
return {
@@ -90,37 +53,35 @@ export const getInventoryAction = async (params: {
};
}
export const createUserAction = async (payload: CreateUser) => {
const { id, confirmPassword, ...payloadWithoutId } = payload;
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,
'/users',
'/inventory',
'POST',
payloadWithoutId,
);
if (error) {
if (error.message === 'Username already exists') {
throw new Error('Ese usuario ya existe');
}
if (error.message === 'Email already exists') {
throw new Error('Ese correo ya está en uso');
}
// console.error('Error:', error);
console.error(error);
throw new Error('Error al crear el usuario');
}
return payloadWithoutId;
};
export const updateUserAction = async (payload: UpdateUser) => {
export const updateUserAction = async (payload: InventoryTable) => {
try {
const { id, ...payloadWithoutId } = payload;
const [error, data] = await safeFetchApi(
productMutate,
`/users/${id}`,
`/inventory/${id}`,
'PATCH',
payloadWithoutId,
);