Skip to content

Commit

Permalink
Add accept-invite function (#1180)
Browse files Browse the repository at this point in the history
## Description

The endpoint is still behind a feature flag, but this makes it
available.

## Documentation

Does this require changes to the WorkOS Docs? E.g. the [API
Reference](https://workos.com/docs/reference) or code snippets need
updates.

```
[x] Yes
```

If yes, link a related docs PR and add a docs maintainer as a reviewer.
Their approval is required.

workos/workos#32872
  • Loading branch information
dandorman authored Dec 3, 2024
1 parent 2a2e07c commit f3eb43c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/user-management/interfaces/invitation.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Invitation {
expiresAt: string;
organizationId: string | null;
inviterUserId: string | null;
acceptedUserId: string | null;
token: string;
acceptInvitationUrl: string;
createdAt: string;
Expand All @@ -24,6 +25,7 @@ export interface InvitationEvent {
expiresAt: string;
organizationId: string | null;
inviterUserId: string | null;
acceptedUserId: string | null;
createdAt: string;
updatedAt: string;
}
Expand All @@ -38,6 +40,7 @@ export interface InvitationResponse {
expires_at: string;
organization_id: string | null;
inviter_user_id: string | null;
accepted_user_id: string | null;
token: string;
accept_invitation_url: string;
created_at: string;
Expand All @@ -54,6 +57,7 @@ export interface InvitationEventResponse {
expires_at: string;
organization_id: string | null;
inviter_user_id: string | null;
accepted_user_id: string | null;
created_at: string;
updated_at: string;
}
2 changes: 2 additions & 0 deletions src/user-management/serializers/invitation.serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const deserializeInvitation = (
expiresAt: invitation.expires_at,
organizationId: invitation.organization_id,
inviterUserId: invitation.inviter_user_id,
acceptedUserId: invitation.accepted_user_id,
token: invitation.token,
acceptInvitationUrl: invitation.accept_invitation_url,
createdAt: invitation.created_at,
Expand All @@ -35,6 +36,7 @@ export const deserializeInvitationEvent = (
expiresAt: invitation.expires_at,
organizationId: invitation.organization_id,
inviterUserId: invitation.inviter_user_id,
acceptedUserId: invitation.accepted_user_id,
createdAt: invitation.created_at,
updatedAt: invitation.updated_at,
});
25 changes: 25 additions & 0 deletions src/user-management/user-management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,31 @@ describe('UserManagement', () => {
});
});

describe('acceptInvitation', () => {
it('sends an Accept Invitation request', async () => {
const invitationId = 'invitation_01H5JQDV7R7ATEYZDEG0W5PRYS';
fetchOnce({
...invitationFixture,
state: 'accepted',
accepted_user_id: 'user_01HGK4K4PXNSG85RNNV0GXYP5W',
});

const response = await workos.userManagement.acceptInvitation(
invitationId,
);

expect(fetchURL()).toContain(
`/user_management/invitations/${invitationId}/accept`,
);
expect(response).toMatchObject({
object: 'invitation',
email: '[email protected]',
state: 'accepted',
acceptedUserId: 'user_01HGK4K4PXNSG85RNNV0GXYP5W',
});
});
});

describe('revokeInvitation', () => {
it('send a Revoke Invitation request', async () => {
const invitationId = 'invitation_01H5JQDV7R7ATEYZDEG0W5PRYS';
Expand Down
9 changes: 9 additions & 0 deletions src/user-management/user-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,15 @@ export class UserManagement {
return deserializeInvitation(data);
}

async acceptInvitation(invitationId: string): Promise<Invitation> {
const { data } = await this.workos.post<InvitationResponse, any>(
`/user_management/invitations/${invitationId}/accept`,
null,
);

return deserializeInvitation(data);
}

async revokeInvitation(invitationId: string): Promise<Invitation> {
const { data } = await this.workos.post<InvitationResponse, any>(
`/user_management/invitations/${invitationId}/revoke`,
Expand Down

0 comments on commit f3eb43c

Please sign in to comment.