The getCustomerContactById tool retrieves a single contact record by its document ID for a specific customer from the Firebase/Firestore database.
tools/getCustomerContactById.js
Retrieves a specific contact by customer ID and contact ID. The contact is retrieved from the Firestore path: /Accounts/{accountId}/Customers/{customerId}/Contacts/{contactId}.
{
customerId: string; // REQUIRED: Customer document ID
contactId: string; // REQUIRED: Contact document ID
}
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
customerId |
string | Yes | - | Firestore document ID of the customer |
contactId |
string | Yes | - | Firestore document ID of the contact |
{
contact: Contact | null;
}
{
id: string; // Firestore document ID
path: string; // Full Firestore path
contactId: number; // Unique contact ID
createdAt: string | Date; // Creation timestamp
createdBy: string; // Creator user ID
customer: string; // Customer reference path
email: string; // Contact email address
firstName: string; // First name
greeting: string; // Greeting/salutation
imported: boolean; // Whether contact was imported
lastName: string; // Last name
mailchimp: {
id: string | null; // Mailchimp ID if synced
};
metadata: Record<string, any>; // Additional metadata
mobilePhone: string; // Mobile phone number
opt_out_email?: boolean; // Email opt-out status
opt_out_letter?: boolean; // Letter opt-out status
ownedBy: string; // Owner user ID
tags: any[]; // Contact tags
telePhone: string; // Telephone number
}
// Get a specific contact for a customer
const result = await getCustomerContactById(context, {
customerId: "customer123",
contactId: "contact456"
});
if (result.contact) {
console.log(result.contact.firstName, result.contact.lastName);
console.log(result.contact.email);
} else {
console.log("Contact not found");
}
const result = await getCustomerContactById(context, {
customerId: "customer123",
contactId: "contact456"
});
if (result.contact === null) {
console.log("Contact does not exist for this customer");
}
The tool transforms the Firestore document by:
id and full pathcreatedAt timestamp to Date objectconst data = docSnap.data();
return {
contact: {
id: docSnap.id,
path: docSnap.ref.path,
...data,
createdAt: safeToDate(data.createdAt) || data.createdAt,
},
};
The tool validates that both required parameters are provided:
if (!customerId) {
throw new Error("The 'customerId' parameter is required.");
}
if (!contactId) {
throw new Error("The 'contactId' parameter is required.");
}
If the contact document doesn’t exist, the tool returns { contact: null } instead of throwing an error.
| Error | Cause | Solution |
|---|---|---|
| “The ‘customerId’ parameter is required.” | Missing customerId | Provide customerId in params |
| “The ‘contactId’ parameter is required.” | Missing contactId | Provide contactId in params |
| “Failed to fetch contact with id [contactId] for customer [customerId].” | Firestore error | Check Firebase connection and permissions |
Errors are logged with both IDs and re-thrown:
catch (error) {
console.error(
`Error fetching contact with id ${contactId} for customer ${customerId}:`,
error
);
throw new Error(
`Failed to fetch contact with id ${contactId} for customer ${customerId}.`
);
}
getDoc() for efficient single document retrieval/Accounts/{accountId}/Customers/{customerId}/Contacts/{contactId}
- contactId: number
- createdAt: timestamp
- createdBy: string
- customer: string (reference)
- email: string
- firstName: string
- greeting: string
- imported: boolean
- lastName: string
- mailchimp: { id: string | null }
- metadata: object
- mobilePhone: string
- opt_out_email: boolean
- opt_out_letter: boolean
- ownedBy: string
- tags: array
- telePhone: string