| 1 | import axios from "axios";
|
|---|
| 2 |
|
|---|
| 3 | const orderRoute = "/orders";
|
|---|
| 4 |
|
|---|
| 5 | const OrderStatus = {
|
|---|
| 6 | PendingUserApproval: 'PendingUserApproval',
|
|---|
| 7 | PendingAdminApproval: 'PendingAdminApproval',
|
|---|
| 8 | Approved: 'Approved',
|
|---|
| 9 | Delivering: 'Delivering',
|
|---|
| 10 | Finished: 'Finished',
|
|---|
| 11 | Terminated: 'Terminated'
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | const GetAllOrders = async (statuses) => {
|
|---|
| 15 | try {
|
|---|
| 16 | const response = await axios.get(`${orderRoute}`, {
|
|---|
| 17 | params: {
|
|---|
| 18 | statuses: statuses
|
|---|
| 19 | }
|
|---|
| 20 | });
|
|---|
| 21 | return response.data;
|
|---|
| 22 | } catch (error) {
|
|---|
| 23 | console.error("Error occured", error);
|
|---|
| 24 | }
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | const CreateOrder = async (formData) => {
|
|---|
| 28 | try {
|
|---|
| 29 | const response = await axios.post(`${orderRoute}`, formData);
|
|---|
| 30 | return response.data;
|
|---|
| 31 | } catch (error) {
|
|---|
| 32 | console.error("Error occured", error);
|
|---|
| 33 | }
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | const UpdateOrder = async (id, formData) => {
|
|---|
| 37 | try {
|
|---|
| 38 | const response = await axios.post(`${orderRoute}/${id}`, formData);
|
|---|
| 39 | return response.data;
|
|---|
| 40 | } catch (error) {
|
|---|
| 41 | console.error("Error occured", error);
|
|---|
| 42 | }
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | const DeleteOrder = async (id) => {
|
|---|
| 46 | try {
|
|---|
| 47 | const response = await axios.delete(`${orderRoute}/${id}`);
|
|---|
| 48 | return response.data;
|
|---|
| 49 | } catch (error) {
|
|---|
| 50 | console.error("Error occured", error);
|
|---|
| 51 | }
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | const GetOrder = async (id) => {
|
|---|
| 55 | try {
|
|---|
| 56 | const response = await axios.get(`${orderRoute}/${id}`);
|
|---|
| 57 | return response.data;
|
|---|
| 58 | } catch (error) {
|
|---|
| 59 | console.error("Error occured", error);
|
|---|
| 60 | }
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | const AssignOrderDriver = async (id, formData) => {
|
|---|
| 64 | try {
|
|---|
| 65 | const response = await axios.post(`${orderRoute}/${id}/driver`, formData);
|
|---|
| 66 | return response.data;
|
|---|
| 67 | } catch (error) {
|
|---|
| 68 | console.error("Error occured", error);
|
|---|
| 69 | }
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | const AssignOrderAdmin = async (id, formData) => {
|
|---|
| 73 | try {
|
|---|
| 74 | const response = await axios.post(`${orderRoute}/${id}/admin`, formData);
|
|---|
| 75 | return response.data;
|
|---|
| 76 | } catch (error) {
|
|---|
| 77 | console.error("Error occured", error);
|
|---|
| 78 | }
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | const GetMyOrders = async (id) => {
|
|---|
| 82 | try {
|
|---|
| 83 | const response = await axios.get(`${orderRoute}/customer/${id}`);
|
|---|
| 84 | return response.data;
|
|---|
| 85 | } catch (error) {
|
|---|
| 86 | console.error("Error occured", error);
|
|---|
| 87 | }
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | const OrderDomainToDtoMapper = (order) => {
|
|---|
| 91 | if (!order)
|
|---|
| 92 | return null;
|
|---|
| 93 |
|
|---|
| 94 | return {
|
|---|
| 95 | potrosuvacId: order.potrosuvac?.id,
|
|---|
| 96 | status: order.status,
|
|---|
| 97 | menuItems: order.narackaMenuItems?.map(nmp => ({menuItemId: nmp?.menuItem?.id, quantity: nmp.quantity})) ?? []
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | const GetActiveOrder = async (id) => {
|
|---|
| 102 | try {
|
|---|
| 103 | const response = await axios.get(`${orderRoute}/customer/${id}/active`);
|
|---|
| 104 | return response.data;
|
|---|
| 105 | } catch (error) {
|
|---|
| 106 | console.error("Error occured", error);
|
|---|
| 107 | }
|
|---|
| 108 | };
|
|---|
| 109 |
|
|---|
| 110 | export {
|
|---|
| 111 | GetAllOrders,
|
|---|
| 112 | GetOrder,
|
|---|
| 113 | CreateOrder,
|
|---|
| 114 | UpdateOrder,
|
|---|
| 115 | DeleteOrder,
|
|---|
| 116 | OrderStatus,
|
|---|
| 117 | AssignOrderDriver,
|
|---|
| 118 | AssignOrderAdmin,
|
|---|
| 119 | GetMyOrders,
|
|---|
| 120 | GetActiveOrder,
|
|---|
| 121 | OrderDomainToDtoMapper
|
|---|
| 122 | }
|
|---|