The getOrderById tool retrieves a single order record by its document ID from the Firebase/Firestore database, with automatic population of related customer, contact, and staff references.
tools/getOrderById.js
Retrieves a specific order by document ID with all related references populated. The order is retrieved from the Firestore path: /Accounts/{accountId}/Orders/{orderId}, and the tool automatically fetches and includes the referenced customer, contact, technician, and responsible staff documents.
{
orderId: string; // REQUIRED: Order document ID
}
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
orderId |
string | Yes | - | Firestore document ID of the order |
{
order: Order | null;
}
{
id: string; // Firestore document ID
path: string; // Full Firestore path
orderId: number; // Unique order ID
title: string; // Order title
description: string; // Order description
stage: number; // Order stage/status number
closed: boolean; // Whether order is closed
createdAt: string | Date; // Creation timestamp (locale string)
lastCommentAt: string | Date; // Last comment timestamp (locale string)
deadline?: string | Date; // Deadline (locale string)
customer: Customer | null; // Populated customer object
contact: Contact | null; // Populated contact object
technician_staff: Staff | null; // Populated technician staff object
responsible_staff: Staff | null; // Populated responsible staff object
// ... other order fields
}
The following references are automatically fetched and populated:
// Get a specific order by ID with all references populated
const result = await getOrderById(context, {
orderId: "order123"
});
if (result.order) {
console.log(result.order.title);
console.log(result.order.customer?.name); // Customer name
console.log(result.order.contact?.email); // Contact email
console.log(result.order.technician_staff?.name); // Technician name
} else {
console.log("Order not found");
}
const result = await getOrderById(context, {
orderId: "order123"
});
if (result.order) {
// Access order details
console.log(`Order #${result.order.orderId}: ${result.order.title}`);
console.log(`Stage: ${result.order.stage}, Closed: ${result.order.closed}`);
// Access customer information
if (result.order.customer) {
console.log(`Customer: ${result.order.customer.name}`);
console.log(`Address: ${result.order.customer.address}, ${result.order.customer.city}`);
}
// Access contact information
if (result.order.contact) {
console.log(`Contact: ${result.order.contact.firstName} ${result.order.contact.lastName}`);
console.log(`Email: ${result.order.contact.email}`);
}
// Access staff information
if (result.order.technician_staff) {
console.log(`Technician: ${result.order.technician_staff.name}`);
}
}
The tool automatically populates related documents:
// Fetch the main order document
const orderDoc = await getDocumentById(db, `Accounts/${accountId}/Orders`, orderId);
// Populate referenced documents
const customer = await getDocument(db, orderDoc.customer);
const contact = await getDocument(db, orderDoc.contact);
const technician = await getDocumentById(db, `Accounts/${accountId}/Staff`, orderDoc.technician_staff);
const responsible = await getDocumentById(db, `Accounts/${accountId}/Staff`, orderDoc.responsible_staff);
The tool transforms the Firestore document by:
id and full pathThe tool validates that orderId is provided:
if (!orderId) {
throw new Error("The 'orderId' parameter is required.");
}
If the order document doesn’t exist, the tool returns { order: null }. If any referenced documents don’t exist, those fields are set to null.
| Error | Cause | Solution |
|---|---|---|
| “The ‘orderId’ parameter is required.” | Missing orderId | Provide orderId in params |
| “Failed to fetch order with id [id].” | Firestore error | Check Firebase connection and permissions |
Errors are logged with the order ID and re-thrown:
catch (error) {
console.error(`Error fetching order with id ${orderId}:`, error);
throw new Error(`Failed to fetch order with id ${orderId}.`);
}
/Accounts/{accountId}/Orders/{orderId}
- orderId: number
- title: string
- description: string
- stage: number
- closed: boolean
- createdAt: timestamp
- lastCommentAt: timestamp
- deadline: timestamp
- customer: string (reference path)
- contact: string (reference path)
- technician_staff: string (staff ID)
- responsible_staff: string (staff ID)