82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import { InstallmentOptionDto, OrderDto, PaymentMethodDto, ProductDto } from '@/proxy/order/models'
|
||
import apiService, { Config } from '@/services/api.service'
|
||
import axios from 'axios'
|
||
|
||
export class OrderService {
|
||
apiName = 'Default'
|
||
|
||
getProductList = async () =>
|
||
apiService.fetchData<ProductDto[]>(
|
||
{
|
||
method: 'GET',
|
||
url: '/api/app/public/product-list',
|
||
},
|
||
{ apiName: this.apiName },
|
||
)
|
||
|
||
getPaymentMethodList = async () =>
|
||
apiService.fetchData<PaymentMethodDto[]>(
|
||
{
|
||
method: 'GET',
|
||
url: '/api/app/public/payment-method-list',
|
||
},
|
||
{ apiName: this.apiName },
|
||
)
|
||
|
||
getInstallmentOptionList = async () =>
|
||
apiService.fetchData<InstallmentOptionDto[]>(
|
||
{
|
||
method: 'GET',
|
||
url: '/api/app/public/installment-option-list',
|
||
},
|
||
{ apiName: this.apiName },
|
||
)
|
||
|
||
// Sipariş oluşturma
|
||
createOrder = async (
|
||
orderData: OrderDto,
|
||
): Promise<{ success: boolean; orderId?: string; error?: string }> => {
|
||
try {
|
||
const response = await apiService.fetchData<{ orderId?: string; id?: string }>(
|
||
{
|
||
method: 'POST',
|
||
url: `/api/app/public/order`,
|
||
data: {
|
||
tenant: orderData.tenant,
|
||
items: orderData.items,
|
||
subtotal: orderData.subtotal,
|
||
commission: orderData.commission,
|
||
total: orderData.total,
|
||
paymentMethod: orderData.paymentMethod,
|
||
installments: orderData.installments,
|
||
installmentName: orderData.installmentName,
|
||
paymentData: orderData.paymentData,
|
||
},
|
||
},
|
||
{ apiName: this.apiName },
|
||
)
|
||
|
||
return {
|
||
success: true,
|
||
orderId: response.data.orderId || response.data.id,
|
||
}
|
||
} catch (error) {
|
||
console.error('Order creation error:', error)
|
||
|
||
let errorMessage = 'Sipariş oluşturulurken hata oluştu'
|
||
|
||
if (axios.isAxiosError(error)) {
|
||
if (error.response) {
|
||
errorMessage = error.response.data?.message || errorMessage
|
||
} else if (error.request) {
|
||
errorMessage = 'Sunucuya bağlanılamadı'
|
||
}
|
||
}
|
||
|
||
return {
|
||
success: false,
|
||
error: errorMessage,
|
||
}
|
||
}
|
||
}
|
||
}
|