Skip to content

Commit

Permalink
fix: 🐛 only send notification when action triggered by visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Oct 24, 2023
1 parent 9192c48 commit c1f34db
Showing 1 changed file with 53 additions and 47 deletions.
100 changes: 53 additions & 47 deletions apps/dashboard/pages/api/conversations/[conversationId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,53 +121,59 @@ export const updateConversation = async (
},
});

const onwerEmail = updated?.organization?.memberships?.[0]?.user?.email!;
const leadEmail = updated?.lead?.email!;
const agent = updated?.agent!;

if (data.status === ConversationStatus.RESOLVED) {
await mailer.sendMail({
from: {
name: 'Chaindesk',
address: process.env.EMAIL_FROM!,
},
to: onwerEmail,
subject: `✅ Conversation resolved automatically by ${
agent?.name || ''
}`,
html: render(
<ConversationResolved
agentName={agent.name}
messages={updated?.messages}
ctaLink={`${
process.env.NEXT_PUBLIC_DASHBOARD_URL
}/logs?tab=all&conversationId=${encodeURIComponent(
conversationId
)}&targetOrgId=${encodeURIComponent(updated.organizationId!)}`}
/>
),
});
} else if (data.status === ConversationStatus.HUMAN_REQUESTED) {
await mailer.sendMail({
from: {
name: 'Chaindesk',
address: process.env.EMAIL_FROM!,
},
to: onwerEmail,
subject: `❓ Assistance requested from Agent ${agent?.name || ''}`,
html: render(
<HelpRequest
visitorEmail={leadEmail}
agentName={agent.name}
messages={updated?.messages}
ctaLink={`${
process.env.NEXT_PUBLIC_DASHBOARD_URL
}/logs?tab=human_requested&conversationId=${encodeURIComponent(
conversationId
)}&targetOrgId=${encodeURIComponent(updated.organizationId!)}`}
/>
),
});
const isAuthenticatedUser = !!session;

if (!isAuthenticatedUser) {
// Only send notification if action performed by a visitor

const onwerEmail = updated?.organization?.memberships?.[0]?.user?.email!;
const leadEmail = updated?.lead?.email!;
const agent = updated?.agent!;

if (data.status === ConversationStatus.RESOLVED) {
await mailer.sendMail({
from: {
name: 'Chaindesk',
address: process.env.EMAIL_FROM!,
},
to: onwerEmail,
subject: `✅ Conversation resolved automatically by ${
agent?.name || ''
}`,
html: render(
<ConversationResolved
agentName={agent.name}
messages={updated?.messages}
ctaLink={`${
process.env.NEXT_PUBLIC_DASHBOARD_URL
}/logs?tab=all&conversationId=${encodeURIComponent(
conversationId
)}&targetOrgId=${encodeURIComponent(updated.organizationId!)}`}
/>
),
});
} else if (data.status === ConversationStatus.HUMAN_REQUESTED) {
await mailer.sendMail({
from: {
name: 'Chaindesk',
address: process.env.EMAIL_FROM!,
},
to: onwerEmail,
subject: `❓ Assistance requested from Agent ${agent?.name || ''}`,
html: render(
<HelpRequest
visitorEmail={leadEmail}
agentName={agent.name}
messages={updated?.messages}
ctaLink={`${
process.env.NEXT_PUBLIC_DASHBOARD_URL
}/logs?tab=human_requested&conversationId=${encodeURIComponent(
conversationId
)}&targetOrgId=${encodeURIComponent(updated.organizationId!)}`}
/>
),
});
}
}

return updated;
Expand Down

0 comments on commit c1f34db

Please sign in to comment.