Skip to content

Commit

Permalink
Merge pull request #1007 from bcgov/feature/AB#26819-custom-field-rep…
Browse files Browse the repository at this point in the history
…orting

Feature/ab#26819 Unity Reporting Enhancements
  • Loading branch information
jimmyPasta authored Jan 30, 2025
2 parents 2e173df + 4a90da9 commit c1522bb
Show file tree
Hide file tree
Showing 138 changed files with 103,269 additions and 371 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace Unity.Flex.Reporting
{
public interface IScoresheetReportingDataGeneratorAppService : IApplicationService
{
Task Generate(Guid scoresheetInstanceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace Unity.Flex.Reporting
{
public interface IScoresheetReportingFieldsGeneratorAppService : IApplicationService
{
Task Generate(Guid scoresheetId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using System;
using Volo.Abp.Application.Services;

namespace Unity.Flex.Reporting
{
public interface IWorksheetReportingDataGeneratorAppService : IApplicationService
{
Task Generate(Guid worksheetInstanceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using System;
using Volo.Abp.Application.Services;

namespace Unity.Flex.Reporting
{
public interface IWorksheetReportingFieldsGeneratorAppService : IApplicationService
{
Task Generate(Guid worksheetId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text.Json;
using Unity.Flex.Scoresheets.Enums;
using Unity.Flex.Worksheets.Definitions;
using Volo.Abp.Application.Dtos;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class CreateWorksheetInstanceDto
public string SheetCorrelationProvider { get; set; } = string.Empty;
public string CorrelationAnchor { get; set; } = string.Empty;
public string? CurrentValue { get; set; }
public string ReportData { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface ICustomFieldValueAppService : IApplicationService
Task ExplicitAddAsync(CustomFieldValueDto value);

[RemoteService(false)]
Task SyncWorksheetInstanceValueAsync(Guid worksheetInstanceId);
Task SyncWorksheetInstanceValueAsync(Guid worksheetInstanceId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using Unity.Flex.Domain.Scoresheets;
using Unity.Modules.Shared.Correlation;
Expand All @@ -25,6 +26,9 @@ public class ScoresheetInstance : FullAuditedAggregateRoot<Guid>, IMultiTenant,

public virtual Collection<Answer> Answers { get; private set; } = [];

[Column(TypeName = "jsonb")]
public virtual string ReportData { get; private set; } = "{}";

public ScoresheetInstance(Guid id,
Guid scoresheetId,
Guid correlationId,
Expand All @@ -41,5 +45,11 @@ public ScoresheetInstance UpdateValue()
Value = JsonSerializer.Serialize(this);
return this;
}

public ScoresheetInstance SetReportingData(string reportingData)
{
ReportData = reportingData;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.ObjectModel;
using Unity.Flex.Scoresheets;
using System.ComponentModel.DataAnnotations.Schema;
using Unity.Flex.Worksheets.Definitions;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;
using Newtonsoft.Json;
using Volo.Abp;
using System.Linq;
using Unity.Flex.Scoresheets.Enums;

namespace Unity.Flex.Domain.Scoresheets
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
using System.Linq;
using Unity.Flex.Domain.Exceptions;
using Unity.Flex.Domain.ScoresheetInstances;
using Unity.Flex.Reporting;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace Unity.Flex.Domain.Scoresheets
{
public class Scoresheet : FullAuditedAggregateRoot<Guid>, IMultiTenant
public class Scoresheet : FullAuditedAggregateRoot<Guid>, IMultiTenant, IReportableEntity<Scoresheet>
{
public virtual string Title { get; set; } = string.Empty;
public virtual string Name { get; private set; } = string.Empty;
public virtual uint Version { get; set; } = 1;
public virtual uint Order { get; set; } = 0;
public virtual bool Published { get; set; } = false;
public virtual bool Published { get; set; } = false;
public Guid? TenantId { get; set; }


// For reporting purposes
public virtual string ReportColumns { get; set; } = string.Empty;
public virtual string ReportKeys { get; set; } = string.Empty;
public virtual string ReportViewName { get; set; } = string.Empty;

public virtual Collection<ScoresheetSection> Sections { get; private set; } = [];
public virtual Collection<ScoresheetInstance> Instances { get; private set; } = [];
Expand Down Expand Up @@ -100,5 +105,14 @@ internal Scoresheet CloneSection(ScoresheetSection clonedSection)
Sections.Add(clonedSection);
return this;
}

public Scoresheet SetReportingFields(string keys, string columns, string reportViewName)
{
ReportColumns = columns;
ReportViewName = reportViewName;
ReportKeys = keys;

return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.Flex.Domain.ScoresheetInstances;
using Unity.Flex.Domain.Scoresheets;
using Unity.Flex.Reporting.DataGenerators;
using Unity.Flex.Scoresheets.Enums;
using Unity.Flex.Scoresheets.Events;
using Unity.Flex.Worksheets.Definitions;
using Unity.Modules.Shared.Features;
using Volo.Abp.Domain.Services;
using Volo.Abp.Features;
using Volo.Abp.Validation;

namespace Unity.Flex.Domain.Services
{
public class ScoresheetsManager : DomainService
public class ScoresheetsManager(IScoresheetInstanceRepository scoresheetInstanceRepository,
IScoresheetRepository scoresheetRepository,
IReportingDataGeneratorService<Scoresheet, ScoresheetInstance> reportingDataGeneratorService,
IFeatureChecker featureChecker) : DomainService
{
public static List<string> ValidateScoresheetAnswersAsync(ScoresheetInstance scoresheetInstance, Scoresheet scoresheet)
{
Expand Down Expand Up @@ -37,6 +48,41 @@ private static string BuildMissingAnswerError(Question question)
{
return $"{question.Section?.Order + 1}.{question.Order + 1}: {question.Label} (required)";
}

public async Task PersistScoresheetData(PersistScoresheetSectionInstanceEto eventData)
{
var instance = await scoresheetInstanceRepository.GetByCorrelationAsync(eventData.AssessmentId) ?? throw new AbpValidationException("Missing ScoresheetInstance.");
var scoresheet = await scoresheetRepository.GetAsync(instance.ScoresheetId);

var scoresheetAnswers = eventData.AssessmentAnswers.ToList();

foreach (var item in scoresheetAnswers)
{
var ans = instance.Answers.FirstOrDefault(a => a.QuestionId == item.QuestionId);

if (ans != null)
{
ans.SetValue(ValueConverter.Convert(item.Answer ?? "", (QuestionType)item.QuestionType));
}
else
{
ans = new Answer(Guid.NewGuid())
{
CurrentValue = ValueConverter.Convert(item?.Answer?.ToString() ?? string.Empty, (QuestionType)item!.QuestionType),
QuestionId = item.QuestionId,
ScoresheetInstanceId = instance.Id
};
instance.Answers.Add(ans);
}

await scoresheetInstanceRepository.UpdateAsync(instance);
}

if (await featureChecker.IsEnabledAsync(FeatureConsts.Reporting))
{
reportingDataGeneratorService.GenerateAndSet(scoresheet, instance);
}
}
}

public static class ScorehseetExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
using Unity.Flex.Domain.WorksheetInstances;
using Unity.Flex.Domain.WorksheetLinks;
using Unity.Flex.Domain.Worksheets;
using Unity.Flex.Reporting.DataGenerators;
using Unity.Flex.WorksheetInstances;
using Unity.Flex.Worksheets.Values;
using Unity.Modules.Shared.Features;
using Volo.Abp.Domain.Services;
using Volo.Abp.Features;

namespace Unity.Flex.Domain.Services
{
public class WorksheetsManager(IWorksheetInstanceRepository worksheetInstanceRepository,
IWorksheetRepository worksheetRepository,
IWorksheetLinkRepository worksheetLinkRepository) : DomainService
IWorksheetLinkRepository worksheetLinkRepository,
IReportingDataGeneratorService<Worksheet, WorksheetInstance> reportingService,
IFeatureChecker featureChecker) : DomainService
{
public async Task PersistWorksheetData(PersistWorksheetIntanceValuesEto eventData)
{
Expand Down Expand Up @@ -68,14 +73,21 @@ public async Task UpdateWorksheetInstanceValueAsync(WorksheetInstance instance)
var worksheet = await worksheetRepository.GetAsync(instance.WorksheetId, true);
var fieldDefinitions = worksheet.Sections.SelectMany(s => s.Fields).ToList();
var instanceCurrentValue = new WorksheetInstanceValue();

foreach (var field in instance.Values)
{
var fieldDefinition = fieldDefinitions.Find(s => s.Id == field.CustomFieldId);
if (fieldDefinition != null)
instanceCurrentValue.Values.Add(new FieldInstanceValue(fieldDefinition.Key,
JsonNode.Parse(field.CurrentValue)?["value"]?.ToString() ?? string.Empty));
}

instance.SetValue(JsonSerializer.Serialize(instanceCurrentValue));

if (await featureChecker.IsEnabledAsync(FeatureConsts.Reporting))
{
reportingService.GenerateAndSet(worksheet, instance);
}
}

private void UpdateExistingWorksheetInstance(WorksheetInstance worksheetInstance, Worksheet? worksheet, List<ValueFieldContainer> fields)
Expand Down Expand Up @@ -183,9 +195,9 @@ private async Task<WorksheetInstance> CreateNewWorksheetInstanceAsync(IWorksheet

foreach (var field in allFields)
{
var match = eventData.CustomFields.Find(s => s.fieldName == field.Name);
var (_, chefsPropertyName, value) = eventData.CustomFields.Find(s => s.fieldName == field.Name);
newInstance.AddValue(field.Id,
ValueConverter.Convert(match.value?.ToString() ?? string.Empty, field.Type, match.chefsPropertyName, eventData.VersionData));
ValueConverter.Convert(value?.ToString() ?? string.Empty, field.Type, chefsPropertyName, eventData.VersionData));
}

var newWorksheetInstance = await worksheetInstanceRepository.InsertAsync(newInstance);
Expand All @@ -206,6 +218,7 @@ public async Task<Worksheet> CloneWorksheetAsync(Guid id)
var highestVersion = worksheetVersions.Max(s => s.Version);
var clonedWorksheet = new Worksheet(Guid.NewGuid(), $"{versionSplit[0]}-v{highestVersion + 1}", worksheet.Title);
clonedWorksheet.SetVersion(highestVersion + 1);

foreach (var section in worksheet.Sections.OrderBy(s => s.Order))
{
var clonedSection = new WorksheetSection(Guid.NewGuid(), section.Name);
Expand All @@ -219,7 +232,7 @@ public async Task<Worksheet> CloneWorksheetAsync(Guid id)

var result = await worksheetRepository.InsertAsync(clonedWorksheet);
return result;
}
}

private static CustomField? FindCustomFieldByName(Worksheet? worksheet, string fieldName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace Unity.Flex.Domain.WorksheetInstances
{
public interface ICustomFieldValueRepository : IBasicRepository<CustomFieldValue, Guid>
{
Task<List<CustomFieldValue>> GetListByWorksheetInstanceAsync(Guid worksheetInstanceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ public class WorksheetInstance : FullAuditedAggregateRoot<Guid>, IMultiTenant, I

public virtual Collection<CustomFieldValue> Values { get; private set; } = [];

[Column(TypeName = "jsonb")]
public virtual string ReportData { get; private set; } = "{}";

protected WorksheetInstance()
{
/* This constructor is for ORMs to be used while getting the entity from the database. */
}

public WorksheetInstance(Guid id,
Guid worksheetId,
Guid correlationId,
Guid correlationId,
string correlationProvider,
Guid worksheetCorrelationId,
string worksheetCorrelationProvider,
string correlationAnchor)
string correlationAnchor,
string? reportData = null)
{
Id = id;
CorrelationId = correlationId;
Expand All @@ -47,6 +51,7 @@ public WorksheetInstance(Guid id,
WorksheetCorrelationProvider = worksheetCorrelationProvider;
UiAnchor = correlationAnchor;
WorksheetId = worksheetId;
ReportData = reportData ?? string.Empty;
}

public WorksheetInstance AddValue(Guid customFieldId, string currentValue)
Expand All @@ -66,5 +71,11 @@ public WorksheetInstance SetAnchor(string uiAnchor)
UiAnchor = uiAnchor;
return this;
}

public WorksheetInstance SetReportingData(string reportingData)
{
ReportData = reportingData;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;
using Unity.Flex.Domain.Worksheets;
using Unity.Modules.Shared.Correlation;
using Volo.Abp.Domain.Entities.Auditing;
Expand All @@ -14,6 +15,7 @@ public class WorksheetLink : AuditedAggregateRoot<Guid>, IMultiTenant, ICorrelat
public string CorrelationProvider { get; private set; } = string.Empty;
public string UiAnchor { get; private set; } = string.Empty;

[JsonIgnore]
public virtual Worksheet Worksheet
{
set => _worksheet = value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text.Json.Serialization;
using Unity.Flex.Worksheets;
using Unity.Flex.Worksheets.Definitions;
using Volo.Abp;
Expand All @@ -23,7 +24,8 @@ public class CustomField : FullAuditedEntity<Guid>, IMultiTenant

public Guid? TenantId { get; set; }

// Navigation
// Navigation
[JsonIgnore]
public virtual WorksheetSection Section
{
set => _section = value;
Expand Down
Loading

0 comments on commit c1522bb

Please sign in to comment.