2023-07-06 20:01:23 +03:00
|
|
|
import { AxiosInstance } from "axios";
|
|
|
|
|
2023-07-12 16:45:45 +03:00
|
|
|
import { BrainRoleType } from "@/lib/components/NavBar/components/NavItems/components/BrainsDropDown/components/BrainActions/types";
|
2023-07-18 15:30:19 +03:00
|
|
|
import { Brain, MinimalBrainForUser } from "@/lib/context/BrainProvider/types";
|
2023-07-06 20:01:23 +03:00
|
|
|
import { Document } from "@/lib/types/Document";
|
|
|
|
|
|
|
|
export const getBrainDocuments = async (
|
|
|
|
brainId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<Document[]> => {
|
|
|
|
const response = await axiosInstance.get<{ documents: Document[] }>(
|
|
|
|
`/explore/?brain_id=${brainId}`
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data.documents;
|
|
|
|
};
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
export const createBrain = async (
|
|
|
|
name: string,
|
|
|
|
axiosInstance: AxiosInstance
|
2023-07-18 15:30:19 +03:00
|
|
|
): Promise<MinimalBrainForUser> => {
|
|
|
|
const createdBrain = (
|
|
|
|
await axiosInstance.post<MinimalBrainForUser>(`/brains/`, { name })
|
|
|
|
).data;
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
return createdBrain;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getBrain = async (
|
|
|
|
brainId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<Brain | undefined> => {
|
|
|
|
const brain = (
|
|
|
|
await axiosInstance.get<Brain | undefined>(`/brains/${brainId}/`)
|
|
|
|
).data;
|
|
|
|
|
|
|
|
return brain;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteBrain = async (
|
|
|
|
brainId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
2023-07-18 10:47:59 +03:00
|
|
|
await axiosInstance.delete(`/brains/${brainId}/subscription`);
|
2023-07-07 13:56:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getDefaultBrain = async (
|
|
|
|
axiosInstance: AxiosInstance
|
2023-07-18 15:30:19 +03:00
|
|
|
): Promise<MinimalBrainForUser | undefined> => {
|
|
|
|
return (await axiosInstance.get<MinimalBrainForUser>(`/brains/default/`))
|
2023-07-07 13:56:08 +03:00
|
|
|
.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getBrains = async (
|
|
|
|
axiosInstance: AxiosInstance
|
2023-07-18 15:30:19 +03:00
|
|
|
): Promise<MinimalBrainForUser[]> => {
|
|
|
|
const brains = (
|
|
|
|
await axiosInstance.get<{ brains: MinimalBrainForUser[] }>(`/brains/`)
|
|
|
|
).data;
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
return brains.brains;
|
|
|
|
};
|
2023-07-12 16:45:45 +03:00
|
|
|
|
2023-07-17 16:45:18 +03:00
|
|
|
export type Subscription = { email: string; rights: BrainRoleType };
|
2023-07-12 16:45:45 +03:00
|
|
|
|
|
|
|
export const addBrainSubscriptions = async (
|
|
|
|
brainId: string,
|
2023-07-17 16:45:18 +03:00
|
|
|
subscriptions: Subscription[],
|
2023-07-12 16:45:45 +03:00
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
2023-07-18 10:47:59 +03:00
|
|
|
await axiosInstance.post(`/brains/${brainId}/subscription`, subscriptions);
|
2023-07-12 16:45:45 +03:00
|
|
|
};
|
2023-07-17 16:45:18 +03:00
|
|
|
|
|
|
|
export const getBrainUsers = async (
|
|
|
|
brainId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<Subscription[]> => {
|
2023-07-18 10:47:59 +03:00
|
|
|
return (await axiosInstance.get<Subscription[]>(`/brains/${brainId}/users`))
|
2023-07-17 16:45:18 +03:00
|
|
|
.data;
|
|
|
|
};
|
2023-07-18 15:30:19 +03:00
|
|
|
|
|
|
|
export type SubscriptionUpdatableProperties = {
|
|
|
|
rights: BrainRoleType | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateBrainAccess = async (
|
|
|
|
brainId: string,
|
|
|
|
userEmail: string,
|
|
|
|
subscription: SubscriptionUpdatableProperties,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
|
|
|
await axiosInstance.put(`/brains/${brainId}/subscription`, {
|
|
|
|
...subscription,
|
|
|
|
email: userEmail,
|
|
|
|
});
|
|
|
|
};
|