public async Task <Result <LockerDTO> > Handle(OpenLockerCommand request, CancellationToken cancellationToken)
        {
            var executor   = request.Executor;
            var commission = _commissionRepo.FindCommissionByParcelCode(request.ParcelCode);

            if (commission == null)
            {
                return(new Result <LockerDTO>(ErrorType.NotFound, "No commission with parcel of given parcel code"));
            }

            if (!commission.SourceParcelLocker.IsClose(executor.CurrentLocation, CLOSE_DISTANCE))
            {
                return(new Result <LockerDTO>(ErrorType.NotFound, "Not in the proximity of parcel locker"));
            }

            var locker = commission.SourceParcelLocker.GetLockerByParcelCode(request.ParcelCode);

            if (locker == null)
            {
                return(new Result <LockerDTO>(ErrorType.NotFound, "This parcel locker does not have the parcel"));
            }

            if (executor.GetType() == typeof(Commissioner) && locker.CanBeOpenedBy <Commissioner>(request.ParcelCode))
            {
                locker.Open();
                locker.LoadLocker(commission.Parcel);
                commission.DeliveryStatus = DeliveryStatus.SubmittedByCommissioner;
            }
            else if (executor.GetType() == typeof(Deliverer) && locker.CanBeOpenedBy <Deliverer>(request.ParcelCode))
            {
                locker.Open();
                locker.EmptyLocker();
                commission.DeliveryStatus = DeliveryStatus.Traveling;
            }
            else if (executor.GetType() == typeof(Recipient) && locker.CanBeOpenedBy <Recipient>(request.ParcelCode))
            {
                locker.Open();
                locker.EmptyLocker();
                commission.CommissionStatus = Domain.Statuses.CommissionStatus.Finished;
            }

            await _commissionRepo.Update(commission);

            return(new Result <LockerDTO>().SetOutput(_mapper.Map <LockerDTO>(locker)));
        }
Exemplo n.º 2
0
        public async Task <Result <CommissionDTO> > Handle(FinishCreatingCommissionCommand request, CancellationToken cancellationToken)
        {
            var cmdLocker     = request.TargetParcelLocker;
            var cmdCommission = request.Commission;

            var dbParcelLocker = await _parcelLockerRepo.FindById(cmdLocker.Id);

            var dbCommission = await _commissionRepo.FindById(cmdCommission.Id);


            if (dbParcelLocker == null || dbCommission == null)
            {
                var resultx = new Result <CommissionDTO>(ErrorType.NotFound);
                return(resultx);
            }

            if (cmdCommission.Parcel.CommissionerCode != dbCommission.Parcel.CommissionerCode)
            {
                return(new Result <CommissionDTO>(ErrorType.WrongArguments, "Parcel Numbers do not match"));
            }

            if (!dbParcelLocker.ContainsLockerOfSize(dbCommission.Parcel.Size))
            {
                var resultx = new Result <CommissionDTO>(ErrorType.NotValid, "There is no locker of given size");
                return(resultx);
            }

            dbCommission.SourceParcelLocker = dbParcelLocker;
            dbCommission.CommissionStatus   = Domain.Statuses.CommissionStatus.Executing;
            await _commissionRepo.Update(request.Commission);

            var commissionDTO = _mapper.Map <CommissionDTO>(dbCommission);
            var result        = new Result <CommissionDTO>();

            result.SetOutput(commissionDTO);

            return(result);
        }