Skip to content

Commit 51fbcf1

Browse files
authored
Merge branch 'dev' into nvm/LNK-4374_CensusManagerQueries
2 parents 1897a49 + f6007c0 commit 51fbcf1

7 files changed

Lines changed: 720 additions & 20 deletions

File tree

DotNet/DataAcquisition.Domain/Application/Queries/QueryPlanQueries.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public interface IQueryPlanQueries
1919
Task<List<QueryPlanModel>> FindAsync(Expression<Func<QueryPlan, bool>> predicate, CancellationToken cancellationToken = default);
2020
Task<List<string>> GetPlanNamesAsync(string facilityId, CancellationToken cancellationToken = default);
2121
Task<PagedConfigModel<QueryPlanModel>> SearchAsync(SearchQueryPlanModel model, CancellationToken cancellationToken = default);
22+
Task<bool> ExistsAsync(string facilityId, Frequency value, CancellationToken cancellationToken);
2223
}
2324

2425
public class QueryPlanQueries : IQueryPlanQueries
@@ -131,4 +132,9 @@ private Expression<Func<T, object>> SetSortBy<T>(string? sortBy)
131132
var converted = Expression.Convert(property, typeof(object));
132133
return Expression.Lambda<Func<T, object>>(converted, parameter);
133134
}
135+
136+
public Task<bool> ExistsAsync(string facilityId, Frequency value, CancellationToken cancellationToken)
137+
{
138+
return _dbContext.QueryPlans.AnyAsync(q => q.FacilityId == facilityId && q.Type == value, cancellationToken);
139+
}
134140
}

DotNet/DataAcquisition.Domain/Application/Serializers/ParameterConverter.cs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using LantanaGroup.Link.DataAcquisition.Domain.Infrastructure.Interfaces;
2-
using LantanaGroup.Link.DataAcquisition.Domain.Infrastructure.Models.QueryConfig.Parameter;
3-
using System.Text.Json;
1+
using System.Text.Json;
42
using System.Text.Json.Serialization;
3+
using LantanaGroup.Link.DataAcquisition.Domain.Infrastructure.Interfaces;
4+
using LantanaGroup.Link.DataAcquisition.Domain.Infrastructure.Models.QueryConfig.Parameter;
55

66
namespace LantanaGroup.Link.DataAcquisition.Domain.Application.Serializers;
77

