Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/enterprise-web-development.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import { infiniteQueryOptions, queryOptions } from "@tanstack/solid-query";
import { QUERY_BASE, type UserId } from "@/user";
export type Id = string & { __brand: "OrganizationId" };export type OrganizationRole = "MEMBER" | "ADMINISTRATOR" | "OWNER";export type Organization = { id: Id; name: string;};
export type OptimisticOrganization = Omit<Organization, "id">;
export const query = (userId: UserId | undefined, signal?: AbortSignal) => queryOptions({ queryKey: [QUERY_BASE, userId, "organizations"], async queryFn(): Promise<(Organization | OptimisticOrganization)[]> { const response = await fetch(`/api/users/${userId}/organizations`, { method: "GET", signal, });
if (!response.ok) throw new Error( `Error fetching organizations: ${response.status} ${await response.text()}`, );
// Safe cast because parsing requires the expectation we know what the API returns return (await response.json()) as Organization[]; }, enabled: Boolean(userId), });
export async function deleteOrganization(id: Id) { const response = await fetch(`/api/organizations/${id}`, { method: "DELETE", });
if (!response.ok) throw new Error("Failed to delete organization. See network response for more details.");}
export async function createOrganization(userId: UserId, name: string) { //TODO change the endpoint to just accept text/plain with the new organization name as that is all that is required const response = await fetch(`/api/users/${userId}/organizations`, { method: "POST", headers: { "Content-Type": "text/plain", }, body: name, });
if (!response.ok) { // TODO error handling console.error("Error creating organization", await response.text()); throw new Error("Failed to create organization. See console for more details."); }}
export async function updateOrganizationName(userId: UserId, id: Id, name: string) { const response = await fetch(`/api/users/${userId}/organizations/${id}/name`, { method: "PUT", headers: { "Content-Type": "text/plain", }, body: name, });
if (!response.ok) { // TODO error handling console.error("Error updating organization name", await response.text()); throw new Error("Failed to update organization name. See console for more details."); }}
export const isOrganization = ( organization: Organization | OptimisticOrganization,): organization is Organization => "id" in organization;
const ORG_PAGE_SIZE = 20;
export const pagedQuery = (userId: UserId | undefined) => infiniteQueryOptions({ queryKey: [QUERY_BASE, userId, "organizations", "paged"], async queryFn({ pageParam, signal }): Promise<Organization[]> { const response = await fetch( `/api/users/${userId}/organizations?page=${pageParam}&size=${ORG_PAGE_SIZE}`, { method: "GET", signal }, );
if (!response.ok) throw new Error( `Error fetching organizations: ${response.status} ${await response.text()}`, );
return (await response.json()) as Organization[]; }, initialPageParam: 0, getNextPageParam: (lastPage, allPages) => lastPage.length === ORG_PAGE_SIZE ? allPages.length : undefined, enabled: Boolean(userId), });
export async function addMember(organizationId: Id, username: string) { const response = await fetch(`/api/organizations/${organizationId}/members/registrations`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username }), });
if (!response.ok) throw new Error("Failed to add member. See network response for more details.");}
export async function removeMember(organizationId: Id, userId: UserId) { const response = await fetch(`/api/organizations/${organizationId}/members/${userId}`, { method: "DELETE", });
if (!response.ok) throw new Error("Failed to remove member. See network response for more details.");}
export async function changeMemberRole(organizationId: Id, userId: UserId, role: OrganizationRole) { const response = await fetch(`/api/organizations/${organizationId}/members/${userId}/role`, { method: "PUT", headers: { "Content-Type": "text/plain" }, body: role, });
if (!response.ok) throw new Error("Failed to change member role. See network response for more details.");}