g1t/apps/web/app/lib/invites.server.ts
| 1 | import type { Invite, InvitesOverview, User } from "@g1t/contracts"; |
| 2 | |
| 3 | import { identity } from "./services.server"; |
| 4 | |
| 5 | /** What the Invites section's forms answer. */ |
| 6 | export type InviteActionData = { inviteCreated?: Invite; inviteError?: string; inviteRevoked?: boolean } | null; |
| 7 | |
| 8 | /** A person's invites, or null when they cannot make any yet (email unconfirmed). */ |
| 9 | export async function loadInvites(user: User): Promise<InvitesOverview | null> { |
| 10 | if (!user.verified) return null; |
| 11 | return identity.listInvites(user).catch(() => null); |
| 12 | } |
| 13 | |
| 14 | /** Handles the Invites section's intents; null for any other intent. */ |
| 15 | export async function inviteAction(user: User, form: FormData): Promise<InviteActionData | undefined> { |
| 16 | switch (form.get("intent")) { |
| 17 | case "create-invite": { |
| 18 | const email = String(form.get("email") ?? "").trim(); |
| 19 | const workspace = String(form.get("charge") ?? "").trim(); |
| 20 | const result = await identity.createInvite(user, { |
| 21 | email: email || null, |
| 22 | workspace: workspace && workspace !== "mine" ? workspace : null, |
| 23 | }); |
| 24 | return result.ok ? { inviteCreated: result.value } : { inviteError: result.error.message }; |
| 25 | } |
| 26 | case "revoke-invite": { |
| 27 | const result = await identity.revokeInvite(user, String(form.get("id") ?? "")); |
| 28 | return result.ok ? { inviteRevoked: true } : { inviteError: result.error.message }; |
| 29 | } |
| 30 | } |
| 31 | return undefined; |
| 32 | } |