public async Task Handle(ReviewUpdateNotification notification, CancellationToken cancellationToken) { var requestfromDb = await _requestRepository.FindAsync(x => x.Id == notification.Request.Id); INotification new_notification = null; if (requestfromDb.Reviews.Any(x => x.IsApproved == false)) { requestfromDb.State = VacationState.Rejected; new_notification = new RequestRejectedNotification { Request = requestfromDb }; } else if (requestfromDb.Reviews.All(x => x.IsApproved == true)) { requestfromDb.State = VacationState.Approved; new_notification = new RequestApprovedNotification { Request = requestfromDb }; await _mediator.Publish(new StatisticAddingHandler { Request = requestfromDb }); } else if (requestfromDb.Reviews.Any(x => x.IsApproved == null)) { requestfromDb.State = VacationState.InProgress; new_notification = new RequestUpdatedNotification { Request = requestfromDb }; } await _requestRepository.UpdateAsync(requestfromDb); await _mediator.Publish(new_notification); }
public async Task UpdateAsync(int requestId, Request newModel) { var requestFromDb = await _repository.FindAsync(r => r.Id == requestId); if (requestFromDb == null) { throw new RequestNotFoundException($"Request not found: RequestId={requestId}", 409); } if (newModel.UserId != requestFromDb.UserId) { throw new ConflictException($"Current user is not the author of the request (userId: {newModel.UserId}, requestId: {requestId})", 409); } bool needToNotify = false; using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled)) { switch (requestFromDb.State) { case VacationState.New: //newModel.ParentRequestId = requestFromDb.Id; if (await IsRequestCorrect(newModel)) { newModel.ParentRequestId = null; newModel.State = VacationState.New; FillReviewers(newModel); needToNotify = true; } break; case VacationState.InProgress: needToNotify = await ChangeAsync(requestFromDb, newModel); break; case VacationState.Approved: await Duplicate(requestFromDb, newModel); break; case VacationState.Rejected: throw new StateException("It is forbidden to change the rejected request", 409); default: throw new StateException("Request status is not allowed or does not exist", 409); } await _repository.UpdateAsync(requestFromDb); transactionScope.Complete(); } if (needToNotify) { var notification = new RequestUpdatedNotification { Request = requestFromDb }; await _mediator.Publish(notification); } }