Files
crewli/apps/app/src/composables/api/useCrowdLists.ts
bert.hausmans 7932e53daf security: A01-13 — nest all event routes under organisation prefix
Move all authenticated organiser-facing event sub-resource routes from
/events/{event}/... to /organisations/{organisation}/events/{event}/...
to enforce multi-tenancy at the routing layer.

Changes:
- Routes: restructured api.php to nest all event sub-resources under
  the existing organisation prefix group
- Controllers: added Organisation parameter and VerifiesOrganisationEvent
  trait to all 12 affected controllers (sections, time-slots, shifts,
  persons, crowd-lists, locations, shift-assignments, registration-fields,
  availabilities, field-values, section-preferences, stats)
- Tests: updated all 20 feature test files with new route paths
- Frontend: updated 8 API composables and 20 Vue components/pages
- API.md: updated documentation to reflect new route structure

Portal routes, public routes (volunteer-register), and invitation routes
remain unchanged as they operate without organisation context.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 08:16:36 +02:00

160 lines
4.8 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
import type { Ref } from 'vue'
import { apiClient } from '@/lib/axios'
import type { CrowdList, CreateCrowdListDto, UpdateCrowdListDto } from '@/types/crowdList'
import type { Person } from '@/types/person'
interface ApiResponse<T> {
success: boolean
data: T
message?: string
}
interface PaginatedResponse<T> {
data: T[]
links: Record<string, string | null>
meta: {
current_page: number
per_page: number
total: number
last_page: number
}
}
export function useCrowdLists(orgId: Ref<string>, eventId: Ref<string>) {
return useQuery({
queryKey: ['crowd-lists', eventId],
queryFn: async () => {
const { data } = await apiClient.get<{ data: CrowdList[] }>(
`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists`,
)
return data.data
},
enabled: () => !!orgId.value && !!eventId.value,
})
}
export function useCrowdListPersons(orgId: Ref<string>, eventId: Ref<string>, crowdListId: Ref<string>) {
return useQuery({
queryKey: ['crowd-lists', eventId, 'persons', crowdListId],
queryFn: async () => {
const { data } = await apiClient.get<PaginatedResponse<Person>>(
`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${crowdListId.value}/persons`,
)
return data
},
enabled: () => !!eventId.value && !!crowdListId.value,
})
}
export function useCreateCrowdList(orgId: Ref<string>, eventId: Ref<string>) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (payload: CreateCrowdListDto) => {
const { data } = await apiClient.post<ApiResponse<CrowdList>>(
`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists`,
payload,
)
return data.data
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] })
},
})
}
export function useUpdateCrowdList(orgId: Ref<string>, eventId: Ref<string>) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ id, ...payload }: UpdateCrowdListDto & { id: string }) => {
const { data } = await apiClient.put<ApiResponse<CrowdList>>(
`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${id}`,
payload,
)
return data.data
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] })
},
})
}
export function useDeleteCrowdList(orgId: Ref<string>, eventId: Ref<string>) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (id: string) => {
await apiClient.delete(`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${id}`)
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] })
},
})
}
export function useAddPersonToCrowdList(orgId: Ref<string>, eventId: Ref<string>) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ listId, personId }: { listId: string; personId: string }) => {
const { data } = await apiClient.post<ApiResponse<CrowdList>>(
`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${listId}/persons`,
{ person_id: personId },
)
return data.data
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] })
},
})
}
export function useRemovePersonFromCrowdList(orgId: Ref<string>, eventId: Ref<string>) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ listId, personId }: { listId: string; personId: string }) => {
await apiClient.delete(
`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${listId}/persons/${personId}`,
)
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] })
queryClient.invalidateQueries({
queryKey: ['crowd-lists', eventId.value, 'persons', variables.listId],
})
},
})
}
export function useApproveAllPending(orgId: Ref<string>, eventId: Ref<string>) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (personIds: string[]) => {
const results = await Promise.allSettled(
personIds.map(id =>
apiClient.post<ApiResponse<Person>>(
`/organisations/${orgId.value}/events/${eventId.value}/persons/${id}/approve`,
),
),
)
const approved = results.filter(r => r.status === 'fulfilled').length
return { approved, total: personIds.length }
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] })
queryClient.invalidateQueries({ queryKey: ['persons', eventId.value] })
},
})
}