public async Task UpdateContract(UpdateContractDTO dto) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } //Must be the assigned CA & correct stage to use this func var order = await DbContext.OnlineOrder .Where(o => o.OrderId == dto.Id) .Include(o => o.AssignUser) .Include(o => o.Stage) .SingleOrDefaultAsync() ?? throw new BussinessException($"Order: {dto.Id} is not exist");; if (order.AssignUser.UserId != UserId) { throw new BussinessException($"Order: {dto.Id} is not assigned to this user"); } //Maybe allow updating contract at multiple stages in the future? if (order.StageId != (int)StageEnum.EnterContractNumber) { throw new BussinessException($"Order: {dto.Id} current stage: {order.Stage.Stage} is not updatable"); } //Check contract already related to any order if (await DbContext.OnlineOrder.AnyAsync(o => o.Induscontract == dto.Contract)) { throw new BussinessException($"Contract: {dto.Contract} is already related to another order"); } // if (!await DbContext.FollowingContracts.AnyAsync(f => f.ContractNumber == dto.Contract)) // { // } //Check INDUS contract exists //Check INDUS contract NatId == case NatId if (!_options.NoNatIdCheck) { var indusContract = await _indus.GetContract(dto.Contract) ?? throw new BussinessException($"Contract: {dto.Contract} is not exist"); if (string.Compare(indusContract.NatId, order.NatId, true) != 0) { throw new BussinessException($"Contract: {dto.Contract} customer nat id is not the same as order"); } } //Proceed //Update order's contract number & stage order.Induscontract = dto.Contract; order.StageId = (int)StageEnum.WaitForFinalStatus; await DbContext.FollowingContracts.AddAsync(new FollowingContracts() { StartDate = DateTime.Now, ContractNumber = dto.Contract }); await DbContext.SaveChangesAsync(); }
public async Task <IActionResult> UpdateContract([FromBody] UpdateContractDTO dto) { if (!ModelState.IsValid) { return(BadRequest()); } try { await _service.UpdateContract(dto); return(Ok()); } catch (BussinessException ex) { _logger.LogDebug(ex.Message); return(BadRequest(ex.Message)); } }