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:
@@ -125,11 +125,15 @@ export async function findAll(
|
||||
);
|
||||
const total = parseInt(countResult?.count || "0");
|
||||
|
||||
// Get contacts
|
||||
const contacts = await query<Contact>(
|
||||
`SELECT * FROM contacts
|
||||
WHERE ${whereClause}
|
||||
ORDER BY ${safeSortBy} ${safeSortOrder}
|
||||
// Get contacts with owner info
|
||||
const contacts = await query<Contact & { owner_first_name?: string; owner_last_name?: string }>(
|
||||
`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]
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user