The getCustomerById tool retrieves a single customer record by its document ID from the Firebase/Firestore database.
tools/getCustomerById.js
Retrieves a specific customer by document ID. The customer is retrieved from the Firestore path: /Accounts/{accountId}/Customers/{customerId}.
{
customerId: string; // REQUIRED: Customer document ID
}
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
customerId |
string | Yes | - | Firestore document ID of the customer |
{
customer: Customer | null;
}
{
id: string; // Firestore document ID
path: string; // Full Firestore path
address: string; // Customer address
city: string; // City
country: string; // Country
createdAt: string | Date; // Creation timestamp (converted to locale string)
createdBy: string; // Creator user ID
customerId: number; // Unique customer ID
metadata: Record<string, any>; // Additional metadata
name: string; // Customer name
ownedBy: string; // Owner user ID
zip: string; // Postal code
}
// Get a specific customer by ID
const result = await getCustomerById(context, {
customerId: "abc123"
});
if (result.customer) {
console.log(result.customer.name); // Customer name
console.log(result.customer.email); // Customer email
} else {
console.log("Customer not found");
}
const result = await getCustomerById(context, {
customerId: "abc123"
});
if (result.customer === null) {
console.log("Customer does not exist");
}
The tool transforms the Firestore document by:
id and full pathcreatedAt timestamp to locale string (de-DE format)const data = docSnap.data();
return {
customer: {
id: docSnap.id,
path: docSnap.ref.path,
...data,
createdAt: safeToLocaleString(data.createdAt) || data.createdAt,
},
};
The tool validates that customerId is provided:
if (!customerId) {
throw new Error("The 'customerId' parameter is required.");
}
If the customer document doesn’t exist, the tool returns { customer: null } instead of throwing an error.
| Error | Cause | Solution |
|---|---|---|
| “The ‘customerId’ parameter is required.” | Missing customerId | Provide customerId in params |
| “Failed to fetch customer with id [id].” | Firestore error | Check Firebase connection and permissions |
Errors are logged with the customer ID and re-thrown:
catch (error) {
console.error(`Error fetching customer with id ${customerId}:`, error);
throw new Error(`Failed to fetch customer with id ${customerId}.`);
}
getDoc() for efficient single document retrieval/Accounts/{accountId}/Customers/{customerId}
- address: string
- city: string
- country: string
- createdAt: timestamp
- createdBy: string
- customerId: number
- metadata: object
- name: string
- ownedBy: string
- zip: string