Skip to main content

Overview

Sampling route actions provide CRUD operations for sampling routes. All functions are located in src/app/actions/sampling-routes.ts.

Sampling Route Operations

getSamplingRoutesService

Retrieves a paginated list of sampling routes 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 sampling routes
data
SamplingRoute[]
Array of sampling route objects
meta
SamplingRoutesMeta
Pagination metadata
page
number
Current page number
limit
number
Items per page
total
number
Total number of sampling routes
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
import { getSamplingRoutesService } from "@/app/actions";

export default async function SamplingRoutesPage({ 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: samplingRoutes, meta } = await getSamplingRoutesService({ 
    page, 
    limit, 
    search 
  });

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

getSamplingRouteService

Retrieves a single sampling route by ID.
id
string
required
Unique sampling route identifier
data
SamplingRoute
Complete sampling route object with site and technician details
import { getSamplingRouteService } from "@/app/actions";

const samplingRoute = await getSamplingRouteService("route-123");

console.log(samplingRoute.name);
console.log(samplingRoute.description);
console.log(samplingRoute.site.name);
console.log(samplingRoute.technician?.first_name);

addSamplingRouteService

Creates a new sampling route.
payload
AddSamplingRoutePayload
required
Sampling route creation data
name
string
required
Sampling route name
description
string
required
Sampling route description
site_id
string
required
Site ID where the route is located
technician_id
string
ID of the assigned technician
status
string
required
Sampling route status
response
ApiResponse
Standard API response object
success
boolean
Whether the operation succeeded
statusCode
number
HTTP status code
message
string
Response message
data
object
Created sampling route data
"use client";
import { addSamplingRouteService } from "@/app/actions";
import { AddSamplingRoutePayload } from "@/schema";
import { toast } from "sonner";

export function AddSamplingRouteForm() {
  const handleSubmit = async (data: AddSamplingRoutePayload) => {
    const response = await addSamplingRouteService(data);
    
    if (response.success) {
      toast.success("Sampling route added successfully");
      router.push("/sampling-routes");
    } else {
      toast.error(response.message || "Failed to add sampling route");
    }
  };

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

editSamplingRouteService

Updates an existing sampling route.
id
string
required
Sampling route ID to update
payload
AddSamplingRoutePayload
required
Updated sampling route data (same structure as addSamplingRouteService)
response
ApiResponse
Standard API response object
import { editSamplingRouteService } from "@/app/actions";
import { toast } from "sonner";

const handleUpdate = async (
  samplingRouteId: string,
  data: AddSamplingRoutePayload
) => {
  const response = await editSamplingRouteService(samplingRouteId, data);
  
  if (response.success) {
    toast.success("Sampling route updated successfully");
  } else {
    toast.error(response.message);
  }
};

deleteSamplingRouteService

Deletes a sampling route.
id
string
required
Sampling route ID to delete
response
ApiResponse
Standard API response object
import { deleteSamplingRouteService } from "@/app/actions";
import { toast } from "sonner";

const handleDelete = async (samplingRouteId: string) => {
  const response = await deleteSamplingRouteService(samplingRouteId);
  
  if (response.success) {
    toast.success("Sampling route deleted successfully");
  } else {
    toast.error(response.message);
  }
};
Deleting a sampling route may affect sampling points assigned to it. Ensure sampling points are reassigned before deletion.

Type Definitions

SamplingRoute

interface SamplingRoute {
  id: string;
  name: string;
  description: string;
  site_id: string;
  site: {
    id: string;
    name: string;
    tag: string;
    installation_environment?: string;
    regulations_and_standards?: string[] | null;
  };
  technician_id?: string | null;
  technician?: {
    id: string;
    first_name: string;
    last_name: string;
    email: string;
    phone?: string;
    role?: string;
  } | null;
  status: string;
  created_at: number;
  created_at_datetime: string;
  updated_at: number;
  updated_at_datetime: string;
}

SamplingRoutesMeta

interface SamplingRoutesMeta {
  page: number;
  limit: number;
  total: number;
  total_pages: number;
  has_next: boolean;
  has_prev: boolean;
}
Import from the appropriate locations:
import {
  getSamplingRoutesService,
  addSamplingRouteService,
  editSamplingRouteService,
  deleteSamplingRouteService,
  getSamplingRouteService,
} from "@/app/actions";
import { SamplingRoute } from "@/types";
import { AddSamplingRoutePayload, ADD_SAMPLING_ROUTE_SCHEMA } from "@/schema";
import { ApiResponse } from "@/app/actions";

Build docs developers (and LLMs) love