fix(whatsapp): preserve history across reauthorization
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import crypto from 'node:crypto'
|
||||
|
||||
export function whatsappCustomerStableId(jid) {
|
||||
const normalized = String(jid || '').trim().toLowerCase()
|
||||
const hash = crypto.createHash('sha256').update(normalized).digest('hex').slice(0, 24)
|
||||
return `wa-customer-${hash}`
|
||||
}
|
||||
|
||||
export function whatsappConversationActivityAt(conversation) {
|
||||
return Date.parse(
|
||||
conversation?.lastMessage?.createdAt
|
||||
|| conversation?.updatedAt
|
||||
|| conversation?.createdAt
|
||||
|| '',
|
||||
) || 0
|
||||
}
|
||||
|
||||
export function isWeakWhatsAppDisplayName(value, jid) {
|
||||
const name = String(value || '').trim()
|
||||
const jidName = String(jid || '').split('@')[0].replace(/\D/g, '')
|
||||
const digits = name.replace(/\D/g, '')
|
||||
return !name || (digits.length >= 7 && (!jidName || digits === jidName))
|
||||
}
|
||||
|
||||
export function reusableWhatsAppConversations(conversations, activeChannelIds, channelId, jid) {
|
||||
return conversations
|
||||
.filter((item) => (
|
||||
item.channel === 'whatsapp'
|
||||
&& String(item.whatsappJid || '') === String(jid)
|
||||
&& (
|
||||
String(item.channelId || '') === String(channelId)
|
||||
|| !activeChannelIds.has(String(item.channelId || String(item.id).split(':')[0]))
|
||||
)
|
||||
))
|
||||
.sort((left, right) => whatsappConversationActivityAt(right) - whatsappConversationActivityAt(left))
|
||||
}
|
||||
Reference in New Issue
Block a user