Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

5.6 KB
Raw Blame
import { request } from '@/lib/api/core/client';
import { pageQuery, type Page, type PageParams } from '@/lib/api/core/paging';
import type { MemberRole } from '@/lib/api/endpoints/members';

export type InviteStatus = 'pending' | 'accepted' | 'rejected';

// The rank an invite puts its invitee on in the team. Only a team owner sends one
// that grants 'owner' or 'manager'.
export type InviteTeamRole = 'owner' | 'manager' | 'member';

// An invite as shown to whoever manages a team's or a project's invites: carries the
// token so they can share the link, and who sent it. The token is null where the
// viewer could not have sent the invite.
export interface InviteRow {
  id: number;
  token: string | null;
  email: string;
  teamRole: InviteTeamRole;
  // The project the invitee joins along with the team, or null for an invite into
  // the team alone.
  projectKey: string | null;
  projectName: string | null;
  // Their role in that project. null when the invite names no project.
  role: MemberRole | null;
  // The custom role the invitee joins the project on. null falls back to the team's
  // default role; roleName resolves it for display. A project owner has both null.
  roleId: number | null;
  roleName: string | null;
  status: InviteStatus;
  createdAt: string;
  // When a pending invite stops working; null once it was answered or when invites
  // never expire.
  expiresAt: string | null;
  respondedAt: string | null;
  invitedByName: string | null;
  invitedByEmail: string | null;
}

export interface InviteCreateResult extends InviteRow {
  token: string;
  emailQueued: boolean;
  // A mail provider is set but the email could not go out; the link still works.
  emailFailed: boolean;
}

export interface InviteEmailResult {
  emailQueued: boolean;
}

// An invite as shown to the invitee opening the link: enough team and project
// context to decide, never the internal project id.
export interface InviteView {
  token: string;
  teamName: string;
  teamRef: string;
  projectKey: string | null;
  projectName: string | null;
  email: string;
  teamRole: InviteTeamRole;
  role: MemberRole | null;
  roleId: number | null;
  roleName: string | null;
  status: InviteStatus;
  createdAt: string;
  expired: boolean;
  // Whether the invited email already has an account, so the accept screen can
  // open in sign-in mode instead of registration.
  hasAccount: boolean;
}

// Where an invitee landed once the invite was accepted.
export interface AcceptedInvite {
  teamName: string;
  teamRef: string;
  projectKey: string | null;
  projectName: string | null;
  role: MemberRole | null;
}

// Invites — the managing side: create, list, email, and revoke the invite links of
// a project or of a team. A project invite joins the team as well; a team invite
// names no project.
export const listInvites = (projectKey: string) =>
  request<InviteRow[]>(`/projects/${projectKey}/invites`);

export const createInvite = (
  projectKey: string,
  input: { email: string; role: MemberRole; roleId?: number | null },
) =>
  request<InviteCreateResult>(`/projects/${projectKey}/invites`, {
    method: 'POST',
    body: JSON.stringify(input),
  });

export const sendInviteEmail = (projectKey: string, inviteId: number) =>
  request<InviteEmailResult>(`/projects/${projectKey}/invites/${inviteId}/email`, {
    method: 'POST',
  });

// A renewed invite is a new one under a new link; the expired one is gone.
export const renewInvite = (projectKey: string, inviteId: number) =>
  request<InviteCreateResult>(`/projects/${projectKey}/invites/${inviteId}/renew`, {
    method: 'POST',
  });

export const deleteInvite = (projectKey: string, inviteId: number) =>
  request<void>(`/projects/${projectKey}/invites/${inviteId}`, { method: 'DELETE' });

export const listTeamInvites = (teamId: number) => request<InviteRow[]>(`/teams/${teamId}/invites`);

// Someone an invite into the team can be addressed to: an account of the instance
// outside the team.
export interface InviteCandidate {
  userId: string;
  name: string;
  email: string;
  image: string | null;
}

export const listInviteCandidates = (teamId: number, search: string) =>
  request<InviteCandidate[]>(
    `/teams/${teamId}/invites/candidates?${new URLSearchParams({ search })}`,
  );

export const createTeamInvite = (teamId: number, input: { email: string; role: InviteTeamRole }) =>
  request<InviteCreateResult>(`/teams/${teamId}/invites`, {
    method: 'POST',
    body: JSON.stringify(input),
  });

export const sendTeamInviteEmail = (teamId: number, inviteId: number) =>
  request<InviteEmailResult>(`/teams/${teamId}/invites/${inviteId}/email`, { method: 'POST' });

export const renewTeamInvite = (teamId: number, inviteId: number) =>
  request<InviteCreateResult>(`/teams/${teamId}/invites/${inviteId}/renew`, { method: 'POST' });

export const deleteTeamInvite = (teamId: number, inviteId: number) =>
  request<void>(`/teams/${teamId}/invites/${inviteId}`, { method: 'DELETE' });

// Invites — invitee side: open a link by token, then accept or reject it. The
// session email must match the invite. Accept returns where to go next.
export const getInvite = (token: string) =>
  request<InviteView>(`/invites/${encodeURIComponent(token)}`);

export const acceptInvite = (token: string) =>
  request<AcceptedInvite>(`/invites/${encodeURIComponent(token)}/accept`, { method: 'POST' });

export const rejectInvite = (token: string) =>
  request<void>(`/invites/${encodeURIComponent(token)}/reject`, { method: 'POST' });

export interface ReceivedInvite {
  token: string;
  teamName: string;
  projectName: string | null;
}

export const listReceivedInvites = (params: PageParams) =>
  request<Page<ReceivedInvite>>(`/me/invites${pageQuery(params)}`);