Overview
User management actions provide CRUD operations for users, including retrieval, creation, updating, activation, and deletion. All functions are located in src/app/actions/user.ts.
User Operations
Retrieves a paginated list of users with optional filtering.
Query parameters for filtering and pagination Items per page (default: 10)
Search term for filtering users
Filter by organization ID
Server Component
With Filters
Error Handling
import { getUsersService } from "@/app/actions" ;
export default async function UsersPage ({ searchParams }) {
const params = await searchParams ;
const page = parseInt ( String ( params ?. page ?? 1 ), 10 );
const limit = parseInt ( String ( params ?. limit ?? 10 ), 10 );
const search = typeof params ?. search === "string" ? params . search : "" ;
const users = await getUsersService ({
page ,
limit ,
search
});
return < UserTable data ={ users } />;
}
On permission errors (403), this function returns an empty array instead of throwing an error. This allows pages that need user data for dropdowns to still load.
viewUserService
Retrieves a single user by ID.
Complete user object with organization and site details
Basic Usage
With Error Handling
import { viewUserService } from "@/app/actions" ;
const user = await viewUserService ( "user-123" );
console . log ( user . first_name );
console . log ( user . email );
console . log ( user . organization . name );
console . log ( user . site . name );
getUserByIdService
Retrieves a single user by ID (alternative to viewUserService).
import { getUserByIdService } from "@/app/actions" ;
const user = await getUserByIdService ( "user-123" );
This function provides the same functionality as viewUserService. Use either based on your preference.
addUserService
Creates a new user account.
User creation data User details object User status (e.g., “active”, “inactive”)
Initial password for the user
Standard API response object Whether the operation succeeded
"use client" ;
import { addUserService } from "@/app/actions" ;
import { toast } from "sonner" ;
export function AddUserForm () {
const handleSubmit = async ( data : any ) => {
const payload = {
user: {
first_name: data . first_name ,
last_name: data . last_name ,
email: data . email ,
role: data . role ,
status: data . status ,
phone: data . phone ,
country: data . country ,
date_of_birth: data . date_of_birth ,
role_id: data . role_id ,
organization: { id: data . organization_id },
site: { id: data . site_id },
},
password: data . password ,
};
const response = await addUserService ( payload );
if ( response . success ) {
toast . success ( "User added successfully" );
router . push ( "/users" );
} else {
toast . error ( response . message || "Failed to add user" );
}
};
return < form onSubmit ={ handleSubmit }> ...</ form > ;
}
editUserService
Updates an existing user account.
Updated user data User details object (same fields as addUserService, excluding password)
Standard API response object
import { editUserService } from "@/app/actions" ;
import { toast } from "sonner" ;
const handleUpdate = async ( userId : string , data : any ) => {
const payload = {
user: {
first_name: data . first_name ,
last_name: data . last_name ,
email: data . email ,
role: data . role ,
status: data . status ,
phone: data . phone ,
country: data . country ,
date_of_birth: data . date_of_birth ,
role_id: data . role_id ,
organization: { id: data . organization_id },
site: { id: data . site_id },
},
};
const response = await editUserService ( userId , payload );
if ( response . success ) {
toast . success ( "User updated successfully" );
} else {
toast . error ( response . message );
}
};
activateUserService
Activates a user account.
Standard API response object
import { activateUserService } from "@/app/actions" ;
import { toast } from "sonner" ;
const handleActivate = async ( userId : string ) => {
const response = await activateUserService ( userId );
if ( response . success ) {
toast . success ( "User activated successfully" );
} else {
toast . error ( response . message );
}
};
deleteUserService
Deletes a user account.
Standard API response object
import { deleteUserService } from "@/app/actions" ;
import { toast } from "sonner" ;
const handleDelete = async ( userId : string ) => {
const response = await deleteUserService ( userId );
if ( response . success ) {
toast . success ( "User deleted successfully" );
} else {
toast . error ( response . message );
}
};
Deleting a user is permanent and cannot be undone. Consider deactivating users instead of deleting them to maintain data integrity.
Type Definitions
USER_TYPE
The complete user object includes:
interface USER_TYPE {
id : string ;
first_name : string ;
last_name : string ;
email : string ;
phone : string ;
country : string ;
date_of_birth : string ;
role : string ;
role_id : string | null ;
role_obj : unknown ;
status : string ;
password_hash : string ;
organization : {
id : string ;
name : string ;
logo_url : string ;
description : string ;
industry : string ;
team_strength : string ;
created_at : number ;
created_at_datetime : string ;
updated_at : number ;
updated_at_datetime : string ;
};
site : {
id : string ;
name : string ;
tag : string ;
address : string ;
city : string ;
country : string ;
description : string ;
installation_environment : string ;
manager_name : string ;
manager_email : string ;
manager_phone_number : string ;
manager_location : string ;
created_at : number ;
created_at_datetime : string ;
updated_at : number ;
updated_at_datetime : string ;
};
created_at : number ;
created_at_datetime : string ;
updated_at : number ;
updated_at_datetime : string ;
}
Import from @/app/actions/user:
import { USER_TYPE } from "@/app/actions/user" ;
import { ApiResponse } from "@/app/actions" ;