From 7bc4691d1316e2577cb5537f8d47026d88c7d78e Mon Sep 17 00:00:00 2001 From: Giannis Katsanos Date: Tue, 1 Nov 2022 12:17:15 +0200 Subject: [PATCH] feat(backend-core): Organization invitation metadata Added support for passing public metadata when creating an organization invitation. --- packages/backend-core/API.md | 3 +++ .../src/__tests__/endpoints/OrganizationApi.test.ts | 5 +++++ packages/backend-core/src/api/endpoints/OrganizationApi.ts | 1 + packages/backend-core/src/api/resources/JSON.ts | 1 + .../backend-core/src/api/resources/OrganizationInvitation.ts | 2 ++ 5 files changed, 12 insertions(+) diff --git a/packages/backend-core/API.md b/packages/backend-core/API.md index 02aed20385..b22cd150ed 100644 --- a/packages/backend-core/API.md +++ b/packages/backend-core/API.md @@ -291,6 +291,8 @@ Create an invitation to join an organization and send an email to the email addr You must pass the ID of the user that invites the new member as `inviterUserId`. The inviter user must be an administrator in the organization. +You can optionally pass metadata for the organization invitation with the `publicMetadata` field. These metadata will be accessible from both the Frontend and the Backend. Once the invitation is accepted, the metadata will be transferred to the newly created organization membership. + Available parameters: - _organizationId_ The unique ID of the organization the invitation is about. @@ -298,6 +300,7 @@ Available parameters: - _role_ The new member's role in the organization. - _inviterUserId_ The ID of the organization administrator that invites the new member. - _redirectUrl_ An optional URL to redirect to after the invited member clicks the link from the invitation email. +- _publicMetadata_ Optionally pass metadata for the organization invitation which will be visible to both your Frontend and Backend. ```js const invitation = await clerkAPI.organizations.createOrganizationInvitation({ diff --git a/packages/backend-core/src/__tests__/endpoints/OrganizationApi.test.ts b/packages/backend-core/src/__tests__/endpoints/OrganizationApi.test.ts index a5b6efe885..db261a4981 100644 --- a/packages/backend-core/src/__tests__/endpoints/OrganizationApi.test.ts +++ b/packages/backend-core/src/__tests__/endpoints/OrganizationApi.test.ts @@ -348,11 +348,13 @@ test('createOrganizationInvitation() creates an invitation for an organization', const status: OrganizationInvitationStatus = 'pending'; const emailAddress = 'invitation@example.com'; const redirectUrl = 'https://example.com'; + const publicMetadata = { foo: 'bar' }; const resJSON: OrganizationInvitationJSON = { object: ObjectType.OrganizationInvitation, id: 'orginv_randomid', role, status, + public_metadata: publicMetadata, email_address: emailAddress, organization_id: organizationId, created_at: 1612378465, @@ -366,6 +368,7 @@ test('createOrganizationInvitation() creates an invitation for an organization', emailAddress, role, redirectUrl, + publicMetadata, inviterUserId: 'user_randomid', }); expect(orgInvitation).toEqual(OrganizationInvitation.fromJSON(resJSON)); @@ -381,6 +384,7 @@ test('getPendingOrganizationInvitationList() returns a list of organization memb email_address: 'invited@example.org', organization_id: organizationId, status: 'pending', + public_metadata: {}, created_at: 1612378465, updated_at: 1612378465, }, @@ -408,6 +412,7 @@ test('revokeOrganizationInvitation() revokes an organization invitation', async email_address: 'invited@example.org', organization_id: organizationId, status: 'revoked', + public_metadata: {}, created_at: 1612378465, updated_at: 1612378465, }; diff --git a/packages/backend-core/src/api/endpoints/OrganizationApi.ts b/packages/backend-core/src/api/endpoints/OrganizationApi.ts index 9d11e9849d..26f9c717bc 100644 --- a/packages/backend-core/src/api/endpoints/OrganizationApi.ts +++ b/packages/backend-core/src/api/endpoints/OrganizationApi.ts @@ -59,6 +59,7 @@ type CreateOrganizationInvitationParams = { inviterUserId: string; emailAddress: string; role: OrganizationMembershipRole; + publicMetadata?: Record; redirectUrl?: string; }; diff --git a/packages/backend-core/src/api/resources/JSON.ts b/packages/backend-core/src/api/resources/JSON.ts index 035e3103b3..a3d9e635c7 100644 --- a/packages/backend-core/src/api/resources/JSON.ts +++ b/packages/backend-core/src/api/resources/JSON.ts @@ -125,6 +125,7 @@ export interface OrganizationJSON extends ClerkResourceJSON { export interface OrganizationInvitationJSON extends ClerkResourceJSON { email_address: string; organization_id: string; + public_metadata: Record; role: OrganizationMembershipRole; status: OrganizationInvitationStatus; created_at: number; diff --git a/packages/backend-core/src/api/resources/OrganizationInvitation.ts b/packages/backend-core/src/api/resources/OrganizationInvitation.ts index d9091d1169..104e7215cc 100644 --- a/packages/backend-core/src/api/resources/OrganizationInvitation.ts +++ b/packages/backend-core/src/api/resources/OrganizationInvitation.ts @@ -9,6 +9,7 @@ export class OrganizationInvitation { readonly organizationId: string, readonly createdAt: number, readonly updatedAt: number, + readonly publicMetadata: Record = {}, readonly status?: OrganizationInvitationStatus, ) {} @@ -20,6 +21,7 @@ export class OrganizationInvitation { data.organization_id, data.created_at, data.updated_at, + data.public_metadata, data.status, ); }