Esempio n. 1
0
        public async Task Add(List <UserLocation> userLocations)
        {
            using var transaction = _dalService.CreateUnitOfWork();
            await transaction.UserLocations.AddAsync(userLocations);

            await transaction.CompleteAsync();
        }
Esempio n. 2
0
 public async Task <Attachment> GetByKeyAsync(int attachmentsCode, int num)
 {
     using var uow = _dalService.CreateUnitOfWork();
     return(await uow.Attachments.FindByIdAsync(new IAttachmentRepository.AttachmentKey
     {
         Code = attachmentsCode, Num = num
     }));
 }
        public async Task <UserData> UpsertUserDataAsync(UserData entity, CancellationToken cancellationToken)
        {
            using var transaction  = _dalService.CreateUnitOfWork();
            entity.LastModifiedUtc = DateTime.Now;
            var x = await transaction.LeadUsersData.UpsertAsync(entity);

            await transaction.CompleteAsync(cancellationToken);

            return(x);
        }
Esempio n. 4
0
        public async Task <BusinessPartner> AddBusinessPartnerAsync(BusinessPartner businessPartner,
                                                                    CancellationToken cancellation)
        {
            var validator = new CustomerValidator(ValidateCardGroupExist, ValidateSalesmanCodeExist);

            validator.RuleFor(x => x.Name).NotEmpty();
            validator.RuleFor(x => x.GroupSn).Must(ValidateCardGroupExist)
            .WithMessage(x => $"Card Group {x.GroupSn} is invalid");
            validator.RuleFor(x => x.SalesmanCode).Must(ValidateSalesmanCodeExist)
            .WithMessage(x => $"Salesman with code {x.SalesmanCode} is  invalid");
            validator.RuleFor(x => x.Key).Null();
            validator.RuleFor(x => x.CreationDateTime).Null();
            var isBusinessPannerValid = await validator.ValidateAsync(businessPartner, cancellation);

            if (!isBusinessPannerValid.IsValid)
            {
                throw new InvalidStateException(isBusinessPannerValid.Errors
                                                .Select(x => x.ErrorMessage).Aggregate((s1, s2) => $"{s1},{s2}"));
            }


            using var transaction            = _dalService.CreateUnitOfWork();
            businessPartner.CreationDateTime = DateTime.Now;
            var result = await transaction.BusinessPartners.AddAsync(businessPartner);

            await transaction.CompleteAsync(cancellation);

            return(result);
        }
Esempio n. 5
0
        public async Task <IEnumerable <ProductGroupEntity> > GetProductGroupsPageAsync(int page, int size, DateTime?modifiedAfter)
        {
            using var transaction = _dalService.CreateUnitOfWork();
            modifiedAfter         = modifiedAfter?.Date;
            var productGroups = await transaction.ProductGroups.FindAllAsync(
                x => !modifiedAfter.HasValue || (x.LastUpdateDateTime != null && x.LastUpdateDateTime > modifiedAfter) ||
                (x.LastUpdateDateTime == null && x.CreationDateTime > modifiedAfter),
                PageRequest.Of(page, size, Sort <ProductGroupEntity> .By(x => x.CreationDateTime)));

            return(productGroups);
        }
Esempio n. 6
0
        public async Task <Activity> UpdateActivityAsync(int code, Activity activity,
                                                         CancellationToken cancellationToken = default)
        {
            if (activity.Code != code)
            {
                throw new IllegalArgumentException("code must match activity's code (cant modify activity code");
            }
            using var uow = _dalService.CreateUnitOfWork();
            //TODO check activity
            var x = await uow.Activities.UpdateAsync(activity);

            await uow.CompleteAsync(cancellationToken);

            return(x);
        }
        public async Task <object> ExecuteAction(Dictionary <string, string> actionProps,
                                                 CancellationToken cancellationToken = default)
        {
            var attachmentsCodeStr = actionProps.GetValueOrDefault(AttachmentsCodeKey, null) ??
                                     throw new IllegalArgumentException($"{AttachmentsCodeKey} property missing");

            if (!attachmentsCodeStr.All(char.IsDigit))
            {
                throw new IllegalArgumentException($"{AttachmentsCodeKey} property must be a Int");
            }
            var attachmentsCode = Convert.ToInt32(attachmentsCodeStr);

            using var uow = _dalService.CreateUnitOfWork();

            var attachment = await uow.Attachments
                             .FirstOrDefaultAsync(x => x.AttachmentsCode == attachmentsCode) ??
                             throw new IllegalArgumentException($"property {AttachmentsCodeKey} not valid");

            var businessPartnerCode = actionProps.GetValueOrDefault(BusinessPartnerCodeKey, null);

            if (businessPartnerCode == null)
            {
                throw new IllegalArgumentException($"property {BusinessPartnerCodeKey} not valid");
            }
            var output = await AssignAttachmentToBusinessPartner(businessPartnerCode, attachment, uow);

            await uow.CompleteAsync(cancellationToken);

            return(output);
        }
