-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreate.razor.cs
More file actions
519 lines (437 loc) · 17.5 KB
/
Create.razor.cs
File metadata and controls
519 lines (437 loc) · 17.5 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using QRCoder;
using Serilog;
namespace MESS.Blazor.Components.Pages.ProductionLog;
using Data.Models;
internal enum Status
{
NotStarted,
InProgress,
Completed
}
/// <summary>
/// Represents the Create component for managing production logs.
/// This component handles the creation, modification, and submission of production logs.
/// </summary>
public partial class Create : ComponentBase, IAsyncDisposable
{
private const string Title = "Production Log";
private bool IsLoading { get; set; } = true;
private ConfirmationModal? popupRef;
private bool IsWorkflowActive { get; set; }
private Status WorkInstructionStatus { get; set; } = Status.NotStarted;
private bool IsSaved { get; set; }
private Product? ActiveProduct { get; set; }
private WorkInstruction? ActiveWorkInstruction { get; set; }
/// <summary>
/// Represents the current production log being created or modified.
/// This object holds all the details of the production log.
/// </summary>
protected ProductionLog ProductionLog = new();
private List<Product>? Products { get; set; }
private List<WorkInstruction>? WorkInstructions { get; set; }
private List<WorkInstruction>? ActiveProductWorkInstructionList { get; set; }
private string? ActiveLineOperator { get; set; }
private string? ProductSerialNumber { get; set; }
private string? QRCodeDataUrl;
private IJSObjectReference? module;
private List<ProductionLogPart> ProductionLogParts { get; set; } = [];
private Func<ProductionLog, Task>? _autoSaveHandler;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
IsLoading = true;
ProductionLogEventService.DisableAutoSave();
await LoadProducts();
await GetInProgressAsync();
var cachedForm = await LoadCachedForm();
if (cachedForm)
{
ProductionLogEventService.EnableAutoSave();
await InvokeAsync(StateHasChanged);
}
await ProductionLogEventService.SetCurrentProductionLog(ProductionLog);
// AutoSave Trigger
_autoSaveHandler = async log =>
{
await LocalCacheManager.SetNewProductionLogFormAsync(log);
await InvokeAsync((() =>
{
IsSaved = true;
StateHasChanged();
}));
};
var result = await AuthProvider.GetAuthenticationStateAsync();
ActiveLineOperator = result.User.Identity?.Name;
// This must come before the LoadCachedForm method since if it finds a cached form, it will set the status to InProgress
WorkInstructionStatus = Status.NotStarted;
ProductionLogEventService.AutoSaveTriggered += _autoSaveHandler;
ProductSerialNumber = SerializationService.CurrentProductNumber;
SerializationService.CurrentProductionLogPartChanged += HandleProductionLogPartsChanged;
SerializationService.CurrentProductNumberChanged += HandleProductNumberChanged;
ProductionLogParts = SerializationService.CurrentProductionLogParts;
IsLoading = false;
}
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
module = await JSRuntime.InvokeAsync<IJSObjectReference>("import",
"./Components/Pages/ProductionLog/Create.razor.js");
}
}
private async Task<bool> LoadCachedForm()
{
var cachedFormData = await LocalCacheManager.GetProductionLogFormAsync();
if (cachedFormData != null && cachedFormData.LogSteps.Count != 0)
{
WorkInstructionStatus = Status.InProgress;
ProductionLog = new ProductionLog
{
Id = cachedFormData.ProductionLogId,
LogSteps = cachedFormData.LogSteps.Select(step => new ProductionLogStep
{
WorkInstructionStepId = step.WorkInstructionStepId,
ProductionLogId = step.ProductionLogId,
Attempts = step.Attempts.Select(a => new ProductionLogStepAttempt
{
Success = a.Success,
Notes = a.Notes ?? "",
SubmitTime = a.SubmitTime
}).ToList()
}).ToList()
};
}
else
{
return false;
}
if (ProductionLog.LogSteps.All(step =>
step.Attempts.Any(a => a.SubmitTime != DateTimeOffset.MinValue)))
{
WorkInstructionStatus = Status.Completed;
}
return true;
}
private async Task SetActiveWorkInstruction(int workInstructionId)
{
if (workInstructionId <= 0)
{
ActiveWorkInstruction = null;
await SetSelectedWorkInstructionId(null);
ProductionLogEventService.SetCurrentWorkInstructionName(string.Empty);
return;
}
if (ActiveProductWorkInstructionList != null)
{
var workInstruction = await WorkInstructionService.GetByIdAsync(workInstructionId);
if (workInstruction?.Products == null)
return;
// Reset the cached log and internal state
await LocalCacheManager.SetNewProductionLogFormAsync(null);
ProductionLog = new ProductionLog(); // clear current log
await ProductionLogEventService.SetCurrentProductionLog(ProductionLog);
// Proceed with setting new state
await SetSelectedWorkInstructionId(workInstructionId);
ActiveWorkInstruction = workInstruction;
ProductionLogEventService.SetCurrentWorkInstructionName(workInstruction.Title);
await LocalCacheManager.SetActiveWorkInstructionIdAsync(workInstruction.Id);
}
}
private async Task SetActiveProduct(int productId)
{
if (Products == null)
return;
if (productId < 0)
{
ActiveWorkInstruction = null;
ActiveProductWorkInstructionList = null;
await SetActiveWorkInstruction(-1);
await LocalCacheManager.SetActiveProductAsync(null);
return;
}
var product = Products.FirstOrDefault(p => p.Id == productId);
if (product?.WorkInstructions == null)
return;
// Reset the cached log and internal state
await LocalCacheManager.SetNewProductionLogFormAsync(null);
ProductionLog = new ProductionLog(); // clear current log
await ProductionLogEventService.SetCurrentProductionLog(ProductionLog);
// Proceed with setting new state
ActiveProduct = product;
ActiveProductWorkInstructionList = product.WorkInstructions.Where(w => w.IsActive).ToList();
ProductionLogEventService.SetCurrentProductName(product.Name);
await SetActiveWorkInstruction(-1);
await LocalCacheManager.SetActiveProductAsync(product);
}
private async Task GetCachedActiveProductAsync()
{
var result = await LocalCacheManager.GetActiveProductAsync();
ActiveProduct = Products?.FirstOrDefault(p => p.Name == result.Name);
if (ActiveProduct == null)
{
return;
}
ActiveProductWorkInstructionList = ActiveProduct.WorkInstructions?
.Where(w => w.IsActive)
.ToList() ?? new List<WorkInstruction>();
ProductionLogEventService.SetCurrentProductName(ActiveProduct.Name);
}
/// Sets the local storage variable
private async Task SetInProgressAsync(bool isActive)
{
await LocalCacheManager.SetIsWorkflowActiveAsync(isActive);
IsWorkflowActive = isActive;
}
private async Task GetInProgressAsync()
{
var result = await LocalCacheManager.GetWorkflowActiveStatusAsync();
// If the result is true, then the operator was previously in the middle of a workflow
if (result)
{
IsWorkflowActive = result;
await GetCachedActiveWorkInstructionAsync();
await GetCachedActiveProductAsync();
return;
}
await SetInProgressAsync(false);
}
private async Task GetCachedActiveWorkInstructionAsync()
{
var result = await LocalCacheManager.GetActiveWorkInstructionIdAsync();
await LoadActiveWorkInstruction(result);
}
private async Task LoadProducts()
{
try
{
var productsAsync = await ProductService.GetAllProductsAsync();
Products = productsAsync.ToList();
}
catch (Exception e)
{
Log.Error("Error loading products for the Create view: {Message}", e.Message);
}
}
private async Task SetSelectedWorkInstructionId(int? value)
{
if (value.HasValue)
{
await SetInProgressAsync(true);
}
await LocalCacheManager.SetActiveWorkInstructionIdAsync(value ?? -1);
}
private async Task LoadActiveWorkInstruction(int id)
{
ActiveWorkInstruction = await WorkInstructionService.GetByIdAsync(id);
if (ActiveWorkInstruction == null)
{
return;
}
ProductionLogEventService.SetCurrentWorkInstructionName(ActiveWorkInstruction.Title);
}
/// <summary>
/// Handles the submission of the production log.
/// Validates if all required parts have serial numbers before proceeding.
/// If parts are missing serial numbers, prompts the user for confirmation.
/// </summary>
/// <remarks>
/// - If `ActiveWorkInstruction` is null, the method exits early.
/// - Calculates the total number of parts needed based on the active work instruction.
/// - Compares the count of serial numbers logged with the total parts needed.
/// - If all parts have serial numbers, proceeds to complete the submission.
/// - Otherwise, displays a confirmation modal to the user.
/// </remarks>
/// <returns>An asynchronous operation.</returns>
protected async Task HandleSubmit()
{
if (ActiveWorkInstruction == null)
{
return;
}
var partNodes = ActiveWorkInstruction.Nodes.Where(node => node.NodeType == WorkInstructionNodeType.Part);
int totalPartsNeeded = 0;
foreach (var node in partNodes)
{
// Cast to PartNode to access its Parts collection
if (node is PartNode partNode)
{
totalPartsNeeded += partNode.Parts.Count;
}
}
bool allStepsHavePartsNeeded = ProductionLogParts.Count >= totalPartsNeeded;
if (!allStepsHavePartsNeeded)
{
popupRef?.Show("There are parts without serial numbers. Are you sure you want to submit this log?");
}
else
{
await CompleteSubmit();
}
}
private async Task HandleConfirmation(bool confirmed)
{
if (confirmed)
{
await CompleteSubmit();
}
}
private async Task CompleteSubmit()
{
var currentTime = DateTimeOffset.UtcNow;
var authState = await AuthProvider.GetAuthenticationStateAsync();
var userId = authState.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
// Update log
ProductionLog.CreatedOn = currentTime;
ProductionLog.LastModifiedOn = currentTime;
ProductionLog.WorkInstruction = ActiveWorkInstruction;
ProductionLog.Product = ActiveProduct;
ProductionLog.OperatorId = userId;
// If no log is created, it gets created now to utilize the id for the QR code
if (ProductionLog.Id <= 0)
{
var id = await ProductionLogService.CreateAsync(ProductionLog);
ProductionLog.Id = id;
}
else
{
await ProductionLogService.UpdateAsync(ProductionLog);
}
if (ActiveWorkInstruction is { ShouldGenerateQrCode: true })
{
await PrintQRCode();
}
ToastService.ShowSuccess("Successfully Created Production Log", 3000);
// Create any associated SerialNumberLogs
await SerializationService.SaveCurrentProductionLogPartsAsync(ProductionLog.Id);
// Reset the local storage values
await LocalCacheManager.SetNewProductionLogFormAsync(null);
// Add the new log to the session
await SessionManager.AddProductionLogAsync(ProductionLog.Id);
await ResetFormState();
}
private void HandleProductionLogPartsChanged()
{
ProductionLogParts = SerializationService.CurrentProductionLogParts;
InvokeAsync(StateHasChanged);
}
private void HandleProductNumberChanged()
{
ProductSerialNumber = SerializationService.CurrentProductNumber;
InvokeAsync(StateHasChanged);
}
private void GenerateQRCode()
{
var productionLogId = ProductionLogEventService.GetCurrentProductionLog()?.Id;
var productionLogIdString = productionLogId + ",";
using var qrGenerator = new QRCodeGenerator();
using var qrCodeData = qrGenerator.CreateQrCode(productionLogIdString, QRCodeGenerator.ECCLevel.Q);
using var qrCode = new BitmapByteQRCode(qrCodeData);
var qrCodeImageArr = qrCode.GetGraphic(20);
QRCodeDataUrl = $"data:image/png;base64,{Convert.ToBase64String(qrCodeImageArr)}";
}
private async Task PrintQRCode()
{
GenerateQRCode();
if (string.IsNullOrEmpty(QRCodeDataUrl))
return;
if (module == null)
{
return;
}
await module.InvokeVoidAsync("printQRCode", QRCodeDataUrl);
}
private async Task ResetFormState()
{
ProductionLog = new ProductionLog();
await ProductionLogEventService.SetCurrentProductionLog(ProductionLog);
ProductionLogEventService.EnableAutoSave();
WorkInstructionStatus = Status.NotStarted;
}
private async Task OnStepCompleted(ProductionLogStep step, bool? success)
{
if (ActiveWorkInstruction == null)
return;
await ProductionLogEventService.SetCurrentProductionLog(ProductionLog);
var currentStatus = await GetWorkInstructionStatus();
WorkInstructionStatus = currentStatus ? Status.Completed : Status.InProgress;
// Find the current node that corresponds to this step
var currentNode = ActiveWorkInstruction.Nodes.FirstOrDefault(n => n.Id == step.WorkInstructionStepId);
if (currentNode != null)
{
// Sort nodes by Position to maintain the correct sequence
var orderedNodes = ActiveWorkInstruction.Nodes
.OrderBy(n => n.Position)
.ToList();
var currentIndex = orderedNodes.FindIndex(n => n.Id == currentNode.Id);
if (currentIndex >= 0 && currentIndex < orderedNodes.Count - 1)
{
var nextStep = orderedNodes[currentIndex + 1];
// If a step completed successfully, scroll to the next work instruction node
if (success == true)
{
string elementId = $"step-{nextStep.Position}";
if (module != null)
{
await module.InvokeVoidAsync("scrollToStep", elementId);
}
}
}
// Scroll to the submit button if it's the last step and it was successful
if (currentIndex == orderedNodes.Count - 1 && success == true)
{
if (module != null)
{
await module.InvokeVoidAsync("scrollToStep", "submit-button");
}
}
}
}
/// <summary>
/// Determines if the operator has completed all steps in the work instruction
/// </summary>
/// <returns>Returns True if each step in the work instruction has been completed. False otherwise.</returns>
private async Task<bool> GetWorkInstructionStatus()
{
try
{
var result = false;
await Task.Run(() =>
{
result = ProductionLog.LogSteps.All(step =>
step.Attempts.Any(a => a.SubmitTime != DateTimeOffset.MinValue));
});
return result;
}
catch (Exception e)
{
Log.Error("Error checking work instruction status: {Message}", e.Message);
return false;
}
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
SerializationService.CurrentProductionLogPartChanged -= HandleProductionLogPartsChanged;
SerializationService.CurrentProductNumberChanged -= HandleProductNumberChanged;
ProductionLogEventService.AutoSaveTriggered -= _autoSaveHandler;
try
{
if (module is not null)
{
await module.DisposeAsync();
}
}
catch (JSDisconnectedException)
{
// Deliberately not acting on the JSDisconnectedException since it is the preferred
// way to handle disposed JS scripts without logging: https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-9.0
}
catch (JSException jsException)
{
Log.Warning("JS Interop Exception thrown, {Message}", jsException.Message);
}
}
}