Skip to main content

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

getUsersService

Retrieves a paginated list of users with optional filtering.
params
object
Query parameters for filtering and pagination
page
number
Page number (default: 1)
limit
number
Items per page (default: 10)
Search term for filtering users
status
string
Filter by user status
role
string
Filter by user role
site_id
string
Filter by site ID
organization_id
string
Filter by organization ID
data
USER_TYPE[]
Array of user objects
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.
id
string
required
Unique user identifier
data
USER_TYPE
Complete user object with organization and site details
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).
id
string
required
Unique user identifier
data
USER_TYPE
Complete user object
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.
payload
AddUserPayload
required
User creation data
user
object
required
User details object
first_name
string
required
User’s first name
last_name
string
required
User’s last name
email
string
required
User’s email address
role
string
required
User role
status
string
required
User status (e.g., “active”, “inactive”)
phone
string
required
User’s phone number
country
string
required
User’s country
date_of_birth
string
required
User’s date of birth
role_id
string
required
ID of the role to assign
organization
object
required
id
string
required
Organization ID
site
object
required
id
string
required
Site ID
password
string
required
Initial password for the user
response
ApiResponse
Standard API response object
success
boolean
Whether the operation succeeded
statusCode
number
HTTP status code
message
string
Response message
data
object
Created user data
"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.
id
string
required
User ID to update
payload
EditUserPayload
required
Updated user data
user
object
required
User details object (same fields as addUserService, excluding password)
response
ApiResponse
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.
id
string
required
User ID to activate
response
ApiResponse
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.
id
string
required
User ID to delete
response
ApiResponse
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";

Build docs developers (and LLMs) love