Ejemplo n.º 1
0
        public async Task <Result <Guid> > Handle(CreateAgreementCommand request, CancellationToken token)
        {
            var store = await _context.Stores.SingleAsync(e => e.Id == request.StoreId, token);

            var producer = await _context.Producers.SingleAsync(e => e.Id == request.ProducerId, token);

            var currentUser = await _context.Users.SingleAsync(e => e.Id == request.RequestUser.Id, token);

            Domain.DeliveryMode delivery = null;
            if (request.DeliveryId.HasValue)
            {
                delivery = await _context.DeliveryModes.SingleAsync(e => e.Id == request.DeliveryId, token);
            }

            var entity = new Domain.Agreement(Guid.NewGuid(), store, producer, currentUser.Kind, delivery);

            if (request.CatalogId.HasValue)
            {
                var catalog = await _context.Catalogs.SingleAsync(e => e.Id == request.CatalogId.Value, token);

                entity.ChangeCatalog(catalog);
            }

            await _context.AddAsync(entity, token);

            await _context.SaveChangesAsync(token);

            return(Success(entity.Id));
        }
Ejemplo n.º 2
0
        public async Task <Result <Guid> > Handle(CreateDeliveryModeCommand request, CancellationToken token)
        {
            DeliveryAddress deliveryModeAddress = null;

            if (request.Address != null)
            {
                deliveryModeAddress = new DeliveryAddress(request.Address.Line1, request.Address.Line2,
                                                          request.Address.Zipcode, request.Address.City, request.Address.Country, request.Address.Longitude,
                                                          request.Address.Latitude);
            }

            var openingHours = new List <DeliveryHours>();

            if (request.DeliveryHours != null)
            {
                foreach (var oh in request.DeliveryHours)
                {
                    openingHours.AddRange(oh.Days.Select(c => new DeliveryHours(c, oh.From, oh.To)));
                }
            }

            var producer = await _context.Producers.SingleAsync(e => e.Id == request.ProducerId, token);

            var entity = new Domain.DeliveryMode(Guid.NewGuid(), request.Kind, producer, request.Available,
                                                 deliveryModeAddress, openingHours, request.Name, request.Description);

            entity.SetLockOrderHoursBeforeDelivery(request.LockOrderHoursBeforeDelivery);
            entity.SetAutoAcceptRelatedPurchaseOrders(request.AutoAcceptRelatedPurchaseOrder);
            entity.SetAutoCompleteRelatedPurchaseOrders(request.AutoCompleteRelatedPurchaseOrder);
            entity.SetMaxPurchaseOrdersPerTimeSlot(request.MaxPurchaseOrdersPerTimeSlot);
            entity.SetAcceptPurchaseOrdersWithAmountGreaterThan(request.AcceptPurchaseOrdersWithAmountGreaterThan);
            entity.SetDeliveryFees(request.ApplyDeliveryFeesWhen, request.DeliveryFeesWholeSalePrice, request.DeliveryFeesMinPurchaseOrdersAmount);

            if (entity.Kind is DeliveryKind.Collective or DeliveryKind.Farm or DeliveryKind.Market)
            {
                producer.SetCanDirectSell(true);
            }

            await _context.AddAsync(entity, token);

            await _context.SaveChangesAsync(token);

            var result =
                await _mediatr.Process(
                    new UpdateOrCreateDeliveryClosingsCommand(request.RequestUser)
                    { DeliveryId = entity.Id, Closings = request.Closings }, token);

            if (!result.Succeeded)
            {
                return(Failure <Guid>(result));
            }

            return(Success(entity.Id));
        }
Ejemplo n.º 3
0
        public async Task <Result> Handle(AcceptAgreementCommand request, CancellationToken token)
        {
            var entity = await _context.Agreements.SingleAsync(e => e.Id == request.AgreementId, token);

            if (request.RequestUser.IsInRole(_roleOptions.Producer.Value) &&
                entity.ProducerId != request.RequestUser.Id)
            {
                return(Failure("Vous n'êtes pas autorisé à accéder à cette ressource."));
            }

            if (request.RequestUser.IsInRole(_roleOptions.Store.Value) && entity.StoreId != request.RequestUser.Id)
            {
                return(Failure("Vous n'êtes pas autorisé à accéder à cette ressource."));
            }

            var alreadyAcceptedAgreement =
                await _context.Agreements.SingleOrDefaultAsync(
                    a => a.Id != request.AgreementId && a.ProducerId == entity.ProducerId &&
                    a.StoreId == entity.StoreId && a.Status == AgreementStatus.Accepted, token);

            if (alreadyAcceptedAgreement != null)
            {
                return(Failure("Vous possédez déjà un partenariat actif."));
            }

            Domain.DeliveryMode delivery = null;
            if (request.DeliveryId.HasValue)
            {
                delivery = await _context.DeliveryModes.SingleAsync(e => e.Id == request.DeliveryId, token);
            }

            var currentUser = await _context.Users.SingleAsync(c => c.Id == request.RequestUser.Id, token);

            entity.AcceptAgreement(delivery, currentUser.Kind);

            if (!entity.CatalogId.HasValue && request.CatalogId.HasValue)
            {
                var catalog = await _context.Catalogs.SingleAsync(e => e.Id == request.CatalogId.Value, token);

                entity.ChangeCatalog(catalog);
            }

            await _context.SaveChangesAsync(token);

            return(Success());
        }