123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- import { WEBUI_API_BASE_URL } from '$lib/constants';
- import { getUserPosition } from '$lib/utils';
- export const getUserGroups = async (token: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/groups`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getUserDefaultPermissions = async (token: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/default/permissions`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const updateUserDefaultPermissions = async (token: string, permissions: object) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/default/permissions`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- },
- body: JSON.stringify({
- ...permissions
- })
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const updateUserRole = async (token: string, id: string, role: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- },
- body: JSON.stringify({
- id: id,
- role: role
- })
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getUsers = async (
- token: string,
- query?: string,
- orderBy?: string,
- direction?: string,
- page = 1
- ) => {
- let error = null;
- let res = null;
- let searchParams = new URLSearchParams();
- searchParams.set('page', `${page}`);
- if (query) {
- searchParams.set('query', query);
- }
- if (orderBy) {
- searchParams.set('order_by', orderBy);
- }
- if (direction) {
- searchParams.set('direction', direction);
- }
- res = await fetch(`${WEBUI_API_BASE_URL}/users/?${searchParams.toString()}`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getAllUsers = async (token: string) => {
- let error = null;
- let res = null;
- res = await fetch(`${WEBUI_API_BASE_URL}/users/all`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getUserSettings = async (token: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const updateUserSettings = async (token: string, settings: object) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/settings/update`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- },
- body: JSON.stringify({
- ...settings
- })
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getUserById = async (token: string, userId: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getUserInfo = async (token: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const updateUserInfo = async (token: string, info: object) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/user/info/update`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- },
- body: JSON.stringify({
- ...info
- })
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const getAndUpdateUserLocation = async (token: string) => {
- const location = await getUserPosition().catch((err) => {
- console.error(err);
- return null;
- });
- if (location) {
- await updateUserInfo(token, { location: location });
- return location;
- } else {
- console.info('Failed to get user location');
- return null;
- }
- };
- export const getUserActiveStatusById = async (token: string, userId: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/active`, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- export const deleteUserById = async (token: string, userId: string) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, {
- method: 'DELETE',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- }
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
- type UserUpdateForm = {
- role: string;
- profile_image_url: string;
- email: string;
- name: string;
- password: string;
- };
- export const updateUserById = async (token: string, userId: string, user: UserUpdateForm) => {
- let error = null;
- const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}/update`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`
- },
- body: JSON.stringify({
- profile_image_url: user.profile_image_url,
- role: user.role,
- email: user.email,
- name: user.name,
- password: user.password !== '' ? user.password : undefined
- })
- })
- .then(async (res) => {
- if (!res.ok) throw await res.json();
- return res.json();
- })
- .catch((err) => {
- console.error(err);
- error = err.detail;
- return null;
- });
- if (error) {
- throw error;
- }
- return res;
- };
|