74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { apiFetch } from './client';
|
|
|
|
export interface CreditSettings {
|
|
credits_enabled: boolean;
|
|
credits_sales_percentage: number;
|
|
credits_sales_return_window_days: number;
|
|
credits_service_percentage: number;
|
|
credits_service_claim_window_days: number;
|
|
credits_minimum_order_value: number;
|
|
credits_expiry_days: number;
|
|
}
|
|
|
|
export interface CreditTransaction {
|
|
transaction_id: string;
|
|
transaction_type: 'EARN' | 'REDEEM' | 'VOID_RETURN' | 'ADJUSTMENT';
|
|
reference_type: 'ORDER' | 'SERVICE_JOB' | 'MANUAL_ADMIN' | 'PURCHASE_REDEMPTION';
|
|
reference_id: string;
|
|
reference_no?: string;
|
|
base_amount: number;
|
|
percentage_applied?: number;
|
|
credit_amount: number;
|
|
status: 'PENDING' | 'MATURED' | 'VOIDED' | 'EXPIRED';
|
|
matures_at?: string;
|
|
matured_at?: string;
|
|
voided_at?: string;
|
|
void_reason?: string;
|
|
description?: string;
|
|
created_at?: string;
|
|
}
|
|
|
|
export interface CustomerWalletSummary {
|
|
wallet_id: string;
|
|
customer_id: string;
|
|
active_balance: number;
|
|
pending_balance: number;
|
|
lifetime_earned: number;
|
|
lifetime_redeemed: number;
|
|
currency: string;
|
|
is_locked: boolean;
|
|
transactions: CreditTransaction[];
|
|
}
|
|
|
|
export const creditService = {
|
|
getSettings: async (): Promise<CreditSettings> => {
|
|
return apiFetch<CreditSettings>('/api/v1/credits/settings');
|
|
},
|
|
|
|
updateSettings: async (payload: Partial<CreditSettings>): Promise<CreditSettings> => {
|
|
return apiFetch<CreditSettings>('/api/v1/credits/settings', {
|
|
method: 'PUT',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
|
|
getCustomerWallet: async (customerId: string): Promise<CustomerWalletSummary> => {
|
|
return apiFetch<CustomerWalletSummary>(`/api/v1/credits/wallets/${customerId}`);
|
|
},
|
|
|
|
adjustWallet: async (
|
|
customerId: string,
|
|
payload: { amount: number; adjustment_type: 'CREDIT' | 'DEBIT'; reason: string }
|
|
) => {
|
|
return apiFetch(`/api/v1/credits/wallets/${customerId}/adjust`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
|
|
triggerMaturitySweep: async () => {
|
|
return apiFetch<{ message: string; result: any }>('/api/v1/cron/process-credit-maturity', {
|
|
method: 'POST',
|
|
});
|
|
},
|
|
};
|