Esempio n. 8
0
        public async Task <object> ExecuteAction(Dictionary <string, string> actionProps, CancellationToken cancellationToken = default)
        {
            var businessPartnerCode = actionProps.GetValueOrDefault("BusinessPartnerCode", null) ??
                                      throw new IllegalArgumentException("BusinessPartnerCode property missing");

            using var uow = _dalService.CreateUnitOfWork();
            var bp = await uow.BusinessPartners.FindByIdAsync(businessPartnerCode);

            if (bp == null)
            {
                throw new IllegalArgumentException("property BusinessPartnerCode not valid");
            }
            bp.IsActive = false;
            var activities = (await uow.Activities.FindAllAsync(
                                  x => x.IsClosed == false && x.BusinessPartnerCode == businessPartnerCode,
                                  PageRequest.Of(0, int.MaxValue)))
                             .Select(x =>
            {
                x.IsClosed = true;
                return(x);
            }).ToList();

            if (activities.Count > 0)
            {
                await uow.Activities.UpdateAsync(activities);
            }
            await uow.BusinessPartners.UpdateAsync(bp);

            await uow.CompleteAsync(cancellationToken);

            return(bp);
        }
Esempio n. 9
0
        public async Task <TDocument> GetByDocNumberAsync(int docNumber)
        {
            using var transaction = DalService.CreateUnitOfWork();
            var header = await GetRepository(transaction).SingleOrDefaultAsync(x => x.Sn == docNumber);

            if (header?.Key == null)
            {
                throw new NotFoundException($"Document:{docNumber} don't exist");
            }
            return(await GetByDocKeyAsync(header.Key.Value));
        }
Esempio n. 10
0
 public async Task <long> CountEmployeesAsync()
 {
     using var transaction = _dalService.CreateUnitOfWork();
     return(await transaction.Employees.CountAsync());
 }
Esempio n. 11
0
 public async Task <CompanyDto> Get()
 {
     return(_mapper.Map <CompanyDto>(await _dalService.CreateUnitOfWork().Company.GetAsync()));
 }
Esempio n. 12
0
 public async Task <IEnumerable <SalesmanDto> > GetPage([FromQuery] int page = 0, [FromQuery] int size = 10)
 {
     return((await _dalService.CreateUnitOfWork().Salesmen
             .GetAllAsync(PageRequest.Of(page, size, Sort <SalesmanEntity> .By(orderBy => orderBy.Sn))))
            .Select(entity => _mapper.Map <SalesmanDto>(entity)));
 }
Esempio n. 13
0
        public async Task UpdateBusinessPartnersGeoLocationAsync(CancellationToken cancellationToken)
        {
            var callCounter       = 0;
            var notRequireCounter = 0;

            //get updated customer
            using var transaction = _dalService.CreateUnitOfWork();

            var partnersWithAddress = (await transaction.BusinessPartners.FindAllAsync(
                                           x => x.IsActive == true,
                                           PageRequest.Of(0, int.MaxValue))).Select(x => new
            {
                Address = x.BillingAddress ?? x.ShippingAddress,
                Info    = x
            }).Where(x => !string.IsNullOrEmpty(x.Address?.City));

            var updatedCustomers = new List <BusinessPartner>(); // logging only

            foreach (var partner in partnersWithAddress)
            {
                var geocoderAddressStr =
                    $"{partner.Address.City}, {partner.Address.Street} {partner.Address.NumAtStreet} ,{partner.Address.Country}";

                if (partner.Info.GeoLocation?.Address == null /*address never evaluated*/ ||
                    !geocoderAddressStr.StartsWith(partner.Info.GeoLocation.Address) /*address changed*/)
                {
                    try
                    {
                        _logger.LogDebug(
                            $"Address {geocoderAddressStr} is been evaluating for customer :{partner.Info.Key}");
                        callCounter++; // logging only
                        var geoLocation = _geocoderService
                                          .GetGeoLocationFromAddress(geocoderAddressStr).Result;
                        if (geoLocation != null)
                        {
                            partner.Info.GeoLocation = new SapGeoLocation
                            {
                                Address   = geocoderAddressStr,
                                Latitude  = geoLocation.Latitude,
                                Longitude = geoLocation.Longitude
                            };
                        }
                        else
                        {
                            _logger.LogDebug(
                                $"Address {geocoderAddressStr} has been evaluated for customer :{partner.Info.Key} with NULL location!!!");
                            partner.Info.GeoLocation = new SapGeoLocation
                            {
                                Address = geocoderAddressStr
                            };
                        }
                    }
                    catch (Exception error)
                    {
                        _logger.LogWarning($"goelocation {geocoderAddressStr} error :{error.Message}");
                        continue; //unsuccessful geocoder call, try next business partner
                    }

                    var customerToUpdate = new BusinessPartner
                    {
                        Key         = partner.Info.Key,
                        GeoLocation = partner.Info.GeoLocation
                    };
                    using var updateUow = _dalService.CreateUnitOfWork();
                    await updateUow.BusinessPartners.UpdateAsync(customerToUpdate);

                    await updateUow.CompleteAsync(cancellationToken);

                    _logger.LogDebug($"goelocation {geocoderAddressStr} updated ,customer :{partner.Info.Key}");
                    updatedCustomers.Add(partner.Info);
                }
                else
                {
                    notRequireCounter++;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
            }

            var cKeys = updatedCustomers.Count > 0
                ? updatedCustomers.Select(x => x.Key).Aggregate((x1, x2) => $"{x1}, {x2}")
                : "";

            _logger.LogInformation(
                $"finish task , call counter={callCounter} ,{updatedCustomers.Count} updated," +
                $" {notRequireCounter} not requires any update --- {cKeys}\n");
        }