2024-12-07 23:40:22 +03:00
|
|
|
import { getConfigValue } from '@brojs/cli';
|
2025-01-19 10:44:58 +03:00
|
|
|
import dayjs from 'dayjs';
|
2024-12-07 23:40:22 +03:00
|
|
|
|
|
|
|
enum ArmEndpoints {
|
|
|
|
ORDERS = '/arm/orders',
|
|
|
|
MASTERS = '/arm/masters',
|
|
|
|
}
|
|
|
|
|
|
|
|
const armService = () => {
|
|
|
|
const endpoint = getConfigValue('dry-wash.api');
|
|
|
|
|
2024-12-14 22:27:07 +03:00
|
|
|
const fetchOrders = async ({ date }: { date: Date }) => {
|
2025-01-19 10:50:28 +03:00
|
|
|
const startDate = dayjs(date).startOf('day').toISOString();
|
|
|
|
const endDate = dayjs(date).endOf('day').toISOString();
|
2025-01-19 10:44:58 +03:00
|
|
|
|
2024-12-14 22:27:07 +03:00
|
|
|
const response = await fetch(`${endpoint}${ArmEndpoints.ORDERS}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2025-01-19 10:44:58 +03:00
|
|
|
body: JSON.stringify({ startDate, endDate }),
|
2024-12-14 22:27:07 +03:00
|
|
|
});
|
2024-12-07 23:40:22 +03:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch orders: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchMasters = async () => {
|
|
|
|
const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}`);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch masters: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
2024-12-22 12:00:12 +03:00
|
|
|
const addMaster = async ({
|
|
|
|
name,
|
|
|
|
phone,
|
|
|
|
}: {
|
|
|
|
name: string;
|
|
|
|
phone: string;
|
|
|
|
}) => {
|
|
|
|
const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ name, phone }),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch masters: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteMaster = async ({ id }: { id: string }) => {
|
|
|
|
const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}/${id}`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch masters: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
2025-01-25 17:12:02 +03:00
|
|
|
const updateOrders = async ({
|
|
|
|
id,
|
|
|
|
status,
|
|
|
|
notes,
|
|
|
|
masterId,
|
|
|
|
}: {
|
|
|
|
id: string;
|
|
|
|
status?: string;
|
|
|
|
notes?: string;
|
|
|
|
masterId?: string;
|
|
|
|
}) => {
|
|
|
|
const body = JSON.stringify({ status, notes, masterId });
|
|
|
|
|
2025-01-26 12:48:54 +03:00
|
|
|
const response = await fetch(`${endpoint}/order/${id}`, {
|
2025-01-25 17:12:02 +03:00
|
|
|
method: 'PATCH',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch update masters: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
2025-01-18 16:06:27 +03:00
|
|
|
const updateMaster = async ({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
phone,
|
|
|
|
}: {
|
|
|
|
id: string;
|
|
|
|
name?: string;
|
|
|
|
phone?: string;
|
|
|
|
}) => {
|
|
|
|
const body = JSON.stringify({ name, phone });
|
|
|
|
|
|
|
|
const response = await fetch(`${endpoint}${ArmEndpoints.MASTERS}/${id}`, {
|
|
|
|
method: 'PATCH',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch update masters: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
};
|
|
|
|
|
2025-01-25 17:12:02 +03:00
|
|
|
return {
|
|
|
|
fetchOrders,
|
|
|
|
fetchMasters,
|
|
|
|
addMaster,
|
|
|
|
deleteMaster,
|
|
|
|
updateMaster,
|
|
|
|
updateOrders,
|
|
|
|
};
|
2024-12-07 23:40:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export { armService, ArmEndpoints };
|