Skip to content
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

DP-544 - Expose JsonValue and OptionValue via the data sharing api #705

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.OrganisationInformation;
using CO.CDP.OrganisationInformation.Persistence;
using System.Text.Json;
using Address = CO.CDP.OrganisationInformation.Address;
using Persistence = CO.CDP.OrganisationInformation.Persistence.Forms;

Expand Down Expand Up @@ -101,7 +102,8 @@ public DataSharingProfile()
.ForMember(m => m.EndValue, o => o.MapFrom(m => m.EndValue))
.ForMember(m => m.DateValue, o => o.MapFrom(m => ToDateOnly(m.DateValue)))
.ForMember(m => m.TextValue, o => o.MapFrom(m => m.TextValue))
.ForMember(m => m.OptionValue, o => o.Ignore());
.ForMember(m => m.OptionValue, o => o.MapFrom(m => m.OptionValue != null ? new string[] { m.OptionValue } : null))
.ForMember(m => m.JsonValue, o => o.MapFrom<JsonValueResolver>());

CreateMap<Persistence.FormQuestion, FormQuestion>()
.ForMember(m => m.Type, o => o.MapFrom<CustomFormQuestionTypeResolver>())
Expand Down Expand Up @@ -176,4 +178,17 @@ private string ToHtmlString(Persistence.FormAddress source)
return
$"{source.StreetAddress}<br/>{source.Locality}<br/>{source.PostalCode}<br/>{source.Region}<br/>{source.CountryName}";
}
}

public class JsonValueResolver : IValueResolver<Persistence.FormAnswer, FormAnswer, Dictionary<string, object>?>
{
public Dictionary<string, object>? Resolve(Persistence.FormAnswer source, FormAnswer destination, Dictionary<string, object>? destMember, ResolutionContext context)
{
if (string.IsNullOrEmpty(source.JsonValue))
{
return null;
}

return JsonSerializer.Deserialize<Dictionary<string, object>>(source.JsonValue);
}
}
3 changes: 3 additions & 0 deletions Services/CO.CDP.DataSharing.WebApi/Model/FormAnswer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ public record FormAnswer

/// <example>["option-id-1"]</example>
public List<string> OptionValue { get; init; } = [];

/// <example>{"id": "00000000-0000-0000-0000-000000000000", "type": "organisation OR connected-entity"}</example>
public Dictionary<string, object>? JsonValue { get; init; }
}