@Test public void testSyncUserAsync() { Account account = Account.builder() .id(UUID.randomUUID().toString()) .name("test_user") .email("test_user@jskillcloud.com") .phoneNumber("1122334455") .confirmedAndActive(true) .memberSince(Instant.now()) .photoUrl("http: .build(); when(envConfig.isDebug()).thenReturn(false); when(accountRepo.findAccountById(anyString())).thenReturn(account); doNothing().when(serviceHelper).syncUserWithIntercom(any(User.class), eq(account.getId())); String companyId = UUID.randomUUID().toString(); WorkerOfList workerOfList = WorkerOfList.builder() .userId(account.getId()) .build(); workerOfList.getTeams().add(TeamDto.builder().name("test_team").companyId(companyId).build()); when(companyClient.getWorkerOf(anyString(), eq(account.getId()))).thenReturn(new GetWorkerOfResponse(workerOfList)); CompanyDto companyDto = CompanyDto.builder() .id(companyId) .name("test_company") .build(); when(companyClient.getCompany(anyString(), eq(companyId))).thenReturn(new GenericCompanyResponse(companyDto)); AdminOfList adminOfList = AdminOfList.builder() .userId(account.getId()) .build(); adminOfList.getCompanies().add(companyDto); when(companyClient.getAdminOf(anyString(), eq(account.getId()))).thenReturn(new GetAdminOfResponse(adminOfList)); serviceHelper.syncUserAsync(account.getId()); ArgumentCaptor<User> argument1 = ArgumentCaptor.forClass(User.class); ArgumentCaptor<String> argument2 = ArgumentCaptor.forClass(String.class); verify(serviceHelper, times(1)).syncUserWithIntercom(argument1.capture(), argument2.capture()); User user = argument1.getValue(); String userId = argument2.getValue(); log.info(user.toString()); log.info(userId); assertThat(user.getUserId()).isEqualTo(account.getId()); assertThat(user.getEmail()).isEqualTo(account.getEmail()); assertThat(user.getName()).isEqualTo(account.getName()); assertThat(user.getSignedUpAt()).isEqualTo(account.getMemberSince().toEpochMilli()); assertThat(user.getAvatar().getImageURL().toString()).isEqualTo(account.getPhotoUrl()); assertThat(user.getCustomAttributes().get("v2")).isEqualTo(CustomAttribute.newBooleanAttribute("v2", true)); assertThat(user.getCustomAttributes().get("phonenumber")).isEqualTo(CustomAttribute.newStringAttribute("phonenumber", account.getPhoneNumber())); assertThat(user.getCustomAttributes().get("confirmed_and_active")).isEqualTo(CustomAttribute.newBooleanAttribute("confirmed_and_active", account.isConfirmedAndActive())); assertThat(user.getCustomAttributes().get("is_worker")).isEqualTo(CustomAttribute.newBooleanAttribute("is_worker", true)); assertThat(user.getCustomAttributes().get("is_admin")).isEqualTo(CustomAttribute.newBooleanAttribute("is_admin", true)); assertThat(user.getCustomAttributes().get("is_staffjoy_support")).isEqualTo(CustomAttribute.newBooleanAttribute("is_staffjoy_support", account.isSupport())); io.intercom.api.Company iCompany = user.getCompanyCollection().next(); assertThat(iCompany.getCompanyID()).isEqualTo(companyId); assertThat(iCompany.getName()).isEqualTo(companyDto.getName()); }
@Async(AppConfig.ASYNC_EXECUTOR_NAME) public void syncUserAsync(String userId) { if (envConfig.isDebug()) { logger.debug("intercom disabled in dev & test environment"); return; } Account account = accountRepo.findAccountById(userId); if (account == null) { throw new ServiceException(ResultCode.NOT_FOUND, String.format("User with id %s not found", userId)); } if (StringUtils.isEmpty(account.getPhoneNumber()) && StringUtils.isEmpty(account.getEmail())) { logger.info(String.format("skipping sync for user %s because no email or phonenumber", account.getId())); return; } Map<String, CompanyDto> memberships = new HashMap<>(); GetWorkerOfResponse workerOfResponse = null; try { workerOfResponse = companyClient.getWorkerOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch(Exception ex) { String errMsg = "could not fetch workOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!workerOfResponse.isSuccess()) { handleError(logger, workerOfResponse.getMessage()); throw new ServiceException(workerOfResponse.getMessage()); } WorkerOfList workerOfList = workerOfResponse.getWorkerOfList(); boolean isWorker = workerOfList.getTeams().size() > 0; for(TeamDto teamDto : workerOfList.getTeams()) { GenericCompanyResponse genericCompanyResponse = null; try { genericCompanyResponse = companyClient.getCompany(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, teamDto.getCompanyId()); } catch (Exception ex) { String errMsg = "could not fetch companyDto from teamDto"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!genericCompanyResponse.isSuccess()) { handleError(logger, genericCompanyResponse.getMessage()); throw new ServiceException(genericCompanyResponse.getMessage()); } CompanyDto companyDto = genericCompanyResponse.getCompany(); memberships.put(companyDto.getId(), companyDto); } GetAdminOfResponse getAdminOfResponse = null; try { getAdminOfResponse = companyClient.getAdminOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch (Exception ex) { String errMsg = "could not fetch adminOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!getAdminOfResponse.isSuccess()) { handleError(logger, getAdminOfResponse.getMessage()); throw new ServiceException(getAdminOfResponse.getMessage()); } AdminOfList adminOfList = getAdminOfResponse.getAdminOfList(); boolean isAdmin = adminOfList.getCompanies().size() > 0; for(CompanyDto companyDto : adminOfList.getCompanies()) { memberships.put(companyDto.getId(), companyDto); } User user = new User(); user.setUserId(account.getId()); user.setEmail(account.getEmail()); user.setName(account.getName()); user.setSignedUpAt(account.getMemberSince().toEpochMilli()); user.setAvatar(new Avatar().setImageURL(account.getPhotoUrl())); user.setLastRequestAt(Instant.now().toEpochMilli()); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("v2", true)); user.addCustomAttribute(CustomAttribute.newStringAttribute("phonenumber", account.getPhoneNumber())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("confirmed_and_active", account.isConfirmedAndActive())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_worker", isWorker)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_admin", isAdmin)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_staffjoy_support", account.isSupport())); for(CompanyDto companyDto : memberships.values()) { user.addCompany(new io.intercom.api.Company().setCompanyID(companyDto.getId()).setName(companyDto.getName())); } this.syncUserWithIntercom(user, account.getId()); }
ServiceHelper { @Async(AppConfig.ASYNC_EXECUTOR_NAME) public void syncUserAsync(String userId) { if (envConfig.isDebug()) { logger.debug("intercom disabled in dev & test environment"); return; } Account account = accountRepo.findAccountById(userId); if (account == null) { throw new ServiceException(ResultCode.NOT_FOUND, String.format("User with id %s not found", userId)); } if (StringUtils.isEmpty(account.getPhoneNumber()) && StringUtils.isEmpty(account.getEmail())) { logger.info(String.format("skipping sync for user %s because no email or phonenumber", account.getId())); return; } Map<String, CompanyDto> memberships = new HashMap<>(); GetWorkerOfResponse workerOfResponse = null; try { workerOfResponse = companyClient.getWorkerOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch(Exception ex) { String errMsg = "could not fetch workOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!workerOfResponse.isSuccess()) { handleError(logger, workerOfResponse.getMessage()); throw new ServiceException(workerOfResponse.getMessage()); } WorkerOfList workerOfList = workerOfResponse.getWorkerOfList(); boolean isWorker = workerOfList.getTeams().size() > 0; for(TeamDto teamDto : workerOfList.getTeams()) { GenericCompanyResponse genericCompanyResponse = null; try { genericCompanyResponse = companyClient.getCompany(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, teamDto.getCompanyId()); } catch (Exception ex) { String errMsg = "could not fetch companyDto from teamDto"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!genericCompanyResponse.isSuccess()) { handleError(logger, genericCompanyResponse.getMessage()); throw new ServiceException(genericCompanyResponse.getMessage()); } CompanyDto companyDto = genericCompanyResponse.getCompany(); memberships.put(companyDto.getId(), companyDto); } GetAdminOfResponse getAdminOfResponse = null; try { getAdminOfResponse = companyClient.getAdminOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch (Exception ex) { String errMsg = "could not fetch adminOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!getAdminOfResponse.isSuccess()) { handleError(logger, getAdminOfResponse.getMessage()); throw new ServiceException(getAdminOfResponse.getMessage()); } AdminOfList adminOfList = getAdminOfResponse.getAdminOfList(); boolean isAdmin = adminOfList.getCompanies().size() > 0; for(CompanyDto companyDto : adminOfList.getCompanies()) { memberships.put(companyDto.getId(), companyDto); } User user = new User(); user.setUserId(account.getId()); user.setEmail(account.getEmail()); user.setName(account.getName()); user.setSignedUpAt(account.getMemberSince().toEpochMilli()); user.setAvatar(new Avatar().setImageURL(account.getPhotoUrl())); user.setLastRequestAt(Instant.now().toEpochMilli()); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("v2", true)); user.addCustomAttribute(CustomAttribute.newStringAttribute("phonenumber", account.getPhoneNumber())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("confirmed_and_active", account.isConfirmedAndActive())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_worker", isWorker)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_admin", isAdmin)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_staffjoy_support", account.isSupport())); for(CompanyDto companyDto : memberships.values()) { user.addCompany(new io.intercom.api.Company().setCompanyID(companyDto.getId()).setName(companyDto.getName())); } this.syncUserWithIntercom(user, account.getId()); } }
ServiceHelper { @Async(AppConfig.ASYNC_EXECUTOR_NAME) public void syncUserAsync(String userId) { if (envConfig.isDebug()) { logger.debug("intercom disabled in dev & test environment"); return; } Account account = accountRepo.findAccountById(userId); if (account == null) { throw new ServiceException(ResultCode.NOT_FOUND, String.format("User with id %s not found", userId)); } if (StringUtils.isEmpty(account.getPhoneNumber()) && StringUtils.isEmpty(account.getEmail())) { logger.info(String.format("skipping sync for user %s because no email or phonenumber", account.getId())); return; } Map<String, CompanyDto> memberships = new HashMap<>(); GetWorkerOfResponse workerOfResponse = null; try { workerOfResponse = companyClient.getWorkerOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch(Exception ex) { String errMsg = "could not fetch workOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!workerOfResponse.isSuccess()) { handleError(logger, workerOfResponse.getMessage()); throw new ServiceException(workerOfResponse.getMessage()); } WorkerOfList workerOfList = workerOfResponse.getWorkerOfList(); boolean isWorker = workerOfList.getTeams().size() > 0; for(TeamDto teamDto : workerOfList.getTeams()) { GenericCompanyResponse genericCompanyResponse = null; try { genericCompanyResponse = companyClient.getCompany(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, teamDto.getCompanyId()); } catch (Exception ex) { String errMsg = "could not fetch companyDto from teamDto"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!genericCompanyResponse.isSuccess()) { handleError(logger, genericCompanyResponse.getMessage()); throw new ServiceException(genericCompanyResponse.getMessage()); } CompanyDto companyDto = genericCompanyResponse.getCompany(); memberships.put(companyDto.getId(), companyDto); } GetAdminOfResponse getAdminOfResponse = null; try { getAdminOfResponse = companyClient.getAdminOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch (Exception ex) { String errMsg = "could not fetch adminOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!getAdminOfResponse.isSuccess()) { handleError(logger, getAdminOfResponse.getMessage()); throw new ServiceException(getAdminOfResponse.getMessage()); } AdminOfList adminOfList = getAdminOfResponse.getAdminOfList(); boolean isAdmin = adminOfList.getCompanies().size() > 0; for(CompanyDto companyDto : adminOfList.getCompanies()) { memberships.put(companyDto.getId(), companyDto); } User user = new User(); user.setUserId(account.getId()); user.setEmail(account.getEmail()); user.setName(account.getName()); user.setSignedUpAt(account.getMemberSince().toEpochMilli()); user.setAvatar(new Avatar().setImageURL(account.getPhotoUrl())); user.setLastRequestAt(Instant.now().toEpochMilli()); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("v2", true)); user.addCustomAttribute(CustomAttribute.newStringAttribute("phonenumber", account.getPhoneNumber())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("confirmed_and_active", account.isConfirmedAndActive())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_worker", isWorker)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_admin", isAdmin)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_staffjoy_support", account.isSupport())); for(CompanyDto companyDto : memberships.values()) { user.addCompany(new io.intercom.api.Company().setCompanyID(companyDto.getId()).setName(companyDto.getName())); } this.syncUserWithIntercom(user, account.getId()); }  }
ServiceHelper { @Async(AppConfig.ASYNC_EXECUTOR_NAME) public void syncUserAsync(String userId) { if (envConfig.isDebug()) { logger.debug("intercom disabled in dev & test environment"); return; } Account account = accountRepo.findAccountById(userId); if (account == null) { throw new ServiceException(ResultCode.NOT_FOUND, String.format("User with id %s not found", userId)); } if (StringUtils.isEmpty(account.getPhoneNumber()) && StringUtils.isEmpty(account.getEmail())) { logger.info(String.format("skipping sync for user %s because no email or phonenumber", account.getId())); return; } Map<String, CompanyDto> memberships = new HashMap<>(); GetWorkerOfResponse workerOfResponse = null; try { workerOfResponse = companyClient.getWorkerOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch(Exception ex) { String errMsg = "could not fetch workOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!workerOfResponse.isSuccess()) { handleError(logger, workerOfResponse.getMessage()); throw new ServiceException(workerOfResponse.getMessage()); } WorkerOfList workerOfList = workerOfResponse.getWorkerOfList(); boolean isWorker = workerOfList.getTeams().size() > 0; for(TeamDto teamDto : workerOfList.getTeams()) { GenericCompanyResponse genericCompanyResponse = null; try { genericCompanyResponse = companyClient.getCompany(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, teamDto.getCompanyId()); } catch (Exception ex) { String errMsg = "could not fetch companyDto from teamDto"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!genericCompanyResponse.isSuccess()) { handleError(logger, genericCompanyResponse.getMessage()); throw new ServiceException(genericCompanyResponse.getMessage()); } CompanyDto companyDto = genericCompanyResponse.getCompany(); memberships.put(companyDto.getId(), companyDto); } GetAdminOfResponse getAdminOfResponse = null; try { getAdminOfResponse = companyClient.getAdminOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch (Exception ex) { String errMsg = "could not fetch adminOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!getAdminOfResponse.isSuccess()) { handleError(logger, getAdminOfResponse.getMessage()); throw new ServiceException(getAdminOfResponse.getMessage()); } AdminOfList adminOfList = getAdminOfResponse.getAdminOfList(); boolean isAdmin = adminOfList.getCompanies().size() > 0; for(CompanyDto companyDto : adminOfList.getCompanies()) { memberships.put(companyDto.getId(), companyDto); } User user = new User(); user.setUserId(account.getId()); user.setEmail(account.getEmail()); user.setName(account.getName()); user.setSignedUpAt(account.getMemberSince().toEpochMilli()); user.setAvatar(new Avatar().setImageURL(account.getPhotoUrl())); user.setLastRequestAt(Instant.now().toEpochMilli()); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("v2", true)); user.addCustomAttribute(CustomAttribute.newStringAttribute("phonenumber", account.getPhoneNumber())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("confirmed_and_active", account.isConfirmedAndActive())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_worker", isWorker)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_admin", isAdmin)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_staffjoy_support", account.isSupport())); for(CompanyDto companyDto : memberships.values()) { user.addCompany(new io.intercom.api.Company().setCompanyID(companyDto.getId()).setName(companyDto.getName())); } this.syncUserWithIntercom(user, account.getId()); }  @Async(AppConfig.ASYNC_EXECUTOR_NAME) void syncUserAsync(String userId); @Async(AppConfig.ASYNC_EXECUTOR_NAME) void trackEventAsync(String userId, String eventName); void sendSmsGreeting(String userId); boolean isAlmostSameInstant(Instant dt1, Instant dt2); void handleError(ILogger log, String errMsg); void handleException(ILogger log, Exception ex, String errMsg); }
ServiceHelper { @Async(AppConfig.ASYNC_EXECUTOR_NAME) public void syncUserAsync(String userId) { if (envConfig.isDebug()) { logger.debug("intercom disabled in dev & test environment"); return; } Account account = accountRepo.findAccountById(userId); if (account == null) { throw new ServiceException(ResultCode.NOT_FOUND, String.format("User with id %s not found", userId)); } if (StringUtils.isEmpty(account.getPhoneNumber()) && StringUtils.isEmpty(account.getEmail())) { logger.info(String.format("skipping sync for user %s because no email or phonenumber", account.getId())); return; } Map<String, CompanyDto> memberships = new HashMap<>(); GetWorkerOfResponse workerOfResponse = null; try { workerOfResponse = companyClient.getWorkerOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch(Exception ex) { String errMsg = "could not fetch workOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!workerOfResponse.isSuccess()) { handleError(logger, workerOfResponse.getMessage()); throw new ServiceException(workerOfResponse.getMessage()); } WorkerOfList workerOfList = workerOfResponse.getWorkerOfList(); boolean isWorker = workerOfList.getTeams().size() > 0; for(TeamDto teamDto : workerOfList.getTeams()) { GenericCompanyResponse genericCompanyResponse = null; try { genericCompanyResponse = companyClient.getCompany(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, teamDto.getCompanyId()); } catch (Exception ex) { String errMsg = "could not fetch companyDto from teamDto"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!genericCompanyResponse.isSuccess()) { handleError(logger, genericCompanyResponse.getMessage()); throw new ServiceException(genericCompanyResponse.getMessage()); } CompanyDto companyDto = genericCompanyResponse.getCompany(); memberships.put(companyDto.getId(), companyDto); } GetAdminOfResponse getAdminOfResponse = null; try { getAdminOfResponse = companyClient.getAdminOf(AuthConstant.AUTHORIZATION_ACCOUNT_SERVICE, userId); } catch (Exception ex) { String errMsg = "could not fetch adminOfList"; handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!getAdminOfResponse.isSuccess()) { handleError(logger, getAdminOfResponse.getMessage()); throw new ServiceException(getAdminOfResponse.getMessage()); } AdminOfList adminOfList = getAdminOfResponse.getAdminOfList(); boolean isAdmin = adminOfList.getCompanies().size() > 0; for(CompanyDto companyDto : adminOfList.getCompanies()) { memberships.put(companyDto.getId(), companyDto); } User user = new User(); user.setUserId(account.getId()); user.setEmail(account.getEmail()); user.setName(account.getName()); user.setSignedUpAt(account.getMemberSince().toEpochMilli()); user.setAvatar(new Avatar().setImageURL(account.getPhotoUrl())); user.setLastRequestAt(Instant.now().toEpochMilli()); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("v2", true)); user.addCustomAttribute(CustomAttribute.newStringAttribute("phonenumber", account.getPhoneNumber())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("confirmed_and_active", account.isConfirmedAndActive())); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_worker", isWorker)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_admin", isAdmin)); user.addCustomAttribute(CustomAttribute.newBooleanAttribute("is_staffjoy_support", account.isSupport())); for(CompanyDto companyDto : memberships.values()) { user.addCompany(new io.intercom.api.Company().setCompanyID(companyDto.getId()).setName(companyDto.getName())); } this.syncUserWithIntercom(user, account.getId()); }  @Async(AppConfig.ASYNC_EXECUTOR_NAME) void syncUserAsync(String userId); @Async(AppConfig.ASYNC_EXECUTOR_NAME) void trackEventAsync(String userId, String eventName); void sendSmsGreeting(String userId); boolean isAlmostSameInstant(Instant dt1, Instant dt2); void handleError(ILogger log, String errMsg); void handleException(ILogger log, Exception ex, String errMsg);  }