All files / src/services membershipRequests.ts

100% Statements 27/27
100% Branches 14/14
100% Functions 9/9
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                  5x     1x       1x       1x               2x     2x                 2x       2x               1x 1x 1x 2x             3x 2x   1x         2x 2x 2x 3x             3x 3x 2x 2x   1x    
/**
 * Membership requests from a flow's public space: the join form on
 * /space/{slug}/join and the approvers' list on the Members page. See
 * docs/architecture/FLOW_SPACE.md, "Membership requests".
 */
import api from '@/services/api'
import type { Entity, EntityField } from '@/stores/entities'
import type { Role } from '@/stores/roles'
 
export type MembershipRequestStatus = 'pending' | 'approved' | 'refused' | 'banned'
 
export interface JoinForm {
  name: string
  defaultLanguage: string
  isMember: boolean
  privacyContact?: string
  fields: EntityField[]
  request: { status: MembershipRequestStatus; createdAt: string; decidedAt?: string } | null
}
 
export interface MembershipRequest {
  userId: string
  status: 'pending' | 'banned'
  name?: string
  nickname?: string
  profilePicture?: string | null
  createdAt: string
  decidedAt?: string
  answers?: Record<string, unknown>
}
 
export type MembershipDecision = 'approve' | 'refuse' | 'ban' | 'unban'
 
const slugPath = (slug: string) => `/v1/spaces/${encodeURIComponent(slug)}/join`
 
export async function getJoinForm(slug: string): Promise<JoinForm> {
  return (await api.get<JoinForm>(slugPath(slug))).data
}
 
export async function requestMembership(slug: string, answers: Record<string, unknown>) {
  await api.post(slugPath(slug), { answers, consent: true })
}
 
export async function withdrawMembershipRequest(slug: string) {
  await api.delete(slugPath(slug))
}
 
export async function listMembershipRequests(
  flowId: string,
  status: 'pending' | 'banned',
  nextToken?: string | null
): Promise<{ requests: MembershipRequest[]; nextToken: string | null }> {
  const response = await api.get(`/v1/flows/${flowId}/membership-requests`, {
    params: { status, ...(nextToken && { nextToken }) }
  })
  return response.data
}
 
export async function decideMembershipRequest(
  flowId: string,
  userId: string,
  decision: MembershipDecision,
  roleIds?: string[]
) {
  const response = await api.post(`/v1/flows/${flowId}/membership-requests/${userId}`, {
    decision,
    ...(roleIds && { roleIds })
  })
  return response.data
}
 
/**
 * Every entity of a flow, read as that flow (which may not be the tab's own):
 * the membership entity must belong to the flow, so linked entities are left out.
 */
export async function fetchOwnEntitiesOf(flowId: string): Promise<Entity[]> {
  const entities: Entity[] = []
  let nextToken: string | null = null
  do {
    const response: { data: { items: Entity[]; nextToken: string | null } } = await api.get(
      '/v1/entities',
      {
        params: { limit: 100, ...(nextToken && { nextToken }) },
        headers: { 'X-Flow-Id': flowId }
      }
    )
    entities.push(...response.data.items.filter((entity) => entity.flowId === flowId))
    nextToken = response.data.nextToken ?? null
  } while (nextToken)
  return entities
}
 
/** Every role of a flow, read as that flow */
export async function fetchRolesOf(flowId: string): Promise<Role[]> {
  const roles: Role[] = []
  let nextToken: string | null = null
  do {
    const response: { data: Role[] | { items: Role[]; nextToken?: string | null } } = await api.get(
      `/v1/flows/${flowId}/roles`,
      {
        params: { limit: 100, ...(nextToken && { nextToken }) },
        headers: { 'X-Flow-Id': flowId }
      }
    )
    const data = response.data
    if (Array.isArray(data)) return data
    roles.push(...data.items)
    nextToken = data.nextToken ?? null
  } while (nextToken)
  return roles
}