public async Task DeleteWithNotification(RemoveTeamMemberDTO teamMember) { var member = await base.GetAsync(teamMember.Id); int memberUserId = member.UserId; bool memberIsAccepted = member.IsAccepted; if (member == null) { throw new NotFoundException(nameof(TeamMember), teamMember.Id); } await base.RemoveAsync(teamMember.Id); var group = await _context.Groups.AsNoTracking().FirstOrDefaultAsync(g => g.Id == teamMember.GroupId); var userMember = await _context.Users.AsNoTracking().FirstOrDefaultAsync(u => u.Id == memberUserId); if (teamMember.InitiatorUserId == memberUserId) { await CheckIsUserAcceptedAsync(memberIsAccepted, teamMember, userMember, group); } else { var initiator = await _context.Users.FirstOrDefaultAsync(u => u.Id == teamMember.InitiatorUserId); var members = await _context.TeamMembers.AsNoTracking().Where(t => t.GroupId == teamMember.GroupId && t.UserId != teamMember.InitiatorUserId && t.IsAccepted).ToListAsync(); foreach (var memb in members) { string msg = $"{initiator?.Username} removed {userMember?.Username} from group {group?.Name}"; await MakeNotificationAsync(msg, memb); } string message = $"{initiator?.Username} removed you from group {group?.Name}"; await MakeNotificationAsync(message, null, memberUserId, -1); } }
private async Task CheckIsUserAcceptedAsync(bool memberIsAccepted, RemoveTeamMemberDTO teamMember, User userMember, Group group) { if (!memberIsAccepted) { var members = await _context.TeamMembers.AsNoTracking().Where(t => t.GroupId == teamMember.GroupId && t.IsAccepted && (t.MemberRole == GroupRole.Admin || t.MemberRole == GroupRole.Owner)).ToListAsync(); foreach (var memb in members) { string message = $"{userMember?.Username} declined the invitation to group {group?.Name}"; await MakeNotificationAsync(message, memb); } } else { var members = await _context.TeamMembers.AsNoTracking().Where(t => t.GroupId == teamMember.GroupId && t.IsAccepted).ToListAsync(); foreach (var memb in members) { string message = $"{userMember?.Username} left group {group?.Name}"; await MakeNotificationAsync(message, memb); } } }
public async Task DeleteMemberWithNotification([FromQuery] RemoveTeamMemberDTO removeObject) { await _teamMemberService.DeleteWithNotification(removeObject); }