update
This commit is contained in:
@@ -4,8 +4,15 @@ using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
namespace FrontOffice.Main.Utilities;
|
||||
|
||||
public record WalletBalances(long CreditBalance, long NetworkBalance);
|
||||
public record WalletBalances(long CreditBalance, long DiscountBalance, long NetworkBalance);
|
||||
public record WalletTransaction(string Date, long Amount, string Channel, string Description);
|
||||
public record WalletWithdrawal(long Id, string WeekNumber, long Amount, int Status, int? Method, string? Iban, string Created);
|
||||
public enum WithdrawalMethodClient
|
||||
{
|
||||
Cash = 0,
|
||||
Diamond = 1
|
||||
}
|
||||
public record WithdrawalSettings(long MinWithdrawalAmount);
|
||||
|
||||
public class WalletService
|
||||
{
|
||||
@@ -21,26 +28,35 @@ public class WalletService
|
||||
try
|
||||
{
|
||||
var response = await _client.GetUserWalletAsync(new Empty());
|
||||
return new WalletBalances(response.Balance, response.NetworkBalance);
|
||||
return new WalletBalances(response.Balance, response.DiscountBalance, response.NetworkBalance);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Fallback to mock data if backend is unavailable
|
||||
return new WalletBalances(350_000, 1_250_000);
|
||||
return new WalletBalances(350_000, 0, 1_250_000);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<WalletTransaction>> GetTransactionsAsync()
|
||||
public async Task<List<WalletTransaction>> GetTransactionsAsync(long? referenceId = null, bool? isIncrease = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _client.GetAllUserWalletChangeLogAsync(new Empty());
|
||||
var request = new GetAllUserWalletChangeLogRequest();
|
||||
if (referenceId.HasValue)
|
||||
{
|
||||
request.ReferenceId = referenceId.Value;
|
||||
}
|
||||
if (isIncrease.HasValue)
|
||||
{
|
||||
request.IsIncrease = isIncrease.Value;
|
||||
}
|
||||
var response = await _client.GetAllUserWalletChangeLogAsync(request);
|
||||
return response.Models
|
||||
.Select(t => new WalletTransaction(
|
||||
ResolveTransactionDate(t),
|
||||
t.CurrentBalance,
|
||||
t.ChangeValue != 0 ? t.ChangeValue : t.CurrentBalance,
|
||||
t.RefrenceId?.ToString() ?? "-",
|
||||
t.IsIncrease ? "شارژ کیف پول" : "برداشت از کیف پول"))
|
||||
ResolveDescription(t)))
|
||||
.OrderByDescending(t => t.Date)
|
||||
.ToList();
|
||||
}
|
||||
@@ -74,5 +90,61 @@ public class WalletService
|
||||
|
||||
return DateTime.Now.MiladiToJalaliWithTime();
|
||||
}
|
||||
}
|
||||
|
||||
private static string ResolveDescription(GetAllUserWalletChangeLogResponseModel model)
|
||||
{
|
||||
if (model.ChangeValue > 0) return "شارژ کیف پول";
|
||||
if (model.ChangeValue < 0) return "برداشت/خرید";
|
||||
return "تراکنش کیف پول";
|
||||
}
|
||||
|
||||
public async Task<bool> RequestWithdrawalAsync(long payoutId, WithdrawalMethodClient method, string? iban)
|
||||
{
|
||||
var request = new WithdrawBalanceRequest
|
||||
{
|
||||
PayoutId = payoutId,
|
||||
WithdrawalMethod = (int)method
|
||||
};
|
||||
if (!string.IsNullOrWhiteSpace(iban))
|
||||
{
|
||||
request.IbanNumber = iban;
|
||||
}
|
||||
try
|
||||
{
|
||||
await _client.WithdrawBalanceAsync(request);
|
||||
return true;
|
||||
}
|
||||
catch (Grpc.Core.RpcException ex)
|
||||
{
|
||||
// surface backend error text
|
||||
throw new InvalidOperationException(ex.Status.Detail ?? "خطا در ثبت برداشت", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<WalletWithdrawal>> GetWithdrawalsAsync(int? status = null)
|
||||
{
|
||||
var request = new GetUserWithdrawalsRequest();
|
||||
if (status.HasValue)
|
||||
{
|
||||
request.Status = status.Value;
|
||||
}
|
||||
|
||||
var response = await _client.GetUserWithdrawalsAsync(request);
|
||||
return response.Models
|
||||
.Select(m => new WalletWithdrawal(
|
||||
m.Id,
|
||||
m.WeekNumber,
|
||||
m.TotalAmount,
|
||||
m.Status,
|
||||
m.WithdrawalMethod?.Value,
|
||||
m.IbanNumber,
|
||||
m.Created.ToDateTime().MiladiToJalaliWithTime()))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<WithdrawalSettings> GetWithdrawalSettingsAsync()
|
||||
{
|
||||
var response = await _client.GetWithdrawalSettingsAsync(new Empty());
|
||||
return new WithdrawalSettings(response.MinWithdrawalAmount);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user