Skip to content

Commit

Permalink
Feat/imp/59/updating user by admin (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaPass authored Oct 18, 2024
1 parent 20e3f8b commit 06ffe1c
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 132 deletions.
11 changes: 9 additions & 2 deletions src/features/profile/actions/ProfileAction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use server';
import { redirect, RedirectType } from 'next/navigation';

import { updateRole } from '@/features/shared/actions/fetchUsers';
import { SupabaseTable } from '@/features/shared/actions/supabaseTables';
import { supabaseClientManager } from '@/lib/SupabaseClientManager';

// import { ProfileType as IProfileType } from '@/features/profile/interfaces/profileResponse';

export const getSession = async () => {
const supabase = supabaseClientManager.getPublicClient();

Expand All @@ -26,20 +25,28 @@ type UpdatedUser = {
first_name?: string;
last_name?: string;
phone?: number;
role?: string;
account_active?: boolean;
};

export const updateUser = async (data: UpdatedUser, id: string) => {
const supabase = supabaseClientManager.getPublicClient();
const phone = data.phone ? data.phone.toString() : null;

const { error } = await supabase
.from(SupabaseTable.PROFILES)
.update({
first_name: data.first_name,
last_name: data.last_name,
account_active: data.account_active,
phone,
})
.eq('id', id);

if (data.role) {
await updateRole({ user_id: id, role_id: data.role });
}

if (error) {
throw new Error('Error updating the user');
}
Expand Down
77 changes: 68 additions & 9 deletions src/features/shared/actions/fetchUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getSession } from '@/features/profile/actions/ProfileAction';
import { handleGetFile } from '@/features/shared/actions/fileAction';
import { SupabaseTable } from '@/features/shared/actions/supabaseTables';
import { filterSupabase, getCurrentUserRole } from '@/features/users/actions/usersAction';
import { PayloadUpdateRole, RolesResponse } from '@/features/users/interfaces/rolesResponse';
import { supabaseClientManager } from '@/lib/SupabaseClientManager';

export interface User {
Expand All @@ -13,7 +14,8 @@ export interface User {
email: string | null;
last_name: string | null;
first_name: string | null;
role: string | null;
role: RolesResponse;
account_active: boolean;
}

export interface PaginatedResponse {
Expand Down Expand Up @@ -44,12 +46,15 @@ type Props = {
queryParams?: QueryParms;
};

export const fetchUser = async (): Promise<User> => {
export const fetchUser = async (id?: string | undefined): Promise<User> => {
const supabase = supabaseClientManager.getPublicClient();
const user = await getSession();

if (user && user.id) {
const { data, error } = await supabase.from(SupabaseTable.PROFILES).select().eq('id', user.id);
if (user && user.id && !id) {
const { data, error } = await supabase
.from(SupabaseTable.USER_HAS_ROLES)
.select('*, user_id!inner(*), role_id!inner(*)')
.eq('user_id', user.id);

if (error) {
throw new Error('Error at getting the user');
Expand All @@ -64,11 +69,38 @@ export const fetchUser = async (): Promise<User> => {
return {
id: user.id,
image_id: image,
phone: data[0]?.phone ?? null,
email: data[0]?.email ?? null,
last_name: data[0]?.last_name,
first_name: data[0]?.first_name,
role: 'operator',
phone: data[0]?.user_id.phone ?? null,
email: data[0]?.user_id.email ?? null,
last_name: data[0]?.user_id.last_name,
first_name: data[0]?.user_id.first_name,
account_active: data[0]?.user_id.account_active,
role: data[0]?.role_id,
};
} else if (id) {
const { data, error } = await supabase
.from(SupabaseTable.USER_HAS_ROLES)
.select('*, user_id!inner(*), role_id!inner(*)')
.eq('user_id', id);

if (error) {
throw new Error('Error at getting the user');
}

let image = data[0]?.image_id ?? null;
if (image) {
image = await handleGetFile(image);
image = image?.path;
}

return {
id: id,
image_id: image,
phone: data[0]?.user_id.phone ?? null,
email: data[0]?.user_id.email ?? null,
last_name: data[0]?.user_id.last_name,
first_name: data[0]?.user_id.first_name,
account_active: data[0]?.user_id.account_active,
role: data[0].role_id,
};
} else {
throw new Error('User not found');
Expand Down Expand Up @@ -130,6 +162,7 @@ export const fetchUsers = async (props?: Props): Promise<PaginatedResponse> => {
email: user.user_id.email,
last_name: user.user_id.last_name,
first_name: user.user_id.first_name,
account_active: user.user_id.account_active,
role: user.role_id.name,
}))
: [];
Expand All @@ -156,3 +189,29 @@ export const fetchUsers = async (props?: Props): Promise<PaginatedResponse> => {

return paginationResponse;
};

export const getRoles = async () => {
const supabase = supabaseClientManager.getPrivateClient();

const { data, error } = await supabase.from('roles').select();
if (error) {
throw new Error('Error getting the roles');
} else {
return data;
}
};

export const updateRole = async ({ user_id, role_id }: PayloadUpdateRole) => {
const supabase = supabaseClientManager.getPrivateClient();

const { error: RolUpdateError } = await supabase
.from(SupabaseTable.USER_HAS_ROLES)
.update({
role_id,
})
.eq('user_id', user_id);

if (RolUpdateError) {
throw new Error('Error updating the role of the user');
}
};
2 changes: 1 addition & 1 deletion src/features/shared/actions/supabaseTables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum SupabaseTable {
ROLES = 'roles',
ROLES_HAS_PERMISSIONS = 'roles_has_permissions',
USER_HAS_ROLES = 'user_has_roles',
USER_HAS_ROLES = 'users_has_roles',
PROFILES = 'profiles',
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import style from '@/features/shared/molecules/btnFormCreateUpdate/btnFormCreate
interface Props {
linkCancel: string;
textCancel: string;
disabledButton: boolean;
disabledButton?: boolean;
textSubmit: string;
}

Expand All @@ -21,7 +21,7 @@ export const BtnFormCreateUpdate = ({ linkCancel, textCancel, disabledButton, te
</Link>
</div>
<div className={style.btnAdd}>
<button type='submit' className={style.addItem} disabled={disabledButton}>
<button type='submit' className={style.addItem} disabled={disabledButton ? disabledButton : false}>
{textSubmit}
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@apply p-2;
}
.containerBtn {
@apply flex justify-center mt-5;
@apply mt-5 flex justify-center;
}
.containerBtn button {
@apply w-[120px] transform transition-all duration-500 active:scale-[98%];
Expand Down
23 changes: 2 additions & 21 deletions src/features/users/actions/adminUserAction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use server';

import { updateRole } from '@/features/shared/actions/fetchUsers';
import { getCurrentUserRole } from '@/features/users/actions/usersAction';
import { supabaseClientManager } from '@/lib/SupabaseClientManager';

Expand All @@ -13,17 +14,6 @@ type NewUserByAdmin = {
account_active: boolean;
};

export const getRoles = async () => {
const supabase = supabaseClientManager.getPrivateClient();

const { data, error } = await supabase.from('roles').select();
if (error) {
throw new Error('Error getting the roles');
} else {
return data;
}
};

export const addNewUserByAdmin = async (props: NewUserByAdmin) => {
const supabase = supabaseClientManager.getPrivateClient();

Expand All @@ -48,16 +38,7 @@ export const addNewUserByAdmin = async (props: NewUserByAdmin) => {
throw new Error('Error creating a new user');
}

const { error: RolUpdateError } = await supabase
.from('users_has_roles')
.update({
role_id: props.role,
})
.eq('user_id', user?.id);

if (RolUpdateError) {
throw new Error('Error updating the role of the user');
}
await updateRole({ user_id: user?.id, role_id: props.role });

if (!props.account_active) {
const { error: SendError } = await supabase.auth.resend({
Expand Down
2 changes: 1 addition & 1 deletion src/features/users/actions/usersAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getCurrentUserRole = async () => {
if (data) {
return data;
} else {
throw new Error('Error at getting the current user');
throw new Error('Error at getting the current user role');
}
};

Expand Down
15 changes: 15 additions & 0 deletions src/features/users/interfaces/IUpdateUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type User = {
first_name: string;
last_name: string;
phone?: number;
email: string;
role: string;
account_active: boolean;
};

export type IUpdateUser = User & {
role: string;
account_active: boolean;
};

export interface UserUpdatePayload extends User {}
5 changes: 5 additions & 0 deletions src/features/users/interfaces/rolesResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ export type Roles = {
};

export interface RolesPayload extends Roles {}

export interface PayloadUpdateRole {
user_id: string | undefined;
role_id: string;
}
2 changes: 1 addition & 1 deletion src/features/users/organisms/UsersList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const UserList = (props: Props) => {
<div className={styleCard.containerInfo}>
{user.first_name ? <h2 className={styleCard.name}>{user.first_name}</h2> : <></>}
<p className='text-md'>Email: {user.email}</p>
<p className='text-md'>Role: {user.role}</p>
<p className='text-md'>Role: {user.role.name}</p>
</div>
}
/>
Expand Down
8 changes: 6 additions & 2 deletions src/features/users/organisms/formCreate/FormCreateUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import { addNewUserByAdmin } from '@/features/users/actions/adminUserAction';
import { activeOptions } from '@/features/users/constants/selectOptionsData';
import { transformToInputOptions } from '@/features/users/helpers/transformToInputOptions';
import { ICreateUser } from '@/features/users/interfaces/ICreateUser';
import { Roles } from '@/features/users/interfaces/rolesResponse';
import { RolesResponse } from '@/features/users/interfaces/rolesResponse';
import { UserPayload } from '@/features/users/interfaces/usersResponse';
import { createUserSchema } from '@/features/users/validations/usersSchema';

import style from './formCreateUser.module.css';

export const FormCreate = ({ roles }: Roles) => {
type Props = {
roles: RolesResponse[];
};

export const FormCreate = ({ roles }: Props) => {
const {
register,
handleSubmit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
.inputBlock {
@apply p-2;
}
.inputBlock input, .inputBlock select {
.inputBlock input,
.inputBlock select {
@apply mt-1 h-12 w-full rounded-lg border-2 border-borderInput bg-bgInputFilter px-2 text-[#292D32] shadow-[0_2px_2px_0px_rgba(0,0,0,0.05)] outline-none;
}
.inputBlock select{
@apply cursor-pointer
.inputBlock select {
@apply cursor-pointer;
}
.inputBlock label {
@apply text-[0.8rem] font-normal text-white;
Expand All @@ -19,8 +20,8 @@
.inputBlock p {
@apply border-none pl-2 pt-1 text-[12px] text-warning;
}
.inputBlock input[type=number]::-webkit-inner-spin-button,
.inputBlock input[type=number]::-webkit-outer-spin-button {
.inputBlock input[type='number']::-webkit-inner-spin-button,
.inputBlock input[type='number']::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
Expand Down
Loading

0 comments on commit 06ffe1c

Please sign in to comment.