This commit is contained in:
2023-07-29 16:25:16 +03:00
parent 90f0386bfe
commit 4312a5c084
34 changed files with 1998 additions and 61 deletions
+84 -9
View File
@@ -14,6 +14,7 @@
using Microsoft.Extensions.Logging;
using MyOffice.Services.Account.Domain;
using System.Collections.Generic;
using Identity;
public class AccountService
{
@@ -29,6 +30,7 @@
private readonly IItemRepository _itemRepository;
private readonly IItemGlobalRepository _itemGlobalRepository;
private readonly ItemService _itemService;
private readonly IContextProvider _contextProvider;
public AccountService(
ILogger<AccountService> logger,
@@ -42,7 +44,8 @@
IMotionRepository motionRepository,
IItemRepository itemRepository,
IItemGlobalRepository itemGlobalRepository,
ItemService itemService
ItemService itemService,
IContextProvider contextProvider
)
{
_logger = logger;
@@ -57,6 +60,7 @@
_itemRepository = itemRepository;
_itemGlobalRepository = itemGlobalRepository;
_itemService = itemService;
_contextProvider = contextProvider;
}
public List<AccountCategoryDto> GetAllCategories(Guid userId)
@@ -69,8 +73,8 @@
public Exec<AccountCategoryDto, GeneralExecStatus> GetCategory(Guid userId, Guid id)
{
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
var category = _accountCategoryRepository.Get(userId, id);
var category = _accountCategoryRepository.Get(userId, id);
if (category == null)
{
@@ -456,7 +460,7 @@
exists.Description = motion.Description;
exists.AmountPlus = motion.Plus;
exists.AmountMinus = motion.Minus;
if (!_motionRepository.Update(exists))
{
return result.Set(MotionUpdateStatus.failure);
@@ -467,8 +471,8 @@
}
public Exec<List<Motion>, GeneralExecStatus> GetMotions(
Guid userId,
Guid accountId,
Guid userId,
Guid accountId,
DateTime dateFrom,
DateTime dateTo
)
@@ -528,13 +532,13 @@
if (account.AccessRights!.Any(x => x.User!.Email.EqualsIgnoreCase(email)))
{
return result.Set(AccessInviteStatus.account_not_found);
return result.Set(AccessInviteStatus.access_exists);
}
var access = _accountAccessInviteRepository.Get(userId, email);
if (access != null)
{
return result.Set(AccessInviteStatus.access_exists);
return result.Set(AccessInviteStatus.invite_exists);
}
var invite = new AccountAccessInvite
@@ -542,7 +546,8 @@
Id = Guid.NewGuid(),
CreatedOn = DateTime.UtcNow,
UserId = userId,
Email = email,
AccountId = account.Id,
Email = email.SafeTrim()!,
IsAllowWrite = isAllowWrite,
};
@@ -606,5 +611,75 @@
return result.Set(_mapper.Map<AccountDto>(account));
}
public List<AccountAccessInviteDto> InvitesGet(string email)
{
return _mapper.Map<List<AccountAccessInviteDto>>(_accountAccessInviteRepository.GetActive(email).ToList());
}
public Exec<AccountDto, InviteAcceptStatus> InviteAccept(Guid userId, Guid id, string name)
{
var result = new Exec<AccountDto, InviteAcceptStatus>(InviteAcceptStatus.success);
var invite = _accountAccessInviteRepository.Get(id);
if (invite == null
|| !invite.Email.EqualsIgnoreCase(_contextProvider.User.Email)
|| invite.AcceptedOn.HasValue
|| invite.RejectedOn.HasValue
)
{
return result.Set(InviteAcceptStatus.invite_not_found);
}
var account = _accountRepository.Get(invite.UserId, invite.AccountId);
if (account == null)
{
return result.Set(InviteAcceptStatus.account_not_found);
}
if (account.AccessRights!.Any(x => x.UserId == userId))
{
result.Set(InviteAcceptStatus.already_accepted);
}
else
{
_accountAccessRepository.Add(new AccountAccess
{
UserId = userId,
AccountId = account.Id,
OwnerId = invite.UserId,
Name = name,
IsAllowWrite = invite.IsAllowWrite,
Type = AccountAccessTypeEnum.external,
});
}
invite.AcceptedOn = DateTime.UtcNow;
_accountAccessInviteRepository.Update(invite);
account = _accountRepository.Get(userId, account.Id);
return result.Set(_mapper.Map<AccountDto>(account));
}
public Exec<AccountAccessInviteDto, GeneralExecStatus> InviteReject(Guid id)
{
var result = new Exec<AccountAccessInviteDto, GeneralExecStatus>(GeneralExecStatus.success);
var invite = _accountAccessInviteRepository.Get(id);
if (invite == null
|| !invite.Email.EqualsIgnoreCase(_contextProvider.User.Email)
|| invite.AcceptedOn.HasValue
|| invite.RejectedOn.HasValue
)
{
return result.Set(GeneralExecStatus.not_found);
}
invite.RejectedOn = DateTime.UtcNow;
_accountAccessInviteRepository.Update(invite);
return result.Set(_mapper.Map<AccountAccessInviteDto>(invite));
}
}
}
@@ -1,8 +1,12 @@
namespace MyOffice.Services.Account.Domain;
using Core.Attributes;
using Data.Models.Accounts;
using Data.Models.Users;
using Mapper;
[Link(typeof(AccountAccess))]
[Link(typeof(AccountServiceProfile))]
public class AccountAccessDto
{
/// <summary>
@@ -0,0 +1,12 @@
namespace MyOffice.Services.Account.Domain;
using Core.Attributes;
using Data.Models.Accounts;
[Link(typeof(AccountAccessInvite))]
public class AccountAccessInviteDto
{
public string Id { get; set; } = null!;
public string Account { get; set; } = null!;
public bool IsAllowWrite { get; set; }
}
@@ -2,11 +2,11 @@
using MyOffice.Data.Models.Currencies;
using System;
using Data.Models.Accounts;
public class AccountDto
{
public Guid Id { get; set; }
public Guid CurrentUserId { get; set; }
public string CurrencyGlobalId { get; set; } = null!;
public CurrencyGlobal? CurrencyGlobal { get; set; }
public Guid CurrencyId { get; set; }
@@ -20,4 +20,8 @@ public class AccountDto
public string Type { get; set; } = null!;
public List<AccountAccessDto>? AccessRights { get; set; }
public List<AccountAccountCategoryDto>? Categories { get; set; }
public Guid CurrentUserId { get; set; }
public AccountAccess? AccountAccess { get; set; }
}
@@ -0,0 +1,10 @@
namespace MyOffice.Services.Account.Domain
{
public enum InviteAcceptStatus
{
success,
invite_not_found,
account_not_found,
already_accepted,
}
}
@@ -14,7 +14,20 @@ public class AccountServiceProfile : Profile
CreateMap<Account, AccountDto>()
.ForMember(x => x.HasMotions, o => o.MapFrom(x => x.Motions!.Any()))
.ForMember(x => x.CurrentUserId, o => o.MapFrom<UserIdResolver>())
.ForMember(x => x.Type, o => o.MapFrom((src, dst) => src.AccessRights!.FirstOrDefault(x => x.UserId == dst.CurrentUserId)!.Type.ToString()))
.ForMember(x => x.AccountAccess, o =>
{
o.MapFrom((src, dst) => src.AccessRights!.FirstOrDefault(x => x.UserId == dst.CurrentUserId));
})
.ForMember(x => x.Type, o =>
{
o.SetMappingOrder(1);
o.MapFrom((src, dst) => dst.AccountAccess?.Type.ToString());
})
.ForMember(x => x.Name, o =>
{
o.SetMappingOrder(1);
o.MapFrom((src, dst) => dst.AccountAccess?.Name ?? src.Name);
})
;
CreateMap<AccountDetailed, AccountDetailedDto>();
@@ -27,5 +40,9 @@ public class AccountServiceProfile : Profile
.ForMember(x => x.Id, o => o.MapFrom(x => x.Id))
.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.Accounts!.Any()))
;
CreateMap<AccountAccessInvite, AccountAccessInviteDto>()
.ForMember(x => x.Account, o => o.MapFrom(x => x.Account!.Name))
;
}
}