Ejemplo n.º 1
0
    public Cashflow?MapToModelOrNull(CashflowEntity?entity)
    {
        if (entity is null)
        {
            return(null);
        }

        var model = new Cashflow()
        {
            Id            = entity.Id,
            AccountId     = entity.AccountId,
            Amount        = entity.Amount,
            CategoryId    = entity.CategoryId,
            Description   = entity.Description,
            EffectiveDate = entity.EffectiveDate,
            Frequency     = entity.Frequency,
            Inactive      = entity.Inactive,
            IntervalType  = entity.IntervalType,
            Recurrence    = entity.Recurrence,
            UserId        = entity.UserId
        };

        return(model.SetTags(_tagsMapper.Map(entity.Tags)));
    }
        private async Task ImportCashflowsAsync(Guid userId, Request request,
                                                Action <ImportDataStatusDto> updateProgress, CancellationToken cancellationToken)
        {
            if (!request.Data.Cashflows.Any())
            {
                return;
            }

            var cashflows = request.Data.Cashflows;

            _importDataStatus = _importDataStatus with
            {
                Status = CommandStatus.InProgress, CashflowsTotal = cashflows.Length
            };
            updateProgress(_importDataStatus);

            foreach (var element in cashflows)
            {
                Cashflow cashflow = null !;

                var exists = await _cashflowRepository.FindByIdAsync(element.Id, userId, cancellationToken);

                if (exists is not null)
                {
                    if (request.UpdateExisting)
                    {
                        cashflow = exists with
                        {
                            EffectiveDate = element.EffectiveDate,
                            IntervalType  = element.IntervalType,
                            Frequency     = element.Frequency,
                            Recurrence    = element.Recurrence,
                            Amount        = element.Amount,
                            Description   = element.Description,
                            CategoryId    = element.CategoryId,
                            AccountId     = element.AccountId,
                            Inactive      = element.Inactive
                        };
                        cashflow = cashflow.SetTags(element.Tags);
                        _logger.LogInformation("----- Modify Cashflow - Cashflow: {@Cashflow}", cashflow);
                    }
                    else
                    {
                        _logger.LogInformation("----- Ignore Cashflow - Cashflow: {@Cashflow}", exists);
                    }
                }
                else
                {
                    cashflow = new()
                    {
                        Id            = element.Id,
                        EffectiveDate = element.EffectiveDate,
                        Amount        = element.Amount,
                        UserId        = userId,
                        IntervalType  = element.IntervalType,
                        Frequency     = element.Frequency,
                        Recurrence    = element.Recurrence,
                        Description   = element.Description,
                        CategoryId    = element.CategoryId,
                        AccountId     = element.AccountId,
                        Inactive      = element.Inactive
                    };
                    cashflow = cashflow.SetTags(element.Tags);
                    _logger.LogInformation("----- Create Cashflow - Cashflow: {@Cashflow}", cashflow);
                }

                await _cashflowRepository.SaveAsync(cashflow, cancellationToken);

                _importDataStatus = _importDataStatus with
                {
                    CashflowsProcessed = _importDataStatus.CashflowsProcessed + 1
                };
                updateProgress(_importDataStatus);
            }
        }