142 lines
3.3 KiB
TypeScript
142 lines
3.3 KiB
TypeScript
/**
|
|
* Balance Service Utility Functions
|
|
* Provides helper functions for balance operations in service deployments
|
|
*/
|
|
|
|
interface BalanceDeductionResult {
|
|
success: boolean
|
|
transactionId?: string
|
|
previousBalance?: number
|
|
newBalance?: number
|
|
error?: string
|
|
}
|
|
|
|
interface ServicePurchase {
|
|
amount: number
|
|
service: string
|
|
serviceId?: string
|
|
description?: string
|
|
transactionId?: string
|
|
}
|
|
|
|
/**
|
|
* Deduct balance for service purchase
|
|
*/
|
|
export async function deductBalance(purchase: ServicePurchase): Promise<BalanceDeductionResult> {
|
|
try {
|
|
const response = await fetch('/api/balance/deduct', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'include',
|
|
body: JSON.stringify(purchase),
|
|
})
|
|
|
|
const data = await response.json()
|
|
|
|
if (!response.ok) {
|
|
return {
|
|
success: false,
|
|
error: data.error?.message || `Balance deduction failed with status ${response.status}`,
|
|
}
|
|
}
|
|
|
|
if (data.success) {
|
|
return {
|
|
success: true,
|
|
transactionId: data.data.transactionId,
|
|
previousBalance: data.data.previousBalance,
|
|
newBalance: data.data.newBalance,
|
|
}
|
|
} else {
|
|
return {
|
|
success: false,
|
|
error: data.error?.message || 'Balance deduction failed',
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Balance deduction error:', error)
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Network error during balance deduction',
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if user has sufficient balance
|
|
*/
|
|
export async function checkSufficientBalance(
|
|
requiredAmount: number
|
|
): Promise<{ sufficient: boolean; currentBalance?: number; error?: string }> {
|
|
try {
|
|
const response = await fetch('/api/user/balance', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'include',
|
|
})
|
|
|
|
const data = await response.json()
|
|
|
|
if (!response.ok) {
|
|
return {
|
|
sufficient: false,
|
|
error: data.error?.message || 'Failed to check balance',
|
|
}
|
|
}
|
|
|
|
if (data.success) {
|
|
const currentBalance = data.data.balance
|
|
return {
|
|
sufficient: currentBalance >= requiredAmount,
|
|
currentBalance,
|
|
}
|
|
} else {
|
|
return {
|
|
sufficient: false,
|
|
error: data.error?.message || 'Failed to retrieve balance',
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Balance check error:', error)
|
|
return {
|
|
sufficient: false,
|
|
error: error instanceof Error ? error.message : 'Network error during balance check',
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format currency amount for display
|
|
*/
|
|
export function formatCurrency(amount: number, currency: string = 'INR'): string {
|
|
return new Intl.NumberFormat('en-IN', {
|
|
style: 'currency',
|
|
currency,
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 2,
|
|
}).format(amount)
|
|
}
|
|
|
|
/**
|
|
* Validate if balance is sufficient for a given amount
|
|
*/
|
|
export function validateBalance(currentBalance: number, requiredAmount: number): boolean {
|
|
return currentBalance >= requiredAmount
|
|
}
|
|
|
|
/**
|
|
* Generate service description for transaction
|
|
*/
|
|
export function generateServiceDescription(
|
|
serviceType: string,
|
|
serviceName: string,
|
|
cycle?: string
|
|
): string {
|
|
const cycleText = cycle ? ` (${cycle})` : ''
|
|
return `${serviceType}: ${serviceName}${cycleText}`
|
|
}
|