feat: Add owner name to contacts list response

- Join users table to get owner name
- Include ownerName in contact response
This commit is contained in:
2026-02-25 13:31:03 +00:00
parent a9835bbd2d
commit 7d4270f379
2 changed files with 15 additions and 6 deletions

View File

@@ -125,11 +125,15 @@ export async function findAll(
); );
const total = parseInt(countResult?.count || "0"); const total = parseInt(countResult?.count || "0");
// Get contacts // Get contacts with owner info
const contacts = await query<Contact>( const contacts = await query<Contact & { owner_first_name?: string; owner_last_name?: string }>(
`SELECT * FROM contacts `SELECT c.*,
WHERE ${whereClause} u.first_name as owner_first_name,
ORDER BY ${safeSortBy} ${safeSortOrder} u.last_name as owner_last_name
FROM contacts c
LEFT JOIN users u ON c.owner_id = u.id
WHERE ${whereClause.replace(/org_id/g, 'c.org_id').replace(/deleted_at/g, 'c.deleted_at')}
ORDER BY c.${safeSortBy} ${safeSortOrder}
LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`, LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`,
[...params, limit, offset] [...params, limit, offset]
); );

View File

@@ -339,7 +339,11 @@ router.delete("/:id/permanent", requireAuth, requireRole("owner", "admin"), asyn
// HELPER FUNCTIONS // HELPER FUNCTIONS
// ============================================ // ============================================
function formatContact(contact: contactRepo.Contact) { function formatContact(contact: contactRepo.Contact & { owner_first_name?: string; owner_last_name?: string }) {
const ownerName = contact.owner_first_name && contact.owner_last_name
? `${contact.owner_first_name} ${contact.owner_last_name}`
: null;
return { return {
id: contact.id, id: contact.id,
firstName: contact.first_name, firstName: contact.first_name,
@@ -365,6 +369,7 @@ function formatContact(contact: contactRepo.Contact) {
}, },
dataSource: contact.data_source, dataSource: contact.data_source,
ownerId: contact.owner_id, ownerId: contact.owner_id,
ownerName: ownerName,
createdBy: contact.created_by, createdBy: contact.created_by,
createdAt: contact.created_at, createdAt: contact.created_at,
updatedAt: contact.updated_at, updatedAt: contact.updated_at,