import { BASE_URL } from '@/api/buster_rest/config'; import type { OrganizationUser, BusterUserResponse, BusterUserFavorite, BusterUserListItem } from '@/api/asset_interfaces/users'; import { mainApi } from '../instances'; import { serverFetch } from '../../createServerInstance'; import { ShareAssetType } from '@/api/asset_interfaces/share'; export const getMyUserInfo = async (): Promise => { return mainApi.get(`/users`).then((response) => response.data); }; export const getMyUserInfo_server = async ({ jwtToken }: { jwtToken: string | undefined; }): Promise => { if (!jwtToken) { //If Anonymous user, it will fail, so we catch the error and return undefined return await serverFetch(`/users`, { method: 'GET' }); } //use fetch instead of serverFetch because... return fetch(`${BASE_URL}/users`, { method: 'GET', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${jwtToken}` } }).then(async (response) => { if (!response.ok) { const errorData = await response.json().catch(() => null); throw { status: response.status, statusText: response.statusText, ...errorData }; } return response.json(); }); }; export const getUser = async ({ userId }: { userId: string }) => { return mainApi.get(`/users/${userId}`).then((response) => response.data); }; export const getUser_server = async ({ userId }: { userId: string }) => { return serverFetch(`/users/${userId}`); }; export const updateOrganizationUser = async ({ userId, ...params }: { userId: string; name?: string; role?: OrganizationUser['role']; }) => { return mainApi .put(`/users/${userId}`, params) .then((response) => response.data); }; export const inviteUser = async ({ emails, team_ids }: { emails: string[]; team_ids?: string[]; }) => { return mainApi.post(`/users/invite`, { emails, team_ids }); }; //USER FAVORITES export const getUserFavorites = async () => { return mainApi.get(`/users/favorites`).then((response) => response.data); }; export const getUserFavorites_server = async () => { return serverFetch(`/users/favorites`); }; export const createUserFavorite = async ( payload: { id: string; asset_type: ShareAssetType; index?: number; name: string; //just used for the UI for optimistic update }[] ) => { return mainApi .post(`/users/favorites`, payload) .then((response) => response.data); }; export const deleteUserFavorite = async (data: string[]) => { return mainApi .delete(`/users/favorites`, { data }) .then((response) => response.data); }; export const updateUserFavorites = async (payload: string[]) => { return mainApi .put(`/users/favorites`, payload) .then((response) => response.data); }; //USER LIST export const getUserList = async (payload: { team_id: string; page?: number; page_size?: number; }) => { return mainApi .get(`/users`, { params: payload }) .then((response) => response.data); }; export const getUserList_server = async (payload: Parameters[0]) => { return serverFetch(`/users`, { params: payload }); };