public async Task <IMerchantsSupervisorMembership> GetWithMerchantsAsync(string employeeId) { ISupervisorMembership membership = await _supervisorMembershipRepository.GetAsync(employeeId); if (membership != null) { var merchants = new List <string>(); foreach (string merchantGroupId in membership.MerchantGroups) { MerchantGroupResponse merchantGroup = await _payMerchantClient.Groups.GetGroupAsync(merchantGroupId); if (merchantGroup != null) { merchants.AddRange(merchantGroup.Merchants); } } return(new MerchantsSupervisorMembership { MerchantId = membership.MerchantId, EmployeeId = membership.EmployeeId, Merchants = merchants.Distinct() }); } return(null); }
public async Task <IMerchantsSupervisorMembership> AddAsync(IMerchantsSupervisorMembership src) { MerchantGroupResponse merchantGroup = await _payMerchantClient.Groups.AddGroupAsync(Mapper.Map <AddMerchantGroupRequest>(src)); try { ISupervisorMembership membership = await _supervisorMembershipRepository.AddAsync( new SupervisorMembership { MerchantId = src.MerchantId, EmployeeId = src.EmployeeId, MerchantGroups = new[] { merchantGroup.Id } }); return(new MerchantsSupervisorMembership { MerchantId = membership.MerchantId, EmployeeId = membership.EmployeeId, Merchants = merchantGroup.Merchants }); } catch (DuplicateKeyException ex) { _log.ErrorWithDetails(ex, src); if (merchantGroup != null) { await _payMerchantClient.Groups.DeleteGroupAsync(merchantGroup.Id); } throw new SupervisorMembershipAlreadyExistsException(src.EmployeeId); } }