Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions SW.Bitween.Api/Domain/Xchange/Xchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,27 @@ public Xchange(int documentId, IWorkGroup workGroup, XchangeFile file, string[]
}

public Xchange(Subscription subscription, XchangeFile file, string[] references = null,
string correlationId = null, Partner gatewayPartner = null,GlobalAdapterValuesSet[] globalAdapterValuesSets = null) :
string correlationId = null, Partner gatewayPartner = null, GlobalAdapterValuesSet[] globalAdapterValuesSets = null) :
this(subscription.DocumentId, subscription.WorkGroup, file, references, subscription.Type)
{
SubscriptionId = subscription.Id;
MapperId = subscription.MapperId;
HandlerId = subscription.HandlerId;
ResponseSubscriptionId = subscription.ResponseSubscriptionId;
ResponseMessageTypeName = subscription.ResponseMessageTypeName;
MapperProperties = subscription.MapperProperties.ToDictionary().Fill(gatewayPartner,globalAdapterValuesSets);
PartnerId = gatewayPartner?.Id ?? subscription.PartnerId;
MapperProperties = subscription.MapperProperties.ToDictionary().Fill(gatewayPartner, globalAdapterValuesSets);
HandlerProperties = subscription.HandlerProperties.ToDictionary()
.Fill(gatewayPartner, globalAdapterValuesSets);
CorrelationId = correlationId;

}

//retry xchange
public Xchange(Xchange xchange, XchangeFile file, IWorkGroup workGroup) :
this(xchange.DocumentId, workGroup, file, xchange.References)
{
SubscriptionId = xchange.SubscriptionId;
PartnerId = xchange.PartnerId;
MapperId = xchange.MapperId;
HandlerId = xchange.HandlerId;
MapperProperties = xchange.MapperProperties;
Expand All @@ -75,6 +76,7 @@ public Xchange(Subscription subscription, Xchange xchange, XchangeFile file) :
this(xchange.DocumentId, subscription.WorkGroup, file, xchange.References)
{
SubscriptionId = xchange.SubscriptionId;
PartnerId = xchange.PartnerId ?? subscription.PartnerId;
MapperId = subscription.MapperId;
HandlerId = subscription.HandlerId;
MapperProperties = subscription.MapperProperties;
Expand All @@ -85,6 +87,7 @@ public Xchange(Subscription subscription, Xchange xchange, XchangeFile file) :
}

public int? SubscriptionId { get; private set; }
public int? PartnerId { get; private set; }
public int DocumentId { get; private set; }
public string HandlerId { get; private set; }
public string MapperId { get; private set; }
Expand Down
54 changes: 49 additions & 5 deletions SW.Bitween.Api/Resources/Mappers/Preview.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SW.Bitween.Domain;
using SW.Bitween.Domain.Accounts;
using SW.Bitween.NativeAdapters.JsonMapper;
using SW.PrimitiveTypes;
Expand All @@ -10,6 +15,7 @@ public class MapperPreviewRequest
{
public string ScribanTemplate { get; set; } = "{}";
public string InputJson { get; set; } = "{}";
public int? PartnerId { get; set; }
}

public class MapperPreviewResponse
Expand All @@ -21,24 +27,62 @@ public class MapperPreviewResponse
public class Preview : ICommandHandler<MapperPreviewRequest, MapperPreviewResponse>
{
private readonly RequestContext _requestContext;
private readonly BitweenDbContext _dbContext;

public Preview(RequestContext requestContext)
public Preview(RequestContext requestContext, BitweenDbContext dbContext)
{
_requestContext = requestContext;
_dbContext = dbContext;
}

public Task<MapperPreviewResponse> Handle(MapperPreviewRequest request)
public async Task<MapperPreviewResponse> Handle(MapperPreviewRequest request)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);

var partner = request.PartnerId.HasValue
? await _dbContext.FindAsync<Partner>(request.PartnerId.Value)
: null;

var globalSets = await _dbContext.Set<GlobalAdapterValuesSet>().ToListAsync();

try
{
var output = ScribanJsonHelper.Render(request.ScribanTemplate, request.InputJson);
return Task.FromResult(new MapperPreviewResponse { OutputJson = output });
var inputJson = request.InputJson;

JObject? jObj = null;
if (JToken.Parse(inputJson) is JObject parsedObj)
jObj = parsedObj;

if (jObj != null)
{
var enriched = false;

if (partner?.AdapterProperties?.Count > 0)
{
jObj["__partner__"] = JObject.FromObject(partner.AdapterProperties);
enriched = true;
}

var nonEmptySets = globalSets.Where(s => s.Values?.Count > 0).ToList();
if (nonEmptySets.Count > 0)
{
var globalsObj = new JObject();
foreach (var set in nonEmptySets)
globalsObj[set.Id] = JObject.FromObject(set.Values);
jObj["__globals__"] = globalsObj;
enriched = true;
}

if (enriched)
inputJson = jObj.ToString(Formatting.None);
}

var output = ScribanJsonHelper.Render(request.ScribanTemplate, inputJson);
return new MapperPreviewResponse { OutputJson = output };
}
catch (Exception ex)
{
return Task.FromResult(new MapperPreviewResponse { Error = ex.Message });
return new MapperPreviewResponse { Error = ex.Message };
}
Comment thread
hamzahalq marked this conversation as resolved.
}
}
31 changes: 31 additions & 0 deletions SW.Bitween.Api/Services/XchangeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SW.Bus.RabbitMqExtensions;

namespace SW.Bitween;
Expand Down Expand Up @@ -132,6 +133,36 @@ private async Task<XchangeFile> RunMapper(Xchange xchange, XchangeFile xchangeFi
{
if (xchange.MapperId == null) return xchangeFile;

// Inject __partner__ adapter properties into the input JSON so Scriban templates
// can reference them as {{ __partner__?.propkey }}
var jObjEnriched = JObject.Parse(xchangeFile.Data);
var enriched = false;

if (xchange.PartnerId.HasValue)
{
var partner = await _dbContext.FindAsync<Partner>(xchange.PartnerId.Value);
if (partner?.AdapterProperties?.Count > 0)
{
jObjEnriched["__partner__"] = JObject.FromObject(partner.AdapterProperties);
enriched = true;
}
}

// Inject __globals__ — all global adapter values sets
// so templates can use {{ __globals__?.setId?.key }}
var globalSets = await _dbContext.Set<GlobalAdapterValuesSet>().ToListAsync();
if (globalSets.Any(s => s.Values?.Count > 0))
{
var globalsObj = new JObject();
foreach (var set in globalSets.Where(s => s.Values?.Count > 0))
globalsObj[set.Id] = JObject.FromObject(set.Values);
jObjEnriched["__globals__"] = globalsObj;
enriched = true;
}

if (enriched)
xchangeFile = new XchangeFile(jObjEnriched.ToString(Formatting.None), xchangeFile.Filename);

var mapperProperties = xchange.MapperProperties.ToDictionary();
mapperProperties["xchangeid"] = xchange.Id;

Expand Down
Loading