Fix import chatMessageFeedback messageIDs unique violation (#4919)
* fix: chatMessageFeedback unique violation * fix: sync chatMessage IDs and chatMessageFeedback messageIDs * feat: optimize chatMessageFeedback IDs replacement
This commit is contained in:
parent
d77919ba50
commit
910a3c5229
|
|
@ -269,11 +269,21 @@ async function replaceDuplicateIdsForChatMessage(
|
||||||
})
|
})
|
||||||
if (records.length < 0) return originalData
|
if (records.length < 0) return originalData
|
||||||
|
|
||||||
// replace duplicate ids found in db to new id
|
// Replace duplicate ChatMessage ids found in db with new ids,
|
||||||
|
// and update corresponding messageId references in ChatMessageFeedback
|
||||||
|
const idMap: { [key: string]: string } = {}
|
||||||
const dbExistingIds = new Set(records.map((record) => record.id))
|
const dbExistingIds = new Set(records.map((record) => record.id))
|
||||||
originalData.ChatMessage = originalData.ChatMessage.map((item) => {
|
originalData.ChatMessage = originalData.ChatMessage.map((item) => {
|
||||||
if (dbExistingIds.has(item.id)) {
|
if (dbExistingIds.has(item.id)) {
|
||||||
return { ...item, id: uuidv4() }
|
const newId = uuidv4()
|
||||||
|
idMap[item.id] = newId
|
||||||
|
return { ...item, id: newId }
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
})
|
||||||
|
originalData.ChatMessageFeedback = originalData.ChatMessageFeedback.map((item) => {
|
||||||
|
if (idMap[item.messageId]) {
|
||||||
|
return { ...item, messageId: idMap[item.messageId] }
|
||||||
}
|
}
|
||||||
return item
|
return item
|
||||||
})
|
})
|
||||||
|
|
@ -408,12 +418,28 @@ async function replaceDuplicateIdsForChatMessageFeedback(
|
||||||
const records = await queryRunner.manager.find(ChatMessageFeedback, {
|
const records = await queryRunner.manager.find(ChatMessageFeedback, {
|
||||||
where: { id: In(ids) }
|
where: { id: In(ids) }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// remove duplicate messageId
|
||||||
|
const seenMessageIds = new Set()
|
||||||
|
originalData.ChatMessageFeedback = originalData.ChatMessageFeedback.filter((feedback) => {
|
||||||
|
if (seenMessageIds.has(feedback.messageId)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
seenMessageIds.add(feedback.messageId)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
if (records.length < 0) return originalData
|
if (records.length < 0) return originalData
|
||||||
for (let record of records) {
|
|
||||||
const oldId = record.id
|
// replace duplicate ids found in db to new id
|
||||||
const newId = uuidv4()
|
const dbExistingIds = new Set(records.map((record) => record.id))
|
||||||
originalData = JSON.parse(JSON.stringify(originalData).replaceAll(oldId, newId))
|
originalData.ChatMessageFeedback = originalData.ChatMessageFeedback.map((item) => {
|
||||||
}
|
if (dbExistingIds.has(item.id)) {
|
||||||
|
const newId = uuidv4()
|
||||||
|
return { ...item, id: newId }
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
})
|
||||||
return originalData
|
return originalData
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new InternalFlowiseError(
|
throw new InternalFlowiseError(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue