diff --git a/server/AyaNova/Controllers/EventLogController.cs b/server/AyaNova/Controllers/EventLogController.cs index cf258124..9b0b8eb5 100644 --- a/server/AyaNova/Controllers/EventLogController.cs +++ b/server/AyaNova/Controllers/EventLogController.cs @@ -99,7 +99,40 @@ namespace AyaNova.Api.Controllers } + /// + /// V7 Import replace log entry + /// (internal use only) + /// + /// + /// Automatically filled from route path, no need to specify in body + /// + [ApiExplorerSettings(IgnoreApi = true)] + [HttpPost] + public async Task PostV7Modify([FromBody] V7Event inObj, ApiVersion apiVersion) + { + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + //Only biz admin full users can do this + if (!Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.BizAdminFull)) + return StatusCode(403, new ApiNotAuthorizedResponse()); + + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + + await EventLogProcessor.V7_Modify_LogAsync(inObj, ct); + return NoContent(); + } + public sealed class V7Event + { + public AyaType AyType { get; set; } + public long AyId { get; set; } + public long Creator { get; set; } + public long Modifier { get; set; } + public DateTime Created { get; set; } + public DateTime Modified { get; set; } + + } //------------ diff --git a/server/AyaNova/biz/EventLogProcessor.cs b/server/AyaNova/biz/EventLogProcessor.cs index 8346203c..efc4702b 100644 --- a/server/AyaNova/biz/EventLogProcessor.cs +++ b/server/AyaNova/biz/EventLogProcessor.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using AyaNova.Models; +using System; namespace AyaNova.Biz @@ -56,7 +57,7 @@ namespace AyaNova.Biz //Set up the query var q = ct.Event.Select(m => m).Skip(offset).Take(limit).AsNoTracking(); - q = q.Where(m => m.AyId == opt.AyId && m.AyType== opt.AyType); + q = q.Where(m => m.AyId == opt.AyId && m.AyType == opt.AyType); q = q.OrderByDescending(m => m.Created); q = q.Skip(offset).Take(limit); @@ -123,6 +124,28 @@ namespace AyaNova.Biz } + /// + /// V7 import handler + /// Import needs to fixup event log CREATED entry to show original created and last modified times + /// and users + /// + internal static async Task V7_Modify_LogAsync(AyaNova.Api.Controllers.EventLogController.V7Event ev, AyContext ct) + { + //delete the automatically created entry from the import object + await ct.Database.ExecuteSqlInterpolatedAsync($"delete from aevent where aytype = {ev.AyType} and ayid={ev.AyId}"); + + //Now create the entries to reflect the original data from v7 + //CREATED + await EventLogProcessor.LogEventToDatabaseAsync(new Event(ev.Creator, ev.AyId, ev.AyType, AyaEvent.Created, ev.Created, null), ct); + + //MODIFIED + await EventLogProcessor.LogEventToDatabaseAsync(new Event(ev.Modifier,ev.AyId, ev.AyType, AyaEvent.Modified, ev.Modified, null), ct); + + + await ct.SaveChangesAsync(); + } + + /////////////////////////////////////////////////////////////////////