/
Program.cs
285 lines (244 loc) · 9.56 KB
/
Program.cs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System.Text.Json.Serialization;
using Helpdesk.Api.Core.Http;
using Helpdesk.Api.Core.Kafka;
using Helpdesk.Api.Core.Marten;
using Helpdesk.Api.Core.SignalR;
using Helpdesk.Api.Incidents;
using Helpdesk.Api.Incidents.GetCustomerIncidentsSummary;
using Helpdesk.Api.Incidents.GetIncidentDetails;
using Helpdesk.Api.Incidents.GetIncidentHistory;
using Helpdesk.Api.Incidents.GetIncidentShortInfo;
using JasperFx.CodeGeneration;
using Marten;
using Marten.AspNetCore;
using Marten.Events.Daemon.Resiliency;
using Marten.Events.Projections;
using Marten.Pagination;
using Marten.Schema.Identity;
using Marten.Services.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Oakton;
using Weasel.Core;
using static Microsoft.AspNetCore.Http.TypedResults;
using static Helpdesk.Api.Incidents.IncidentService;
using static Helpdesk.Api.Core.Http.ETagExtensions;
using static System.DateTimeOffset;
using JsonOptions = Microsoft.AspNetCore.Http.Json.JsonOptions;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddEndpointsApiExplorer()
.AddSwaggerGen()
.AddMarten(sp =>
{
var options = new StoreOptions();
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "Helpdesk";
options.Events.DatabaseSchemaName = schemaName;
options.DatabaseSchemaName = schemaName;
options.Connection(builder.Configuration.GetConnectionString("Incidents") ??
throw new InvalidOperationException());
options.UseSystemTextJsonForSerialization(EnumStorage.AsString);
options.Projections.Errors.SkipApplyErrors = false;
options.Projections.Errors.SkipSerializationErrors = false;
options.Projections.Errors.SkipUnknownEvents = false;
options.Projections.LiveStreamAggregation<Incident>();
options.Projections.Add<IncidentHistoryTransformation>(ProjectionLifecycle.Inline);
options.Projections.Add<IncidentDetailsProjection>(ProjectionLifecycle.Inline);
options.Projections.Add<IncidentShortInfoProjection>(ProjectionLifecycle.Inline);
options.Projections.Add<CustomerIncidentsSummaryProjection>(ProjectionLifecycle.Async);
options.Projections.Add(new KafkaProducer(builder.Configuration), ProjectionLifecycle.Async);
options.Projections.Add(
new SignalRProducer((IHubContext)sp.GetRequiredService<IHubContext<IncidentsHub>>()),
ProjectionLifecycle.Async
);
return options;
})
.OptimizeArtifactWorkflow(TypeLoadMode.Static)
.UseLightweightSessions()
.AddAsyncDaemon(DaemonMode.Solo);
builder.Services
.AddCors(options =>
options.AddPolicy("ClientPermission", policy =>
policy.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
)
)
.Configure<JsonOptions>(o => o.SerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o =>
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()))
.AddSignalR();
builder.Host.ApplyOaktonExtensions();
var app = builder.Build();
var customersIncidents = app.MapGroup("api/customers/{customerId:guid}/incidents/").WithTags("Customer", "Incident");
var agentIncidents = app.MapGroup("api/agents/{agentId:guid}/incidents/").WithTags("Agent", "Incident");
var incidents = app.MapGroup("api/incidents").WithTags("Incident");
customersIncidents.MapPost("",
async (
IDocumentSession documentSession,
Guid customerId,
LogIncidentRequest body,
CancellationToken ct) =>
{
var (contact, description) = body;
var incidentId = CombGuidIdGeneration.NewGuid();
await documentSession.Add<Incident>(incidentId,
Handle(new LogIncident(incidentId, customerId, contact, description, customerId, Now)), ct);
return Created($"/api/incidents/{incidentId}", incidentId);
}
);
agentIncidents.MapPost("{incidentId:guid}/category",
(
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
CategoriseIncidentRequest body,
CancellationToken ct
) =>
documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new CategoriseIncident(incidentId, body.Category, agentId, Now)), ct)
);
agentIncidents.MapPost("{incidentId:guid}/priority",
(
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
PrioritiseIncidentRequest body,
CancellationToken ct
) =>
documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new PrioritiseIncident(incidentId, body.Priority, agentId, Now)), ct)
);
agentIncidents.MapPost("{incidentId:guid}/assign",
(
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
CancellationToken ct
) =>
documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new AssignAgentToIncident(incidentId, agentId, Now)), ct)
);
customersIncidents.MapPost("{incidentId:guid}/responses/",
(
IDocumentSession documentSession,
Guid incidentId,
Guid customerId,
[FromIfMatchHeader] string eTag,
RecordCustomerResponseToIncidentRequest body,
CancellationToken ct
) =>
documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state,
new RecordCustomerResponseToIncident(incidentId,
new IncidentResponse.FromCustomer(customerId, body.Content), Now)), ct)
);
agentIncidents.MapPost("{incidentId:guid}/responses/",
(
IDocumentSession documentSession,
[FromIfMatchHeader] string eTag,
Guid incidentId,
Guid agentId,
RecordAgentResponseToIncidentRequest body,
CancellationToken ct
) =>
{
var (content, visibleToCustomer) = body;
return documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state,
new RecordAgentResponseToIncident(incidentId,
new IncidentResponse.FromAgent(agentId, content, visibleToCustomer), Now)), ct);
}
);
agentIncidents.MapPost("{incidentId:guid}/resolve",
(
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
ResolveIncidentRequest body,
CancellationToken ct
) =>
documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new ResolveIncident(incidentId, body.Resolution, agentId, Now)), ct)
);
customersIncidents.MapPost("{incidentId:guid}/acknowledge",
(
IDocumentSession documentSession,
Guid incidentId,
Guid customerId,
[FromIfMatchHeader] string eTag,
CancellationToken ct
) =>
documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new AcknowledgeResolution(incidentId, customerId, Now)), ct)
);
agentIncidents.MapPost("{incidentId:guid}/close",
async (
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
CancellationToken ct) =>
{
await documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new CloseIncident(incidentId, agentId, Now)), ct);
return Ok();
}
);
customersIncidents.MapGet("",
(IQuerySession querySession, Guid customerId, [FromQuery] int? pageNumber, [FromQuery] int? pageSize,
CancellationToken ct) =>
querySession.Query<IncidentShortInfo>().Where(i => i.CustomerId == customerId)
.ToPagedListAsync(pageNumber ?? 1, pageSize ?? 10, ct)
);
incidents.MapGet("{incidentId:guid}",
(HttpContext context, IQuerySession querySession, Guid incidentId) =>
querySession.Json.WriteById<IncidentDetails>(incidentId, context)
);
incidents.MapGet("{incidentId:guid}/history",
(HttpContext context, IQuerySession querySession, Guid incidentId) =>
querySession.Query<IncidentHistory>().Where(i => i.IncidentId == incidentId).WriteArray(context)
);
customersIncidents.MapGet("incidents-summary",
(HttpContext context, IQuerySession querySession, Guid customerId) =>
querySession.Json.WriteById<CustomerIncidentsSummary>(customerId, context)
);
if (app.Environment.IsDevelopment())
{
app.UseSwagger()
.UseSwaggerUI();
}
app.UseCors("ClientPermission");
app.MapHub<IncidentsHub>("/hubs/incidents");
return await app.RunOaktonCommands(args);
public class IncidentsHub: Hub
{
}
public record LogIncidentRequest(
Contact Contact,
string Description
);
public record CategoriseIncidentRequest(
IncidentCategory Category
);
public record PrioritiseIncidentRequest(
IncidentPriority Priority
);
public record RecordCustomerResponseToIncidentRequest(
string Content
);
public record RecordAgentResponseToIncidentRequest(
string Content,
bool VisibleToCustomer
);
public record ResolveIncidentRequest(
ResolutionType Resolution
);
public partial class Program
{
}