From 7d4270f379a54740ccbb36163fea7df6ad277bc0 Mon Sep 17 00:00:00 2001 From: Flux_bot Date: Wed, 25 Feb 2026 13:31:03 +0000 Subject: [PATCH] feat: Add owner name to contacts list response - Join users table to get owner name - Include ownerName in contact response --- src/repositories/contact.ts | 14 +++++++++----- src/routes/contacts.ts | 7 ++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/repositories/contact.ts b/src/repositories/contact.ts index 283d9f2..0041b7f 100644 --- a/src/repositories/contact.ts +++ b/src/repositories/contact.ts @@ -125,11 +125,15 @@ export async function findAll( ); const total = parseInt(countResult?.count || "0"); - // Get contacts - const contacts = await query( - `SELECT * FROM contacts - WHERE ${whereClause} - ORDER BY ${safeSortBy} ${safeSortOrder} + // Get contacts with owner info + const contacts = await query( + `SELECT c.*, + u.first_name as owner_first_name, + 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}`, [...params, limit, offset] ); diff --git a/src/routes/contacts.ts b/src/routes/contacts.ts index 17f4154..326b414 100644 --- a/src/routes/contacts.ts +++ b/src/routes/contacts.ts @@ -339,7 +339,11 @@ router.delete("/:id/permanent", requireAuth, requireRole("owner", "admin"), asyn // 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 { id: contact.id, firstName: contact.first_name, @@ -365,6 +369,7 @@ function formatContact(contact: contactRepo.Contact) { }, dataSource: contact.data_source, ownerId: contact.owner_id, + ownerName: ownerName, createdBy: contact.created_by, createdAt: contact.created_at, updatedAt: contact.updated_at,