2024-12-07 23:40:22 +03:00
|
|
|
import { getConfigValue } from '@brojs/cli';
|
|
|
|
|
|
|
|
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 }) => {
|
|
|
|
const response = await fetch(`${endpoint}${ArmEndpoints.ORDERS}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ date }),
|
|
|
|
});
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
return { fetchOrders, fetchMasters, addMaster, deleteMaster };
|
2024-12-07 23:40:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export { armService, ArmEndpoints };
|