user.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { showError } from "../utils/error.js";
  2. export async function getUserData(token) {
  3. try {
  4. const response = await fetch('/api/users/user', {
  5. headers: {
  6. 'Content-Type': 'application/json',
  7. 'Authorization': `Bearer ${token}`
  8. }
  9. });
  10. if (response.status === 401) {
  11. showError("No autorizado. Verifica tu sesión.");
  12. throw new Error("Unauthorized access");
  13. }
  14. if (response.status === 429) {
  15. showError("Demasiados intentos. Intenta más tarde.");
  16. throw new Error("Too many requests");
  17. }
  18. if (response.status === 500) {
  19. showError("Error interno del servidor. Intenta más tarde.");
  20. throw new Error("Internal server error");
  21. }
  22. if (response.status === 403) {
  23. showError("Acceso prohibido. Verifica tus permisos.");
  24. throw new Error("Forbidden access");
  25. }
  26. if (response.status !== 200) {
  27. showError(response.message);
  28. throw new Error(`Error fetching user data: ${response.statusText}`);
  29. }
  30. const userData = await response.json();
  31. return userData;
  32. } catch (error) {
  33. console.error('Error fetching user data:', error);
  34. throw error;
  35. }
  36. }
  37. async function fetchUserSales(userId, token) {
  38. try {
  39. const response = await fetch(`/api/sales/user/${userId}`, {
  40. headers: {
  41. 'Content-Type': 'application/json',
  42. 'Authorization': `Bearer ${token}`
  43. }
  44. });
  45. if (response.status === 404) {
  46. showError("No se encontraron ventas para este usuario.");
  47. return [];
  48. } else if (response.status === 401) {
  49. showError("No autorizado. Verifica tu sesión.");
  50. throw new Error("Unauthorized access");
  51. } else if (response.status === 429) {
  52. showError("Demasiados intentos. Intenta más tarde.");
  53. throw new Error("Too many requests");
  54. } else if (response.status === 500) {
  55. showError("Error interno del servidor. Intenta más tarde.");
  56. throw new Error("Internal server error");
  57. } else if (response.status === 403) {
  58. showError("Acceso prohibido. Verifica tus permisos.");
  59. throw new Error("Forbidden access");
  60. }
  61. if (response.status !== 200) {
  62. showError(response.message);
  63. throw new Error(`Error fetching user sales: ${response.statusText}`);
  64. }
  65. const sales = await response.json();
  66. return sales.sales;
  67. } catch (error) {
  68. console.error('Error fetching user sales:', error);
  69. throw error;
  70. }
  71. }
  72. async function historyProducts(userId, token) {
  73. const sales = await fetchUserSales(userId, token);
  74. if (!sales || sales.length === 0) {
  75. console.warn("No hay ventas para mostrar en el historial.");
  76. return [];
  77. }
  78. const products = sales.map(sale => (
  79. sale.products.map(product => ({
  80. quantity: product.quantity,
  81. productName: product.name,
  82. price: product.price.toFixed(0)
  83. }))
  84. ));
  85. return products.flat(2);
  86. }
  87. export { fetchUserSales, historyProducts };