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,