buster/web/src/api/buster_rest/users/requests.ts

136 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-03-04 11:10:44 +08:00
import { BASE_URL } from '@/api/buster_rest/config';
2025-03-11 12:36:25 +08:00
import type {
OrganizationUser,
BusterUserResponse,
BusterUserFavorite,
BusterUserListItem
} from '@/api/asset_interfaces/users';
import { mainApi } from '../instances';
import { serverFetch } from '../../createServerInstance';
2025-03-20 13:16:42 +08:00
import { ShareAssetType } from '@/api/asset_interfaces/share';
2025-02-15 12:02:25 +08:00
export const getMyUserInfo = async (): Promise<BusterUserResponse> => {
return mainApi.get<BusterUserResponse>(`/users`).then((response) => response.data);
};
export const getMyUserInfo_server = async ({
jwtToken
}: {
jwtToken: string | undefined;
}): Promise<BusterUserResponse | undefined> => {
if (!jwtToken) {
2025-02-19 06:30:38 +08:00
try {
//If Anonymous user, it will fail, so we catch the error and return undefined
const res = await serverFetch<BusterUserResponse>(`/users`, {
method: 'GET'
});
return res;
} catch (error) {
return undefined;
}
}
//use fetch instead of serverFetch because...
return fetch(`${BASE_URL}/users`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwtToken}`
}
})
.then((response) => {
2025-02-18 08:30:05 +08:00
if (!response.ok) {
return undefined;
}
return response.json();
})
.catch((error) => {
return undefined;
});
};
export const getUser = async ({ userId }: { userId: string }) => {
return mainApi.get<OrganizationUser>(`/users/${userId}`).then((response) => response.data);
};
export const getUser_server = async ({ userId }: { userId: string }) => {
2025-02-15 12:20:22 +08:00
return serverFetch<OrganizationUser>(`/users/${userId}`);
};
export const updateOrganizationUser = async ({
userId,
...params
}: {
userId: string;
name?: string;
2025-02-18 05:42:24 +08:00
role?: OrganizationUser['role'];
}) => {
return mainApi
.put<OrganizationUser>(`/users/${userId}`, params)
.then((response) => response.data);
};
2025-02-18 06:03:41 +08:00
export const inviteUser = async ({
emails,
team_ids
}: {
emails: string[];
team_ids?: string[];
}) => {
2025-03-14 04:31:54 +08:00
return mainApi.post<null>(`/users/invite`, {
2025-02-18 06:03:41 +08:00
emails,
team_ids
});
};
2025-03-11 12:36:25 +08:00
2025-03-14 04:31:54 +08:00
//USER FAVORITES
2025-03-11 12:36:25 +08:00
export const getUserFavorites = async () => {
return mainApi.get<BusterUserFavorite[]>(`/users/favorites`).then((response) => response.data);
};
export const getUserFavorites_server = async () => {
return serverFetch<BusterUserFavorite[]>(`/users/favorites`);
};
2025-04-09 23:54:40 +08:00
export const createUserFavorite = async (
payload: {
id: string;
asset_type: ShareAssetType;
index?: number;
name: string; //just used for the UI for optimistic update
}[]
) => {
2025-03-11 12:36:25 +08:00
return mainApi
.post<BusterUserFavorite[]>(`/users/favorites`, payload)
.then((response) => response.data);
};
2025-04-09 23:54:40 +08:00
export const deleteUserFavorite = async (data: string[]) => {
2025-03-11 12:36:25 +08:00
return mainApi
2025-04-09 23:54:40 +08:00
.delete<BusterUserFavorite[]>(`/users/favorites`, { data })
2025-03-11 12:36:25 +08:00
.then((response) => response.data);
};
2025-03-14 04:31:54 +08:00
export const updateUserFavorites = async (payload: string[]) => {
2025-03-11 12:36:25 +08:00
return mainApi
.put<BusterUserFavorite[]>(`/users/favorites`, payload)
.then((response) => response.data);
};
2025-03-14 04:31:54 +08:00
//USER LIST
2025-03-20 01:41:41 +08:00
export const getUserList = async (payload: {
team_id: string;
page?: number;
page_size?: number;
}) => {
2025-03-11 12:36:25 +08:00
return mainApi
2025-03-14 04:31:54 +08:00
.get<BusterUserListItem[]>(`/users`, { params: payload })
2025-03-11 12:36:25 +08:00
.then((response) => response.data);
};
2025-03-20 01:41:41 +08:00
export const getUserList_server = async (payload: Parameters<typeof getUserList>[0]) => {
2025-03-14 04:31:54 +08:00
return serverFetch<BusterUserListItem[]>(`/users`, { params: payload });
2025-03-11 12:36:25 +08:00
};