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.
Query parameters for filtering and pagination Items per page (default: 10)
Search term for filtering assets
[key: string]
string | number | string[]
Additional filter parameters (e.g., site_id, status)
Pagination metadata Whether there is a next page
Whether there is a previous page
Server Component
With Filters
Error Handling
"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.
Complete asset object with all fields
Basic Usage
With Error Handling
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.
Asset creation data Asset type (e.g., “Pump”, “Motor”, “Compressor”)
Manufacturer model number
Criticality level (e.g., “high”, “medium”, “low”)
Date asset was commissioned
Datasheet file information
Standard API response object Whether the operation succeeded
Client Component
With Datasheet Upload
"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.
Updated asset data (same structure as addAssetService)
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.
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.
Query parameters for filtering and pagination Items per page (default: 10)
Pagination metadata (same structure as assets)
Basic Usage
Filter by Organization
import { getSitesService } from "@/app/actions" ;
const { data : sites , meta } = await getSitesService ({
page: 1 ,
limit: 10 ,
});
getSiteService
Retrieves a single site by ID.
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.
Standard API response object
editSiteService
Updates an existing site.
Standard API response object
deleteSiteService
Deletes a site.
Standard API response object
Analytics Operations
getAssetsAnalyticsService
Retrieves asset analytics for dashboard metrics.
Asset analytics data or null on error 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.
Site analytics data or null on error Number of sites with issues
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" ;