Repository navigation
Partner portal: the app's own frame and Exchanges page, and retry #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using SW.Bitween.Domain; | ||
| using SW.Bitween.Model; | ||
| using SW.Bitween.Services; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Resources.Portal; | ||
|
|
||
| /// <summary> | ||
| /// What the partner portal's Exchanges page reads: our team's own search, counts and retry chain, | ||
| /// limited to the caller's partner and without what is ours to know. The same rows, so the same page | ||
| /// shows them. | ||
| /// </summary> | ||
| internal static class PortalExchangeViews | ||
| { | ||
| /// <summary> | ||
| /// The filters and sorts the portal's page sends. Anything else is refused rather than ignored, so | ||
| /// a hand-made request can't sort or filter by a column they aren't shown, such as an error they may | ||
| /// not read, and learn it from the order or the count. | ||
| /// </summary> | ||
| private static readonly HashSet<string> Filterable = new(StringComparer.Ordinal) | ||
| { | ||
| nameof(XchangeRow.StartedOn), nameof(XchangeRow.SubscriptionId), nameof(XchangeRow.DocumentId), | ||
| nameof(XchangeRow.CorrelationId), nameof(XchangeRow.PartnerId), "Id", "StatusFilter", "LatestOnly", | ||
| "IsRetry", "HasScheduledRetry", "PromotedPropertyKey", nameof(XchangeRow.PromotedPropertiesRaw), | ||
| "PromotedProperty" | ||
| }; | ||
|
|
||
| private static readonly HashSet<string> Sortable = new(StringComparer.Ordinal) | ||
| { | ||
| nameof(XchangeRow.StartedOn), nameof(XchangeRow.FinishedOn) | ||
| }; | ||
|
|
||
| public static void EnsureAllowed(SearchyRequest request, PortalCaller caller) | ||
| { | ||
| var withErrors = caller.Can(Model.Permissions.Portal.ExchangeErrorsView); | ||
| foreach (var filter in request.Conditions.SelectMany(c => c.Filters)) | ||
| if (!Filterable.Contains(filter.Field) && !(withErrors && filter.Field == "ErrorContains")) | ||
| throw new SWValidationException("FILTER_NOT_ALLOWED", $"The exchanges can't be filtered by {filter.Field}."); | ||
| foreach (var sort in request.Sorts ?? []) | ||
| if (!Sortable.Contains(sort.Field)) | ||
| throw new SWValidationException("SORT_NOT_ALLOWED", $"The exchanges can't be sorted by {sort.Field}."); | ||
| } | ||
|
|
||
| /// <summary>Takes out of a row what is ours to know, and what their role doesn't show.</summary> | ||
| public static XchangeRow Strip(XchangeRow row, PortalCaller caller) | ||
| { | ||
| // How their exchange was wired, on our side: which adapters ran, what fed it, where it was rolled up. | ||
| row.HandlerId = null; | ||
| // Whether one mapped it is theirs to see (the page shows a skipped stage); which adapter isn't. | ||
| row.MapperId = row.MapperId is null ? null : "mapper"; | ||
| row.References = null; | ||
| row.SourceXchangeId = null; | ||
| row.SourceValues = null; | ||
| row.AggregationXchangeId = null; | ||
| row.AggregatedOn = null; | ||
| row.RetryBlockedReason = null; | ||
| row.Warning = null; | ||
| // Where storage keeps the files; the links below are what they open them with. | ||
| row.InputKey = null; | ||
| row.OutputKey = null; | ||
| row.ResponseKey = null; | ||
| row.InputFileHash = null; | ||
|
|
||
| if (!caller.Can(Model.Permissions.Portal.ExchangeErrorsView)) | ||
| row.Exception = null; | ||
| if (!caller.Can(Model.Permissions.Portal.ExchangeFilesView)) | ||
| { | ||
| row.InputUrl = row.OutputUrl = row.ResponseUrl = null; | ||
| row.InputFileName = row.OutputFileName = row.ResponseFileName = null; | ||
| row.InputFileSize = row.OutputFileSize = row.ResponseFileSize = 0; | ||
| } | ||
| return row; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Their exchanges, as our team's list shows exchanges: the same filters, rows and paging.</summary> | ||
| [HandlerName("xchangesearch")] | ||
| public class SearchExchanges(BitweenDbContext dbContext, RequestContext requestContext, XchangeService xchangeService) | ||
| : ISearchyHandler | ||
| { | ||
| public async Task<object> Handle(SearchyRequest searchyRequest, bool lookup = false, string searchPhrase = null) | ||
| { | ||
| var caller = await PortalCaller.EnsureAsync(dbContext, requestContext, Model.Permissions.Portal.ExchangesView); | ||
| PortalExchangeViews.EnsureAllowed(searchyRequest, caller); | ||
| var found = await new SW.Bitween.Resources.Xchanges.Search(dbContext, xchangeService, requestContext) | ||
| .SearchAsync(searchyRequest, caller.PartnerId); | ||
| foreach (var row in found.Result) | ||
| PortalExchangeViews.Strip(row, caller); | ||
| return found; | ||
| } | ||
| } | ||
|
|
||
| /// <summary>How many of their exchanges a filter matches, by status: the counts on their Exchanges page.</summary> | ||
| [HandlerName("xchangesummary")] | ||
| public class CountExchanges(BitweenDbContext dbContext, RequestContext requestContext, SlowCounts slowCounts) | ||
| : ISearchyHandler | ||
| { | ||
| public async Task<object> Handle(SearchyRequest searchyRequest, bool lookup = false, string searchPhrase = null) | ||
| { | ||
| var caller = await PortalCaller.EnsureAsync(dbContext, requestContext, Model.Permissions.Portal.ExchangesView); | ||
| PortalExchangeViews.EnsureAllowed(searchyRequest, caller); | ||
| return await new SW.Bitween.Resources.Xchanges.Summary(dbContext, requestContext, slowCounts).SummarizeAsync(searchyRequest, caller.PartnerId); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>The attempts of one of their exchanges, as our team's retry chain shows them.</summary> | ||
| [HandlerName("retrytree")] | ||
| public class ExchangeAttempts(BitweenDbContext dbContext, RequestContext requestContext) | ||
| : IQueryHandler<XchangeRetryTreeRequest, object> | ||
| { | ||
| public async Task<object> Handle(XchangeRetryTreeRequest request) | ||
| { | ||
| var caller = await PortalCaller.EnsureAsync(dbContext, requestContext, Model.Permissions.Portal.ExchangesView); | ||
| // Someone else's exchange is simply not there for them. | ||
| if (!await dbContext.Set<Xchange>().AnyAsync(x => x.Id == request.Id && x.PartnerId == caller.PartnerId)) | ||
| throw new SWNotFoundException(request.Id); | ||
|
|
||
| var tree = await new SW.Bitween.Resources.Xchanges.RetryTree(dbContext, requestContext).TreeAsync(request, caller.PartnerId); | ||
| var withErrors = caller.Can(Model.Permissions.Portal.ExchangeErrorsView); | ||
| foreach (var node in tree.Nodes) | ||
| { | ||
| node.RetryBlockedReason = null; | ||
| if (!withErrors) node.Exception = null; | ||
| } | ||
| // An original that wasn't theirs is left out, so the chain starts at their first attempt. | ||
| if (tree.Nodes.All(n => n.Id != tree.RootId) && tree.Nodes.Count > 0) | ||
| tree.RootId = tree.Nodes[0].Id; | ||
| return tree; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using FluentValidation; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using SW.Bitween.Domain; | ||
| using SW.Bitween.Model; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Resources.Portal; | ||
|
|
||
| /// <summary> | ||
| /// Retries one of their failed exchanges with the subscription's current settings, so a mapping or | ||
| /// detail they just fixed is used. Only the latest attempt, as our team's retry, and not while a | ||
| /// retry policy has one scheduled. | ||
| /// </summary> | ||
| [HandlerName("retry")] | ||
| public class Retry(BitweenDbContext dbContext, RequestContext requestContext, XchangeService xchangeService) | ||
| : ICommandHandler<PortalRetry, object> | ||
| { | ||
| public async Task<object> Handle(PortalRetry request) | ||
| { | ||
| var caller = await PortalCaller.EnsureAsync(dbContext, requestContext, Model.Permissions.Portal.ExchangesOperate); | ||
| // Someone else's exchange is simply not there for them. | ||
| var xchange = await dbContext.Set<Xchange>() | ||
| .SingleOrDefaultAsync(x => x.Id == request.XchangeId && x.PartnerId == caller.PartnerId) | ||
| ?? throw new SWNotFoundException(request.XchangeId); | ||
|
|
||
| var result = await dbContext.Set<XchangeResult>().AsNoTracking().SingleOrDefaultAsync(r => r.Id == xchange.Id); | ||
| if (result is null || result.Success) | ||
| throw new SWValidationException("NOT_FAILED", "Only a failed exchange can be retried."); | ||
| if (await dbContext.Set<DelayedRetry>().AnyAsync(d => d.Id == xchange.Id)) | ||
| throw new SWValidationException("AUTO_RETRY_SCHEDULED", | ||
| "This exchange is already scheduled to be tried again. It runs on its own shortly."); | ||
| if (await dbContext.Set<XchangeRetryClaim>().AnyAsync(c => c.Id == xchange.Id) || | ||
| await dbContext.Set<Xchange>().AnyAsync(x => x.RetryFor == xchange.Id)) | ||
| throw new SWValidationException("ALREADY_RETRIED", "This exchange was already retried. Retry its latest attempt instead."); | ||
|
|
||
| var subscription = await dbContext.Subscriptions().FirstOrDefaultAsync(s => s.Id == xchange.SubscriptionId) | ||
| ?? throw new SWValidationException("SUBSCRIPTION_NOT_FOUND", | ||
| "This exchange can't be retried: what it ran for no longer exists."); | ||
|
|
||
| var input = await xchangeService.GetFile(xchange, XchangeFileType.Input); | ||
| await xchangeService.CreateXchange(subscription, xchange, new XchangeFile(input, xchange.InputName), | ||
| manualRetry: true); | ||
|
Comment on lines
+43
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| dbContext.Record(requestContext, OperatorAction.Retry, xchange.Id, "from the partner portal"); | ||
| try | ||
| { | ||
| await dbContext.SaveChangesAsync(); | ||
| } | ||
| catch (DbUpdateException) | ||
| { | ||
| // Another retry of the same exchange was saved a moment before this one. | ||
| if (await dbContext.Set<XchangeRetryClaim>().AsNoTracking().AnyAsync(c => c.Id == xchange.Id)) | ||
| throw new SWValidationException("ALREADY_RETRIED", | ||
| "This exchange was retried a moment ago. Retry its latest attempt instead."); | ||
| throw; | ||
| } | ||
|
|
||
| // The claim the retry made names the exchange it made. | ||
| var retryId = await dbContext.Set<XchangeRetryClaim>().AsNoTracking() | ||
| .Where(c => c.Id == xchange.Id).Select(c => c.RetryXchangeId).SingleAsync(); | ||
| return new PortalRetried { XchangeId = retryId }; | ||
| } | ||
|
|
||
| private class Validate : AbstractValidator<PortalRetry> | ||
| { | ||
| public Validate() => RuleFor(i => i.XchangeId).NotEmpty().MaximumLength(50); | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent
Idfilters from testing hidden aggregation wiring.If owned exchange X has
AggregationXchangeId=G, anId=Gfilter returns X.ApplySpecialFiltersmatchesAggregationXchangeIdas well asIdandRetryFor. The summary also counts that match.Removing aggregation fields from the response does not hide this relationship. Use portal-specific ID matching that excludes
AggregationXchangeId, while preserving permitted exchange and retry matching.