2023-07-20 19:17:55 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-07-06 20:01:23 +03:00
|
|
|
import { AxiosInstance } from "axios";
|
|
|
|
|
2023-08-29 16:52:22 +03:00
|
|
|
import { BrainRoleType } from "@/lib/components/BrainUsers/types";
|
2023-07-20 19:17:55 +03:00
|
|
|
import {
|
|
|
|
BackendMinimalBrainForUser,
|
|
|
|
Brain,
|
|
|
|
MinimalBrainForUser,
|
2023-09-21 10:35:53 +03:00
|
|
|
PublicBrain,
|
2023-07-20 19:17:55 +03:00
|
|
|
} from "@/lib/context/BrainProvider/types";
|
2023-07-06 20:01:23 +03:00
|
|
|
import { Document } from "@/lib/types/Document";
|
|
|
|
|
2023-07-26 00:12:46 +03:00
|
|
|
import {
|
|
|
|
CreateBrainInput,
|
|
|
|
SubscriptionUpdatableProperties,
|
|
|
|
UpdateBrainInput,
|
|
|
|
} from "./types";
|
2023-07-20 19:17:55 +03:00
|
|
|
import { mapBackendMinimalBrainToMinimalBrain } from "./utils/mapBackendMinimalBrainToMinimalBrain";
|
|
|
|
import {
|
|
|
|
BackendSubscription,
|
|
|
|
mapSubscriptionToBackendSubscription,
|
|
|
|
} from "./utils/mapSubscriptionToBackendSubscription";
|
|
|
|
import { mapSubscriptionUpdatablePropertiesToBackendSubscriptionUpdatableProperties } from "./utils/mapSubscriptionUpdatablePropertiesToBackendSubscriptionUpdatableProperties";
|
|
|
|
|
2023-07-06 20:01:23 +03:00
|
|
|
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 (
|
2023-07-25 13:08:08 +03:00
|
|
|
brain: CreateBrainInput,
|
2023-07-07 13:56:08 +03:00
|
|
|
axiosInstance: AxiosInstance
|
2023-07-18 15:30:19 +03:00
|
|
|
): Promise<MinimalBrainForUser> => {
|
2023-07-20 19:17:55 +03:00
|
|
|
return mapBackendMinimalBrainToMinimalBrain(
|
2023-07-25 13:08:08 +03:00
|
|
|
(await axiosInstance.post<BackendMinimalBrainForUser>(`/brains/`, brain))
|
2023-07-20 19:17:55 +03:00
|
|
|
.data
|
|
|
|
);
|
2023-07-07 13:56:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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> => {
|
2023-07-20 19:17:55 +03:00
|
|
|
return mapBackendMinimalBrainToMinimalBrain(
|
|
|
|
(await axiosInstance.get<BackendMinimalBrainForUser>(`/brains/default/`))
|
|
|
|
.data
|
|
|
|
);
|
2023-07-07 13:56:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getBrains = async (
|
|
|
|
axiosInstance: AxiosInstance
|
2023-07-18 15:30:19 +03:00
|
|
|
): Promise<MinimalBrainForUser[]> => {
|
2023-07-20 19:17:55 +03:00
|
|
|
const { brains } = (
|
|
|
|
await axiosInstance.get<{ brains: BackendMinimalBrainForUser[] }>(
|
|
|
|
`/brains/`
|
|
|
|
)
|
2023-07-18 15:30:19 +03:00
|
|
|
).data;
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-07-20 19:17:55 +03:00
|
|
|
return brains.map(mapBackendMinimalBrainToMinimalBrain);
|
2023-07-07 13:56:08 +03:00
|
|
|
};
|
2023-07-12 16:45:45 +03:00
|
|
|
|
2023-07-20 19:17:55 +03:00
|
|
|
export type Subscription = { email: string; role: 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-20 19:17:55 +03:00
|
|
|
await axiosInstance.post(
|
|
|
|
`/brains/${brainId}/subscription`,
|
|
|
|
subscriptions.map(mapSubscriptionToBackendSubscription)
|
|
|
|
);
|
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-20 19:17:55 +03:00
|
|
|
const brainsUsers = (
|
|
|
|
await axiosInstance.get<BackendSubscription[]>(`/brains/${brainId}/users`)
|
|
|
|
).data;
|
2023-07-18 15:30:19 +03:00
|
|
|
|
2023-07-20 19:17:55 +03:00
|
|
|
return brainsUsers.map((brainUser) => ({
|
|
|
|
email: brainUser.email,
|
|
|
|
role: brainUser.rights,
|
|
|
|
}));
|
2023-07-18 15:30:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const updateBrainAccess = async (
|
|
|
|
brainId: string,
|
|
|
|
userEmail: string,
|
|
|
|
subscription: SubscriptionUpdatableProperties,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
|
|
|
await axiosInstance.put(`/brains/${brainId}/subscription`, {
|
2023-07-20 19:17:55 +03:00
|
|
|
...mapSubscriptionUpdatablePropertiesToBackendSubscriptionUpdatableProperties(
|
|
|
|
subscription
|
|
|
|
),
|
2023-07-18 15:30:19 +03:00
|
|
|
email: userEmail,
|
|
|
|
});
|
|
|
|
};
|
2023-07-25 13:08:08 +03:00
|
|
|
|
|
|
|
export const setAsDefaultBrain = async (
|
|
|
|
brainId: string,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
2023-07-25 16:22:17 +03:00
|
|
|
await axiosInstance.post(`/brains/${brainId}/default`);
|
2023-07-25 13:08:08 +03:00
|
|
|
};
|
2023-07-26 00:12:46 +03:00
|
|
|
|
|
|
|
export const updateBrain = async (
|
|
|
|
brainId: string,
|
|
|
|
brain: UpdateBrainInput,
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<void> => {
|
|
|
|
await axiosInstance.put(`/brains/${brainId}/`, brain);
|
|
|
|
};
|
2023-09-21 10:35:53 +03:00
|
|
|
|
|
|
|
export const getPublicBrains = async (
|
|
|
|
axiosInstance: AxiosInstance
|
|
|
|
): Promise<PublicBrain[]> => {
|
|
|
|
return (await axiosInstance.get<PublicBrain[]>(`/brains/public`)).data;
|
|
|
|
};
|