public async Task <int> CreateScanRequest(IRepairRequestScanDto scanRequest) { try { // Check Open Scan Requests. if (Db.ScanRequests.Any( r => r.Repair.OrderId == scanRequest.OrderId && r.Report == null)) { throw new Exception("Open Scan Request Found."); } // Load Repair. var repair = Db.RepairOrders.Find(scanRequest.OrderId); if (repair == null) { throw new NullReferenceException("Unable to Locate Repair."); } if (repair.Status != RepairStatuses.Active) { throw new NullReferenceException("Unable to Create Request for a Closed Repair."); } Guid.TryParse(scanRequest.ContactUserGuid, out var contactUserId); var isUser = Db.Users.Any(x => x.Id == contactUserId); // Create New Request. var request = new RequestEntityModel { OrderId = repair.OrderId, ProblemDescription = scanRequest.ProblemDescription, OtherWarningInfo = scanRequest.OtherWarningInfo, Notes = scanRequest.Notes, ContactUserGuid = contactUserId == Guid.Empty || !isUser ? null : new Guid?(contactUserId), ShopContactGuid = contactUserId == Guid.Empty || isUser ? null : new Guid?(contactUserId), SeatRemovedInd = scanRequest.SeatRemovedInd, CreatedBy = User, CreatedDt = DateTimeOffset.UtcNow, RequestTypeId = scanRequest.RequestTypeID, RequestCategoryId = scanRequest.RequestTypeCategoryId == 0 ? null : scanRequest.RequestTypeCategoryId, }; if (!string.IsNullOrWhiteSpace(scanRequest.ContactOtherFirstName) && !string.IsNullOrWhiteSpace(scanRequest.ContactOtherLastName) && !string.IsNullOrWhiteSpace(scanRequest.ContactOtherPhone)) { ShopContactEntityModel shopContact = new ShopContactEntityModel { ShopGuid = repair.ShopGuid, FirstName = scanRequest.ContactOtherFirstName, LastName = scanRequest.ContactOtherLastName, PhoneNumber = scanRequest.ContactOtherPhone }; request.ShopContact = shopContact; } // Set Tool. if (scanRequest.ToolId > 0) { request.ToolId = scanRequest.ToolId; } // Add Request. Db.ScanRequests.Add(request); // Add Warning Indicators. if (scanRequest.WarningIndicators != null) { foreach (int i in scanRequest.WarningIndicators) { var indicator = Db.ScanWarningIndicators.Find(i); if (indicator != null) { var requestIndicator = new RequestWarningIndicatorEntityModel { Request = request, WarningIndicator = indicator }; Db.ScanRequestWarningIndicators.Add(requestIndicator); } } } // Update DB. await Db.SaveChangesAsync(); // Notify Technicians (If NOT Self Scan). if (request.RequestTypeId == 6) { return(request.RequestId); } using (var queue = new MessageQueue()) { // Sent Email. var sendMessage = queue .AddNotificationQueueMessageAsync(NotificationTemplate.ScanRequestEmail, request.RequestId, User.Id) .ConfigureAwait(continueOnCapturedContext: false); // Complete. await sendMessage; } return(request.RequestId); } catch (Exception ex) { Logger.LogException(ex, scanRequest); throw; } }
private DateTime?MapUpload(RequestEntityModel arg) { var lastScan = arg.ScanUploads.FirstOrDefault(); return(lastScan?.CreatedDt.UtcDateTime); }