@@ -11,14 +11,52 @@ public override IParameter Read(ref Utf8JsonReader reader, Type typeToConvert, J
1111
{
1212
using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
1313
{
14-
if (!doc.RootElement.TryGetProperty("ParameterType", out JsonElement typeElement))
14+
JsonElement typeElement;
15+
string configType = null;
16+
17+
if (doc.RootElement.TryGetProperty("ParameterType", out typeElement) ||
18+
doc.RootElement.TryGetProperty("parameterType", out typeElement))
19+
{
20+
configType = typeElement.GetString();
21+
}
22+
else if (doc.RootElement.TryGetProperty("$type", out typeElement))
23+
{
24+
var typeName = typeElement.GetString();
25+
if (typeName?.Contains("LiteralParameter") == true)
26+
{
27+
configType = "Literal";
28+
}
29+
else if (typeName?.Contains("ResourceIdsParameter") == true)
30+
{
31+
configType = "ResourceIds";
32+
}
33+
else if (typeName?.Contains("VariableParameter") == true)
34+
{
35+
configType = "Variable";
36+
}
37+
}
38+
39+
if (configType == null)
1540
{
16-
if (!doc.RootElement.TryGetProperty("parameterType", out typeElement))
41+
// Fallback to property inspection
42+
if (doc.RootElement.TryGetProperty("Literal", out _))
1743
{
18-
throw new JsonException("Missing ParameterType property.");
44+
configType = "Literal";
45+
}
46+
else if (doc.RootElement.TryGetProperty("Resource", out _) && doc.RootElement.TryGetProperty("Paged", out _))
47+
{
48+
configType = "ResourceIds";
49+
}
50+
else if (doc.RootElement.TryGetProperty("Variable", out _))
51+
{
52+
configType = "Variable";
53+
}
54+
else
55+
{
56+
throw new JsonException("Unable to determine ParameterType. Missing type discriminator or distinguishing properties.");
1957
}
2058
}
21-
var configType = typeElement.GetString();
59+
2260
return configType switch
2361
{
2462
"Literal" => JsonSerializer.Deserialize<LiteralParameter>(doc.RootElement.GetRawText(), options),
@@ -28,6 +66,7 @@ public override IParameter Read(ref Utf8JsonReader reader, Type typeToConvert, J
2866
};
2967
}
3068
}
69+
3170
public override void Write(Utf8JsonWriter writer, IParameter value, JsonSerializerOptions options)
3271
{
3372
JsonSerializer.Serialize(writer, value, value.GetType(), options);

DotNet/DataAcquisition.Domain/Application/Serializers/QueryConfigConverter.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,44 @@ public override IQueryConfig Read(ref Utf8JsonReader reader, Type typeToConvert,
1111
{
1212
using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
1313
{
14-
if (!doc.RootElement.TryGetProperty("QueryConfigType", out JsonElement typeElement))
14+
JsonElement typeElement;
15+
string configType = null;
16+
17+
if (doc.RootElement.TryGetProperty("QueryConfigType", out typeElement) ||
18+
doc.RootElement.TryGetProperty("queryConfigType", out typeElement))
19+
{
20+
configType = typeElement.GetString();
21+
}
22+
else if (doc.RootElement.TryGetProperty("$type", out typeElement))
23+
{
24+
var typeName = typeElement.GetString();
25+
if (typeName?.Contains("ParameterQueryConfig") == true)
26+
{
27+
configType = "Parameter";
28+
}
29+
else if (typeName?.Contains("ReferenceQueryConfig") == true)
30+
{
31+
configType = "Reference";
32+
}
33+
}
34+
35+
if (configType == null)
1536
{
16-
if (!doc.RootElement.TryGetProperty("queryConfigType", out typeElement))
37+
// Fallback to property inspection if no type discriminator is found
38+
if (doc.RootElement.TryGetProperty("Parameters", out _))
39+
{
40+
configType = "Parameter";
41+
}
42+
else if (doc.RootElement.TryGetProperty("Paged", out _))
1743
{
18-
throw new JsonException("Missing QueryConfigType property.");
44+
configType = "Reference";
45+
}
46+
else
47+
{
48+
throw new JsonException("Unable to determine QueryConfigType. Missing type discriminator or distinguishing properties.");
1949
}
2050
}
21-
var configType = typeElement.GetString();
51+
2252
return configType switch
2353
{
2454
"Parameter" => JsonSerializer.Deserialize<ParameterQueryConfig>(doc.RootElement.GetRawText(), options),
@@ -27,6 +57,7 @@ public override IQueryConfig Read(ref Utf8JsonReader reader, Type typeToConvert,
2757
};
2858
}
2959
}
60+
3061
public override void Write(Utf8JsonWriter writer, IQueryConfig value, JsonSerializerOptions options)
3162
{
3263
JsonSerializer.Serialize(writer, value, value.GetType(), options);

DotNet/DataAcquisition/Controllers/QueryPlanConfigController.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DataAcquisition.Domain.Application.Models;
22
using DataAcquisition.Domain.Application.Models.Exceptions;
3+
using Hl7.Fhir.Model;
34
using LantanaGroup.Link.DataAcquisition.Domain.Application.Managers;
45
using LantanaGroup.Link.DataAcquisition.Domain.Application.Models;
56
using LantanaGroup.Link.DataAcquisition.Domain.Application.Models.Exceptions;
@@ -139,9 +140,9 @@ public async Task<IActionResult> CreateQueryPlan(
139140

140141
if (ModelState.IsValid)
141142
{
142-
var existing = await _queryPlanQueries.GetAsync(facilityId, queryPlan.Type.Value, cancellationToken);
143+
var exists = await _queryPlanQueries.ExistsAsync(facilityId, queryPlan.Type.Value, cancellationToken);
143144

144-
if (existing != null)
145+
if (exists)
145146
{
146147
throw new EntityAlreadyExistsException($"A Query Plan already exists for facilityId: {facilityId}.");
147148
}
@@ -254,9 +255,9 @@ public async Task<ActionResult> UpdateQueryPlan(
254255

255256
if (ModelState.IsValid)
256257
{
257-
var existing = await _queryPlanQueries.GetAsync(facilityId, queryPlan.Type.Value, cancellationToken);
258+
var exists = await _queryPlanQueries.ExistsAsync(facilityId, queryPlan.Type.Value, cancellationToken);
258259

259-
if (existing == null)
260+
if (!exists)
260261
{
261262
throw new NotFoundException($"A Query Plan was not found for facilityId: {facilityId}.");
262263
}
@@ -340,6 +341,7 @@ public async Task<ActionResult> DeleteQueryPlan(
340341

341342
try
342343
{
344+
facilityId = facilityId.SanitizeAndRemove();
343345
if (string.IsNullOrWhiteSpace(facilityId))
344346
{
345347
throw new BadRequestException("parameter facilityId is required.");
@@ -350,14 +352,14 @@ public async Task<ActionResult> DeleteQueryPlan(
350352
throw new BadRequestException("type query parameter must be defined.");
351353
}
352354

353-
var existing = await _queryPlanQueries.GetAsync(facilityId.Sanitize(), parameters.Type.Value, cancellationToken);
355+
var exists = await _queryPlanQueries.ExistsAsync(facilityId, parameters.Type.Value, cancellationToken);
354356

355-
if (existing == null)
357+
if (!exists)
356358
{
357359
throw new NotFoundException($"A QueryPlan or Query component was not found for facilityId: {facilityId}.");
358360
}
359361

360-
await _queryPlanManager.DeleteAsync(facilityId.Sanitize(), parameters.Type.Value, cancellationToken);
362+
await _queryPlanManager.DeleteAsync(facilityId, parameters.Type.Value, cancellationToken);
361363

362364
return Accepted();
363365
}

DotNet/ServiceTests/UnitTests/DataAcquisition/Controllers/QueryPlanConfigControllerTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public async Task UpdateQueryPlanTest()
8787
{
8888
var facilityId = "test-facility-id";
8989
var _mocker = new AutoMocker();
90+
_mocker.GetMock<IQueryPlanQueries>().Setup(x => x.ExistsAsync(It.IsAny<string>(), Frequency.Monthly, CancellationToken.None))
91+
.ReturnsAsync(true);
9092
_mocker.GetMock<IQueryPlanQueries>().Setup(x => x.GetAsync(It.IsAny<string>(), Frequency.Monthly, CancellationToken.None))
9193
.ReturnsAsync(new QueryPlanModel());
9294
_mocker.GetMock<IQueryPlanManager>().Setup(x => x.UpdateAsync(It.IsAny<UpdateQueryPlanModel>(), CancellationToken.None))
@@ -135,8 +137,8 @@ public async Task DeleteQueryPlanTest()
135137
_mocker.GetMock<IQueryPlanManager>().Setup(x => x.AddAsync(It.IsAny<CreateQueryPlanModel>(), CancellationToken.None))
136138
.ReturnsAsync(queryPlan);
137139

138-
_mocker.GetMock<IQueryPlanQueries>().Setup(x => x.GetAsync(It.IsAny<string>(), Frequency.Monthly, CancellationToken.None))
139-
.ReturnsAsync(queryPlan);
140+
_mocker.GetMock<IQueryPlanQueries>().Setup(x => x.ExistsAsync(It.IsAny<string>(), Frequency.Monthly, CancellationToken.None))
141+
.ReturnsAsync(true);
140142

141143
var _createController = _mocker.CreateInstance<QueryPlanConfigController>();
142144

0 commit comments

Comments
 (0)