-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMigrateCorrespondenceMapper.cs
More file actions
258 lines (234 loc) · 13.6 KB
/
MigrateCorrespondenceMapper.cs
File metadata and controls
258 lines (234 loc) · 13.6 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
255
256
257
258
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application.Helpers;
using Altinn.Correspondence.Application.MigrateCorrespondence;
using Altinn.Correspondence.Application.SyncCorrespondenceEvent;
using Altinn.Correspondence.Core.Models.Entities;
using Altinn.Correspondence.Core.Models.Enums;
namespace Altinn.Correspondence.Mappers;
internal static class MigrateCorrespondenceMapper
{
internal static async Task<MigrateCorrespondenceRequest> MapToRequestAsync(MigrateCorrespondenceExt migrateCorrespondenceExt, ServiceOwnerHelper serviceOwnerHelper, CancellationToken cancellationToken)
{
var publishedTime = migrateCorrespondenceExt.EventHistory.FirstOrDefault(e => e.Status == MigrateCorrespondenceStatusExt.Published)?.StatusChanged;
var correspondence = new CorrespondenceEntity
{
Altinn2CorrespondenceId = migrateCorrespondenceExt.Altinn2CorrespondenceId,
Statuses = MapMigrateCorrespondenceStatusesExtToInternal(migrateCorrespondenceExt.EventHistory),
Notifications = [.. migrateCorrespondenceExt.NotificationHistory.Select(MapNotificationToInternal)],
ForwardingEvents = [.. migrateCorrespondenceExt.ForwardingHistory.Select(MapForwardingEventToInternal)],
SendersReference = migrateCorrespondenceExt.CorrespondenceData.Correspondence.SendersReference,
Recipient = migrateCorrespondenceExt.CorrespondenceData.Recipients.First(),
ResourceId = migrateCorrespondenceExt.CorrespondenceData.Correspondence.ResourceId,
Sender = migrateCorrespondenceExt.CorrespondenceData.Correspondence.Sender,
ServiceOwnerId = await serviceOwnerHelper.GetSafeServiceOwnerIdAsync(migrateCorrespondenceExt.CorrespondenceData.Correspondence.Sender, cancellationToken),
MessageSender = migrateCorrespondenceExt.CorrespondenceData.Correspondence.MessageSender,
RequestedPublishTime = (DateTimeOffset)migrateCorrespondenceExt.CorrespondenceData.Correspondence.RequestedPublishTime,
DueDateTime = migrateCorrespondenceExt.CorrespondenceData.Correspondence.DueDateTime,
PropertyList = migrateCorrespondenceExt.CorrespondenceData.Correspondence.PropertyList,
ReplyOptions = migrateCorrespondenceExt.CorrespondenceData.Correspondence.ReplyOptions != null ? CorrespondenceReplyOptionsMapper.MapListToEntities(migrateCorrespondenceExt.CorrespondenceData.Correspondence.ReplyOptions) : new List<CorrespondenceReplyOptionEntity>(),
IgnoreReservation = migrateCorrespondenceExt.CorrespondenceData.Correspondence.IgnoreReservation,
ExternalReferences = migrateCorrespondenceExt.CorrespondenceData.Correspondence.ExternalReferences != null ? ExternalReferenceMapper.MapListToEntities(migrateCorrespondenceExt.CorrespondenceData.Correspondence.ExternalReferences) : new List<ExternalReferenceEntity>(),
Created = migrateCorrespondenceExt.Created,
Content = migrateCorrespondenceExt.CorrespondenceData.Correspondence.Content != null ? new CorrespondenceContentEntity
{
Language = migrateCorrespondenceExt.CorrespondenceData.Correspondence.Content.Language,
MessageTitle = migrateCorrespondenceExt.CorrespondenceData.Correspondence.Content.MessageTitle,
MessageSummary = migrateCorrespondenceExt.CorrespondenceData.Correspondence.Content.MessageSummary,
MessageBody = migrateCorrespondenceExt.CorrespondenceData.Correspondence.Content.MessageBody,
Attachments = []
} : null,
IsConfirmationNeeded = migrateCorrespondenceExt.CorrespondenceData.Correspondence.IsConfirmationNeeded,
IsMigrating = migrateCorrespondenceExt.IsMigrating,
PartyId = migrateCorrespondenceExt.PartyId,
Published = publishedTime
};
var deleteEvents = MapCorrespondenceStatusEventsToDeleteEvents(migrateCorrespondenceExt.EventHistory);
return new MigrateCorrespondenceRequest()
{
CorrespondenceEntity = correspondence,
Altinn2CorrespondenceId = migrateCorrespondenceExt.Altinn2CorrespondenceId,
ExistingAttachments = migrateCorrespondenceExt.CorrespondenceData.ExistingAttachments ?? new List<Guid>(),
MakeAvailable = migrateCorrespondenceExt.MakeAvailable,
DeleteEventEntities = deleteEvents
};
}
private static List<CorrespondenceDeleteEventEntity> MapCorrespondenceStatusEventsToDeleteEvents(List<MigrateCorrespondenceStatusEventExt> eventHistory)
{
return eventHistory
.Where(e => e.Status is MigrateCorrespondenceStatusExt.SoftDeletedByRecipient
or MigrateCorrespondenceStatusExt.RestoredByRecipient
or MigrateCorrespondenceStatusExt.PurgedByRecipient // Should never occur as we do not migrate hard deletes
or MigrateCorrespondenceStatusExt.PurgedByAltinn) // Should never occur as we do not migrate hard deletes
.Select(MapCorrespondenceStatusEventToDeleteEvent)
.ToList();
}
private static List<CorrespondenceStatusEntity> MapMigrateCorrespondenceStatusesExtToInternal(List<MigrateCorrespondenceStatusEventExt> eventHistory)
{
// Filter out delete events as these are represented as deletion events in the internal model, and should not be added as status events
return eventHistory
.Where(e => e.Status is not (MigrateCorrespondenceStatusExt.SoftDeletedByRecipient
or MigrateCorrespondenceStatusExt.RestoredByRecipient
or MigrateCorrespondenceStatusExt.PurgedByRecipient
or MigrateCorrespondenceStatusExt.PurgedByAltinn))
.Select(MapCorrespondenceStatusEventToInternal)
.ToList();
}
internal static CorrespondenceMigrationStatusExt MapCorrespondenceMigrationStatusToExternal(MigrateCorrespondenceResponse migrateCorrespondenceResponse)
{
return new CorrespondenceMigrationStatusExt()
{
CorrespondenceId = migrateCorrespondenceResponse.CorrespondenceId,
Altinn2CorrespondenceId = migrateCorrespondenceResponse.Altinn2CorrespondenceId,
AttachmentStatuses = MapAttachmentMigrationStatusToExternal(migrateCorrespondenceResponse.AttachmentMigrationStatuses),
DialogId = migrateCorrespondenceResponse.DialogId
};
}
internal static MakeCorrespondenceAvailableRequest MapMakeAvailableToInternal(MakeCorrespondenceAvailableRequestExt maExt)
{
return new MakeCorrespondenceAvailableRequest()
{
CreateEvents = maExt.CreateEvents,
CorrespondenceId = maExt.CorrespondenceId,
CorrespondenceIds = maExt.CorrespondenceIds,
AsyncProcessing = maExt.AsyncProcessing,
BatchSize = maExt.BatchSize,
CreatedFrom = maExt.CreatedFrom,
CreatedTo = maExt.CreatedTo
};
}
internal static MakeCorrespondenceAvailableResponseExt MapMakeAvailableResponseToExternal(MakeCorrespondenceAvailableResponse response)
{
return new MakeCorrespondenceAvailableResponseExt
{
Statuses = response.Statuses?.Select(s => new MakeCorrespondenceAvailableStatusExt(s.CorrespondenceId, s.Error, s.DialogId, s.Ok)).ToList()
};
}
internal static SyncCorrespondenceStatusEventRequest MapSyncStatusEventToInternal(SyncCorrespondenceStatusEventRequestExt requestExt)
{
SyncCorrespondenceStatusEventRequest requestInt = new SyncCorrespondenceStatusEventRequest
{
CorrespondenceId = requestExt.CorrespondenceId,
};
if (requestExt.SyncedEvents == null || requestExt.SyncedEvents.Count == 0)
{
return requestInt;
}
foreach (var syncedEvent in requestExt.SyncedEvents)
{
if(syncedEvent.Status == MigrateCorrespondenceStatusExt.SoftDeletedByRecipient
|| syncedEvent.Status == MigrateCorrespondenceStatusExt.RestoredByRecipient
|| syncedEvent.Status == MigrateCorrespondenceStatusExt.PurgedByRecipient
|| syncedEvent.Status == MigrateCorrespondenceStatusExt.PurgedByAltinn)
{
if(requestInt.SyncedDeleteEvents == null)
{
requestInt.SyncedDeleteEvents = new List<CorrespondenceDeleteEventEntity>();
}
requestInt.SyncedDeleteEvents.Add(MapCorrespondenceStatusEventToDeleteEvent(syncedEvent));
}
else
{
if (requestInt.SyncedEvents == null)
{
requestInt.SyncedEvents = new List<CorrespondenceStatusEntity>();
}
requestInt.SyncedEvents.Add(MapCorrespondenceStatusEventToInternal(syncedEvent));
}
}
return requestInt;
}
private static CorrespondenceDeleteEventType MapMigrateCorrespondenceStatusExtToDeleteEventType(MigrateCorrespondenceStatusExt status)
{
switch (status)
{
case MigrateCorrespondenceStatusExt.SoftDeletedByRecipient:
return CorrespondenceDeleteEventType.SoftDeletedByRecipient;
case MigrateCorrespondenceStatusExt.RestoredByRecipient:
return CorrespondenceDeleteEventType.RestoredByRecipient;
case MigrateCorrespondenceStatusExt.PurgedByRecipient:
return CorrespondenceDeleteEventType.HardDeletedByRecipient;
case MigrateCorrespondenceStatusExt.PurgedByAltinn:
return CorrespondenceDeleteEventType.HardDeletedByServiceOwner;
default:
throw new ArgumentOutOfRangeException(nameof(status), $"Not expected status value: {status}");
}
}
internal static SyncCorrespondenceForwardingEventRequest MapSyncForwardingEventToInternal(SyncCorrespondenceForwardingEventRequestExt requestExt)
{
return new SyncCorrespondenceForwardingEventRequest()
{
CorrespondenceId = requestExt.CorrespondenceId,
SyncedEvents = [.. requestExt.SyncedEvents.Select(MapForwardingEventToInternal)]
};
}
internal static SyncCorrespondenceNotificationEventRequest MapSyncCorrespondenceNotificationEventToInternal(SyncCorrespondenceNotificationEventRequestExt requestExt)
{
return new SyncCorrespondenceNotificationEventRequest()
{
CorrespondenceId = requestExt.CorrespondenceId,
SyncedEvents = [.. requestExt.SyncedEvents.Select(MapNotificationToInternal)]
};
}
private static CorrespondenceStatusEntity MapCorrespondenceStatusEventToInternal(MigrateCorrespondenceStatusEventExt statusEventExt)
{
return new CorrespondenceStatusEntity
{
Status = (CorrespondenceStatus)statusEventExt.Status,
StatusChanged = statusEventExt.StatusChanged,
StatusText = statusEventExt.StatusText ?? statusEventExt.Status.ToString(),
PartyUuid = statusEventExt.EventUserPartyUuid
};
}
private static CorrespondenceDeleteEventEntity MapCorrespondenceStatusEventToDeleteEvent(MigrateCorrespondenceStatusEventExt statusEventExt)
{
return new CorrespondenceDeleteEventEntity
{
EventType = MapMigrateCorrespondenceStatusExtToDeleteEventType(statusEventExt.Status),
EventOccurred = statusEventExt.StatusChanged,
PartyUuid = statusEventExt.EventUserPartyUuid
};
}
private static CorrespondenceForwardingEventEntity MapForwardingEventToInternal(MigrateCorrespondenceForwardingEventExt forwardingEvent)
{
return new CorrespondenceForwardingEventEntity()
{
ForwardedOnDate = forwardingEvent.ForwardedOnDate,
ForwardedByPartyUuid = forwardingEvent.ForwardedByPartyUuid,
ForwardedByUserId = forwardingEvent.ForwardedByUserId,
ForwardedByUserUuid = forwardingEvent.ForwardedByUserUuid,
ForwardedToUserId = forwardingEvent.ForwardedToUserId,
ForwardedToUserUuid = forwardingEvent.ForwardedToUserUuid,
ForwardingText = forwardingEvent.ForwardingText,
ForwardedToEmailAddress = forwardingEvent.ForwardedToEmail,
MailboxSupplier = forwardingEvent.MailboxSupplier
};
}
private static List<AttachmentMigrationStatusExt>? MapAttachmentMigrationStatusToExternal(List<AttachmentMigrationStatus>? attachmentMigrationStatuses)
{
if (attachmentMigrationStatuses == null)
return null;
return attachmentMigrationStatuses.Select(MapAttachmentMigrationStatusToExternal).ToList();
}
private static AttachmentMigrationStatusExt MapAttachmentMigrationStatusToExternal(AttachmentMigrationStatus attachmentMigrationStatus)
{
return new AttachmentMigrationStatusExt()
{
AttachmentId = attachmentMigrationStatus.AttachmentId,
Status = (AttachmentStatusExt)attachmentMigrationStatus.AttachmentStatus
};
}
private static CorrespondenceNotificationEntity MapNotificationToInternal(MigrateCorrespondenceNotificationExt notificationExt)
{
return new CorrespondenceNotificationEntity()
{
Created = notificationExt.NotificationSent,
NotificationSent = notificationExt.NotificationSent,
NotificationChannel = (NotificationChannel)notificationExt.NotificationChannel,
NotificationTemplate = NotificationTemplate.Altinn2Message,
NotificationAddress = notificationExt.NotificationAddress,
Altinn2NotificationId = notificationExt.Altinn2NotificationId,
IsReminder = notificationExt.IsReminder,
};
}
}