Files
jube-fork/Jube.App/Controllers/Repository/CaseController.cs
richard-churchman 811c938462 Improved Case Automation POST body conext and other minor issues:
* Pushed Case Workflow Display HTML rendering logic down into a controller for improved security and consistent tokenisation of the Jube token format against context. Included a migration to update the default demo entry to use proper tokenisation. Updated the repository to support fetching CaseWorkflowDisplay with appropriate security.
* Implemented improved tokenisation of the EntityAnalysisModelInstanceEntryPayload model via a common extension method on EntityAnalysisModelInstanceEntryPayload, used consistently across the system for serialisation and storage.
* Included a migration to update the tokenisation format for full CaseWorkflowDisplay support.
* Fixed an issue where CaseWorkflowFilter was incorrectly filtering on CaseWorkflowStatus.Guid instead of CaseWorkflowStatus.Name, causing the GUID to be returned rather than the display name on the template/demo model. (The remainder was fixed in GitHub issue #6.) Included a migration with a corrective SQL statement.
* Fixed an issue where tokens were not being stored alongside CaseWorkflowFilter definition configuration. It now behaves identically to CaseSearch. Note: migrating missing tokens is not feasible as it requires JS-side parsing via JQueryBuilder.
* Reduced the size of the CaseWorkflowForm invocation to include only the required fields given the new POST body context being rendered server side. Improved API serialisation by using a dictionary for form payload keys and values.
* Consolidated JSON serialisation settings into DI and updated usages throughout.
* Updated EntityAnalysisModelInstanceEntryPayload write serialisers to handle deserialisation, which is required to support tokenisation of new Case Automation POST bodies.
* Created an extension method on the EntityAnalysisModelInstanceEntryPayload to centralise tokenisation of strings across Activation and Case Automation on a consistent basis. Simplified Case Notification and Case HTTP dispatch by removing tokenisation logic from those classes.
* Removed a redundant field from the CaseWorkflowFormEntry POCO (database left unchanged for now).
* Fixed a bug in Case session SQL rebuilding where the rebuild date was not being correctly checked for null or default value, which caused a failure in the last migration.
* Made minor modifications to the Create Case process and method signature to skip serialisation when the payload object required for tokenisation is already available on the thread (e.g. during synchronous reprocessing).
* Updated Case Workflow Macro processing to fetch the archive payload for the case from the database, improving security of that invocation endpoint.
* Removed what appeared to be a duplicate EntityAnalysisModelInstanceEntryPayload model introduced during refactoring.
* Fixed a but in Case Worfkflow Display Role assigment owing to volatile Guid in its update.
2026-02-22 13:08:42 +02:00

483 lines
19 KiB
C#

/* Copyright (C) 2022-present Jube Holdings Limited.
*
* This file is part of Jube™ software.
*
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
* see <https://www.gnu.org/licenses/>.
*/
namespace Jube.App.Controllers.Repository
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Case;
using Code;
using Data.Context;
using Data.Poco;
using Data.Repository;
using Dto;
using DynamicEnvironment;
using Engine.EntityAnalysisModelInvoke.Models.Payload.EntityAnalysisModelInstanceEntryPayload;
using Engine.EntityAnalysisModelInvoke.Models.Payload.EntityAnalysisModelInstanceEntryPayload.Extensions;
using Engine.Helpers;
using FluentValidation;
using FluentValidation.Results;
using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Validators;
[Route("api/[controller]")]
[Produces("application/json")]
[Authorize]
public class CaseController : Controller
{
private readonly DbContext dbContext;
private readonly DynamicEnvironment dynamicEnvironment;
private readonly JsonSerializationHelper jsonSerializationHelper;
private readonly ILog log;
private readonly IMapper mapper;
private readonly PermissionValidation permissionValidation;
private readonly CaseRepository repositoryCase;
private readonly CaseEventRepository repositoryCaseEvent;
private readonly string userName;
private readonly IValidator<CaseDto> validator;
public CaseController(ILog log, DynamicEnvironment dynamicEnvironment
, IHttpContextAccessor httpContextAccessor, JsonSerializationHelper jsonSerializationHelper)
{
if (httpContextAccessor.HttpContext?.User.Identity != null)
{
userName = httpContextAccessor.HttpContext.User.Identity.Name;
}
this.log = log;
dbContext =
DataConnectionDbContext.GetDbContextDataConnection(dynamicEnvironment.AppSettings("ConnectionString"));
permissionValidation = new PermissionValidation(dbContext, userName);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Case, CaseDto>();
cfg.CreateMap<CaseDto, Case>();
});
mapper = new Mapper(config);
repositoryCase = new CaseRepository(dbContext, userName);
repositoryCaseEvent = new CaseEventRepository(dbContext, userName);
validator = new CaseDtoValidator();
this.dynamicEnvironment = dynamicEnvironment;
this.jsonSerializationHelper = jsonSerializationHelper;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
dbContext.Close();
dbContext.Dispose();
}
base.Dispose(disposing);
}
[HttpGet]
public async Task<ActionResult<List<CaseDto>>> GetAsync(CancellationToken token = default)
{
try
{
if (!permissionValidation.Validate(new[]
{
1
}))
{
return Forbid();
}
return Ok(mapper.Map<List<CaseDto>>(await repositoryCase.GetAsyncActiveOnlyAsync(token)));
}
catch (Exception e)
{
log.Error(e);
return StatusCode(500);
}
}
[HttpGet]
[Route("{id:int}")]
public async Task<ActionResult<CaseDto>> GetByCaseIdActiveOnlyAsync(int id, CancellationToken token = default)
{
try
{
if (!permissionValidation.Validate(new[]
{
1
}))
{
return Forbid();
}
return Ok(mapper.Map<CaseDto>(
await repositoryCase.GetByIdActiveOnlyAsync(id, token)));
}
catch (Exception e)
{
log.Error(e);
return StatusCode(500);
}
}
[HttpPost]
[ProducesResponseType(typeof(CaseDto), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationResult), (int)HttpStatusCode.BadRequest)]
public async Task<ActionResult<CaseDto>> CreateCaseAsync([FromBody] CaseDto model, CancellationToken token = default)
{
try
{
if (!permissionValidation.Validate(new[]
{
1
}))
{
return Forbid();
}
var results = await validator.ValidateAsync(model, token);
if (results.IsValid)
{
return Ok(await repositoryCase.InsertAsync(mapper.Map<Case>(model), token));
}
return BadRequest(results);
}
catch (Exception e)
{
log.Error(e);
return StatusCode(500);
}
}
[HttpPut]
[ProducesResponseType(typeof(CaseDto), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationResult), (int)HttpStatusCode.BadRequest)]
public async Task<ActionResult<CaseDto>> UpdateCaseAsync([FromBody] CaseDto model, CancellationToken token = default)
{
try
{
if (!permissionValidation.Validate(new[]
{
1
}))
{
return Forbid();
}
var results = await validator.ValidateAsync(model, token);
if (!results.IsValid)
{
return BadRequest(results);
}
var existing = await repositoryCase.GetByIdActiveOnlyAsync(model.Id, token);
var caseEvents = new List<CaseEvent>();
if (existing.ClosedStatusId is 0 or 1 or 2)
{
switch (model.ClosedStatusId)
{
case 3:
existing.ClosedUser = userName;
existing.ClosedDate = DateTime.Now;
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 5,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
After = model.ClosedDate.ToString(CultureInfo.InvariantCulture),
CreatedDate = existing.ClosedDate,
CreatedUser = userName
}
);
break;
case 2:
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 12,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
Before = existing.ClosedDate.ToString(),
After = model.ClosedDate.ToString(CultureInfo.InvariantCulture),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
break;
case 1:
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 13,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
Before = existing.ClosedDate.ToString(),
After = model.ClosedDate.ToString(CultureInfo.InvariantCulture),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
break;
}
}
else
{
switch (model.ClosedStatusId)
{
case 2:
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 12,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
Before = existing.ClosedDate.ToString(),
After = model.ClosedDate.ToString(CultureInfo.InvariantCulture),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
break;
case 1:
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 13,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
Before = existing.ClosedDate.ToString(),
After = model.ClosedDate.ToString(CultureInfo.InvariantCulture),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
break;
}
}
existing.ClosedStatusId = model.ClosedStatusId;
if (existing.LockedUser != model.LockedUser)
{
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 14,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
Before = existing.LockedUser,
After = model.LockedUser,
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
}
existing.LockedUser = model.LockedUser;
if (existing.Locked is 0 or null)
{
if (existing.Locked == 1)
{
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 6,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.CaseKeyValue,
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
existing.LockedDate = DateTime.Now;
existing.LockedUser = userName;
}
else
{
existing.LockedDate = null;
}
}
existing.Locked = (byte)(model.Locked ? 1 : 0);
if (existing.DiaryDate != model.DiaryDate)
{
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 10,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.LockedUser,
Before = existing.DiaryDate.ToString(),
After = model.DiaryDate.ToString(CultureInfo.InvariantCulture),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
}
existing.DiaryDate = model.DiaryDate;
if (existing.Diary is 0 or null)
{
if (model.Diary)
{
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 7,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.LockedUser,
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
existing.DiaryUser = userName;
}
}
existing.Diary = (byte)(model.Diary ? 1 : 0);
if (existing.CaseWorkflowStatusGuid != model.CaseWorkflowStatusGuid)
{
var caseWorkflowStatusRepository = new CaseWorkflowStatusRepository(dbContext, userName);
var caseWorkflowStatuses = await caseWorkflowStatusRepository.GetByCasesWorkflowGuidActiveOnlyAsync(existing.CaseWorkflowGuid, token);
var caseWorkflowStatus = caseWorkflowStatuses.FirstOrDefault(f => f.Guid == model.CaseWorkflowStatusGuid);
if (caseWorkflowStatus == null)
{
return Forbid();
}
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 9,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.LockedUser,
Before = existing.CaseWorkflowStatusGuid.ToString(),
After = model.CaseWorkflowStatusGuid.ToString(),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
if (model.Payload != null)
{
if (caseWorkflowStatus.EnableNotification == 1 ||
caseWorkflowStatus.EnableHttpEndpoint == 1)
{
var payload = JsonConvert.DeserializeObject<EntityAnalysisModelInstanceEntryPayload>(existing.Json, jsonSerializationHelper.DefaultJsonSerializerSettingsSettings);
if (caseWorkflowStatus.EnableNotification == 1)
{
var notification = new Notification(log, dynamicEnvironment);
var notificationSubject = payload.ReplaceTokens(caseWorkflowStatus.NotificationSubject);
var notificationDestination = payload.ReplaceTokens(caseWorkflowStatus.NotificationDestination);
var notificationBody = payload.ReplaceTokens(caseWorkflowStatus.NotificationBody);
await notification.SendAsync(caseWorkflowStatus.NotificationTypeId ?? 1,
notificationDestination,
notificationSubject,
notificationBody, token);
}
if (caseWorkflowStatus.EnableHttpEndpoint == 1)
{
var endpoint = payload.ReplaceTokens(caseWorkflowStatus.HttpEndpoint);
if (caseWorkflowStatus.HttpEndpointTypeId == 1)
{
await SendHttpEndpoint.PostAsync(endpoint, PreparePostBodyString(existing, payload, caseWorkflowStatus), log);
}
else
{
await SendHttpEndpoint.GetAsync(endpoint, log);
}
}
}
}
}
existing.CaseWorkflowStatusGuid = model.CaseWorkflowStatusGuid;
if (existing.Rating != model.Rating)
{
caseEvents.Add(new CaseEvent
{
CaseEventTypeId = 11,
CaseId = existing.Id,
CaseKey = existing.CaseKey,
CaseKeyValue = existing.LockedUser,
Before = existing.Rating.ToString(),
After = model.Rating.ToString(),
CreatedDate = DateTime.Now,
CreatedUser = userName
}
);
}
existing.Rating = model.Rating;
existing = await repositoryCase.UpdateCaseAsync(existing, token);
await repositoryCaseEvent.BulkInsertAsync(caseEvents, token);
return Ok(existing);
}
catch (KeyNotFoundException)
{
return StatusCode(204);
}
catch (Exception e)
{
log.Error(e);
return StatusCode(500);
}
}
private string PreparePostBodyString(Case existing, EntityAnalysisModelInstanceEntryPayload payload, CaseWorkflowStatus caseWorkflowStatus)
{
var caseJObject = JObject.FromObject(existing, jsonSerializationHelper.ArchiveJsonSerializer);
caseJObject["caseWorkflowStatusName"] = caseWorkflowStatus.Name;
var payloadJObject = JObject.FromObject(payload, jsonSerializationHelper.ArchiveJsonSerializer);
caseJObject.Remove("json");
caseJObject["payload"] = payloadJObject;
return caseJObject.ToString();
}
}
}