The getStaffById tool retrieves a single staff member record by its document ID from the Firebase/Firestore database.
tools/getStaffById.js
Retrieves a specific staff member by document ID. The staff member is retrieved from the Firestore path: /Accounts/{accountId}/Staff/{staffId}.
{
staffId: string; // REQUIRED: Staff document ID
}
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
staffId |
string | Yes | - | Firestore document ID of the staff member |
{
staff: Staff | null;
}
{
id: string; // Firestore document ID
path: string; // Full Firestore path
active?: boolean; // Whether the staff member is active
createdAt?: string | null; // Creation timestamp (locale string)
createdBy?: string; // ID of the user who created this record
email?: string; // Staff member email (optional)
mobilePhone?: string; // Mobile phone number
name?: string; // Staff member name (optional)
ownedBy?: string; // Owner ID
phone?: string; // Landline phone number
}
// Get a specific staff member by ID
const result = await getStaffById(context, {
staffId: "staff123"
});
if (result.staff) {
console.log(result.staff.name); // Staff member name
console.log(result.staff.email); // Staff member email
} else {
console.log("Staff member not found");
}
const result = await getStaffById(context, {
staffId: "staff123"
});
if (result.staff) {
if (result.staff.active) {
console.log(`${result.staff.name} is active`);
} else {
console.log(`${result.staff.name} is inactive`);
}
} else {
console.log("Staff member does not exist");
}
The tool transforms the Firestore document by:
id and full pathreturn {
staff: {
id: docSnap.id,
path: docSnap.ref.path,
...docSnap.data(),
},
};
The tool validates that staffId is provided:
if (!staffId) {
throw new Error("The 'staffId' parameter is required.");
}
If the staff document doesn’t exist, the tool returns { staff: null } instead of throwing an error.
| Error | Cause | Solution |
|---|---|---|
| “The ‘staffId’ parameter is required.” | Missing staffId | Provide staffId in params |
| “Failed to fetch staff with id [id].” | Firestore error | Check Firebase connection and permissions |
Errors are logged with the staff ID and re-thrown:
catch (error) {
console.error(`Error fetching staff with id ${staffId}:`, error);
throw new Error(`Failed to fetch staff with id ${staffId}.`);
}
getDoc() for efficient single document retrieval/Accounts/{accountId}/Staff/{staffId}
- staffId: number
- name: string
- email: string
- role: string
- active: boolean