-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNormalizationsController.cs
More file actions
254 lines (214 loc) · 9.42 KB
/
Copy pathNormalizationsController.cs
File metadata and controls
254 lines (214 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using Automation.UI.Models;
using Automation.UI.Services.Persistence;
using Microsoft.AspNetCore.Mvc;
namespace Automation.UI.Controllers;
public class NormalizationsController(INormalizationStore store) : Controller
{
[HttpGet]
public async Task<IActionResult> Index(CancellationToken ct)
{
var operations = await store.GetAllOperationsAsync(ct);
var sequences = await store.GetAllSequencesAsync(ct);
var suites = await store.GetAllSuitesAsync(ct);
ViewBag.Operations = operations;
ViewBag.Sequences = sequences;
ViewBag.Suites = suites;
return View(operations);
}
// ===== Operations =====
[HttpGet]
public async Task<IActionResult> GetOperationJson(Guid id, CancellationToken ct)
{
var op = await store.GetOperationByIdAsync(id, ct);
if (op == null) return NotFound();
return Json(op);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveOperation([FromBody] NormalizationOperationDefinition model, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(model.Name))
return BadRequest("Operation name is required.");
if (string.IsNullOrWhiteSpace(model.OperationType))
return BadRequest("Operation type is required.");
if (model.ResourceTypes.Count == 0)
return BadRequest("At least one resource type is required.");
var existing = await store.GetOperationByIdAsync(model.Id, ct);
if (existing is { IsSystem: true })
return StatusCode(StatusCodes.Status403Forbidden, "System operations cannot be modified.");
model.IsSystem = false;
model.UpdatedAt = DateTimeOffset.UtcNow;
await store.UpsertOperationAsync(model, ct);
return Json(new { id = model.Id });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteOperation([FromBody] IdRequest request, CancellationToken ct)
{
var op = await store.GetOperationByIdAsync(request.Id, ct);
if (op == null) return NotFound();
if (op.IsSystem)
return StatusCode(StatusCodes.Status403Forbidden, "System operations cannot be deleted.");
var sequences = await store.GetAllSequencesAsync(ct);
var referencedBySequence = sequences.Any(s => s.Entries.Any(e => e.OperationId == request.Id));
if (referencedBySequence)
return Conflict("Operation is referenced by one or more sequences and cannot be deleted.");
var suites = await store.GetAllSuitesAsync(ct);
var referencedBySuite = suites.Any(s => s.OperationIds.Contains(request.Id));
if (referencedBySuite)
return Conflict("Operation is referenced by one or more suites and cannot be deleted.");
await store.DeleteOperationAsync(request.Id, ct);
return Ok();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CloneOperation([FromBody] IdRequest request, CancellationToken ct)
{
var source = await store.GetOperationByIdAsync(request.Id, ct);
if (source == null) return NotFound();
var clone = new NormalizationOperationDefinition
{
Id = Guid.NewGuid(),
Name = $"{source.Name} (Copy)",
Description = source.Description,
OperationType = source.OperationType,
ResourceTypes = [..source.ResourceTypes],
SourceFhirPath = source.SourceFhirPath,
TargetFhirPath = source.TargetFhirPath,
ConditionTargetFhirPath = source.ConditionTargetFhirPath,
ConditionTargetValue = source.ConditionTargetValue,
Conditions = [..source.Conditions],
CodeMapFhirPath = source.CodeMapFhirPath,
CodeSystemMaps = [..source.CodeSystemMaps],
ExtensionUrls = [..source.ExtensionUrls],
IsSystem = false,
UpdatedAt = DateTimeOffset.UtcNow
};
await store.UpsertOperationAsync(clone, ct);
return Json(new { id = clone.Id });
}
// ===== Sequences =====
[HttpGet]
public async Task<IActionResult> GetSequenceJson(Guid id, CancellationToken ct)
{
var seq = await store.GetSequenceByIdAsync(id, ct);
if (seq == null) return NotFound();
return Json(seq);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveSequence([FromBody] NormalizationSequenceDefinition model, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(model.Name))
return BadRequest("Sequence name is required.");
if (model.Entries.Count == 0)
return BadRequest("At least one operation entry is required.");
var existing = await store.GetSequenceByIdAsync(model.Id, ct);
if (existing is { IsSystem: true })
return StatusCode(StatusCodes.Status403Forbidden, "System sequences cannot be modified.");
model.IsSystem = false;
model.UpdatedAt = DateTimeOffset.UtcNow;
await store.UpsertSequenceAsync(model, ct);
return Json(new { id = model.Id });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteSequence([FromBody] IdRequest request, CancellationToken ct)
{
var seq = await store.GetSequenceByIdAsync(request.Id, ct);
if (seq == null) return NotFound();
if (seq.IsSystem)
return StatusCode(StatusCodes.Status403Forbidden, "System sequences cannot be deleted.");
var suites = await store.GetAllSuitesAsync(ct);
var referencedBySuite = suites.Any(s => s.SequenceIds.Contains(request.Id));
if (referencedBySuite)
return Conflict("Sequence is referenced by one or more suites and cannot be deleted.");
await store.DeleteSequenceAsync(request.Id, ct);
return Ok();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CloneSequence([FromBody] IdRequest request, CancellationToken ct)
{
var source = await store.GetSequenceByIdAsync(request.Id, ct);
if (source == null) return NotFound();
var clone = new NormalizationSequenceDefinition
{
Id = Guid.NewGuid(),
Name = $"{source.Name} (Copy)",
Description = source.Description,
Entries = source.Entries.Select(e => new NormalizationSequenceEntry { OperationId = e.OperationId, Sequence = e.Sequence }).ToList(),
IsSystem = false,
UpdatedAt = DateTimeOffset.UtcNow
};
await store.UpsertSequenceAsync(clone, ct);
return Json(new { id = clone.Id });
}
// ===== Suites =====
[HttpGet]
public async Task<IActionResult> GetSuiteJson(Guid id, CancellationToken ct)
{
var suite = await store.GetSuiteByIdAsync(id, ct);
if (suite == null) return NotFound();
return Json(suite);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveSuite([FromBody] NormalizationSuiteDefinition model, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(model.Name))
return BadRequest("Suite name is required.");
var existing = await store.GetSuiteByIdAsync(model.Id, ct);
if (existing is { IsSystem: true })
return StatusCode(StatusCodes.Status403Forbidden, "System suites cannot be modified.");
// Existing suites preserve their current default flag. New suites may
// carry an explicit initial IsDefault value from the caller.
model.IsDefault = existing?.IsDefault ?? model.IsDefault;
model.IsSystem = false;
model.UpdatedAt = DateTimeOffset.UtcNow;
await store.UpsertSuiteAsync(model, ct);
return Json(new { id = model.Id });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteSuite([FromBody] IdRequest request, CancellationToken ct)
{
var suite = await store.GetSuiteByIdAsync(request.Id, ct);
if (suite == null) return NotFound();
if (suite.IsSystem)
return StatusCode(StatusCodes.Status403Forbidden, "System suites cannot be deleted.");
if (suite.IsDefault)
return Conflict("Default suite cannot be deleted. Promote another suite first.");
await store.DeleteSuiteAsync(request.Id, ct);
return Ok();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CloneSuite([FromBody] IdRequest request, CancellationToken ct)
{
var source = await store.GetSuiteByIdAsync(request.Id, ct);
if (source == null) return NotFound();
var clone = new NormalizationSuiteDefinition
{
Id = Guid.NewGuid(),
Name = $"{source.Name} (Copy)",
Description = source.Description,
OperationIds = [..source.OperationIds],
SequenceIds = [..source.SequenceIds],
IsSystem = false,
IsDefault = false,
UpdatedAt = DateTimeOffset.UtcNow
};
await store.UpsertSuiteAsync(clone, ct);
return Json(new { id = clone.Id });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SetDefaultSuite([FromBody] IdRequest request, CancellationToken ct)
{
var suite = await store.GetSuiteByIdAsync(request.Id, ct);
if (suite == null) return NotFound();
await store.SetDefaultSuiteAsync(request.Id, ct);
return Ok();
}
}