🐛 (wa) Fix WhatsApp session stuck if state object is empty
Closes #1513
This commit is contained in:
@ -1,11 +1,19 @@
|
|||||||
import prisma from '@typebot.io/lib/prisma'
|
import prisma from '@typebot.io/lib/prisma'
|
||||||
import { sessionStateSchema } from '@typebot.io/schemas'
|
import { sessionStateSchema } from '@typebot.io/schemas'
|
||||||
|
import { deleteSession } from './deleteSession'
|
||||||
|
|
||||||
export const getSession = async (sessionId: string) => {
|
export const getSession = async (sessionId: string) => {
|
||||||
const session = await prisma.chatSession.findUnique({
|
const session = await prisma.chatSession.findUnique({
|
||||||
where: { id: sessionId },
|
where: { id: sessionId },
|
||||||
select: { id: true, state: true, updatedAt: true, isReplying: true },
|
select: { id: true, state: true, updatedAt: true, isReplying: true },
|
||||||
})
|
})
|
||||||
if (!session) return null
|
if (!session?.state) return null
|
||||||
return { ...session, state: sessionStateSchema.parse(session.state) }
|
if (Object.keys(session.state).length === 0) {
|
||||||
|
await deleteSession(session.id)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...session,
|
||||||
|
state: sessionStateSchema.parse(session.state),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
import prisma from '@typebot.io/lib/prisma'
|
||||||
|
|
||||||
|
export const removeIsReplyingInChatSession = async (id: string) =>
|
||||||
|
prisma.chatSession.updateMany({
|
||||||
|
where: { id },
|
||||||
|
data: { isReplying: false },
|
||||||
|
})
|
@ -4,7 +4,7 @@ type Props = {
|
|||||||
existingSessionId: string | undefined
|
existingSessionId: string | undefined
|
||||||
newSessionId: string
|
newSessionId: string
|
||||||
}
|
}
|
||||||
export const setChatSessionHasReplying = async ({
|
export const setIsReplyingInChatSession = async ({
|
||||||
existingSessionId,
|
existingSessionId,
|
||||||
newSessionId,
|
newSessionId,
|
||||||
}: Props) => {
|
}: Props) => {
|
@ -13,7 +13,8 @@ import { saveStateToDatabase } from '../saveStateToDatabase'
|
|||||||
import prisma from '@typebot.io/lib/prisma'
|
import prisma from '@typebot.io/lib/prisma'
|
||||||
import { isDefined } from '@typebot.io/lib/utils'
|
import { isDefined } from '@typebot.io/lib/utils'
|
||||||
import { Reply } from '../types'
|
import { Reply } from '../types'
|
||||||
import { setChatSessionHasReplying } from '../queries/setChatSessionHasReplying'
|
import { setIsReplyingInChatSession } from '../queries/setIsReplyingInChatSession'
|
||||||
|
import { removeIsReplyingInChatSession } from '../queries/removeIsReplyingInChatSession'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
receivedMessage: WhatsAppIncomingMessage
|
receivedMessage: WhatsAppIncomingMessage
|
||||||
@ -75,7 +76,7 @@ export const resumeWhatsAppFlow = async ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await setChatSessionHasReplying({
|
await setIsReplyingInChatSession({
|
||||||
existingSessionId: session?.id,
|
existingSessionId: session?.id,
|
||||||
newSessionId: sessionId,
|
newSessionId: sessionId,
|
||||||
})
|
})
|
||||||
@ -101,6 +102,7 @@ export const resumeWhatsAppFlow = async ({
|
|||||||
: { error: 'workspaceId not found' }
|
: { error: 'workspaceId not found' }
|
||||||
|
|
||||||
if ('error' in resumeResponse) {
|
if ('error' in resumeResponse) {
|
||||||
|
await removeIsReplyingInChatSession(sessionId)
|
||||||
console.log('Chat not starting:', resumeResponse.error)
|
console.log('Chat not starting:', resumeResponse.error)
|
||||||
return {
|
return {
|
||||||
message: 'Message received',
|
message: 'Message received',
|
||||||
|
Reference in New Issue
Block a user