|
| 1 | +import type { |
| 2 | + ClerkPaginatedResponse, |
| 3 | + GetUserOrganizationInvitationsParams, |
| 4 | + MembershipRole, |
| 5 | + OrganizationInvitationStatus, |
| 6 | + OrganizationResource, |
| 7 | + UserOrganizationInvitationJSON, |
| 8 | + UserOrganizationInvitationResource, |
| 9 | +} from '@clerk/types'; |
| 10 | + |
| 11 | +import { unixEpochToDate } from '../../utils/date'; |
| 12 | +import { BaseResource, Organization } from './internal'; |
| 13 | + |
| 14 | +export class UserOrganizationInvitation extends BaseResource implements UserOrganizationInvitationResource { |
| 15 | + id!: string; |
| 16 | + emailAddress!: string; |
| 17 | + organization!: OrganizationResource; |
| 18 | + publicMetadata: OrganizationInvitationPublicMetadata = {}; |
| 19 | + status!: OrganizationInvitationStatus; |
| 20 | + role!: MembershipRole; |
| 21 | + createdAt!: Date; |
| 22 | + updatedAt!: Date; |
| 23 | + |
| 24 | + static async retrieve( |
| 25 | + params?: GetUserOrganizationInvitationsParams, |
| 26 | + ): Promise<ClerkPaginatedResponse<UserOrganizationInvitation>> { |
| 27 | + return await BaseResource._fetch({ |
| 28 | + path: '/me/organization_invitations', |
| 29 | + method: 'GET', |
| 30 | + search: params as any, |
| 31 | + }) |
| 32 | + .then(res => { |
| 33 | + const { data: invites, total_count } = |
| 34 | + res?.response as unknown as ClerkPaginatedResponse<UserOrganizationInvitationJSON>; |
| 35 | + |
| 36 | + return { |
| 37 | + total_count, |
| 38 | + data: invites.map(invitation => new UserOrganizationInvitation(invitation)), |
| 39 | + }; |
| 40 | + }) |
| 41 | + .catch(() => ({ |
| 42 | + total_count: 0, |
| 43 | + data: [], |
| 44 | + })); |
| 45 | + } |
| 46 | + |
| 47 | + constructor(data: UserOrganizationInvitationJSON) { |
| 48 | + super(); |
| 49 | + this.fromJSON(data); |
| 50 | + } |
| 51 | + protected fromJSON(data: UserOrganizationInvitationJSON | null): this { |
| 52 | + if (data) { |
| 53 | + this.id = data.id; |
| 54 | + this.emailAddress = data.email_address; |
| 55 | + this.organization = new Organization(data.organization); |
| 56 | + this.publicMetadata = data.public_metadata; |
| 57 | + this.role = data.role; |
| 58 | + this.status = data.status; |
| 59 | + this.createdAt = unixEpochToDate(data.created_at); |
| 60 | + this.updatedAt = unixEpochToDate(data.updated_at); |
| 61 | + } |
| 62 | + return this; |
| 63 | + } |
| 64 | +} |
0 commit comments