Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions packages/api/src/controllers/Projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,23 @@ export class Projects {
}

const { query } = z.object({ query: z.string().min(1) }).parse(req.query);

const contacts = await prisma.contact.findMany({
where: {
projectId: project.id,
OR: [{ email: { contains: query, mode: "insensitive" } }, { data: { contains: query, mode: "insensitive" } }],
},
select: {
id: true,
email: true,
subscribed: true,
createdAt: true,
triggers: { select: { createdAt: true } },
emails: { select: { createdAt: true } },
},
orderBy: [{ createdAt: "desc" }],
});
const queryLike = `%${query}%`;
const contacts = await prisma.$queryRaw<any[]>`
SELECT
c."id",
c."email",
c."subscribed",
GREATEST(c."createdAt", MAX(e."createdAt"), MAX(t."createdAt")) AS "createdAt"
FROM
contacts c
LEFT JOIN triggers t on c.id = t."contactId"
LEFT JOIN emails e on e.id = e."contactId"
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JOIN condition is incorrect. It should be e."contactId" = c.id instead of e.id = e."contactId". The current condition joins emails table on itself rather than joining it with contacts.

Suggested change
LEFT JOIN emails e on e.id = e."contactId"
LEFT JOIN emails e on e."contactId" = c.id

Copilot uses AI. Check for mistakes.

WHERE (c."projectId" = ${project.id}
AND(c."email" ILIKE ${queryLike} OR c."data" ILIKE ${queryLike}))
GROUP BY c.id
ORDER BY
"createdAt" DESC;
`

return res.status(200).json({
contacts,
Expand Down
67 changes: 32 additions & 35 deletions packages/api/src/services/ProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,41 +145,38 @@ export class ProjectService {
const itemsPerPage = 10;
const skip = (page - 1) * itemsPerPage;

const triggers = await prisma.trigger.findMany({
where: { contact: { projectId: id } },
include: {
contact: {
select: {
id: true,
email: true,
},
},
event: {
select: {
name: true,
},
},
},
orderBy: { createdAt: "desc" },
});

const emails = await prisma.email.findMany({
where: { contact: { projectId: id } },
include: {
contact: {
select: {
id: true,
email: true,
},
},
},
orderBy: { createdAt: "desc" },
});

const combined = [...triggers, ...emails];
combined.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());

return combined.slice(skip, skip + itemsPerPage);
const results = await prisma.$queryRaw<{ body: string }[]>`
(
SELECT
to_jsonb (t) || jsonb_build_object (
'contact', jsonb_build_object ('id', c.id, 'email', c.email),
'event', CASE WHEN e.name IS NOT NULL THEN jsonb_build_object ('name', e.name) ELSE NULL END
) as body,
t."createdAt"
FROM
triggers t
JOIN contacts c ON t. "contactId" = c.id
LEFT JOIN events e ON t. "eventId" = e.id
WHERE
c. "projectId" = ${id}
)
UNION ALL (
SELECT
to_jsonb (e) || jsonb_build_object (
'contact', jsonb_build_object ('id', c.id, 'email', c.email)
) as body,
e."createdAt"
FROM
emails e
JOIN contacts c ON e. "contactId" = c.id
WHERE
c. "projectId" = ${id}
)
ORDER BY
"createdAt" DESC
OFFSET ${skip} LIMIT ${itemsPerPage};
`
return results.map((row: { body: string }) => row.body);
}

public static usage(id: string) {
Expand Down
10 changes: 2 additions & 8 deletions packages/dashboard/src/lib/hooks/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,13 @@ export function searchContacts(query: string | undefined) {

if (!query) {
return useSWR<{
contacts: (Contact & {
triggers: Trigger[];
emails: Email[];
})[];
contacts: Contact[];
count: number;
}>(activeProject ? `/projects/id/${activeProject.id}/contacts` : null);
}

return useSWR<{
contacts: (Contact & {
triggers: Trigger[];
emails: Email[];
})[];
contacts: Contact[];
count: number;
}>(activeProject ? `/projects/id/${activeProject.id}/contacts/search?query=${query}` : null, {
revalidateOnFocus: false,
Expand Down
17 changes: 2 additions & 15 deletions packages/dashboard/src/pages/contacts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,11 @@ export default function Index() {
<>
<Table
values={filtered
.sort((a, b) => {
const aTrigger = a.triggers.length > 0 ? a.triggers.sort()[0].createdAt : a.createdAt;

const bTrigger = b.triggers.length > 0 ? b.triggers.sort()[0].createdAt : b.createdAt;

return bTrigger > aTrigger ? 1 : -1;
})
.map((u) => {
return {
Email: u.email,
"Last Activity": dayjs()
.to(
[...u.triggers, ...u.emails].length > 0
? [...u.triggers, ...u.emails].sort((a, b) => {
return a.createdAt > b.createdAt ? -1 : 1;
})[0].createdAt
: u.createdAt,
)
.to(u.createdAt)
.toString(),
Subscribed: u.subscribed,
Edit: (
Expand Down Expand Up @@ -238,7 +225,7 @@ export default function Index() {
<div className="hidden sm:block">
<p className="text-sm text-neutral-700">
Showing <span className="font-medium">{(page - 1) * 20}</span> to{" "}
<span className="font-medium">{page * 20}</span> of <span className="font-medium">{filtered.length}</span>{" "}
<span className="font-medium">{page * 20}</span> of <span className="font-medium">{contacts.count}</span>{" "}
contacts
</p>
</div>
Expand Down