116 lines
3.9 KiB
JavaScript
116 lines
3.9 KiB
JavaScript
export const ticketNumber = (id) => `SYN-${String(id).padStart(5, '0')}`
|
|
const ticketPattern = /(?<![\p{L}\p{N}_])SYN-(\d{1,16})(?![\p{L}\p{N}_-])/giu
|
|
|
|
export function subjectTicketIds(subject) {
|
|
if (typeof subject !== 'string' || subject.length > 2000) return []
|
|
return [
|
|
...new Set(
|
|
[...subject.matchAll(ticketPattern)]
|
|
.map((match) => Number(match[1]))
|
|
.filter((id) => Number.isSafeInteger(id) && id > 0),
|
|
),
|
|
]
|
|
}
|
|
|
|
export function outgoingSubject(ticket, subject = ticket.subject, { reply = false } = {}) {
|
|
const base = String(subject || '')
|
|
.replace(/[\r\n\0]/g, ' ')
|
|
.replace(/\[\s*SYN-\d+\s*\]/gi, '')
|
|
.replace(ticketPattern, '')
|
|
.trim()
|
|
.replace(/^(?:(?:re|aw|antwort|fw|fwd|wg)\s*:\s*)+/i, '')
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
return `${reply ? 'Re: ' : ''}[${ticketNumber(ticket.id)}] ${base || '(Ohne Betreff)'}`.slice(
|
|
0,
|
|
998,
|
|
)
|
|
}
|
|
|
|
export function referenceIds(data) {
|
|
const values = [
|
|
...(Array.isArray(data.references) ? data.references.slice(-500) : [data.references]),
|
|
data.inReplyTo,
|
|
]
|
|
const found = []
|
|
for (const value of values) {
|
|
if (typeof value !== 'string') continue
|
|
const text = value.slice(-128000).trim()
|
|
const ids = text.match(/<[^<>\r\n]+>/g) || (text && !/\s/.test(text) ? [text] : [])
|
|
found.push(...ids.filter((id) => id.length <= 998))
|
|
}
|
|
return [...new Set(found.slice(-500).reverse())]
|
|
}
|
|
|
|
export function findThread(s, data, queue) {
|
|
const sender = data.from.toLowerCase()
|
|
let agent
|
|
const participant = (ticket) => {
|
|
if (ticket.customer.toLowerCase() === sender) return true
|
|
if (
|
|
s.get(
|
|
`SELECT 1 FROM message_recipients r JOIN messages m ON m.id=r.message_id
|
|
WHERE m.ticket_id=? AND m.kind='outgoing' AND lower(r.address)=?`,
|
|
ticket.id,
|
|
sender,
|
|
)
|
|
)
|
|
return true
|
|
if (agent === undefined)
|
|
agent =
|
|
s.get(
|
|
'SELECT id FROM users WHERE active=1 AND deleted_at IS NULL AND lower(email)=?',
|
|
sender,
|
|
) || null
|
|
return !!agent && s.can(agent, queue.id, 'view') && s.can(agent, queue.id, 'reply')
|
|
}
|
|
for (const ref of referenceIds(data)) {
|
|
const alternate = ref.startsWith('<') && ref.endsWith('>') ? ref.slice(1, -1) : `<${ref}>`
|
|
const candidates = s.all(
|
|
`SELECT t.*,m.message_id matched_reference FROM messages m JOIN tickets t ON t.id=m.ticket_id
|
|
WHERE m.message_id IN (?,?) AND m.kind!='note' AND t.queue_id=?
|
|
ORDER BY EXISTS(SELECT 1 FROM mail_imports i WHERE i.message_row_id=m.id AND i.queue_id=?) DESC,m.created_at DESC,m.rowid DESC`,
|
|
ref,
|
|
alternate,
|
|
queue.id,
|
|
queue.id,
|
|
)
|
|
for (const ticket of candidates)
|
|
if (participant(ticket))
|
|
return {
|
|
ticket,
|
|
method: 'message-id',
|
|
reference: ticket.matched_reference,
|
|
reason: 'Mail-Referenz und Absender passen zum Ticket dieser Queue.',
|
|
}
|
|
}
|
|
const ids = subjectTicketIds(data.subject)
|
|
if (ids.length === 1) {
|
|
const ticket = s.get('SELECT * FROM tickets WHERE id=? AND queue_id=?', ids[0], queue.id)
|
|
if (ticket && participant(ticket))
|
|
return {
|
|
ticket,
|
|
method: 'ticket-number',
|
|
reason:
|
|
'Ticketnummer im Betreff, Queue und Absender passen; keine passende Mail-Referenz gefunden.',
|
|
}
|
|
return {
|
|
ticket: null,
|
|
method: 'new-ticket',
|
|
reason: ticket
|
|
? 'Ticketnummer gefunden, aber Absender ist kein Kundenkontakt, eingeladener Empfänger oder berechtigter Bearbeiter.'
|
|
: 'Ticketnummer gehört zu keinem Ticket der adressierten Queue.',
|
|
}
|
|
}
|
|
return {
|
|
ticket: null,
|
|
method: 'new-ticket',
|
|
reason:
|
|
ids.length > 1
|
|
? 'Mehrere Ticketnummern im Betreff; keine eindeutige Zuordnung.'
|
|
: referenceIds(data).length
|
|
? 'Keine passende Mail-Referenz mit zulässigem Absender in dieser Queue gefunden.'
|
|
: 'Keine Mail-Referenz und keine eindeutige Ticketnummer im Betreff vorhanden.',
|
|
}
|
|
}
|