product.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. async function sendOrder(order, token) {
  2. try {
  3. const response = await fetch("/api/orders/send", {
  4. method: "POST",
  5. headers: {
  6. "Content-Type": "application/json",
  7. "Authorization": `Bearer ${token}`
  8. },
  9. body: JSON.stringify(order)
  10. });
  11. if (!response.ok) {
  12. const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
  13. throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  14. }
  15. const data = await response.json();
  16. return data;
  17. } catch (error) {
  18. console.error("Error al enviar la orden:", error);
  19. throw error;
  20. }
  21. }
  22. export async function freeBeer(token, tableNumber) {
  23. try {
  24. const response = await fetch("/api/users/reward", {
  25. method: "POST",
  26. headers: {
  27. "Content-Type": "application/json",
  28. "Authorization": `Bearer ${token}`
  29. },
  30. body: JSON.stringify({ tableNumber })
  31. });
  32. if (!response.ok) {
  33. const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
  34. throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  35. }
  36. const data = await response.json();
  37. return data;
  38. } catch (error) {
  39. console.error("Error al obtener cerveza gratis:", error);
  40. throw error;
  41. }
  42. }
  43. async function getProducts(token){
  44. const response = await fetch("/api/products?status=1", {
  45. headers: {
  46. "Content-Type": "application/json",
  47. "Authorization": `Bearer ${token}`
  48. }
  49. });
  50. if (!response.ok) {
  51. const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
  52. throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  53. }
  54. const data = await response.json();
  55. data.products.map(product => {
  56. if (product.promotion) {
  57. product.price = product.promotion.price;
  58. }
  59. })
  60. return data.products;
  61. }
  62. export {sendOrder, getProducts}