1135 lines
40 KiB
JavaScript
1135 lines
40 KiB
JavaScript
import { scheduledTime } from './time.js'
|
|
import { acknowledge } from './mail-config.js'
|
|
import { safeHtml, composeContent, normaliseAttachments } from './content.js'
|
|
import { signatureContext, validateSignatureTemplate } from './signature-placeholders.js'
|
|
import { uid, now, actions, behaviors, createQueue, passwordHash } from './db.js'
|
|
export class HttpError extends Error {
|
|
constructor(status, message) {
|
|
super(message)
|
|
this.status = status
|
|
}
|
|
}
|
|
const fail = (status, msg) => {
|
|
throw new HttpError(status, msg)
|
|
}
|
|
const json = (v) => JSON.stringify(v ?? null)
|
|
const emailRE = /^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/
|
|
export class Service {
|
|
constructor(db) {
|
|
this.db = db
|
|
}
|
|
all(sql, ...p) {
|
|
return this.db.prepare(sql).all(...p)
|
|
}
|
|
get(sql, ...p) {
|
|
return this.db.prepare(sql).get(...p)
|
|
}
|
|
run(sql, ...p) {
|
|
return this.db.prepare(sql).run(...p)
|
|
}
|
|
tx(fn) {
|
|
return this.db.transaction(fn)()
|
|
}
|
|
user(id) {
|
|
return (
|
|
this.get(
|
|
'SELECT id,name,email,active,prefs,phone,job_title,department FROM users WHERE id=? AND active=1',
|
|
id,
|
|
) || fail(401, 'Bitte erneut anmelden.')
|
|
)
|
|
}
|
|
permissions(user) {
|
|
let p = {}
|
|
for (const g of this.all(
|
|
'SELECT g.permissions FROM groups_ g JOIN memberships m ON m.group_id=g.id WHERE m.user_id=?',
|
|
user.id,
|
|
))
|
|
for (const [q, a] of Object.entries(JSON.parse(g.permissions)))
|
|
p[q] = [...new Set([...(p[q] || []), ...a])]
|
|
return p
|
|
}
|
|
can(user, q, action) {
|
|
const p = this.permissions(user)
|
|
return (p['*'] || []).includes(action) || (p[q] || []).includes(action)
|
|
}
|
|
require(user, q, action) {
|
|
if (!this.can(user, q, action)) fail(403, 'Keine Berechtigung für diese Aktion.')
|
|
}
|
|
audit(actor, action, entity, before, after, ticket = null, queue = null) {
|
|
this.run(
|
|
'INSERT INTO audits(ticket_id,queue_id,actor,action,entity,before_value,after_value,created_at) VALUES(?,?,?,?,?,?,?,?)',
|
|
ticket,
|
|
queue,
|
|
actor,
|
|
action,
|
|
String(entity),
|
|
json(before),
|
|
json(after),
|
|
now(),
|
|
)
|
|
}
|
|
queue(id) {
|
|
const q =
|
|
this.get('SELECT * FROM queues WHERE id=? AND deleted_at IS NULL', id) ||
|
|
fail(404, 'Queue nicht gefunden.')
|
|
return {
|
|
...q,
|
|
addresses: JSON.parse(q.addresses),
|
|
signatures: JSON.parse(q.signatures).map((s) => ({
|
|
...s,
|
|
body: s.format === 'html' ? safeHtml(s.body) : s.body,
|
|
})),
|
|
statuses: this.all('SELECT * FROM statuses WHERE queue_id=? ORDER BY position', id),
|
|
}
|
|
}
|
|
openStatus(q) {
|
|
return (
|
|
this.get(
|
|
"SELECT * FROM statuses WHERE queue_id=? AND behavior='open' AND active=1 ORDER BY position LIMIT 1",
|
|
q,
|
|
) || fail(409, 'Kein aktiver Offen-Status konfiguriert.')
|
|
)
|
|
}
|
|
ticket(user, id, action = 'view') {
|
|
const t =
|
|
this.get('SELECT * FROM tickets WHERE id=?', id) || fail(404, 'Ticket nicht gefunden.')
|
|
this.require(user, t.queue_id, 'view')
|
|
this.require(user, t.queue_id, action)
|
|
return t
|
|
}
|
|
bootstrap(user) {
|
|
return {
|
|
user: { ...user, prefs: JSON.parse(user.prefs) },
|
|
permissions: this.permissions(user),
|
|
queues: this.all('SELECT id FROM queues WHERE deleted_at IS NULL')
|
|
.filter((q) => this.can(user, q.id, 'view'))
|
|
.map((q) => this.queue(q.id)),
|
|
users: this.all('SELECT id,name FROM users WHERE active=1'),
|
|
adapter: process.env.MAIL_MODE || 'development',
|
|
}
|
|
}
|
|
tickets(user, filters = {}) {
|
|
const allowed = this.all('SELECT id FROM queues WHERE deleted_at IS NULL')
|
|
.filter((q) => this.can(user, q.id, 'view'))
|
|
.map((q) => q.id)
|
|
if (!allowed.length) return []
|
|
let rows = this.all(
|
|
`SELECT t.*,s.name status_name,s.color status_color,s.behavior,q.name queue_name,q.color queue_color,u.name assignee_name,(SELECT body FROM messages WHERE ticket_id=t.id ORDER BY created_at DESC LIMIT 1) preview FROM tickets t JOIN statuses s ON s.id=t.status_id JOIN queues q ON q.id=t.queue_id LEFT JOIN users u ON u.id=t.assignee WHERE t.queue_id IN (${allowed.map(() => '?')})`,
|
|
...allowed,
|
|
)
|
|
return rows
|
|
.filter(
|
|
(t) =>
|
|
(!filters.q ||
|
|
`${t.id} ${t.subject} ${t.customer} ${t.customer_name}`
|
|
.toLowerCase()
|
|
.includes(filters.q.toLowerCase())) &&
|
|
(!filters.queue || t.queue_id === filters.queue) &&
|
|
(!filters.status || t.behavior === filters.status || t.status_id === filters.status) &&
|
|
(!filters.assignee ||
|
|
(filters.assignee === 'unassigned' ? !t.assignee : t.assignee === filters.assignee)),
|
|
)
|
|
.sort((a, b) =>
|
|
filters.sort === 'oldest'
|
|
? a.updated_at.localeCompare(b.updated_at)
|
|
: filters.sort === 'subject'
|
|
? a.subject.localeCompare(b.subject)
|
|
: b.updated_at.localeCompare(a.updated_at),
|
|
)
|
|
}
|
|
detail(user, id) {
|
|
const t = this.ticket(user, id)
|
|
return {
|
|
...t,
|
|
messages: this.all(
|
|
'SELECT * FROM messages WHERE ticket_id=? ORDER BY created_at,rowid',
|
|
t.id,
|
|
).map((m) => ({
|
|
...m,
|
|
html: this.get('SELECT html FROM message_html WHERE message_id=?', m.id)?.html || null,
|
|
attachments: this.all(
|
|
'SELECT id,name,mime,length(content) size FROM attachments WHERE message_id=?',
|
|
m.id,
|
|
),
|
|
})),
|
|
activities: this.all(
|
|
'SELECT a.*,u.name actor_name FROM audits a LEFT JOIN users u ON u.id=a.actor WHERE a.ticket_id=? ORDER BY a.id DESC',
|
|
t.id,
|
|
),
|
|
}
|
|
}
|
|
notify(ticket, event, key, title) {
|
|
for (const u of this.all('SELECT id,email,prefs FROM users WHERE active=1')) {
|
|
if (
|
|
!this.can(u, ticket.queue_id, 'view') ||
|
|
(event === 'assigned' && ticket.assignee !== u.id)
|
|
)
|
|
continue
|
|
const prefs = JSON.parse(u.prefs)
|
|
const channels = prefs[event] || { bell: true, email: false }
|
|
const n = uid()
|
|
const insert = this.run(
|
|
'INSERT OR IGNORE INTO notifications(id,user_id,ticket_id,event,event_key,title,is_read,created_at) VALUES(?,?,?,?,?,?,?,?)',
|
|
n,
|
|
u.id,
|
|
ticket.id,
|
|
event,
|
|
key,
|
|
title,
|
|
channels.bell ? 0 : 1,
|
|
now(),
|
|
)
|
|
if (insert.changes && channels.email)
|
|
this.run(
|
|
'INSERT INTO outbox(id,notification_id,payload,next_at) VALUES(?,?,?,?)',
|
|
uid(),
|
|
n,
|
|
json({
|
|
to: u.email,
|
|
from: this.queue(ticket.queue_id).addresses.find((a) => a.outgoing && a.default)?.email,
|
|
subject: title,
|
|
text: `Ticket #${String(ticket.id).padStart(5, '0')}: ${ticket.subject}\n${process.env.APP_ORIGIN || 'http://127.0.0.1:5173'}/#ticket=${ticket.id}`,
|
|
}),
|
|
now(),
|
|
)
|
|
}
|
|
}
|
|
notifications(user) {
|
|
return this.all(
|
|
'SELECT n.* FROM notifications n JOIN tickets t ON t.id=n.ticket_id WHERE n.user_id=? ORDER BY n.created_at DESC LIMIT 100',
|
|
user.id,
|
|
).filter((n) => {
|
|
const t = this.get('SELECT queue_id FROM tickets WHERE id=?', n.ticket_id)
|
|
const prefs = JSON.parse(user.prefs || '{}')
|
|
return t && this.can(user, t.queue_id, 'view') && (prefs[n.event]?.bell ?? true)
|
|
})
|
|
}
|
|
readNotifications(user, id) {
|
|
const ids = this.notifications(user)
|
|
.filter((n) => !id || id === n.id)
|
|
.map((n) => n.id)
|
|
this.tx(() =>
|
|
ids.forEach((n) =>
|
|
this.run('UPDATE notifications SET is_read=1 WHERE id=? AND user_id=?', n, user.id),
|
|
),
|
|
)
|
|
}
|
|
transition(t, status, actor, extra = {}) {
|
|
const before = { status_id: t.status_id, due_at: t.due_at, timezone: t.timezone }
|
|
this.run(
|
|
'UPDATE tickets SET status_id=?,due_at=?,timezone=?,revision=revision+1,updated_at=? WHERE id=?',
|
|
status.id,
|
|
extra.due_at || null,
|
|
extra.timezone || null,
|
|
now(),
|
|
t.id,
|
|
)
|
|
this.audit(
|
|
actor,
|
|
'Status geändert',
|
|
t.id,
|
|
before,
|
|
{ status_id: status.id, name: status.name, ...extra },
|
|
t.id,
|
|
t.queue_id,
|
|
)
|
|
}
|
|
updateTicket(user, id, data) {
|
|
return this.tx(() => {
|
|
let t = this.ticket(user, id)
|
|
if (data.revision !== undefined && data.revision !== t.revision)
|
|
fail(409, 'Das Ticket wurde zwischenzeitlich geändert. Bitte neu laden.')
|
|
if (data.queue_id && data.queue_id !== t.queue_id) {
|
|
this.require(user, t.queue_id, 'move')
|
|
this.require(user, data.queue_id, 'move')
|
|
this.require(user, data.queue_id, 'view')
|
|
const q = this.queue(data.queue_id)
|
|
if (!q.addresses.some((a) => a.outgoing && a.default))
|
|
fail(409, 'Zielqueue benötigt einen Standardabsender.')
|
|
const old = this.get('SELECT behavior FROM statuses WHERE id=?', t.status_id)
|
|
const s =
|
|
q.statuses.find((s) => s.id === data.status_id && s.active) ||
|
|
q.statuses.find((s) => s.behavior === old.behavior && s.active) ||
|
|
this.openStatus(q.id)
|
|
if (s.behavior === 'wait_date' && !t.due_at) fail(400, 'Termin erforderlich.')
|
|
this.run(
|
|
'UPDATE tickets SET queue_id=?,status_id=?,assignee=NULL,due_at=CASE WHEN ? THEN due_at ELSE NULL END,timezone=CASE WHEN ? THEN timezone ELSE NULL END,revision=revision+1,updated_at=? WHERE id=?',
|
|
q.id,
|
|
s.id,
|
|
s.behavior === 'wait_date' ? 1 : 0,
|
|
s.behavior === 'wait_date' ? 1 : 0,
|
|
now(),
|
|
t.id,
|
|
)
|
|
this.audit(
|
|
user.id,
|
|
'Queue gewechselt',
|
|
id,
|
|
{ queue: t.queue_id, status: t.status_id, assignee: t.assignee },
|
|
{ queue: q.id, status: s.id, assignee: null },
|
|
t.id,
|
|
q.id,
|
|
)
|
|
t = this.get('SELECT * FROM tickets WHERE id=?', id)
|
|
}
|
|
if (data.status_id || data.due_at !== undefined) {
|
|
this.require(user, t.queue_id, 'status')
|
|
const s =
|
|
this.get(
|
|
'SELECT * FROM statuses WHERE id=? AND queue_id=? AND active=1',
|
|
data.status_id || t.status_id,
|
|
t.queue_id,
|
|
) || fail(400, 'Ungültiger Status.')
|
|
let extra = {}
|
|
if (s.behavior === 'wait_date') {
|
|
const due = data.due_at ?? t.due_at
|
|
const zone = data.timezone ?? t.timezone
|
|
try {
|
|
new Intl.DateTimeFormat('de', { timeZone: zone }).format()
|
|
if (!zone || !due || !Number.isFinite(Date.parse(due))) throw Error()
|
|
} catch {
|
|
fail(400, 'Gültiger Termin mit Zeitzone erforderlich.')
|
|
}
|
|
try {
|
|
extra = { due_at: scheduledTime(due, zone), timezone: zone }
|
|
} catch {
|
|
fail(
|
|
400,
|
|
'Gültige Ortszeit und Zeitzone erforderlich. Die Uhrzeit darf nicht in einer Sommerzeitlücke liegen.',
|
|
)
|
|
}
|
|
}
|
|
this.transition(t, s, user.id, extra)
|
|
t = this.get('SELECT * FROM tickets WHERE id=?', id)
|
|
}
|
|
if (data.assignee !== undefined) {
|
|
this.require(user, t.queue_id, 'assign')
|
|
if (data.assignee) {
|
|
const u = this.user(data.assignee)
|
|
if (!this.can(u, t.queue_id, 'view'))
|
|
fail(400, 'Benutzer hat keinen Zugriff auf diese Queue.')
|
|
}
|
|
this.run(
|
|
'UPDATE tickets SET assignee=?,updated_at=?,revision=revision+1 WHERE id=?',
|
|
data.assignee || null,
|
|
now(),
|
|
t.id,
|
|
)
|
|
this.audit(
|
|
user.id,
|
|
'Zuständigkeit geändert',
|
|
id,
|
|
t.assignee,
|
|
data.assignee || null,
|
|
t.id,
|
|
t.queue_id,
|
|
)
|
|
if (data.assignee)
|
|
this.notify(
|
|
{ ...t, assignee: data.assignee },
|
|
'assigned',
|
|
uid(),
|
|
'Ihnen wurde ein Ticket zugewiesen',
|
|
)
|
|
}
|
|
return this.detail(user, id)
|
|
})
|
|
}
|
|
goods(user, id) {
|
|
return this.tx(() => {
|
|
const t = this.ticket(user, id, 'goods')
|
|
if (
|
|
this.get('SELECT behavior FROM statuses WHERE id=?', t.status_id).behavior !== 'wait_goods'
|
|
)
|
|
fail(409, 'Ticket wartet nicht auf Ware.')
|
|
this.audit(
|
|
user.id,
|
|
'Wareneingang bestätigt',
|
|
id,
|
|
null,
|
|
{ confirmed_at: now() },
|
|
t.id,
|
|
t.queue_id,
|
|
)
|
|
this.transition(t, this.openStatus(t.queue_id), user.id)
|
|
return this.detail(user, id)
|
|
})
|
|
}
|
|
reply(user, id, data) {
|
|
return this.tx(() => {
|
|
const kind = data.kind === 'note' ? 'note' : 'outgoing'
|
|
const t = this.ticket(user, id, kind === 'note' ? 'note' : 'reply')
|
|
if (
|
|
(data.body !== undefined && typeof data.body !== 'string') ||
|
|
(data.body || '').length > 100000
|
|
)
|
|
fail(400, 'Nachricht erforderlich (max. 100.000 Zeichen).')
|
|
if (!data.key || data.key.length > 100) fail(400, 'Idempotenzschlüssel erforderlich.')
|
|
const mid = `<${data.key}@syncova.local>`
|
|
const old = this.get('SELECT * FROM messages WHERE message_id=?', mid)
|
|
if (old) {
|
|
if (old.ticket_id !== t.id) fail(409, 'Schlüssel bereits verwendet.')
|
|
return old
|
|
}
|
|
const attachments = normaliseAttachments(data.attachments)
|
|
if (!(data.body || '').trim() && !attachments.length)
|
|
fail(400, 'Nachricht oder Anhang erforderlich.')
|
|
let from = user.email
|
|
const q = this.queue(t.queue_id)
|
|
if (kind === 'outgoing') {
|
|
if (!q.addresses.some((a) => a.email === data.from && a.outgoing))
|
|
fail(400, 'Absender ist für diese Queue nicht freigegeben.')
|
|
from = data.from
|
|
}
|
|
const signature =
|
|
kind === 'outgoing'
|
|
? data.signature_id
|
|
? q.signatures.find((s) => s.id === data.signature_id)
|
|
: q.signatures.find((s) => s.default)
|
|
: null
|
|
if (kind === 'outgoing' && data.signature_id && !signature)
|
|
fail(400, 'Signatur gehört nicht zur Queue.')
|
|
const content =
|
|
kind === 'outgoing'
|
|
? composeContent(data.body || '', signature, signatureContext(this, user, q, t, from))
|
|
: { text: data.body || '', html: null }
|
|
const m = uid(),
|
|
refs = this.all(
|
|
"SELECT message_id FROM messages WHERE ticket_id=? AND kind!='note' ORDER BY created_at",
|
|
t.id,
|
|
).map((m) => m.message_id)
|
|
this.run(
|
|
'INSERT INTO messages VALUES(?,?,?,?,?,?,?,?,?,?)',
|
|
m,
|
|
t.id,
|
|
kind,
|
|
from,
|
|
kind === 'note' ? 'intern' : t.customer,
|
|
content.text,
|
|
mid,
|
|
json(refs),
|
|
now(),
|
|
kind === 'note' ? 'internal' : 'queued',
|
|
)
|
|
if (content.html) this.run('INSERT INTO message_html VALUES(?,?)', m, content.html)
|
|
for (const a of attachments)
|
|
this.run('INSERT INTO attachments VALUES(?,?,?,?,?)', uid(), m, a.name, a.mime, a.content)
|
|
if (kind === 'outgoing')
|
|
this.run(
|
|
'INSERT INTO outbox(id,message_id,payload,next_at) VALUES(?,?,?,?)',
|
|
uid(),
|
|
m,
|
|
json({
|
|
from,
|
|
to: t.customer,
|
|
subject: `Re: ${t.subject}`,
|
|
text: content.text,
|
|
...(content.html ? { html: content.html } : {}),
|
|
messageId: mid,
|
|
inReplyTo: refs.at(-1),
|
|
references: refs,
|
|
}),
|
|
now(),
|
|
)
|
|
this.run('UPDATE tickets SET updated_at=?,revision=revision+1 WHERE id=?', now(), t.id)
|
|
this.audit(
|
|
user.id,
|
|
kind === 'note' ? 'Interne Notiz hinzugefügt' : 'Antwort zum Versand vorgemerkt',
|
|
m,
|
|
null,
|
|
{
|
|
from,
|
|
kind,
|
|
attachments: attachments.map((a) => ({ name: a.name, size: a.content.length })),
|
|
},
|
|
t.id,
|
|
t.queue_id,
|
|
)
|
|
return this.get('SELECT * FROM messages WHERE id=?', m)
|
|
})
|
|
}
|
|
createTicket(user, data) {
|
|
return this.tx(() => {
|
|
this.require(user, data.queue_id, 'view')
|
|
this.require(user, data.queue_id, 'reply')
|
|
const q = this.queue(data.queue_id)
|
|
if (
|
|
typeof data.key !== 'string' ||
|
|
!data.key ||
|
|
data.key.length > 100 ||
|
|
typeof data.subject !== 'string' ||
|
|
!data.subject.trim() ||
|
|
data.subject.length > 500 ||
|
|
typeof data.body !== 'string' ||
|
|
!data.body.trim() ||
|
|
data.body.length > 100000 ||
|
|
!emailRE.test(data.customer) ||
|
|
typeof data.customer_name !== 'string' ||
|
|
data.customer_name.length > 200
|
|
)
|
|
fail(400, 'Queue, Kundenkontakt, Betreff und Beschreibung gültig ausfüllen.')
|
|
const previous = this.get(
|
|
'SELECT ticket_id FROM manual_requests WHERE user_id=? AND request_key=?',
|
|
user.id,
|
|
data.key,
|
|
)
|
|
if (previous) return this.detail(user, previous.ticket_id)
|
|
const status =
|
|
q.statuses.find((s) => s.active && s.behavior === 'new') || this.openStatus(q.id),
|
|
at = now()
|
|
const id = Number(
|
|
this.run(
|
|
'INSERT INTO tickets(subject,customer,customer_name,queue_id,status_id,created_at,updated_at) VALUES(?,?,?,?,?,?,?)',
|
|
data.subject.trim(),
|
|
data.customer.toLowerCase(),
|
|
data.customer_name.trim() || data.customer,
|
|
q.id,
|
|
status.id,
|
|
at,
|
|
at,
|
|
).lastInsertRowid,
|
|
)
|
|
this.run(
|
|
'INSERT INTO messages VALUES(?,?,?,?,?,?,?,?,?,?)',
|
|
uid(),
|
|
id,
|
|
'manual',
|
|
data.customer,
|
|
q.name,
|
|
data.body,
|
|
`<manual-${uid()}@syncova.local>`,
|
|
'[]',
|
|
at,
|
|
'manual',
|
|
)
|
|
this.run('INSERT INTO manual_requests VALUES(?,?,?)', user.id, data.key, id)
|
|
this.audit(
|
|
user.id,
|
|
'Ticket manuell erstellt',
|
|
id,
|
|
null,
|
|
{ queue: q.id, customer: data.customer },
|
|
id,
|
|
q.id,
|
|
)
|
|
const t = this.get('SELECT * FROM tickets WHERE id=?', id)
|
|
this.notify(t, 'new', `manual:${id}`, 'Neues Ticket manuell erstellt')
|
|
return this.detail(user, id)
|
|
})
|
|
}
|
|
ingest(data, expectedQueue) {
|
|
return this.tx(() => {
|
|
if (
|
|
!data.messageId ||
|
|
data.messageId.length > 998 ||
|
|
!emailRE.test(data.from) ||
|
|
!data.to?.length ||
|
|
!data.body?.trim()
|
|
)
|
|
fail(400, 'Ungültige E-Mail.')
|
|
if (
|
|
(data.autoSubmitted && data.autoSubmitted !== 'no') ||
|
|
(data.precedence && ['bulk', 'junk', 'list'].includes(data.precedence))
|
|
)
|
|
return { ignored: true }
|
|
if (this.get('SELECT id FROM messages WHERE message_id=?', data.messageId))
|
|
return { duplicate: true }
|
|
const addresses = data.to.map((a) => a.toLowerCase())
|
|
const qs = this.all('SELECT id FROM queues WHERE deleted_at IS NULL').map((q) =>
|
|
this.queue(q.id),
|
|
)
|
|
const matched = qs.filter((q) =>
|
|
q.addresses.some((a) => a.incoming && addresses.includes(a.email.toLowerCase())),
|
|
)
|
|
if (matched.length !== 1) fail(400, 'Empfänger muss genau einer Queue zugeordnet sein.')
|
|
const q = matched[0]
|
|
if (expectedQueue && q.id !== expectedQueue)
|
|
fail(400, 'Empfänger gehört nicht zur abrufenden Queue.')
|
|
if (
|
|
qs.some((q) => q.addresses.some((a) => a.email.toLowerCase() === data.from.toLowerCase()))
|
|
)
|
|
return { ignored: true }
|
|
let t
|
|
for (const ref of [...(data.references || []), data.inReplyTo].filter(Boolean).reverse()) {
|
|
const m = this.get(
|
|
'SELECT t.* FROM messages m JOIN tickets t ON t.id=m.ticket_id WHERE m.message_id=? AND m.kind!=?',
|
|
ref,
|
|
'note',
|
|
)
|
|
if (m && m.queue_id === q.id && m.customer.toLowerCase() === data.from.toLowerCase()) {
|
|
t = m
|
|
break
|
|
}
|
|
}
|
|
const isNew = !t
|
|
const at = now()
|
|
if (!t) {
|
|
const s = q.statuses.find((s) => s.active && s.behavior === 'new') || this.openStatus(q.id)
|
|
const r = this.run(
|
|
'INSERT INTO tickets(subject,customer,customer_name,queue_id,status_id,created_at,updated_at) VALUES(?,?,?,?,?,?,?)',
|
|
(data.subject || '(Ohne Betreff)').slice(0, 500),
|
|
data.from,
|
|
data.name || data.from,
|
|
q.id,
|
|
s.id,
|
|
at,
|
|
at,
|
|
)
|
|
t = this.get('SELECT * FROM tickets WHERE id=?', r.lastInsertRowid)
|
|
}
|
|
const id = uid()
|
|
this.run(
|
|
'INSERT INTO messages(id,ticket_id,kind,sender,recipient,body,message_id,refs,created_at) VALUES(?,?,?,?,?,?,?,?,?)',
|
|
id,
|
|
t.id,
|
|
'incoming',
|
|
data.from,
|
|
addresses.join(', '),
|
|
data.body.slice(0, 100000),
|
|
data.messageId,
|
|
json(data.references || []),
|
|
at,
|
|
)
|
|
for (const a of normaliseAttachments(data.attachments)) {
|
|
this.run('INSERT INTO attachments VALUES(?,?,?,?,?)', uid(), id, a.name, a.mime, a.content)
|
|
}
|
|
this.run('UPDATE tickets SET updated_at=?,revision=revision+1 WHERE id=?', at, t.id)
|
|
this.audit(
|
|
'E-Mail-Adapter',
|
|
'E-Mail empfangen',
|
|
id,
|
|
null,
|
|
{ messageId: data.messageId },
|
|
t.id,
|
|
q.id,
|
|
)
|
|
if (
|
|
this.get('SELECT behavior FROM statuses WHERE id=?', t.status_id).behavior ===
|
|
'wait_customer'
|
|
) {
|
|
this.transition(t, this.openStatus(q.id), 'Kundenantwort')
|
|
this.notify(
|
|
t,
|
|
'reopened',
|
|
`reopened:${data.messageId}`,
|
|
'Ticket durch Kundenantwort wieder geöffnet',
|
|
)
|
|
}
|
|
this.notify(
|
|
t,
|
|
isNew ? 'new' : 'reply',
|
|
data.messageId,
|
|
isNew ? 'Neue Anfrage eingegangen' : 'Neue Kundenantwort',
|
|
)
|
|
if (isNew) acknowledge(this, t, data)
|
|
return { ticket_id: t.id }
|
|
})
|
|
}
|
|
due() {
|
|
return this.tx(() => {
|
|
let count = 0
|
|
for (const t of this.all(
|
|
"SELECT t.* FROM tickets t JOIN statuses s ON s.id=t.status_id WHERE s.behavior='wait_date' AND t.due_at<=?",
|
|
now(),
|
|
)) {
|
|
this.transition(t, this.openStatus(t.queue_id), 'Terminprozess')
|
|
this.notify(
|
|
t,
|
|
'reopened',
|
|
`due:${t.id}:${t.revision}`,
|
|
'Ticket automatisch wieder geöffnet',
|
|
)
|
|
count++
|
|
}
|
|
return count
|
|
})
|
|
}
|
|
saveQueue(user, id, data) {
|
|
return this.tx(() => {
|
|
this.require(user, id || '*', 'queue')
|
|
const before = id ? this.queue(id) : null
|
|
if (!data.name?.trim()) fail(400, 'Queue-Name erforderlich.')
|
|
if (
|
|
!Array.isArray(data.addresses) ||
|
|
!data.addresses.some((a) => a.incoming) ||
|
|
data.addresses.filter((a) => a.outgoing && a.default).length !== 1
|
|
)
|
|
fail(400, 'Mindestens ein Eingang und genau ein Standardabsender erforderlich.')
|
|
const seen = new Set()
|
|
for (const a of data.addresses) {
|
|
a.email = String(a.email).toLowerCase().trim()
|
|
if (!emailRE.test(a.email) || seen.has(a.email))
|
|
fail(400, 'Ungültige oder doppelte Adresse.')
|
|
seen.add(a.email)
|
|
for (const q of this.all(
|
|
'SELECT id,addresses FROM queues WHERE id!=? AND deleted_at IS NULL',
|
|
id || '',
|
|
))
|
|
if (JSON.parse(q.addresses).some((b) => b.email === a.email))
|
|
fail(409, 'Adresse wird bereits von einer anderen Queue verwendet.')
|
|
}
|
|
const sigs = before?.signatures || [
|
|
{
|
|
id: uid(),
|
|
name: 'Standard',
|
|
body: `Freundliche Grüße\nIhr ${data.name}-Team`,
|
|
default: true,
|
|
},
|
|
]
|
|
if (id)
|
|
this.run(
|
|
'UPDATE queues SET name=?,color=?,addresses=? WHERE id=?',
|
|
data.name,
|
|
data.color || before.color,
|
|
json(data.addresses),
|
|
id,
|
|
)
|
|
else
|
|
id = createQueue(this.db, {
|
|
name: data.name,
|
|
color: data.color,
|
|
addresses: data.addresses,
|
|
signatures: sigs,
|
|
})
|
|
this.audit(user.id, 'Queue konfiguriert', id, before, this.queue(id), null, id)
|
|
return this.queue(id)
|
|
})
|
|
}
|
|
queueDeletion(user, id) {
|
|
this.require(user, id, 'queue')
|
|
const q = this.queue(id)
|
|
return {
|
|
id: q.id,
|
|
name: q.name,
|
|
tickets: this.get('SELECT count(*) count FROM tickets WHERE queue_id=?', id).count,
|
|
statuses: q.statuses.map((s) => ({
|
|
...s,
|
|
count: this.get(
|
|
'SELECT count(*) count FROM tickets WHERE queue_id=? AND status_id=?',
|
|
id,
|
|
s.id,
|
|
).count,
|
|
})),
|
|
targets: this.all('SELECT id FROM queues WHERE deleted_at IS NULL AND id!=?', id)
|
|
.filter(
|
|
(q) =>
|
|
this.can(user, q.id, 'view') &&
|
|
this.can(user, q.id, 'move') &&
|
|
this.can(user, q.id, 'status'),
|
|
)
|
|
.map((q) => this.queue(q.id)),
|
|
}
|
|
}
|
|
deleteQueue(user, id, data) {
|
|
return this.tx(() => {
|
|
this.require(user, id, 'queue')
|
|
const q = this.queue(id)
|
|
if (data.confirm !== q.name) fail(400, 'Zum Bestätigen den exakten Queue-Namen eingeben.')
|
|
const tickets = this.all('SELECT * FROM tickets WHERE queue_id=?', id)
|
|
const inbound = this.get('SELECT config FROM queue_inbound WHERE queue_id=?', id)
|
|
if (inbound && JSON.parse(inbound.config).enabled)
|
|
fail(409, 'IMAP-Empfang vor dem Löschen deaktivieren.')
|
|
if (
|
|
this.get(
|
|
"SELECT count(*) n FROM outbox o JOIN messages m ON m.id=o.message_id JOIN tickets t ON t.id=m.ticket_id WHERE t.queue_id=? AND o.state='sending'",
|
|
id,
|
|
).n
|
|
)
|
|
fail(409, 'Ein E-Mail-Versand läuft. Bitte abwarten.')
|
|
if (tickets.length) {
|
|
if (!data.target || data.target === id)
|
|
fail(409, 'Für vorhandene Tickets eine Zielqueue auswählen.')
|
|
this.require(user, id, 'move')
|
|
this.require(user, data.target, 'view')
|
|
this.require(user, data.target, 'move')
|
|
this.require(user, data.target, 'status')
|
|
const target = this.queue(data.target)
|
|
this.openStatus(target.id)
|
|
if (
|
|
!target.addresses.some((a) => a.outgoing && a.default) ||
|
|
!target.signatures.some((s) => s.default)
|
|
)
|
|
fail(409, 'Zielqueue benötigt Standardabsender und Standardsignatur.')
|
|
for (const t of tickets) {
|
|
const sourceStatus = q.statuses.find((s) => s.id === t.status_id),
|
|
chosen = data.mapping?.[t.status_id]
|
|
const status = chosen
|
|
? target.statuses.find((s) => s.id === chosen && s.active)
|
|
: target.statuses.find((s) => s.active && s.behavior === sourceStatus.behavior) ||
|
|
this.openStatus(target.id)
|
|
if (!status) fail(400, 'Ungültige Statuszuordnung.')
|
|
this.updateTicket(user, t.id, { queue_id: target.id, status_id: status.id })
|
|
}
|
|
}
|
|
for (const g of this.all('SELECT * FROM groups_')) {
|
|
const permissions = JSON.parse(g.permissions)
|
|
if (permissions[id]) {
|
|
const before = structuredClone(permissions)
|
|
delete permissions[id]
|
|
this.run('UPDATE groups_ SET permissions=? WHERE id=?', json(permissions), g.id)
|
|
this.audit(user.id, 'Queue-Berechtigungen entfernt', g.id, before, permissions, null, id)
|
|
}
|
|
}
|
|
this.run('DELETE FROM queue_mail WHERE queue_id=?', id)
|
|
this.run('DELETE FROM queue_inbound WHERE queue_id=?', id)
|
|
this.run('DELETE FROM inbound_receipts WHERE queue_id=?', id)
|
|
this.run('UPDATE statuses SET active=0 WHERE queue_id=?', id)
|
|
this.run(
|
|
"UPDATE queues SET deleted_at=?,addresses='[]',signatures='[]' WHERE id=?",
|
|
now(),
|
|
id,
|
|
)
|
|
this.audit(
|
|
user.id,
|
|
'Queue gelöscht',
|
|
id,
|
|
q,
|
|
{ deleted: true, target: data.target || null, moved: tickets.length },
|
|
null,
|
|
id,
|
|
)
|
|
return { ok: true, moved: tickets.length }
|
|
})
|
|
}
|
|
saveSignatures(user, id, signatures) {
|
|
return this.tx(() => {
|
|
this.require(user, id, 'signatures')
|
|
const q = this.queue(id)
|
|
if (
|
|
!Array.isArray(signatures) ||
|
|
signatures.filter((s) => s.default).length !== 1 ||
|
|
signatures.some(
|
|
(s) =>
|
|
!s.name?.trim() ||
|
|
typeof s.body !== 'string' ||
|
|
!s.body.trim() ||
|
|
s.body.length > 100000 ||
|
|
(s.format && !['text', 'html'].includes(s.format)),
|
|
)
|
|
)
|
|
fail(400, 'Genau eine Standardsignatur und ausgefüllte Felder erforderlich.')
|
|
const clean = signatures.map((s) => ({
|
|
id: s.id || uid(),
|
|
name: s.name.trim(),
|
|
format: s.format || 'text',
|
|
body: s.format === 'html' ? safeHtml(s.body) : s.body,
|
|
default: !!s.default,
|
|
}))
|
|
clean.forEach((s) => validateSignatureTemplate(s.body))
|
|
if (clean.some((s) => !s.body.trim()))
|
|
fail(400, 'Signatur enthält keinen unterstützten Inhalt.')
|
|
this.run('UPDATE queues SET signatures=? WHERE id=?', json(clean), id)
|
|
this.audit(user.id, 'Signaturen geändert', id, q.signatures, clean, null, id)
|
|
})
|
|
}
|
|
saveStatuses(user, id, statuses, mapping = {}) {
|
|
return this.tx(() => {
|
|
this.require(user, id, 'statuses')
|
|
const q = this.queue(id)
|
|
if (
|
|
!Array.isArray(statuses) ||
|
|
!statuses.some((s) => s.active && s.behavior === 'open') ||
|
|
statuses.some(
|
|
(s) =>
|
|
!s.name?.trim() ||
|
|
!behaviors.includes(s.behavior) ||
|
|
!/^#[0-9a-fA-F]{6}$/.test(s.color),
|
|
)
|
|
)
|
|
fail(400, 'Gültige Status und ein aktiver Offen-Status erforderlich.')
|
|
const suppliedIds = statuses.filter((s) => s.id).map((s) => s.id)
|
|
if (new Set(suppliedIds).size !== suppliedIds.length)
|
|
fail(400, 'Doppelte Statuskennungen sind nicht erlaubt.')
|
|
const oldIds = new Set(q.statuses.map((s) => s.id))
|
|
for (const s of statuses) {
|
|
if (s.id && !oldIds.has(s.id)) fail(400, 'Status gehört nicht zur Queue.')
|
|
s.id ||= uid()
|
|
const old = q.statuses.find((o) => o.id === s.id)
|
|
if (
|
|
old &&
|
|
old.behavior !== s.behavior &&
|
|
this.get('SELECT count(*) n FROM tickets WHERE status_id=?', s.id).n
|
|
)
|
|
fail(409, 'Verhalten verwendeter Status ist unveränderlich. Bitte neuen Status anlegen.')
|
|
this.run(
|
|
'INSERT INTO statuses VALUES(?,?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,color=excluded.color,behavior=excluded.behavior,position=excluded.position,active=excluded.active',
|
|
s.id,
|
|
id,
|
|
s.name,
|
|
s.color,
|
|
s.behavior,
|
|
statuses.indexOf(s),
|
|
s.active ? 1 : 0,
|
|
)
|
|
}
|
|
for (const old of q.statuses) {
|
|
if (!statuses.some((s) => s.id === old.id && s.active)) {
|
|
const tickets = this.all('SELECT * FROM tickets WHERE status_id=?', old.id)
|
|
if (tickets.length) {
|
|
const replacement = statuses.find(
|
|
(s) => s.id === mapping[old.id] && s.active && s.id !== old.id,
|
|
)
|
|
if (!replacement || replacement.behavior === 'wait_date')
|
|
fail(
|
|
409,
|
|
'Verwendete Status benötigen eine gültige Zuordnung zu einem aktiven Status ohne Terminvoraussetzung.',
|
|
)
|
|
for (const t of tickets) this.transition(t, replacement, user.id)
|
|
}
|
|
this.run('UPDATE statuses SET active=0 WHERE id=?', old.id)
|
|
}
|
|
}
|
|
this.openStatus(id)
|
|
this.audit(user.id, 'Status konfiguriert', id, q.statuses, statuses, null, id)
|
|
return this.queue(id)
|
|
})
|
|
}
|
|
admin(user) {
|
|
this.require(user, '*', 'users')
|
|
return {
|
|
users: this.all(
|
|
'SELECT id,name,email,active,phone,job_title,department FROM users WHERE deleted_at IS NULL',
|
|
).map((u) => ({
|
|
...u,
|
|
groups: this.all('SELECT group_id FROM memberships WHERE user_id=?', u.id).map(
|
|
(g) => g.group_id,
|
|
),
|
|
})),
|
|
groups: this.all('SELECT * FROM groups_').map((g) => ({
|
|
...g,
|
|
permissions: JSON.parse(g.permissions),
|
|
})),
|
|
actions,
|
|
}
|
|
}
|
|
saveUser(user, data) {
|
|
return this.tx(() => {
|
|
this.require(user, '*', 'users')
|
|
if (!data.name?.trim() || !emailRE.test(data.email) || !Array.isArray(data.groups))
|
|
fail(400, 'Ungültige Benutzerdaten.')
|
|
if (data.id === user.id && (!data.active || !data.groups.includes('admins')))
|
|
fail(409, 'Eigenen Administratorzugriff beibehalten.')
|
|
for (const g of data.groups)
|
|
if (!this.get('SELECT id FROM groups_ WHERE id=?', g)) fail(400, 'Gruppe nicht gefunden.')
|
|
let id = data.id || uid()
|
|
const before = this.get(
|
|
'SELECT id,name,email,active,phone,job_title,department FROM users WHERE id=? AND deleted_at IS NULL',
|
|
id,
|
|
)
|
|
if (data.id && !before) fail(404, 'Benutzer nicht gefunden.')
|
|
const profile = {}
|
|
for (const field of ['phone', 'job_title', 'department']) {
|
|
const value = data[field] ?? before?.[field] ?? ''
|
|
if (typeof value !== 'string' || value.length > 200)
|
|
fail(400, 'Telefon, Position und Abteilung dürfen höchstens 200 Zeichen enthalten.')
|
|
profile[field] = value.trim()
|
|
}
|
|
if (!before && (!data.password || data.password.length < 12))
|
|
fail(400, 'Passwort mit mindestens 12 Zeichen erforderlich.')
|
|
if (before)
|
|
this.run(
|
|
'UPDATE users SET name=?,email=?,active=? WHERE id=?',
|
|
data.name,
|
|
data.email,
|
|
data.active ? 1 : 0,
|
|
id,
|
|
)
|
|
else
|
|
this.run(
|
|
'INSERT INTO users(id,name,email,password) VALUES(?,?,?,?)',
|
|
id,
|
|
data.name,
|
|
data.email,
|
|
passwordHash(data.password),
|
|
)
|
|
if (data.password) {
|
|
if (data.password.length < 12) fail(400, 'Passwort zu kurz.')
|
|
this.run('UPDATE users SET password=? WHERE id=?', passwordHash(data.password), id)
|
|
this.run('DELETE FROM sessions WHERE user_id=?', id)
|
|
}
|
|
this.run(
|
|
'UPDATE users SET phone=?,job_title=?,department=? WHERE id=?',
|
|
profile.phone,
|
|
profile.job_title,
|
|
profile.department,
|
|
id,
|
|
)
|
|
this.run('DELETE FROM memberships WHERE user_id=?', id)
|
|
data.groups.forEach((g) => this.run('INSERT INTO memberships VALUES(?,?)', id, g))
|
|
if (
|
|
!this.get(
|
|
"SELECT 1 FROM users u JOIN memberships m ON m.user_id=u.id WHERE m.group_id='admins' AND u.active=1 AND u.deleted_at IS NULL",
|
|
)
|
|
)
|
|
fail(409, 'Mindestens ein aktiver Administrator muss erhalten bleiben.')
|
|
this.audit(user.id, 'Benutzer geändert', id, before, {
|
|
name: data.name,
|
|
email: data.email,
|
|
active: data.active,
|
|
groups: data.groups,
|
|
...profile,
|
|
})
|
|
return { id }
|
|
})
|
|
}
|
|
saveGroup(user, data) {
|
|
return this.tx(() => {
|
|
this.require(user, '*', 'users')
|
|
if (data.id === 'admins') fail(409, 'Die Administratorgruppe ist geschützt.')
|
|
if (!data.name?.trim() || !data.permissions || typeof data.permissions !== 'object')
|
|
fail(400, 'Gruppendaten fehlen.')
|
|
for (const [q, a] of Object.entries(data.permissions)) {
|
|
if (q !== '*') this.queue(q)
|
|
if (!Array.isArray(a) || a.some((p) => !actions.includes(p)))
|
|
fail(400, 'Ungültige Berechtigungen.')
|
|
}
|
|
const id = data.id || uid()
|
|
const old = this.get('SELECT * FROM groups_ WHERE id=?', id)
|
|
if (data.id && !old) fail(404, 'Gruppe nicht gefunden.')
|
|
this.run(
|
|
'INSERT INTO groups_ VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,permissions=excluded.permissions',
|
|
id,
|
|
data.name,
|
|
json(data.permissions),
|
|
)
|
|
this.audit(user.id, 'Gruppe geändert', id, old, data)
|
|
return { id }
|
|
})
|
|
}
|
|
userDeletion(user, id) {
|
|
this.require(user, '*', 'users')
|
|
const target = this.get(
|
|
'SELECT id,name,email,active FROM users WHERE id=? AND deleted_at IS NULL',
|
|
id,
|
|
)
|
|
if (!target) fail(404, 'Benutzer nicht gefunden.')
|
|
const lastAdmin =
|
|
target.active &&
|
|
this.get("SELECT 1 FROM memberships WHERE user_id=? AND group_id='admins'", id) &&
|
|
!this.get(
|
|
"SELECT 1 FROM users u JOIN memberships m ON m.user_id=u.id WHERE m.group_id='admins' AND u.active=1 AND u.deleted_at IS NULL AND u.id!=?",
|
|
id,
|
|
)
|
|
return {
|
|
...target,
|
|
confirm: target.email,
|
|
assignedTickets: this.get('SELECT count(*) n FROM tickets WHERE assignee=?', id).n,
|
|
blockedReason:
|
|
id === user.id
|
|
? 'Das eigene Benutzerkonto kann nicht gelöscht werden.'
|
|
: lastAdmin
|
|
? 'Der letzte aktive Administrator kann nicht gelöscht werden.'
|
|
: '',
|
|
}
|
|
}
|
|
releaseAssignments(user, ids, reason, onlyWithoutAccess = false) {
|
|
let count = 0
|
|
for (const id of ids)
|
|
for (const t of this.all('SELECT id,queue_id,assignee FROM tickets WHERE assignee=?', id)) {
|
|
if (onlyWithoutAccess && this.can({ id }, t.queue_id, 'view')) continue
|
|
this.run(
|
|
'UPDATE tickets SET assignee=NULL,revision=revision+1,updated_at=? WHERE id=?',
|
|
now(),
|
|
t.id,
|
|
)
|
|
this.audit(
|
|
user.id,
|
|
'Zuständigkeit aufgehoben',
|
|
t.id,
|
|
{ assignee: id },
|
|
{ assignee: null, reason },
|
|
t.id,
|
|
t.queue_id,
|
|
)
|
|
count++
|
|
}
|
|
return count
|
|
}
|
|
deleteUser(user, id, data = {}) {
|
|
return this.tx(() => {
|
|
const target = this.userDeletion(user, id)
|
|
if (target.blockedReason) fail(409, target.blockedReason)
|
|
if (data.confirm !== target.confirm)
|
|
fail(400, 'E-Mail-Adresse zur Bestätigung stimmt nicht überein.')
|
|
const groups = this.all('SELECT group_id FROM memberships WHERE user_id=?', id).map(
|
|
(m) => m.group_id,
|
|
)
|
|
const assignedTickets = this.releaseAssignments(user, [id], 'Benutzer gelöscht')
|
|
this.run('DELETE FROM sessions WHERE user_id=?', id)
|
|
this.run('DELETE FROM memberships WHERE user_id=?', id)
|
|
this.run(
|
|
"UPDATE outbox SET state='cancelled',error='Benutzer gelöscht' WHERE state IN ('pending','blocked') AND notification_id IN (SELECT id FROM notifications WHERE user_id=?)",
|
|
id,
|
|
)
|
|
this.run(
|
|
"UPDATE users SET active=0,deleted_at=?,email=?,password='',prefs='{}',phone='',job_title='',department='' WHERE id=?",
|
|
now(),
|
|
`deleted-${uid()}@syncova.invalid`,
|
|
id,
|
|
)
|
|
this.audit(
|
|
user.id,
|
|
'Benutzer gelöscht',
|
|
id,
|
|
{ id, name: target.name, email: target.email, active: target.active, groups },
|
|
{ deleted: true, assignedTickets },
|
|
)
|
|
return { ok: true, assignedTickets }
|
|
})
|
|
}
|
|
groupDeletion(user, id) {
|
|
this.require(user, '*', 'users')
|
|
const group = this.get('SELECT * FROM groups_ WHERE id=?', id)
|
|
if (!group) fail(404, 'Gruppe nicht gefunden.')
|
|
const keepsAccess = this.all(
|
|
'SELECT g.permissions FROM groups_ g JOIN memberships m ON m.group_id=g.id WHERE m.user_id=? AND g.id!=?',
|
|
user.id,
|
|
id,
|
|
).some((g) => (JSON.parse(g.permissions)['*'] || []).includes('users'))
|
|
return {
|
|
id: group.id,
|
|
name: group.name,
|
|
confirm: group.name,
|
|
members: this.get('SELECT count(*) n FROM memberships WHERE group_id=?', id).n,
|
|
blockedReason:
|
|
id === 'admins'
|
|
? 'Die Administratorgruppe ist geschützt.'
|
|
: !keepsAccess
|
|
? 'Die Gruppe gewährt Ihren einzigen Zugriff auf die Benutzerverwaltung. Zuerst einen anderen Verwaltungszugang einrichten.'
|
|
: '',
|
|
}
|
|
}
|
|
deleteGroup(user, id, data = {}) {
|
|
return this.tx(() => {
|
|
const info = this.groupDeletion(user, id)
|
|
if (info.blockedReason) fail(409, info.blockedReason)
|
|
if (data.confirm !== info.confirm)
|
|
fail(400, 'Gruppenname zur Bestätigung stimmt nicht überein.')
|
|
const group = this.get('SELECT * FROM groups_ WHERE id=?', id)
|
|
const members = this.all('SELECT user_id FROM memberships WHERE group_id=?', id).map(
|
|
(m) => m.user_id,
|
|
)
|
|
this.run('DELETE FROM memberships WHERE group_id=?', id)
|
|
this.run('DELETE FROM groups_ WHERE id=?', id)
|
|
const assignedTickets = this.releaseAssignments(user, members, 'Gruppe gelöscht', true)
|
|
this.audit(
|
|
user.id,
|
|
'Gruppe gelöscht',
|
|
id,
|
|
{ name: group.name, permissions: JSON.parse(group.permissions), members },
|
|
{ deleted: true, assignedTickets },
|
|
)
|
|
return { ok: true, members: members.length, assignedTickets }
|
|
})
|
|
}
|
|
logs(user) {
|
|
return this.all(
|
|
'SELECT a.*,u.name actor_name FROM audits a LEFT JOIN users u ON u.id=a.actor WHERE a.ticket_id IS NULL ORDER BY a.id DESC LIMIT 500',
|
|
).filter(
|
|
(a) =>
|
|
this.can(user, a.queue_id || '*', 'logs') &&
|
|
(!a.queue_id || this.can(user, a.queue_id, 'view')),
|
|
)
|
|
}
|
|
}
|