Skip to content

Commit

Permalink
fix: 🐛 crisp human out of sync
Browse files Browse the repository at this point in the history
  • Loading branch information
OdapX authored and gmpetrov committed Mar 26, 2024
1 parent ef3dea7 commit 88a0745
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const updateConversation = async (
try {
const session = req.session;
const conversationId = req.query.conversationId as string;
const updates = ConversationUpdateSchema.parse(req.body);
const payload = ConversationUpdateSchema.parse(req.body);

const conversation = await prisma.conversation.findUnique({
where: {
Expand Down Expand Up @@ -156,7 +156,7 @@ export const updateConversation = async (
id: conversationId,
},
data: {
...updates,
...payload,
},
include: {
lead: true,
Expand Down Expand Up @@ -195,15 +195,15 @@ export const updateConversation = async (
const leadEmail = updated?.lead?.email!;
const agent = updated?.agent!;

if (updates.status === ConversationStatus.RESOLVED) {
if (payload.status === ConversationStatus.RESOLVED) {
await EventDispatcher.dispatch({
type: 'conversation-resolved',
agent: agent,
conversation: conversation,
messages: updated?.messages,
adminEmail: onwerEmail,
});
} else if (updates.status === ConversationStatus.HUMAN_REQUESTED) {
} else if (payload.status === ConversationStatus.HUMAN_REQUESTED) {
await EventDispatcher.dispatch({
type: 'human-requested',
agent: agent,
Expand Down
91 changes: 64 additions & 27 deletions apps/dashboard/pages/api/conversations/[conversationId]/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const sendMessage = async (
const conversationId = req.query.conversationId as string;
const payload = chatBodySchema.parse(req.body);
const session = req.session;
let externalMessageId: string | undefined;

const {
id,
Expand Down Expand Up @@ -93,35 +92,73 @@ export const sendMessage = async (
select: {
name: true,
picture: true,
customPicture: true,
image: true,
},
});
await CrispClient.website.sendMessageInConversation(
channelCredentials?.externalId, // websiteId
channelExternalId, // sessionId
{
type: 'text',
from: 'operator',
origin: 'chat',
content: payload.message,
user: {
type: 'website',
nickname: user?.name || 'Operator',
avatar: user?.picture || 'https://chaindesk.ai/logo.png',
},
}
);

// disable AI
await CrispClient.website.updateConversationMetas(
channelCredentials?.externalId, // websiteId
channelExternalId, // sessionId
{
data: {
aiStatus: AIStatus.disabled,
aiDisabledDate: new Date(),
},
}
);
const attachementCalls =
payload?.attachments?.map((attachement) => {
return CrispClient.website.sendMessageInConversation(
channelCredentials?.externalId, // websiteId
channelExternalId, // sessionId
{
type: 'file',
from: 'operator',
origin: 'chat',
content: {
url: attachement.url,
name: attachement.name,
type: attachement.mimeType,
},
user: {
type: 'website',
nickname: user?.name || 'Operator',
avatar:
user?.picture ||
user?.customPicture ||
user?.image ||
'https://chaindesk.ai/logo.png',
},
}
);
}) || [];

await Promise.all([
// send text content
CrispClient.website.sendMessageInConversation(
channelCredentials?.externalId, // websiteId
channelExternalId, // sessionId
{
type: 'text',
from: 'operator',
origin: 'chat',
content: payload.message,
user: {
type: 'website',
nickname: user?.name || 'Operator',
avatar:
user?.picture ||
user?.customPicture ||
user?.image ||
'https://chaindesk.ai/logo.png',
},
}
),
// send attachements
...attachementCalls,
// disable AI
CrispClient.website.updateConversationMetas(
channelCredentials?.externalId, // websiteId
channelExternalId, // sessionId
{
data: {
aiStatus: AIStatus.disabled,
aiDisabledDate: new Date(),
},
}
),
]);
} catch (e) {
console.error(e);
throw Error(
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/pages/api/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ export const getLogs = async (req: AppNextApiRequest, res: NextApiResponse) => {
},
},
messages: {
take: 1,
take: 2,
orderBy: {
createdAt: 'asc',
createdAt: 'desc',
},
},
},
Expand Down
36 changes: 7 additions & 29 deletions apps/dashboard/pages/logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -928,15 +928,6 @@ export default function LogsPage() {
borderRadius: 0,
},

// backgroundColor: {
// [ConversationStatus.RESOLVED]:
// theme.palette.success.softActiveBg,
// [ConversationStatus.UNRESOLVED]:
// theme.palette.danger.softActiveBg,
// [ConversationStatus.HUMAN_REQUESTED]:
// theme.palette.warning.softActiveBg,
// }[each?.status] as ColorPaletteProp,

...(state.currentConversationId === each.id && {
backgroundColor: theme.palette.action.hover,
}),
Expand Down Expand Up @@ -1068,7 +1059,13 @@ export default function LogsPage() {
level="body-sm"
className="pr-4 line-clamp-2"
>
{each?.messages?.[0]?.text}
{/* last human message */}
{each?.messages?.[each?.messages.length - 1]
?.from === 'human'
? each?.messages?.[each?.messages.length - 1]
?.text
: each?.messages?.[each?.messages.length - 2]
?.text}
</Typography>
</Stack>
<Stack
Expand Down Expand Up @@ -1166,25 +1163,6 @@ export default function LogsPage() {
startDecorator={<Notifications />}
endDecorator={
<Stack direction="row" spacing={1}>
{/* {!isHumanHandoffButtonHidden && (
<Button
variant="solid"
color={state.isAiEnabled ? 'primary' : 'warning'}
size="md"
loading={conversationUpdater.isMutating}
endDecorator={
state.isAiEnabled ? (
<CommentRoundedIcon fontSize="sm" />
) : (
<SmartToyIcon fontSize="sm" />
)
}
onClick={toggleAi}
>
{state.isAiEnabled ? 'Reply' : 'Re-Enable AI'}
</Button>
)} */}

<BannerActions
status={getConversationQuery?.data?.status}
email={getConversationQuery?.data?.lead?.email!}
Expand Down
Loading

0 comments on commit 88a0745

Please sign in to comment.