228 lines
6.8 KiB
TypeScript
228 lines
6.8 KiB
TypeScript
import { authFetch } from "./client";
|
|
import { cachedGetJson, invalidateApiCache } from "./cache";
|
|
|
|
export interface Workspace {
|
|
id: string;
|
|
name: string;
|
|
description?: string;
|
|
thumbnail?: string | null;
|
|
owner?: string;
|
|
my_role?: 'owner' | 'admin' | 'member' | 'guest';
|
|
[key: string]: any;
|
|
}
|
|
|
|
export interface PaginatedResponse<T> {
|
|
count: number;
|
|
next: string | null;
|
|
previous: string | null;
|
|
results: T[];
|
|
}
|
|
|
|
export interface WorkspaceMembership {
|
|
id: string;
|
|
workspace: string;
|
|
user: {
|
|
id: string;
|
|
email?: string;
|
|
first_name?: string;
|
|
last_name?: string;
|
|
mobile?: string;
|
|
profile_picture?: string | null;
|
|
[key: string]: any;
|
|
};
|
|
role: 'owner' | 'admin' | 'member' | 'guest';
|
|
is_active: boolean;
|
|
joined_at?: string;
|
|
[key: string]: any;
|
|
}
|
|
|
|
|
|
type QueryValue = string | number | boolean | undefined | null;
|
|
|
|
const toQueryString = (params?: Record<string, QueryValue>) => {
|
|
if (!params) return "";
|
|
const query = new URLSearchParams();
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null && value !== "") {
|
|
query.set(key, String(value));
|
|
}
|
|
});
|
|
return query.toString();
|
|
};
|
|
|
|
export const fetchWorkspaces = async (params?: Record<string, QueryValue>): Promise<PaginatedResponse<Workspace>> => {
|
|
const query = toQueryString(params);
|
|
const url = `/api/workspaces/${query ? `?${query}` : ''}`;
|
|
const data = await cachedGetJson<any>(url, {
|
|
ttlMs: 60 * 1000,
|
|
namespaces: ["workspaces"],
|
|
});
|
|
|
|
if (Array.isArray(data)) {
|
|
return { count: data.length, next: null, previous: null, results: data };
|
|
}
|
|
|
|
return {
|
|
count: data.count || data.results?.length || 0,
|
|
next: data.next || null,
|
|
previous: data.previous || null,
|
|
results: data.results || data
|
|
};
|
|
};
|
|
|
|
export const getWorkspace = async (id: string): Promise<Workspace> => {
|
|
const response = await authFetch(`/api/workspaces/${id}/`);
|
|
if (!response.ok) throw new Error("Failed to fetch workspace details");
|
|
return await response.json();
|
|
};
|
|
|
|
export const createWorkspace = async (data: {
|
|
name: string;
|
|
description: string;
|
|
members?: any[];
|
|
thumbnail?: File | null;
|
|
}): Promise<Workspace> => {
|
|
const hasFile = data.thumbnail instanceof File;
|
|
const body = hasFile
|
|
? (() => {
|
|
const formData = new FormData();
|
|
formData.append("name", data.name);
|
|
formData.append("description", data.description);
|
|
if (Array.isArray(data.members)) {
|
|
formData.append("members", JSON.stringify(data.members));
|
|
}
|
|
if (data.thumbnail) {
|
|
formData.append("thumbnail", data.thumbnail);
|
|
}
|
|
return formData;
|
|
})()
|
|
: JSON.stringify({
|
|
name: data.name,
|
|
description: data.description,
|
|
members: data.members,
|
|
});
|
|
|
|
const response = await authFetch('/api/workspaces/', {
|
|
method: 'POST',
|
|
body,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to create workspace');
|
|
}
|
|
const payload = await response.json();
|
|
invalidateApiCache(["workspaces", "workspace-memberships", "reports"]);
|
|
return payload;
|
|
};
|
|
|
|
export const updateWorkspace = async (
|
|
id: string,
|
|
data: {
|
|
name?: string;
|
|
description?: string;
|
|
thumbnail?: File | null;
|
|
clear_thumbnail?: boolean;
|
|
},
|
|
): Promise<Workspace> => {
|
|
const hasFile = data.thumbnail instanceof File;
|
|
const shouldClear = Boolean(data.clear_thumbnail);
|
|
const useForm = hasFile || shouldClear;
|
|
const body = useForm
|
|
? (() => {
|
|
const formData = new FormData();
|
|
if (data.name !== undefined) formData.append("name", data.name);
|
|
if (data.description !== undefined) formData.append("description", data.description);
|
|
if (data.thumbnail) formData.append("thumbnail", data.thumbnail);
|
|
if (shouldClear) formData.append("clear_thumbnail", "true");
|
|
return formData;
|
|
})()
|
|
: JSON.stringify(data);
|
|
|
|
const response = await authFetch(`/api/workspaces/${id}/`, {
|
|
method: 'PATCH',
|
|
body,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to update workspace');
|
|
}
|
|
const payload = await response.json();
|
|
invalidateApiCache(["workspaces"]);
|
|
return payload;
|
|
};
|
|
|
|
export const deleteWorkspace = async (id: string): Promise<void> => {
|
|
const response = await authFetch(`/api/workspaces/${id}/`, {
|
|
method: 'DELETE',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to delete workspace');
|
|
}
|
|
invalidateApiCache(["workspaces", "workspace-memberships", "workspace-rates", "reports"]);
|
|
};
|
|
|
|
export const fetchWorkspaceMemberships = async (params?: Record<string, QueryValue>): Promise<PaginatedResponse<WorkspaceMembership>> => {
|
|
const queryParams = toQueryString(params);
|
|
const data = await cachedGetJson<any>(`/api/workspace-memberships/?${queryParams.toString()}`, {
|
|
ttlMs: 5 * 60 * 1000,
|
|
namespaces: ["workspace-memberships"],
|
|
});
|
|
|
|
if (Array.isArray(data)) {
|
|
return { count: data.length, next: null, previous: null, results: data };
|
|
}
|
|
|
|
return {
|
|
count: data.count || data.results?.length || 0,
|
|
next: data.next || null,
|
|
previous: data.previous || null,
|
|
results: data.results || data
|
|
};
|
|
};
|
|
|
|
export const addWorkspaceMembership = async (data: { workspace: string; user: string; role: string }) => {
|
|
const response = await authFetch(`/api/workspace-memberships/`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to add workspace membership');
|
|
}
|
|
|
|
const payload = await response.json();
|
|
invalidateApiCache(["workspace-memberships", "reports"]);
|
|
return payload;
|
|
};
|
|
|
|
export const removeWorkspaceMembership = async (membershipId: string): Promise<void> => {
|
|
const response = await authFetch(`/api/workspace-memberships/${membershipId}/`, {
|
|
method: 'DELETE',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to remove workspace membership');
|
|
}
|
|
invalidateApiCache(["workspace-memberships", "reports"]);
|
|
};
|
|
|
|
export const updateWorkspaceMembership = async (membershipId: string | number, data: { role: string }) => {
|
|
const response = await authFetch(`/api/workspace-memberships/${membershipId}/`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to update membership');
|
|
}
|
|
|
|
const payload = await response.json();
|
|
invalidateApiCache(["workspace-memberships", "reports"]);
|
|
return payload;
|
|
};
|