mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-23 04:19:48 +00:00
93 lines
2.0 KiB
TypeScript
93 lines
2.0 KiB
TypeScript
// Types
|
|
import { IUser } from '@type/users';
|
|
import { IDataState } from '@type/common';
|
|
|
|
// Services
|
|
import supabase from './supabaseService';
|
|
|
|
const USERS_TABLE = 'users';
|
|
const ERROR_FETCHING = "Users can't be loaded!";
|
|
const ERROR_CREATE_USER = "Can't create user!";
|
|
const ERROR_UPDATE_USER = "Can't update user!";
|
|
|
|
/**
|
|
* Create user to the database
|
|
* @param user The user object need to be created
|
|
*/
|
|
const createUser = async (user: IUser): Promise<IUser> => {
|
|
const { data, error } = await supabase
|
|
.from(USERS_TABLE)
|
|
.insert(user)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) {
|
|
console.error(error.message);
|
|
throw new Error(ERROR_CREATE_USER);
|
|
}
|
|
|
|
return data;
|
|
};
|
|
|
|
/**
|
|
* Update the user to the database
|
|
* @param user The user object need to be updated
|
|
*/
|
|
const updateUser = async (user: IUser): Promise<IUser> => {
|
|
const { data, error } = await supabase
|
|
.from(USERS_TABLE)
|
|
.update(user)
|
|
.eq('id', user.id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) {
|
|
console.error(error.message);
|
|
throw new Error(ERROR_UPDATE_USER);
|
|
}
|
|
|
|
return data;
|
|
};
|
|
|
|
/**
|
|
* Return data of users from database
|
|
* @param sortBy Sort by column
|
|
* @param orderBy Order by ascending or descending
|
|
* @param phoneSearch The phone need to be search
|
|
* @returns The data of users from database
|
|
*/
|
|
const getAllUsers = async (
|
|
sortBy: string,
|
|
orderBy: string,
|
|
phoneSearch: string
|
|
): Promise<IUser[]> => {
|
|
const { data, error } = await supabase
|
|
.from(USERS_TABLE)
|
|
.select('*')
|
|
.order(sortBy, { ascending: orderBy === 'asc' })
|
|
.ilike('phone', `%${phoneSearch}%`);
|
|
|
|
if (error) {
|
|
console.error(error.message);
|
|
throw new Error(ERROR_FETCHING);
|
|
}
|
|
|
|
return data;
|
|
};
|
|
|
|
const getUserNotBooked = async (): Promise<IDataState[]> => {
|
|
const { data, error } = await supabase
|
|
.from(USERS_TABLE)
|
|
.select('id, name')
|
|
.eq('isBooked', false);
|
|
|
|
if (error) {
|
|
console.error(error.message);
|
|
throw new Error(ERROR_FETCHING);
|
|
}
|
|
|
|
return data;
|
|
};
|
|
|
|
export { updateUser, createUser, getAllUsers, getUserNotBooked };
|