Files
BimAI/BimAI.Infrastructure/Sync/ProductSyncService.cs

87 lines
3.2 KiB
C#
Raw Normal View History

2025-06-23 21:52:09 +02:00
using System.Text.Json;
2025-07-17 14:29:02 +02:00
using System.Web;
2025-10-11 11:33:46 +02:00
using BimAI.Domain.Entities;
using BimAI.Infrastructure.Data;
2025-06-23 21:52:09 +02:00
using Microsoft.Extensions.Configuration;
2025-10-11 11:33:46 +02:00
namespace BimAI.Infrastructure.Sync;
2025-06-23 21:52:09 +02:00
2025-10-11 11:33:46 +02:00
public class ProductSyncService(HttpClient httpClient, BimAIDbContext db, IConfiguration configuration)
2025-06-23 21:52:09 +02:00
{
2025-07-17 14:29:02 +02:00
/// <summary>
/// Dekoduje encje HTML w ciągu znaków (np. &quot; na ")
/// </summary>
private string DecodeHtmlEntities(string text)
{
if (string.IsNullOrEmpty(text))
return text;
2025-06-23 21:52:09 +02:00
2025-07-17 14:29:02 +02:00
return HttpUtility.HtmlDecode(text);
}
2025-06-23 21:52:09 +02:00
public async Task RunAsync()
{
2025-07-17 14:29:02 +02:00
var apiKey = configuration["E5_CRM:ApiKey"];
var syncState = db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
2025-06-23 21:52:09 +02:00
var url = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.list&since={syncState.LastSynced}";
2025-07-17 14:29:02 +02:00
var response = await httpClient.GetStringAsync(url);
2025-06-23 21:52:09 +02:00
var products = JsonSerializer.Deserialize<List<JsonElement>>(response);
if (products == null) return;
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
foreach (var p in products)
{
var idStr = p.GetProperty("id").GetString() ?? "";
2025-07-17 14:29:02 +02:00
var name = DecodeHtmlEntities(p.GetProperty("name").GetString() ?? "");
var code = DecodeHtmlEntities(p.GetProperty("code").GetString() ?? "");
var stockAddresses = DecodeHtmlEntities(p.GetProperty("stock_addresses").GetString() ?? "");
var ean = DecodeHtmlEntities(p.GetProperty("ean").GetString() ?? "");
2025-06-23 21:52:09 +02:00
if (!Guid.TryParse(idStr, out Guid id))
{
Console.WriteLine($"[SYNC] Skipping product with wrong ID: '{idStr}', Name: '{name}'");
continue;
}
2025-07-17 14:29:02 +02:00
var existing = db.Products.FirstOrDefault(x => x.Id == id);
2025-06-23 21:52:09 +02:00
if (existing == null)
{
var product = new Product
{
Id = id,
Name = name,
2025-06-28 18:54:08 +01:00
Ean = ean,
Code = code,
StockAddresses = stockAddresses,
2025-06-23 21:52:09 +02:00
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
2025-07-17 14:29:02 +02:00
db.Products.Add(product);
2025-06-23 21:52:09 +02:00
}
else
{
existing.Name = name;
existing.UpdatedAt = DateTime.UtcNow;
}
Console.WriteLine($"[SYNC] Updated product ID: '{idStr}', Name: '{name}'");
var exportedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var updateUrl = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.setExportedAt&id={id}&exportedAt={exportedAt}";
2025-07-17 14:29:02 +02:00
await httpClient.GetAsync(updateUrl);
2025-06-23 21:52:09 +02:00
}
syncState.LastSynced = now;
2025-07-17 14:29:02 +02:00
if (db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
2025-06-23 21:52:09 +02:00
{
2025-07-17 14:29:02 +02:00
db.SyncStates.Add(syncState);
2025-06-23 21:52:09 +02:00
}
else
{
2025-07-17 14:29:02 +02:00
db.SyncStates.Update(syncState);
2025-06-23 21:52:09 +02:00
}
2025-07-17 14:29:02 +02:00
await db.SaveChangesAsync();
2025-06-23 21:52:09 +02:00
}
}