🐛 (editor) Fix lost changes when typebot takes a long time to update
Closes #1231
This commit is contained in:
@@ -208,7 +208,11 @@ export const TypebotProvider = ({
|
|||||||
const saveTypebot = useCallback(
|
const saveTypebot = useCallback(
|
||||||
async (updates?: Partial<TypebotV6>) => {
|
async (updates?: Partial<TypebotV6>) => {
|
||||||
if (!localTypebot || !typebot || isReadOnly) return
|
if (!localTypebot || !typebot || isReadOnly) return
|
||||||
const typebotToSave = { ...localTypebot, ...updates }
|
const typebotToSave = {
|
||||||
|
...localTypebot,
|
||||||
|
...updates,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
}
|
||||||
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
|
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
|
||||||
return
|
return
|
||||||
setLocalTypebot({ ...typebotToSave })
|
setLocalTypebot({ ...typebotToSave })
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ const typebotUpdateSchemaPick = {
|
|||||||
whatsAppCredentialsId: true,
|
whatsAppCredentialsId: true,
|
||||||
riskLevel: true,
|
riskLevel: true,
|
||||||
events: true,
|
events: true,
|
||||||
|
updatedAt: true,
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
export const updateTypebot = authenticatedProcedure
|
export const updateTypebot = authenticatedProcedure
|
||||||
@@ -67,12 +68,6 @@ export const updateTypebot = authenticatedProcedure
|
|||||||
title: 'Typebot V5',
|
title: 'Typebot V5',
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
updatedAt: z
|
|
||||||
.date()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
'Used for checking if there is a newer version of the typebot in the database'
|
|
||||||
),
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.output(
|
.output(
|
||||||
@@ -80,8 +75,7 @@ export const updateTypebot = authenticatedProcedure
|
|||||||
typebot: typebotV6Schema,
|
typebot: typebotV6Schema,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.mutation(
|
.mutation(async ({ input: { typebotId, typebot }, ctx: { user } }) => {
|
||||||
async ({ input: { typebotId, typebot, updatedAt }, ctx: { user } }) => {
|
|
||||||
const existingTypebot = await prisma.typebot.findFirst({
|
const existingTypebot = await prisma.typebot.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: typebotId,
|
id: typebotId,
|
||||||
@@ -125,8 +119,9 @@ export const updateTypebot = authenticatedProcedure
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (
|
if (
|
||||||
updatedAt &&
|
typebot.updatedAt &&
|
||||||
updatedAt.getTime() > new Date(existingTypebot?.updatedAt).getTime()
|
new Date(existingTypebot?.updatedAt).getTime() >
|
||||||
|
typebot.updatedAt.getTime()
|
||||||
)
|
)
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: 'CONFLICT',
|
code: 'CONFLICT',
|
||||||
@@ -207,6 +202,7 @@ export const updateTypebot = authenticatedProcedure
|
|||||||
typebot.customDomain === null ? null : typebot.customDomain,
|
typebot.customDomain === null ? null : typebot.customDomain,
|
||||||
isClosed: typebot.isClosed,
|
isClosed: typebot.isClosed,
|
||||||
whatsAppCredentialsId: typebot.whatsAppCredentialsId ?? undefined,
|
whatsAppCredentialsId: typebot.whatsAppCredentialsId ?? undefined,
|
||||||
|
updatedAt: typebot.updatedAt,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -215,8 +211,7 @@ export const updateTypebot = authenticatedProcedure
|
|||||||
)
|
)
|
||||||
|
|
||||||
return { typebot: migratedTypebot }
|
return { typebot: migratedTypebot }
|
||||||
}
|
})
|
||||||
)
|
|
||||||
|
|
||||||
const isPublicIdValid = (str: string) =>
|
const isPublicIdValid = (str: string) =>
|
||||||
/^([a-z0-9]+-[a-z0-9]*)*$/.test(str) || /^[a-z0-9]*$/.test(str)
|
/^([a-z0-9]+-[a-z0-9]*)*$/.test(str) || /^[a-z0-9]*$/.test(str)
|
||||||
|
|||||||
@@ -4401,6 +4401,9 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"updatedAt": {
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"title": "Typebot V6"
|
"title": "Typebot V6"
|
||||||
@@ -7003,15 +7006,14 @@
|
|||||||
},
|
},
|
||||||
"events": {
|
"events": {
|
||||||
"type": "array"
|
"type": "array"
|
||||||
|
},
|
||||||
|
"updatedAt": {
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"title": "Typebot V5"
|
"title": "Typebot V5"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
|
||||||
"updatedAt": {
|
|
||||||
"type": "string",
|
|
||||||
"description": "Used for checking if there is a newer version of the typebot in the database"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
|
|||||||
Reference in New Issue
Block a user