Skip to content

Commit

Permalink
simplify create invitation endpoint (#11522)
Browse files Browse the repository at this point in the history
  • Loading branch information
flutra-osmani committed Mar 6, 2024
1 parent 3888b97 commit 8cae4cb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public UserInvitationRead createUserInvitation(@Body final UserInvitationCreateR
return ApiHelper.execute(() -> {
final User currentUser = currentUserService.getCurrentUser();

return userInvitationHandler.create(invitationCreateRequestBody, currentUser.getUserId());
return userInvitationHandler.create(invitationCreateRequestBody, currentUser);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
package io.airbyte.server.handlers;

import io.airbyte.api.model.generated.InviteCodeRequestBody;
import io.airbyte.api.model.generated.UserIdRequestBody;
import io.airbyte.api.model.generated.UserInvitationCreateRequestBody;
import io.airbyte.api.model.generated.UserInvitationRead;
import io.airbyte.api.model.generated.UserRead;
import io.airbyte.commons.server.errors.OperationNotAllowedException;
import io.airbyte.commons.server.handlers.UserHandler;
import io.airbyte.config.InvitationStatus;
import io.airbyte.config.User;
import io.airbyte.config.UserInvitation;
Expand All @@ -30,18 +27,15 @@ public class UserInvitationHandler {

final UserInvitationService service;
final UserInvitationMapper mapper;
final UserHandler userHandler;
final WebUrlHelper webUrlHelper;
final CustomerIoEmailNotificationSender customerIoEmailNotificationSender;

public UserInvitationHandler(final UserInvitationService service,
final UserInvitationMapper mapper,
final UserHandler userHandler,
final CustomerIoEmailNotificationSender customerIoEmailNotificationSender,
final WebUrlHelper webUrlHelper) {
this.service = service;
this.mapper = mapper;
this.userHandler = userHandler;
this.webUrlHelper = webUrlHelper;
this.customerIoEmailNotificationSender = customerIoEmailNotificationSender;
}
Expand All @@ -56,11 +50,11 @@ public UserInvitationRead getByInviteCode(final String inviteCode, final User cu
return mapper.toApi(invitation);
}

public UserInvitationRead create(final UserInvitationCreateRequestBody req, final UUID currentUserId)
public UserInvitationRead create(final UserInvitationCreateRequestBody req, final User currentUser)
throws JsonValidationException, ConfigNotFoundException, IOException {
final UserInvitation model = mapper.toDomain(req);

model.setInviterUserId(currentUserId);
model.setInviterUserId(currentUser.getUserId());

// For now, inviteCodes are simply UUIDs that are converted to strings, to virtually guarantee
// uniqueness.
Expand All @@ -77,7 +71,6 @@ public UserInvitationRead create(final UserInvitationCreateRequestBody req, fina
// send invite email to the user
// the email content includes the name of the inviter and the invite link
// the invite link should look like cloud.airbyte.com/accept-invite?inviteCode=randomCodeHere
final UserRead currentUser = userHandler.getUser(new UserIdRequestBody().userId(currentUserId));
final String inviteLink = webUrlHelper.getBaseUrl() + "/accept-invite?inviteCode=" + saved.getInviteCode();
customerIoEmailNotificationSender.sendInviteToUser(new CustomerIoEmailConfig(req.getInvitedEmail()), currentUser.getName(), inviteLink);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.airbyte.api.model.generated.UserIdRequestBody;
import io.airbyte.api.model.generated.UserInvitationCreateRequestBody;
import io.airbyte.api.model.generated.UserInvitationRead;
import io.airbyte.api.model.generated.UserRead;
import io.airbyte.commons.server.handlers.UserHandler;
import io.airbyte.config.InvitationStatus;
import io.airbyte.config.User;
import io.airbyte.config.UserInvitation;
import io.airbyte.config.persistence.ConfigNotFoundException;
import io.airbyte.data.services.UserInvitationService;
Expand All @@ -42,9 +40,6 @@ public class UserInvitationHandlerTest {
@Mock(strictness = LENIENT)
UserInvitationMapper mapper;

@Mock(strictness = LENIENT)
UserHandler userHandler;

@Mock(strictness = LENIENT)
CustomerIoEmailNotificationSender customerIoEmailNotificationSender;

Expand All @@ -55,37 +50,36 @@ public class UserInvitationHandlerTest {

@BeforeEach
void setup() {
handler = new UserInvitationHandler(service, mapper, userHandler, customerIoEmailNotificationSender, webUrlHelper);
handler = new UserInvitationHandler(service, mapper, customerIoEmailNotificationSender, webUrlHelper);
}

@Test
public void testCreateUserInvitation() throws JsonValidationException, ConfigNotFoundException, IOException {
// mocked data
UserInvitationCreateRequestBody req = new UserInvitationCreateRequestBody();
req.setInvitedEmail("test@example.com");
User currentUser = new User();
UUID currentUserId = UUID.randomUUID();
currentUser.setUserId(currentUserId);
currentUser.setName("inviterName");
UserInvitation saved = new UserInvitation();
saved.setInviteCode("randomCode");
saved.setInviterUserId(currentUserId);
saved.setStatus(InvitationStatus.PENDING);
UserRead currentUser = new UserRead();
currentUser.setName("inviterName");
UserInvitationRead expected = new UserInvitationRead();
expected.setInviteCode(saved.getInviteCode());
expected.setInvitedEmail(req.getInvitedEmail());
expected.setInviterUserId(currentUserId);

when(mapper.toDomain(req)).thenReturn(new UserInvitation());
when(service.createUserInvitation(any(UserInvitation.class))).thenReturn(saved);
when(userHandler.getUser(any(UserIdRequestBody.class))).thenReturn(currentUser);
when(webUrlHelper.getBaseUrl()).thenReturn("cloud.airbyte.com");
when(mapper.toApi(saved)).thenReturn(expected);

final UserInvitationRead result = handler.create(req, currentUserId);
final UserInvitationRead result = handler.create(req, currentUser);

verify(mapper, times(1)).toDomain(req);
verify(service, times(1)).createUserInvitation(any(UserInvitation.class));
verify(userHandler, times(1)).getUser(any(UserIdRequestBody.class));
verify(webUrlHelper, times(1)).getBaseUrl();
verify(customerIoEmailNotificationSender, times(1))
.sendInviteToUser(any(CustomerIoEmailConfig.class), anyString(), anyString());
Expand Down

0 comments on commit 8cae4cb

Please sign in to comment.