Skip to content

Commit

Permalink
feat(clerk-js,shared): Introduce OrganizationMembershipRequest and me…
Browse files Browse the repository at this point in the history
…mbershipRequests within useOrganization (#1572)

* feat(clerk-js): Introduce OrganizationMembershipRequest resource

* feat(shared): Introduce membershipRequests in useOrganization

* test(clerk-js): Update snapshots of Organization and OrganizationMembership

* test(clerk-js): Add snapshots test for OrganizationMembershipRequest

* chore(clerk-js): Add changeset
  • Loading branch information
panteliselef authored Aug 10, 2023
1 parent 3158752 commit 96cc192
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 75 deletions.
9 changes: 9 additions & 0 deletions .changeset/eighty-planets-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@clerk/clerk-js': patch
'@clerk/shared': patch
'@clerk/types': patch
---

Introduces a new resource called OrganizationMembership

+ useOrganization has been updated in order to return a list of domain with the above type
27 changes: 27 additions & 0 deletions packages/clerk-js/src/core/resources/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ClerkResourceReloadParams,
CreateOrganizationParams,
GetDomainsParams,
GetMembershipRequestParams,
GetMembershipsParams,
GetPendingInvitationsParams,
InviteMemberParams,
Expand All @@ -13,6 +14,8 @@ import type {
OrganizationInvitationJSON,
OrganizationJSON,
OrganizationMembershipJSON,
OrganizationMembershipRequestJSON,
OrganizationMembershipRequestResource,
OrganizationResource,
SetOrganizationLogoParams,
UpdateMembershipParams,
Expand All @@ -23,6 +26,7 @@ import { unixEpochToDate } from '../../utils/date';
import { convertPageToOffset } from '../../utils/pagesToOffset';
import { BaseResource, OrganizationInvitation, OrganizationMembership } from './internal';
import { OrganizationDomain } from './OrganizationDomain';
import { OrganizationMembershipRequest } from './OrganizationMembershipRequest';

export class Organization extends BaseResource implements OrganizationResource {
pathRoot = '/organizations';
Expand Down Expand Up @@ -114,6 +118,29 @@ export class Organization extends BaseResource implements OrganizationResource {
return new OrganizationDomain(json);
};

getMembershipRequests = async (
getRequestParam?: GetMembershipRequestParams,
): Promise<ClerkPaginatedResponse<OrganizationMembershipRequestResource>> => {
return await BaseResource._fetch({
path: `/organizations/${this.id}/membership_requests`,
method: 'GET',
search: convertPageToOffset(getRequestParam) as any,
})
.then(res => {
const { data: requests, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationMembershipRequestJSON>;

return {
total_count,
data: requests.map(request => new OrganizationMembershipRequest(request)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
};

createDomain = async (name: string): Promise<OrganizationDomainResource> => {
return OrganizationDomain.create(this.id, { name });
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { OrganizationMembershipRequest } from './internal';

describe('OrganizationMembership', () => {
it('has the same initial properties', () => {
const organizationMembershipRequest = new OrganizationMembershipRequest({
object: 'organization_membership_request',
id: 'test_id',
organization_id: 'test_org_id',
status: 'pending',
created_at: 12345,
updated_at: 5678,
public_user_data: {
object: 'public_user_data',
first_name: 'test_first_name',
last_name: 'test_last_name',
profile_image_url: 'test_url',
image_url: 'https://clerk.com',
identifier: 'test@identifier.gr',
id: 'test_user_id',
has_image: true,
},
});

expect(organizationMembershipRequest).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { OrganizationInvitationStatus, OrganizationMembershipRequestResource, PublicUserData } from '@clerk/types';
import type { OrganizationMembershipRequestJSON } from '@clerk/types';

import { BaseResource } from './Base';

export class OrganizationMembershipRequest extends BaseResource implements OrganizationMembershipRequestResource {
id!: string;
organizationId!: string;
status!: OrganizationInvitationStatus;
publicUserData!: PublicUserData;
createdAt!: Date;
updatedAt!: Date;

constructor(data: OrganizationMembershipRequestJSON) {
super();
this.fromJSON(data);
}

accept = async (): Promise<OrganizationMembershipRequestResource> => {
return await this._basePost({
path: `/organizations/${this.organizationId}/membership_requests/${this.id}/accept`,
});
};

protected fromJSON(data: OrganizationMembershipRequestJSON | null): this {
if (data) {
this.id = data.id;
this.organizationId = data.organization_id;
this.status = data.status;
if (data.public_user_data) {
this.publicUserData = {
firstName: data.public_user_data.first_name,
lastName: data.public_user_data.last_name,
profileImageUrl: data.public_user_data.profile_image_url,
imageUrl: data.public_user_data.image_url,
hasImage: data.public_user_data.has_image,
identifier: data.public_user_data.identifier,
userId: data.public_user_data.user_id,
};
}
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Organization {
"destroy": [Function],
"getDomain": [Function],
"getDomains": [Function],
"getMembershipRequests": [Function],
"getMemberships": [Function],
"getPendingInvitations": [Function],
"hasImage": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ OrganizationMembership {
"destroy": [Function],
"getDomain": [Function],
"getDomains": [Function],
"getMembershipRequests": [Function],
"getMemberships": [Function],
"getPendingInvitations": [Function],
"hasImage": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`OrganizationMembership has the same initial properties 1`] = `
OrganizationMembershipRequest {
"accept": [Function],
"id": "test_id",
"organizationId": "test_org_id",
"pathRoot": "",
"publicUserData": {
"firstName": "test_first_name",
"hasImage": true,
"identifier": "test@identifier.gr",
"imageUrl": "https://clerk.com",
"lastName": "test_last_name",
"profileImageUrl": "test_url",
"userId": undefined,
},
"status": "pending",
}
`;
1 change: 1 addition & 0 deletions packages/clerk-js/src/core/resources/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './Organization';
export * from './OrganizationDomain';
export * from './OrganizationInvitation';
export * from './OrganizationMembership';
export * from './OrganizationMembershipRequest';
export * from './SamlAccount';
export * from './Session';
export * from './SessionWithActivities';
Expand Down
Loading

0 comments on commit 96cc192

Please sign in to comment.