Skip to main content

Overview

Asset management actions provide CRUD operations for assets and sites, plus analytics endpoints for dashboard metrics. All functions are located in src/app/actions/inventory.ts.

Asset Operations

getAssetsService

Retrieves a paginated list of assets 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 assets
[key: string]
string | number | string[]
Additional filter parameters (e.g., site_id, status)
data
Asset[]
Array of asset objects
meta
AssetsMeta
Pagination metadata
page
number
Current page number
limit
number
Items per page
total
number
Total number of assets
total_pages
number
Total number of pages
has_next
boolean
Whether there is a next page
has_prev
boolean
Whether there is a previous page
"use server";
import { getAssetsService } from "@/app/actions";

export default async function AssetsPage({ 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 { data: assets, meta } = await getAssetsService({ 
    page, 
    limit, 
    search 
  });

  return <AssetTable data={assets} meta={meta} />;
}
On permission errors (403), this function returns an empty data array with zero total instead of throwing an error.

getAssetService

Retrieves a single asset by ID.
id
string
required
Unique asset identifier
data
Asset
Complete asset object with all fields
import { getAssetService } from "@/app/actions";

const asset = await getAssetService("asset-123");

console.log(asset.name);
console.log(asset.serial_number);
console.log(asset.assignee.first_name);

addAssetService

Creates a new asset.
payload
AddAssetPayload
required
Asset creation data
name
string
required
Asset name
tag
string
required
Asset tag/identifier
parent_site
object
required
id
string
required
Parent site ID
type
string
required
Asset type (e.g., “Pump”, “Motor”, “Compressor”)
model_number
string
required
Manufacturer model number
serial_number
string
required
Serial number
criticality_level
string
required
Criticality level (e.g., “high”, “medium”, “low”)
status
string
required
Operational status
assignee
object
required
id
string
required
Assigned user ID
operating_hours
string
required
Current operating hours
commissioned_date
string
required
Date asset was commissioned
maintenance_strategy
string
required
Maintenance strategy
power_rating
string
required
Power rating in Kw
speed
string
required
Speed in RPM
capacity
string
required
Capacity in m3/h
datasheet
object
Datasheet file information
file_url
string
required
File URL
file_name
string
required
File name
uploaded_at
string
required
Upload timestamp
response
ApiResponse
Standard API response object
success
boolean
Whether the operation succeeded
statusCode
number
HTTP status code
message
string
Response message
data
object
Created asset data
"use client";
import { addAssetService } from "@/app/actions";
import { AddAssetPayload } from "@/schema";
import { toast } from "sonner";

export function AddAssetForm() {
  const handleSubmit = async (data: AddAssetPayload) => {
    const payload: AddAssetPayload = {
      name: data.name,
      tag: data.tag,
      parent_site: { id: data.parent_site.id },
      type: data.type,
      model_number: data.model_number,
      serial_number: data.serial_number,
      criticality_level: data.criticality_level,
      operating_hours: data.operating_hours,
      commissioned_date: data.commissioned_date,
      status: data.status,
      maintenance_strategy: data.maintenance_strategy,
      last_performed_maintenance: data.last_performed_maintenance,
      major_overhaul: data.major_overhaul,
      last_date_overhaul: data.last_date_overhaul,
      assignee: { id: data.assignee.id },
      power_rating: data.power_rating,
      speed: data.speed,
      capacity: data.capacity,
    };

    const response = await addAssetService(payload);
    
    if (response.success) {
      toast.success("Asset added successfully");
      router.push("/assets");
    } else {
      toast.error(response.message || "Failed to add asset");
    }
  };

  return <form onSubmit={handleSubmit}>...</form>;
}

editAssetService

Updates an existing asset.
id
string
required
Asset ID to update
payload
AddAssetPayload
required
Updated asset data (same structure as addAssetService)
response
ApiResponse
Standard API response object
import { editAssetService } from "@/app/actions";
import { toast } from "sonner";

const handleUpdate = async (assetId: string, data: AddAssetPayload) => {
  const response = await editAssetService(assetId, data);
  
  if (response.success) {
    toast.success("Asset updated successfully");
  } else {
    toast.error(response.message);
  }
};

deleteAssetService

Deletes an asset.
id
string
required
Asset ID to delete
response
ApiResponse
Standard API response object
import { deleteAssetService } from "@/app/actions";
import { toast } from "sonner";

const handleDelete = async (assetId: string) => {
  const response = await deleteAssetService(assetId);
  
  if (response.success) {
    toast.success("Asset deleted successfully");
  } else {
    toast.error(response.message);
  }
};

Site Operations

getSitesService

Retrieves a paginated list of sites 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
string
Search term
status
string
Filter by status
organization_id
string
Filter by organization
data
Sites[]
Array of site objects
meta
SitesMeta
Pagination metadata (same structure as assets)
import { getSitesService } from "@/app/actions";

const { data: sites, meta } = await getSitesService({
  page: 1,
  limit: 10,
});

getSiteService

Retrieves a single site by ID.
id
string
required
Site ID
data
Sites
Complete site object
import { getSiteService } from "@/app/actions";

const site = await getSiteService("site-123");
console.log(site.name);
console.log(site.organization.name);

addSiteService

Creates a new site.
payload
AddSitesPayload
required
Site creation data
response
ApiResponse
Standard API response object

editSiteService

Updates an existing site.
id
string
required
Site ID to update
payload
EditSitePayload
required
Updated site data
response
ApiResponse
Standard API response object

deleteSiteService

Deletes a site.
id
string
required
Site ID to delete
response
ApiResponse
Standard API response object

Analytics Operations

getAssetsAnalyticsService

Retrieves asset analytics for dashboard metrics.
data
AssetAnalytics | null
Asset analytics data or null on error
total_assets
number
Total number of assets
asset_trend
object
Asset growth trend
growth
number
Number of new assets
percentage
number
Percentage change
critical_assets
object
Critical assets metrics
assets_with_alarms
object
Assets with active alarms
import { getAssetsAnalyticsService } from "@/app/actions";

export default async function Dashboard() {
  const analytics = await getAssetsAnalyticsService();

  if (!analytics) {
    return <div>Analytics unavailable</div>;
  }

  return (
    <div>
      <MetricCard 
        title="Total Assets" 
        value={analytics.total_assets}
        trend={analytics.asset_trend.percentage}
      />
      <MetricCard 
        title="Critical Assets" 
        value={analytics.critical_assets.total}
      />
    </div>
  );
}
Returns null on permission errors (403) or network issues instead of throwing.

getSitesAnalyticsService

Retrieves site analytics for dashboard metrics.
data
SitesAnalytics | null
Site analytics data or null on error
total_sites
number
Total number of sites
sites_trend_percentage
number
Site growth percentage
sites_with_issues
number
Number of sites with issues
issues_trend_percentage
number
Issues trend percentage
import { getSitesAnalyticsService } from "@/app/actions";

const analytics = await getSitesAnalyticsService();

if (analytics) {
  console.log(`${analytics.total_sites} sites`);
  console.log(`${analytics.sites_with_issues} with issues`);
}

Type Definitions

Import types from @/types and schemas from @/schema:
import { Asset, Sites, AssetAnalytics, SitesAnalytics } from "@/types";
import { AddAssetPayload, ADD_ASSET_SCHEMA } from "@/schema";
import { ApiResponse } from "@/app/actions";

Build docs developers (and LLMs) love