Skip to content

Commit 01e2fa3

Browse files
committed
Split DefaultTaskEvents in process engine into smaller interfaces and classes. Restructuring of process task handling in general.
1 parent 43065ef commit 01e2fa3

File tree

73 files changed

+2833
-1610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2833
-1610
lines changed

src/Altinn.App.Api/Controllers/ActionsController.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Altinn.App.Api.Infrastructure.Filters;
22
using Altinn.App.Api.Models;
33
using Altinn.App.Core.Extensions;
4+
using Altinn.App.Core.Features;
45
using Altinn.App.Core.Features.Action;
56
using Altinn.App.Core.Helpers;
67
using Altinn.App.Core.Internal.App;
78
using Altinn.App.Core.Internal.Data;
89
using Altinn.App.Core.Internal.Instances;
10+
using Altinn.App.Core.Internal.Process;
911
using Altinn.App.Core.Internal.Validation;
1012
using Altinn.App.Core.Models;
1113
using Altinn.App.Core.Models.Process;
@@ -72,6 +74,7 @@ public ActionsController(
7274
[Authorize]
7375
[ProducesResponseType(typeof(UserActionResponse), 200)]
7476
[ProducesResponseType(typeof(ProblemDetails), 400)]
77+
[ProducesResponseType(typeof(RedirectResult), 302)]
7578
[ProducesResponseType(409)]
7679
[ProducesResponseType(500)]
7780
[ProducesResponseType(401)]
@@ -83,7 +86,7 @@ public async Task<ActionResult<UserActionResponse>> Perform(
8386
[FromBody] UserActionRequest actionRequest,
8487
[FromQuery] string? language = null)
8588
{
86-
var action = actionRequest.Action;
89+
string? action = actionRequest.Action;
8790
if (action == null)
8891
{
8992
return new BadRequestObjectResult(new ProblemDetails()
@@ -95,8 +98,7 @@ public async Task<ActionResult<UserActionResponse>> Perform(
9598
});
9699
}
97100

98-
var instance = await _instanceClient.GetInstance(app, org, instanceOwnerPartyId, instanceGuid);
99-
101+
Instance instance = await _instanceClient.GetInstance(app, org, instanceOwnerPartyId, instanceGuid);
100102
if (instance?.Process == null)
101103
{
102104
return Conflict($"Process is not started.");
@@ -107,20 +109,20 @@ public async Task<ActionResult<UserActionResponse>> Perform(
107109
return Conflict($"Process is ended.");
108110
}
109111

110-
var userId = HttpContext.User.GetUserIdAsInt();
112+
int? userId = HttpContext.User.GetUserIdAsInt();
111113
if (userId == null)
112114
{
113115
return Unauthorized();
114116
}
115117

116-
var authorized = await _authorization.AuthorizeAction(new AppIdentifier(org, app), new InstanceIdentifier(instanceOwnerPartyId, instanceGuid), HttpContext.User, action, instance.Process?.CurrentTask?.ElementId);
118+
bool authorized = await _authorization.AuthorizeAction(new AppIdentifier(org, app), new InstanceIdentifier(instanceOwnerPartyId, instanceGuid), HttpContext.User, action, instance.Process?.CurrentTask?.ElementId);
117119
if (!authorized)
118120
{
119121
return Forbid();
120122
}
121123

122-
UserActionContext userActionContext = new UserActionContext(instance, userId.Value, actionRequest.ButtonId, actionRequest.Metadata);
123-
var actionHandler = _userActionService.GetActionHandler(action);
124+
UserActionContext userActionContext = new(instance, userId.Value, actionRequest.ButtonId, actionRequest.Metadata);
125+
IUserAction? actionHandler = _userActionService.GetActionHandler(action);
124126
if (actionHandler == null)
125127
{
126128
return new NotFoundObjectResult(new UserActionResponse()
@@ -133,9 +135,13 @@ public async Task<ActionResult<UserActionResponse>> Perform(
133135
});
134136
}
135137

136-
var result = await actionHandler.HandleAction(userActionContext);
138+
UserActionResult result = await actionHandler.HandleAction(userActionContext);
139+
if (result.ResultType == ResultType.Redirect)
140+
{
141+
return new RedirectResult(result.RedirectUrl ?? throw new ProcessException("Redirect URL missing"));
142+
}
137143

138-
if (!result.Success)
144+
if (result.ResultType != ResultType.Success)
139145
{
140146
return StatusCode(
141147
statusCode: result.ErrorType switch

src/Altinn.App.Api/Controllers/InstancesController.cs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ public async Task<ActionResult<Instance>> Post(
221221
}
222222

223223
ApplicationMetadata application = await _appMetadata.GetApplicationMetadata();
224-
225-
RequestPartValidator requestValidator = new RequestPartValidator(application);
224+
RequestPartValidator requestValidator = new(application);
226225
string? multipartError = requestValidator.ValidateParts(parsedRequest.Parts);
227226

228227
if (!string.IsNullOrEmpty(multipartError))
@@ -250,7 +249,6 @@ public async Task<ActionResult<Instance>> Post(
250249
}
251250

252251
EnforcementResult enforcementResult = await AuthorizeAction(org, app, party.PartyId, null, "instantiate");
253-
254252
if (!enforcementResult.Authorized)
255253
{
256254
return Forbidden(enforcementResult);
@@ -278,13 +276,13 @@ public async Task<ActionResult<Instance>> Post(
278276
try
279277
{
280278
// start process and goto next task
281-
ProcessStartRequest processStartRequest = new ProcessStartRequest
279+
ProcessStartRequest processStartRequest = new()
282280
{
283281
Instance = instanceTemplate,
284-
User = User,
285-
Dryrun = true
282+
User = User
286283
};
287-
var result = await _processEngine.StartProcess(processStartRequest);
284+
285+
ProcessChangeResult result = await _processEngine.GenerateProcessStartEvents(processStartRequest);
288286
if (!result.Success)
289287
{
290288
return Conflict(result.ErrorMessage);
@@ -308,14 +306,8 @@ public async Task<ActionResult<Instance>> Post(
308306
instance = await _instanceClient.GetInstance(app, org, int.Parse(instance.InstanceOwner.PartyId), Guid.Parse(instance.Id.Split("/")[1]));
309307

310308
// notify app and store events
311-
var request = new ProcessStartRequest()
312-
{
313-
Instance = instance,
314-
User = User,
315-
Dryrun = false,
316-
};
317309
_logger.LogInformation("Events sent to process engine: {Events}", change?.Events);
318-
await _processEngine.UpdateInstanceAndRerunEvents(request, change?.Events);
310+
await _processEngine.HandleEventsAndUpdateStorage(instance, null, change?.Events);
319311
}
320312
catch (Exception exception)
321313
{
@@ -409,14 +401,14 @@ public async Task<ActionResult<Instance>> PostSimplified(
409401
return StatusCode((int)HttpStatusCode.Forbidden, $"Party {party.PartyId} is not allowed to instantiate this application {org}/{app}");
410402
}
411403

412-
Instance instanceTemplate = new Instance()
404+
Instance instanceTemplate = new()
413405
{
414406
InstanceOwner = instansiationInstance.InstanceOwner,
415407
VisibleAfter = instansiationInstance.VisibleAfter,
416-
DueBefore = instansiationInstance.DueBefore
408+
DueBefore = instansiationInstance.DueBefore,
409+
Org = application.Org
417410
};
418411

419-
instanceTemplate.Org = application.Org;
420412
ConditionallySetReadStatus(instanceTemplate);
421413

422414
// Run custom app logic to validate instantiation
@@ -437,11 +429,10 @@ public async Task<ActionResult<Instance>> PostSimplified(
437429
{
438430
Instance = instanceTemplate,
439431
User = User,
440-
Dryrun = true,
441432
Prefill = instansiationInstance.Prefill
442433
};
443434

444-
processResult = await _processEngine.StartProcess(request);
435+
processResult = await _processEngine.GenerateProcessStartEvents(request);
445436

446437
Instance? source = null;
447438

@@ -473,15 +464,7 @@ public async Task<ActionResult<Instance>> PostSimplified(
473464
}
474465

475466
instance = await _instanceClient.GetInstance(instance);
476-
477-
var updateRequest = new ProcessStartRequest()
478-
{
479-
Instance = instance,
480-
User = User,
481-
Dryrun = false,
482-
Prefill = instansiationInstance.Prefill
483-
};
484-
await _processEngine.UpdateInstanceAndRerunEvents(updateRequest, processResult.ProcessStateChange?.Events);
467+
await _processEngine.HandleEventsAndUpdateStorage(instance, instansiationInstance.Prefill, processResult.ProcessStateChange?.Events);
485468
}
486469
catch (Exception exception)
487470
{
@@ -573,24 +556,18 @@ public async Task<ActionResult> CopyInstance(
573556
ProcessStartRequest processStartRequest = new()
574557
{
575558
Instance = targetInstance,
576-
User = User,
577-
Dryrun = true
559+
User = User
578560
};
579-
var startResult = await _processEngine.StartProcess(processStartRequest);
561+
562+
ProcessChangeResult startResult = await _processEngine.GenerateProcessStartEvents(processStartRequest);
580563

581564
targetInstance = await _instanceClient.CreateInstance(org, app, targetInstance);
582565

583566
await CopyDataFromSourceInstance(application, targetInstance, sourceInstance);
584567

585568
targetInstance = await _instanceClient.GetInstance(targetInstance);
586569

587-
ProcessStartRequest rerunRequest = new()
588-
{
589-
Instance = targetInstance,
590-
Dryrun = false,
591-
User = User
592-
};
593-
await _processEngine.UpdateInstanceAndRerunEvents(rerunRequest, startResult.ProcessStateChange?.Events);
570+
await _processEngine.HandleEventsAndUpdateStorage(targetInstance, null, startResult.ProcessStateChange?.Events);
594571

595572
await RegisterEvent("app.instance.created", targetInstance);
596573

src/Altinn.App.Api/Controllers/ProcessController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,16 @@ public async Task<ActionResult<AppProcessState>> StartProcess(
126126
{
127127
Instance = instance,
128128
StartEventId = startEvent,
129-
User = User,
130-
Dryrun = false
129+
User = User
131130
};
132-
var result = await _processEngine.StartProcess(request);
131+
ProcessChangeResult result = await _processEngine.GenerateProcessStartEvents(request);
133132
if (!result.Success)
134133
{
135134
return Conflict(result.ErrorMessage);
136135
}
137136

137+
await _processEngine.HandleEventsAndUpdateStorage(instance, null, result.ProcessStateChange?.Events);
138+
138139
AppProcessState appProcessState = await ConvertAndAuthorizeActions(instance, result.ProcessStateChange?.NewProcessState);
139140
return Ok(appProcessState);
140141
}

src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
using Altinn.App.Core.Internal.Pdf;
3131
using Altinn.App.Core.Internal.Prefill;
3232
using Altinn.App.Core.Internal.Process;
33-
using Altinn.App.Core.Internal.Process.Action;
33+
using Altinn.App.Core.Internal.Process.EventHandlers;
34+
using Altinn.App.Core.Internal.Process.EventHandlers.ProcessTask;
35+
using Altinn.App.Core.Internal.Process.ProcessTasks;
36+
using Altinn.App.Core.Internal.Process.ServiceTasks;
3437
using Altinn.App.Core.Internal.Profile;
3538
using Altinn.App.Core.Internal.Registers;
3639
using Altinn.App.Core.Internal.Secrets;
37-
using Altinn.App.Core.Internal.Sign;
3840
using Altinn.App.Core.Internal.Texts;
3941
using Altinn.App.Core.Internal.Validation;
4042
using Altinn.App.Core.Models;
@@ -53,6 +55,8 @@
5355
using IProcessReader = Altinn.App.Core.Internal.Process.IProcessReader;
5456
using ProcessEngine = Altinn.App.Core.Internal.Process.ProcessEngine;
5557
using ProcessReader = Altinn.App.Core.Internal.Process.ProcessReader;
58+
using Altinn.App.Core.Internal.Sign;
59+
using Altinn.App.Core.Internal.Process.Authorization;
5660

5761
namespace Altinn.App.Core.Extensions
5862
{
@@ -140,7 +144,6 @@ public static void AddAppServices(this IServiceCollection services, IConfigurati
140144
services.TryAddSingleton<IAppMetadata, AppMetadata>();
141145
services.TryAddSingleton<IFrontendFeatures, FrontendFeatures>();
142146
services.TryAddTransient<IAppEvents, DefaultAppEvents>();
143-
services.TryAddTransient<ITaskEvents, DefaultTaskEvents>();
144147
#pragma warning disable CS0618, CS0612 // Type or member is obsolete
145148
services.TryAddTransient<IPageOrder, DefaultPageOrder>();
146149
#pragma warning restore CS0618, CS0612 // Type or member is obsolete
@@ -152,14 +155,17 @@ public static void AddAppServices(this IServiceCollection services, IConfigurati
152155
services.TryAddTransient<IDataListsService, DataListsService>();
153156
services.TryAddTransient<LayoutEvaluatorStateInitializer>();
154157
services.TryAddTransient<IPatchService, PatchService>();
158+
services.AddTransient<IDataService, DataService>();
155159
services.Configure<Altinn.Common.PEP.Configuration.PepSettings>(configuration.GetSection("PEPSettings"));
156160
services.Configure<Altinn.Common.PEP.Configuration.PlatformSettings>(configuration.GetSection("PlatformSettings"));
157161
services.Configure<AccessTokenSettings>(configuration.GetSection("AccessTokenSettings"));
158162
services.Configure<FrontEndSettings>(configuration.GetSection(nameof(FrontEndSettings)));
159163
services.Configure<PdfGeneratorSettings>(configuration.GetSection(nameof(PdfGeneratorSettings)));
164+
160165
AddAppOptions(services);
161166
AddActionServices(services);
162167
AddPdfServices(services);
168+
AddSignatureServices(services);
163169
AddEventServices(services);
164170
AddProcessServices(services);
165171
AddFileAnalyserServices(services);
@@ -237,6 +243,11 @@ private static void AddPdfServices(IServiceCollection services)
237243
#pragma warning restore CS0618 // Type or member is obsolete
238244
}
239245

246+
private static void AddSignatureServices(IServiceCollection services)
247+
{
248+
services.AddHttpClient<ISignClient, SignClient>();
249+
}
250+
240251
private static void AddAppOptions(IServiceCollection services)
241252
{
242253
// Main service for interacting with options
@@ -256,16 +267,35 @@ private static void AddProcessServices(IServiceCollection services)
256267
services.TryAddTransient<IProcessEngine, ProcessEngine>();
257268
services.TryAddTransient<IProcessNavigator, ProcessNavigator>();
258269
services.TryAddSingleton<IProcessReader, ProcessReader>();
270+
services.TryAddSingleton<IProcessEventHandlerDelegator, ProcessEventHandlingDelegator>();
259271
services.TryAddTransient<IProcessEventDispatcher, ProcessEventDispatcher>();
260272
services.AddTransient<IProcessExclusiveGateway, ExpressionsExclusiveGateway>();
261273
services.TryAddTransient<ExclusiveGatewayFactory>();
274+
275+
services.AddTransient<IProcessTaskInitializer, ProcessTaskInitializer>();
276+
services.AddTransient<IProcessTaskFinalizer, ProcessTaskFinalizer>();
277+
services.AddTransient<IProcessTaskDataLocker, ProcessTaskDataLocker>();
278+
services.AddTransient<IStartTaskEventHandler, StartTaskEventHandler>();
279+
services.AddTransient<IEndTaskEventHandler, EndTaskEventHandler>();
280+
services.AddTransient<IAbandonTaskEventHandler, AbandonTaskEventHandler>();
281+
services.AddTransient<IEndEventEventHandler, EndEventEventHandler>();
282+
283+
//PROCESS TASKS
284+
services.AddTransient<IProcessTask, DataProcessTask>();
285+
services.AddTransient<IProcessTask, ConfirmationProcessTask>();
286+
services.AddTransient<IProcessTask, FeedbackProcessTask>();
287+
services.AddTransient<IProcessTask, SigningProcessTask>();
288+
services.AddTransient<IProcessTask, NullTypeProcessTask>();
289+
290+
//SERVICE TASKS
291+
services.AddKeyedTransient<IServiceTask, PdfServiceTask>("pdfService");
292+
services.AddKeyedTransient<IServiceTask, EformidlingServiceTask>("eFormidlingService");
262293
}
263294

264295
private static void AddActionServices(IServiceCollection services)
265296
{
266297
services.TryAddTransient<UserActionService>();
267298
services.AddTransient<IUserAction, SigningUserAction>();
268-
services.AddHttpClient<ISignClient, SignClient>();
269299
services.AddTransientUserActionAuthorizerForActionInAllTasks<UniqueSignatureAuthorizer>("sign");
270300
}
271301

0 commit comments

Comments
 (0)