Skip to main content

getProduct

Fetches a single product by its _id, with the full depth-2 projection available (parent, tags). Open to every authenticated role. This is the detail view behind a product listing.

note

Import alias The code below imports the framework as lesan โ€” that's the path alias this repo's app uses (see Project Layout). In your own project, import from @hemedani/lesan (npm/Bun) or jsr:@hemedani/lesan (Deno) instead.

Registration (mod.ts)โ€‹

import { grantAccess, setTokens, setUser } from "@lib";
import { coreApp } from "../../../mod.ts";
import { getProductFn } from "./getProduct.fn.ts";
import { getProductValidator } from "./getProduct.val.ts";

export const getProductSetup = () =>
coreApp.acts.setAct({
schema: "product",
actName: "getProduct",
preAct: [setTokens, setUser, grantAccess([{ roles: ["Manager", "Admin", "OrgHead", "UnitHead", "Employee", "Ordinary"] }])],
validator: getProductValidator(),
fn: getProductFn,
});

The validator (getProduct.val.ts)โ€‹

import { object, objectIdValidation } from "lesan";
import { selectStruct } from "../../../mod.ts";
import { activeRoleMixin } from "@lib";

export const getProductValidator = () => {
return object({
set: object({
...activeRoleMixin,
_id: objectIdValidation,
}),
get: selectStruct("product", 2),
});
};
  • set is activeRoleId + _id.
  • get is depth 2, so you can project parent (itself a product) and the tags array (each a tag snapshot).

The implementation (getProduct.fn.ts)โ€‹

import { type ActFn, ObjectId } from "lesan";
import { product } from "../../../mod.ts";
import { throwError } from "@lib";

export const getProductFn: ActFn = async (body) => {
const {
set: { _id },
get,
} = body.details;

const foundedProduct = await product.findOne({
filters: { _id: new ObjectId(_id as string) },
projection: get,
});

!foundedProduct && throwError("product not found");
return foundedProduct;
};
  1. The string _id is converted with new ObjectId(_id).
  2. product.findOne returns the doc shaped by the client's get projection. Because relations are embedded snapshots, parent and tags come back directly โ€” no additional queries.
  3. !foundedProduct && throwError("product not found") turns a miss into an error.

In the workflowโ€‹

The single-item view used when a purchase order or inventory entry references a product. Pair it with getProducts for lists, and updateProductRelations for adjusting its relations.

Run itโ€‹

curl http://localhost:1380/lesan \
-H "Content-Type: application/json" \
-H "token: <jwt>" \
-d '{
"model": "product",
"act": "getProduct",
"details": {
"set": { "activeRoleId": "ghost-role", "_id": "<productId>" },
"get": { "_id": 1, "name": 1, "price": 1, "parent": { "_id": 1, "name": 1 }, "tags": { "_id": 1, "name": 1 } }
}
}'

Errors & fixesโ€‹

ErrorMeaningFix
product not foundNo product has that _id.Check the id โ€” get it from getProducts.
activeRoleId is requiredNo activeRoleId in set.Add it (ghost: any string, e.g. "ghost-role").
Active role not foundactiveRoleId isn't one of the user's roles.Pass a real roleId.
You cant do thisActive role not in the allowed list.Use an allowed role or the ghost.
Invalid or expired token / you should send your id with token key in req headerAuth header problem.Send token: <jwt>; re-login if expired.
superstruct "expected ObjectId-like string"_id isn't a valid ObjectId.Send the 24-hex id string.