feat: implement VAT management service and integrate VAT calculations across components

This commit is contained in:
masoodafar-web
2025-12-19 00:30:33 +03:30
parent 6b457d0ce6
commit 9ee464b8b4
25 changed files with 751 additions and 268 deletions
+61 -3
View File
@@ -1,6 +1,7 @@
using DateTimeConverterCL;
using FrontOffice.BFF.ShopingCart.Protobuf.Protos.ShopingCart;
using Google.Protobuf.WellKnownTypes;
using Blazored.LocalStorage;
namespace FrontOffice.Main.Utilities;
@@ -16,13 +17,18 @@ public record CartItem(long cartId,long ProductId, string Title, string ImageUrl
public class CartService
{
private readonly ShopingCartContract.ShopingCartContractClient _client;
private readonly ILocalStorageService _localStorage;
private readonly List<CartItem> _items = new();
private const string TokenStorageKey = "auth:token";
private bool _isInitialized;
public event Action? OnChange;
public CartService(ShopingCartContract.ShopingCartContractClient client)
public CartService(ShopingCartContract.ShopingCartContractClient client, ILocalStorageService localStorage)
{
_client = client;
_ = LoadFromServerAsync();
_localStorage = localStorage;
// لود سبد خرید به صورت lazy انجام میشه - نه در constructor
}
public IReadOnlyList<CartItem> Items => _items.AsReadOnly();
@@ -33,6 +39,13 @@ public class CartService
public async Task Add(Product product, int quantity = 1)
{
if (quantity <= 0) return;
// اطمینان از لود شدن سبد خرید
await EnsureInitializedAsync();
// چک لاگین بودن کاربر
if (!await IsAuthenticatedAsync()) return;
var existing = _items.FirstOrDefault(i => i.ProductId == product.Id);
int newQuantity;
@@ -82,11 +95,14 @@ public class CartService
public async Task UpdateQuantity(long productId, int quantity)
{
// چک لاگین بودن کاربر
if (!await IsAuthenticatedAsync()) return;
var existing = _items.FirstOrDefault(i => i.ProductId == productId);
if (existing is null) return;
if (quantity <= 0)
{
Remove(existing.cartId);
await Remove(existing.cartId);
return;
}
var idx = _items.IndexOf(existing);
@@ -109,6 +125,9 @@ public class CartService
public async Task Remove(long cartId)
{
// چک لاگین بودن کاربر
if (!await IsAuthenticatedAsync()) return;
_items.RemoveAll(i => i.cartId == cartId);
Notify();
@@ -129,6 +148,9 @@ public class CartService
public async Task Clear()
{
// چک لاگین بودن کاربر
if (!await IsAuthenticatedAsync()) return;
var productIds = _items.Select(i => i.ProductId).ToList();
_items.Clear();
Notify();
@@ -152,8 +174,44 @@ public class CartService
private void Notify() => OnChange?.Invoke();
/// <summary>
/// بررسی اینکه کاربر لاگین کرده یا نه
/// </summary>
private async Task<bool> IsAuthenticatedAsync()
{
try
{
var token = await _localStorage.GetItemAsync<string>(TokenStorageKey);
return !string.IsNullOrWhiteSpace(token);
}
catch
{
return false;
}
}
/// <summary>
/// اطمینان از لود شدن سبد خرید - فقط اگر کاربر لاگین کرده باشد
/// </summary>
public async Task EnsureInitializedAsync()
{
if (_isInitialized) return;
if (await IsAuthenticatedAsync())
{
await LoadFromServerAsync();
}
_isInitialized = true;
}
private async Task LoadFromServerAsync()
{
// ابتدا چک کن که کاربر لاگین کرده
if (!await IsAuthenticatedAsync())
{
return;
}
try
{
var response = await _client.GetAllUserCartAsync(new Empty());