fix
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
namespace MyOffice.Services.Account
|
||||
{
|
||||
using AutoMapper;
|
||||
using Core;
|
||||
using Core.Extensions;
|
||||
using Core.Helpers;
|
||||
@@ -16,7 +17,9 @@
|
||||
public class AccountService
|
||||
{
|
||||
private ILogger<AccountService> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IAccountCategoryRepository _accountCategoryRepository;
|
||||
private readonly IAccountAccessRepository _accountAccessRepository;
|
||||
private readonly IAccountRepository _accountRepository;
|
||||
private readonly ICurrencyRepository _currencyRepository;
|
||||
private readonly IAccountAccountCategoryRepository _accountAccountCategoryRepository;
|
||||
@@ -27,7 +30,9 @@
|
||||
|
||||
public AccountService(
|
||||
ILogger<AccountService> logger,
|
||||
IMapper mapper,
|
||||
IAccountCategoryRepository accountCategoryRepository,
|
||||
IAccountAccessRepository accountAccessRepository,
|
||||
IAccountRepository accountRepository,
|
||||
ICurrencyRepository currencyRepository,
|
||||
IAccountAccountCategoryRepository accountAccountCategoryRepository,
|
||||
@@ -38,7 +43,9 @@
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_accountCategoryRepository = accountCategoryRepository;
|
||||
_accountAccessRepository = accountAccessRepository;
|
||||
_accountRepository = accountRepository;
|
||||
_currencyRepository = currencyRepository;
|
||||
_accountAccountCategoryRepository = accountAccountCategoryRepository;
|
||||
@@ -130,24 +137,24 @@
|
||||
return result.Set(exists);
|
||||
}
|
||||
|
||||
public List<Account> GetAllAccounts(Guid userId)
|
||||
public List<AccountDto> GetAllAccounts(Guid userId)
|
||||
{
|
||||
return _accountRepository.GetAll(userId);
|
||||
return _mapper.Map<List<AccountDto>>(_accountRepository.GetAll(userId));
|
||||
}
|
||||
|
||||
public List<Account> GetByCategory(Guid userId, Guid categoryId)
|
||||
public List<AccountDto> GetByCategory(Guid userId, Guid categoryId)
|
||||
{
|
||||
return _accountRepository.GetByCategory(userId, categoryId);
|
||||
return _mapper.Map<List<AccountDto>>(_accountRepository.GetByCategory(userId, categoryId));
|
||||
}
|
||||
|
||||
public List<AccountDetailed> GetByCategoryDetailed(Guid userId, Guid categoryId)
|
||||
public List<AccountDetailedDto> GetByCategoryDetailed(Guid userId, Guid categoryId)
|
||||
{
|
||||
return _accountRepository.GetByCategoryDetailed(userId, categoryId);
|
||||
return _mapper.Map<List<AccountDetailedDto>>(_accountRepository.GetByCategoryDetailed(userId, categoryId));
|
||||
}
|
||||
|
||||
public Exec<AccountDetailed, GeneralExecStatus> GetByIdDetailed(Guid userId, Guid id)
|
||||
public Exec<AccountDetailedDto, GeneralExecStatus> GetByIdDetailed(Guid userId, Guid id)
|
||||
{
|
||||
var result = new Exec<AccountDetailed, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
var result = new Exec<AccountDetailedDto, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var account = _accountRepository.GetByIdDetailed(userId, id);
|
||||
if (account == null)
|
||||
@@ -155,7 +162,7 @@
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
return result.Set(account);
|
||||
return result.Set(_mapper.Map<AccountDetailedDto>(account));
|
||||
}
|
||||
|
||||
public Exec<Account, AccountAddResult> AccountAdd(Guid userId, AccountAdd input)
|
||||
@@ -208,9 +215,40 @@
|
||||
return result.Set(AccountAddResult.failure);
|
||||
}
|
||||
|
||||
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
|
||||
if (access != null && access.Type.ToString() != input.Type)
|
||||
{
|
||||
if (Enum.TryParse<AccountAccessTypeEnum>(input.Type, out var enumType))
|
||||
{
|
||||
access.Type = enumType;
|
||||
_accountAccessRepository.Update(access);
|
||||
}
|
||||
}
|
||||
|
||||
return result.Set(account);
|
||||
}
|
||||
|
||||
public Exec<Account, GeneralExecStatus> AccountDelete(Guid userId, string id)
|
||||
{
|
||||
var result = new Exec<Account, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var account = _accountRepository.Get(userId, id.AsGuid());
|
||||
if (account == null)
|
||||
{
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
if (account.Motions!.Any())
|
||||
{
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
_accountRepository.Delete(account);
|
||||
result.Set(account);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Exec<Account, AccountEditResult> AccountUpdate(Guid userId, Guid accountId, AccountEdit input)
|
||||
{
|
||||
if (input == null)
|
||||
@@ -263,6 +301,16 @@
|
||||
return result.Set(AccountEditResult.failure);
|
||||
}
|
||||
|
||||
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
|
||||
if (access != null && access.Type.ToString() != input.Type)
|
||||
{
|
||||
if (Enum.TryParse<AccountAccessTypeEnum>(input.Type, out var enumType))
|
||||
{
|
||||
access.Type = enumType;
|
||||
_accountAccessRepository.Update(access);
|
||||
}
|
||||
}
|
||||
|
||||
return result.Set(account);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,6 @@
|
||||
public string Name { get; set; } = null!;
|
||||
public string CurrencyId { get; set; } = null!;
|
||||
public Guid CategoryId { get; set; }
|
||||
public string Type { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MyOffice.Services.Account.Domain;
|
||||
|
||||
public class AccountDetailedDto
|
||||
{
|
||||
public AccountDto Account { get; set; } = null!;
|
||||
public decimal TotalPlus { get; set; }
|
||||
public decimal TotalMinus { get; set; }
|
||||
public decimal Rest => TotalPlus - TotalMinus;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using MyOffice.Data.Models.Accounts;
|
||||
using MyOffice.Data.Models.Users;
|
||||
using MyOffice.Services.Account.Domain;
|
||||
|
||||
namespace MyOffice.Services.Account.Domain
|
||||
{
|
||||
using AutoMapper;
|
||||
using MyOffice.Data.Models.Accounts;
|
||||
using MyOffice.Data.Models.Currencies;
|
||||
using MyOffice.Data.Models.Users;
|
||||
using System;
|
||||
|
||||
public class AccountDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string CurrencyGlobalId { get; set; } = null!;
|
||||
public CurrencyGlobal? CurrencyGlobal { get; set; }
|
||||
public Guid CurrencyId { get; set; }
|
||||
public Currency? Currency { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public UserDto? User { get; set; }
|
||||
public Guid OwnerId { get; set; }
|
||||
public UserDto? Owner { get; set; }
|
||||
public string Name { get; set; } = null!;
|
||||
public bool HasMotions { get; set; }
|
||||
public List<AccountAccessDto>? AccessRights { get; set; }
|
||||
public List<AccountAccountCategoryDto>? Categories { get; set; }
|
||||
}
|
||||
|
||||
public class AccountAccessDto
|
||||
{
|
||||
public Guid AccountId { get; set; }
|
||||
public AccountDto? Account { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public User? User { get; set; }
|
||||
|
||||
public bool IsAllowRead { get; set; }
|
||||
public bool IsAllowWrite { get; set; }
|
||||
public bool IsAllowManage { get; set; }
|
||||
public AccountAccessTypeEnum Type { get; set; }
|
||||
}
|
||||
|
||||
public class AccountAccountCategoryDto
|
||||
{
|
||||
public Guid CategoryId { get; set; }
|
||||
public AccountCategoryDto? Category { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class AccountMappingProfile : Profile
|
||||
{
|
||||
public AccountMappingProfile()
|
||||
{
|
||||
CreateMap<User, UserDto>();
|
||||
|
||||
CreateMap<Account, AccountDto>()
|
||||
.ForMember(x => x.HasMotions, o => o.MapFrom(x => x.Motions.Any()))
|
||||
;
|
||||
|
||||
CreateMap<AccountDetailed, AccountDetailedDto>();
|
||||
|
||||
CreateMap<AccountAccess, AccountAccessDto>();
|
||||
|
||||
CreateMap<AccountAccountCategory, AccountAccountCategoryDto>();
|
||||
|
||||
CreateMap<AccountCategory, AccountCategoryDto>()
|
||||
.ForMember(x => x.Id, o => o.MapFrom(x => x.Id))
|
||||
.ForMember(x => x.Id, o => o.MapFrom(x => x.Id))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public class UserDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string UserName { get; set; } = null!;
|
||||
public string Email { get; set; } = null!;
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? FullName { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
|
||||
public string CurrencyId { get; set; }
|
||||
public CurrencyGlobal? Currency { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class AccountCategoryDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public string Name { get; set; } = null!;
|
||||
}
|
||||
@@ -6,4 +6,5 @@ public class AccountEdit
|
||||
public string CurrencyId { get; set; } = null!;
|
||||
public Guid? CategoryId { get; set; }
|
||||
public Guid? UserId { get; set; }
|
||||
public string? Type { get; set; }
|
||||
}
|
||||
@@ -27,21 +27,37 @@ public class DashboardService
|
||||
|
||||
public DashboardData GetDashboardRestData(Guid userId)
|
||||
{
|
||||
/*var lastDaysStart = DateTime.UtcNow.AddMonths(-3).StartOfDay();
|
||||
var lastDaysEnd = lastDaysStart.AddMonths(3).EndOfDay();
|
||||
var previousDaysStart = lastDaysStart.AddMonths(-3).StartOfDay();
|
||||
var previousDaysEnd = previousDaysStart.AddMonths(3).EndOfDay();*/
|
||||
var rests = _accountRepository.GetRestAtDate(userId, DateTime.UtcNow);
|
||||
|
||||
var result = new DashboardData
|
||||
{
|
||||
//IncomeLast = _accountRepository.GetPeriodIncome(userId, lastDaysStart, lastDaysEnd),
|
||||
//IncomePrevious = _accountRepository.GetPeriodIncome(userId, previousDaysStart, previousDaysEnd),
|
||||
//OutcomeLast = _accountRepository.GetPeriodOutcome(userId, lastDaysStart, lastDaysEnd),
|
||||
//OutcomePrevious = _accountRepository.GetPeriodOutcome(userId, previousDaysStart, previousDaysEnd),
|
||||
Balance = rests.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
|
||||
BalanceDebit = rests.Where(x => x.Balance > 0).Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
|
||||
BalanceCredit = rests.Where(x => x.Balance < 0).Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
|
||||
Balance = rests
|
||||
.Where(x => (x.Type == AccountAccessTypeEnum.balance || x.Type == AccountAccessTypeEnum.credit) && x.CurrencyRate.HasValue)
|
||||
.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
|
||||
|
||||
BalanceDebit = rests
|
||||
.Where(x => x.Type == AccountAccessTypeEnum.balance && x.CurrencyRate.HasValue)
|
||||
.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
|
||||
|
||||
BalanceCredit = rests
|
||||
.Where(x => x.Type == AccountAccessTypeEnum.credit && x.CurrencyRate.HasValue)
|
||||
.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),
|
||||
|
||||
BalanceRests = rests
|
||||
.Where(x => x.Type == AccountAccessTypeEnum.balance)
|
||||
.Where(x => x.CurrencyRate.HasValue && x.Balance.HasValue && x.Balance != 0)
|
||||
.Select(x => new DashboardRestData
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Balance = x.Balance,
|
||||
CurrencyName = x.CurrencyName,
|
||||
CurrencyShortName = x.CurrencyShortName,
|
||||
CurrencyRate = x.CurrencyRate,
|
||||
CurrencyQuantity = x.CurrencyQuantity,
|
||||
})
|
||||
.OrderByDescending(x => x.BalanceAtRate)
|
||||
.ToList(),
|
||||
};
|
||||
|
||||
return result;
|
||||
|
||||
@@ -13,13 +13,18 @@ public class DashboardData
|
||||
public decimal? Balance { get; set; }
|
||||
public decimal? BalanceDebit { get; set; }
|
||||
public decimal? BalanceCredit { get; set; }
|
||||
public List<DashboardRestData> BalanceRests { get; set; }
|
||||
}
|
||||
|
||||
public class DashboardRestData
|
||||
{
|
||||
public AccountSimple AccountSimple { get; set; }
|
||||
public decimal? Balance => AccountSimple.Balance;
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = null!;
|
||||
public string CurrencyName { get; set; } = null!;
|
||||
public string CurrencyShortName { get; set; } = null!;
|
||||
public decimal? Balance { get; set; }
|
||||
public decimal? CurrencyRate { get; set; }
|
||||
public decimal? CurrencyQuantity { get; set; }
|
||||
|
||||
public decimal? BalanceAtRate =>
|
||||
AccountSimple.Balance * (AccountSimple.CurrencyRate * AccountSimple.CurrencyQuantity);
|
||||
public decimal? BalanceAtRate => Balance * (CurrencyRate * CurrencyQuantity);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MyOffice.Services.Identity;
|
||||
|
||||
using Data.Models.Users;
|
||||
|
||||
public interface IContextProvider
|
||||
{
|
||||
User User { get; }
|
||||
Guid UserId { get; }
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MyOffice.Core\MyOffice.Core.csproj" />
|
||||
<ProjectReference Include="..\MyOffice.Data.Repositories\MyOffice.Data.Repositories.csproj" />
|
||||
|
||||
Reference in New Issue
Block a user