|
@@ -13,6 +13,7 @@ using MTWorkHR.Application.Services.Interfaces;
|
|
using MTWorkHR.Core.Email;
|
|
using MTWorkHR.Core.Email;
|
|
using MTWorkHR.Core.Entities;
|
|
using MTWorkHR.Core.Entities;
|
|
using MTWorkHR.Infrastructure.UnitOfWorks;
|
|
using MTWorkHR.Infrastructure.UnitOfWorks;
|
|
|
|
+using System.Linq;
|
|
|
|
|
|
namespace MTWorkHR.Application.Services
|
|
namespace MTWorkHR.Application.Services
|
|
{
|
|
{
|
|
@@ -35,17 +36,48 @@ namespace MTWorkHR.Application.Services
|
|
public override async Task<TeamDto> Create(TeamDto input)
|
|
public override async Task<TeamDto> Create(TeamDto input)
|
|
{
|
|
{
|
|
var entity = MapperObject.Mapper.Map<Team>(input);
|
|
var entity = MapperObject.Mapper.Map<Team>(input);
|
|
|
|
+
|
|
if (entity is null)
|
|
if (entity is null)
|
|
{
|
|
{
|
|
throw new AppException(ExceptionEnum.MapperIssue);
|
|
throw new AppException(ExceptionEnum.MapperIssue);
|
|
}
|
|
}
|
|
|
|
+ entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
|
|
|
|
+
|
|
|
|
+ var team = await _unitOfWork.Team.AddAsync(entity);
|
|
|
|
+ await _unitOfWork.CompleteAsync();
|
|
|
|
+
|
|
|
|
+ var response = MapperObject.Mapper.Map<TeamDto>(team);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public override async Task<TeamDto> Update(TeamDto input)
|
|
|
|
+ {
|
|
|
|
+ var entity = await _unitOfWork.Team.GetByIdAsync(input.Id);
|
|
|
|
+ if (entity is null)
|
|
|
|
+ throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
+ entity.NameAr = input.NameAr;
|
|
|
|
+ entity.NameEn = input.NameEn;
|
|
|
|
+ var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
|
|
|
|
+
|
|
|
|
+ var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
|
|
|
|
+ var newTeamUsers = NewItems?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
|
|
|
|
+ var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
|
|
|
|
+ foreach (var delUser in DeletedItems)
|
|
|
|
+ {
|
|
|
|
+ var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
|
|
|
|
+ if(removeItem != null) entity.TeamUsers.Remove(removeItem);
|
|
|
|
+ }
|
|
|
|
+ foreach (var newUsr in newTeamUsers)
|
|
|
|
+ {
|
|
|
|
+ entity.TeamUsers.Add(newUsr);
|
|
|
|
+ }
|
|
|
|
|
|
- var task = await _unitOfWork.Team.AddAsync(entity);
|
|
|
|
await _unitOfWork.CompleteAsync();
|
|
await _unitOfWork.CompleteAsync();
|
|
|
|
|
|
- var response = MapperObject.Mapper.Map<TeamDto>(task);
|
|
|
|
|
|
+ var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
|
|
return response;
|
|
return response;
|
|
}
|
|
}
|
|
|
|
+
|
|
public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
|
|
public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
|
|
{
|
|
{
|
|
var result = false;
|
|
var result = false;
|