/// <summary> /// Removes the load from loadshop, if removed from booked tab, a rating question is prompted and the answer will be recorded /// </summary> /// <param name="loadId"></param> /// <param name="username"></param> /// <param name="ratingQuestionAnswer"></param> /// <returns></returns> public async Task <ShippingLoadData> DeleteLoad(Guid loadId, string username, RatingQuestionAnswerData ratingQuestionAnswer = null) { _securityService.GuardAction(SecurityActions.Loadshop_Ui_Shopit_Load_Manual_Delete); var updateLoad = _context.Loads .Include(x => x.LoadTransactions) .Include(x => x.CarrierScacs) .Include(x => x.PostedLoadCarrierGroups) .SingleOrDefault(x => x.LoadId == loadId); if (updateLoad == null) { throw new Exception("Load not found"); } if (!AuthorizedForCustomer(updateLoad.CustomerId)) { throw new UnauthorizedAccessException($"User is not authorized for customer: {updateLoad.CustomerId}"); } AddLoadTransaction(updateLoad, TransactionTypes.PendingRemove); /** * https://kbxltrans.visualstudio.com/Suite/_workitems/edit/38578 * Delete all LoadCarrierScacs associated with this Load */ _context.LoadCarrierScacs.RemoveRange(updateLoad.CarrierScacs); _context.PostedLoadCarrierGroups.RemoveRange(updateLoad.PostedLoadCarrierGroups); if (ratingQuestionAnswer != null) { // the load was removed from a booked tab var loadClaim = GetLatestLoadClaim(updateLoad.LoadTransactions); ratingQuestionAnswer.LoadClaimId = loadClaim.LoadClaimId; ratingQuestionAnswer.LoadId = loadId; await _ratingService.AddRatingQuestionAnswer(ratingQuestionAnswer); } await _context.SaveChangesAsync(username); return(_mapper.Map <ShippingLoadData>(updateLoad)); }