Log rekne co zpusobilo jakou zmenu, prevzeti ticketu a pozvanky
Nalezeno na bezicim serveru: TK-4946 mel 177 udalosti a 620 radku logu, pritom se skoro nic nestalo. Zmereno proti fronte: ve stejnem okne vzniklo presne tolik behu, kolik prislo udalosti (22 a 22), kazdy s jednim pokusem. Fronta nenasobi nic, odesilatel poslal 177 POSTu. Nase vina byla, ze to z historie neslo poznat. - Data udalosti se ukladaji. Kdyz krok nema vlastni, ulozi se to, cim beh zacal - u webhooku prijate telo. Prazdna udalost je horsi nez zadna. - Shodna udalost se pocita (repeats, lastAt), nezaklada dalsi radek. Ticket se pritom nemeni, takze duplikat nerozblika dashboard ani nespusti automatizaci na zmenu ticketu. Zahodit ji nejde, jinak by nikdo nezjistil, ze proti nam neco tluce. - Zmeny se radi pod udalost, ktera je zpusobila, a u udalosti stoji jmeno automatizace. Log se cte jako "prislo tohle -> zmenilo to tohle". - Poznamka o stavu jen kdyz se stav zmenil. "z in-progress na in-progress" u kazde zpravy byl zdroj tech 620 radku. - runsToday konecne znamena dnes: behy po dnech, k tomu vcera a celkem. Dosud to byl citac od zalozeni automatizace, jen se jmenoval "dnes". Vedle toho prace, o kterou slo predtim: - Prevzeti ticketu ze skupiny (POST /tickets/:id/claim) a krok Predat skupine s prepinacem automatickeho prideleni nejvolnejsimu. - Pozvanky do firmy: odkaz s nahodnym kodem, heslo si nastavi pozvany. - Resitele, skupiny a pozvanky presunuty z Nastaveni do zalozky Lide, cleny skupiny se vybiraji klikanim. - Ctyri AI znaky, ktere zbyvaly v kodu, pryc. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
fbe5b8ff6f
commit
f1e8253169
+107
-9
@@ -87,7 +87,7 @@ export type FlowStep =
|
||||
/** id parametru z trigger.fields */
|
||||
fieldId: string;
|
||||
operator: ConditionOperator;
|
||||
/** Chybi u operatoru, ktere hodnotu nepotrebuji (isEmpty, isTrue…). */
|
||||
/** Chybi u operatoru, ktere hodnotu nepotrebuji (isEmpty, isTrue apod.). */
|
||||
value?: string;
|
||||
yes: FlowStep[];
|
||||
no: FlowStep[];
|
||||
@@ -111,7 +111,19 @@ export interface Automation {
|
||||
name: string;
|
||||
kind: AutomationKind;
|
||||
enabled: boolean;
|
||||
/**
|
||||
* Behy za **dnesni den**. Po pulnoci zacina od nuly.
|
||||
*
|
||||
* Driv to byl citac od zalozeni automatizace, jen se jmenoval "dnes".
|
||||
* U automatizace, ktera bezi tri mesice, to rikalo cislo, ktere s dneskem
|
||||
* nemelo nic spolecneho.
|
||||
*/
|
||||
runsToday: number;
|
||||
/** Behy za vcerejsi den, at je s cim srovnat. */
|
||||
runsYesterday: number;
|
||||
/** Behy od zalozeni. Puvodni citac se neztratil, jen se spravne jmenuje. */
|
||||
runsTotal: number;
|
||||
/** Uspesnost dnesnich behu. Kdyz dnes zadny nebyl, posledniho dne, kdy byly. */
|
||||
successRate: number;
|
||||
avgDurationMs: number;
|
||||
lastRunAt: string;
|
||||
@@ -138,6 +150,15 @@ interface StoredAutomation {
|
||||
name: string;
|
||||
kind: AutomationKind;
|
||||
enabled: boolean;
|
||||
/**
|
||||
* Behy po dnech, nejnovejsi nakonec. Drzi se poslednich `KEEP_DAYS`.
|
||||
*
|
||||
* Proc historie a ne jen citac pro dnesek: bez ni po pulnoci neni s cim
|
||||
* srovnat a nikdo nepozna, jestli je dnesnich devet malo nebo hodne.
|
||||
*/
|
||||
days?: Array<{ day: string; runs: number; ok: number }>;
|
||||
/** Behy od zalozeni. */
|
||||
runsTotal?: number;
|
||||
runsToday: number;
|
||||
successRate: number;
|
||||
avgDurationMs: number;
|
||||
@@ -147,6 +168,53 @@ interface StoredAutomation {
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
/** Kolik dni zpatky se drzi. Dva tydny staci na "je to dnes jinak nez obvykle". */
|
||||
const KEEP_DAYS = 14;
|
||||
|
||||
/** Dnesni den jako `2026-08-17`. Podle nej se pozna prelom dne. */
|
||||
function today(): string {
|
||||
return new Date().toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function dayBefore(day: string): string {
|
||||
return new Date(new Date(`${day}T00:00:00Z`).getTime() - 86_400_000).toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Statistika za dnesek z ulozene historie.
|
||||
*
|
||||
* Kdyz dnes jeste nic nebezelo, vraci nulu - **ne vcerejsi cislo**. Ukazat
|
||||
* vcerejsi pocet jako dnesni je to, co bylo spatne.
|
||||
*/
|
||||
function statsOf(stored: StoredAutomation): {
|
||||
runsToday: number;
|
||||
runsYesterday: number;
|
||||
runsTotal: number;
|
||||
successRate: number;
|
||||
} {
|
||||
const days = stored.days ?? [];
|
||||
const now = today();
|
||||
const mine = days.find((entry) => entry.day === now);
|
||||
const before = days.find((entry) => entry.day === dayBefore(now));
|
||||
|
||||
/*
|
||||
* Uspesnost dnesnich behu. Kdyz dnes zadny nebyl, bere se posledni den, kdy
|
||||
* byly - nula procent u automatizace, ktera dnes jen nemela co delat, by
|
||||
* vypadala jako porucha.
|
||||
*/
|
||||
const source = mine ?? [...days].reverse().find((entry) => entry.runs > 0);
|
||||
|
||||
return {
|
||||
runsToday: mine?.runs ?? 0,
|
||||
runsYesterday: before?.runs ?? 0,
|
||||
runsTotal: stored.runsTotal ?? stored.runsToday,
|
||||
successRate:
|
||||
source && source.runs > 0
|
||||
? Math.round((source.ok / source.runs) * 1000) / 10
|
||||
: stored.successRate,
|
||||
};
|
||||
}
|
||||
|
||||
function minutesAgo(minutes: number): string {
|
||||
return new Date(Date.now() - minutes * 60_000).toISOString();
|
||||
}
|
||||
@@ -374,7 +442,7 @@ function seed(
|
||||
*/
|
||||
function seedDemoAutomations(): void {
|
||||
seed({
|
||||
name: 'Objednávka → sklad → fakturace',
|
||||
name: 'Objednávka, sklad a fakturace',
|
||||
enabled: true,
|
||||
runsToday: 428,
|
||||
successRate: 99.3,
|
||||
@@ -888,9 +956,20 @@ if (!config.seedWebhookToken) {
|
||||
}
|
||||
|
||||
function toSummary(stored: StoredAutomation): Automation {
|
||||
const { flow: _flow, createdAt: _createdAt, updatedAt: _updatedAt, ...rest } = stored;
|
||||
const {
|
||||
flow: _flow,
|
||||
createdAt: _createdAt,
|
||||
updatedAt: _updatedAt,
|
||||
days: _days,
|
||||
...rest
|
||||
} = stored;
|
||||
return {
|
||||
...rest,
|
||||
/*
|
||||
* Cisla se pocitaji z historie po dnech, ne z ulozeneho citace. Po pulnoci
|
||||
* je "dnes" nula, dokud opravdu neco nebezi.
|
||||
*/
|
||||
...statsOf(stored),
|
||||
stepCount: countSteps(stored.flow.steps),
|
||||
configured: stored.flow.trigger !== null,
|
||||
issues: collectFlowIssues(stored.flow),
|
||||
@@ -1077,15 +1156,34 @@ export function recordRun(id: string, ok = true, tenantIds?: string[]): Automati
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const runs = stored.runsToday + 1;
|
||||
// Klouzavy prumer uspesnosti pres dnesni behy, ne prepisovani na 0 nebo 100.
|
||||
const previousOk = (stored.successRate / 100) * stored.runsToday;
|
||||
const successRate = runs === 0 ? 100 : ((previousOk + (ok ? 1 : 0)) / runs) * 100;
|
||||
/*
|
||||
* Zapisuje se do dnesniho dne. Po pulnoci vznikne novy zaznam, takze citac
|
||||
* nepokracuje pres den - to byla puvodni chyba.
|
||||
*/
|
||||
const now = today();
|
||||
const days = [...(stored.days ?? [])];
|
||||
const index = days.findIndex((entry) => entry.day === now);
|
||||
|
||||
if (index === -1) {
|
||||
days.push({ day: now, runs: 1, ok: ok ? 1 : 0 });
|
||||
} else {
|
||||
days[index] = {
|
||||
day: now,
|
||||
runs: days[index].runs + 1,
|
||||
ok: days[index].ok + (ok ? 1 : 0),
|
||||
};
|
||||
}
|
||||
if (days.length > KEEP_DAYS) days.splice(0, days.length - KEEP_DAYS);
|
||||
|
||||
const total = (stored.runsTotal ?? stored.runsToday) + 1;
|
||||
const dayEntry = days.find((entry) => entry.day === now)!;
|
||||
|
||||
const updated: StoredAutomation = {
|
||||
...stored,
|
||||
runsToday: runs,
|
||||
successRate: Math.round(successRate * 10) / 10,
|
||||
days,
|
||||
runsTotal: total,
|
||||
runsToday: dayEntry.runs,
|
||||
successRate: Math.round((dayEntry.ok / dayEntry.runs) * 1000) / 10,
|
||||
lastRunAt: new Date().toISOString(),
|
||||
};
|
||||
save(updated);
|
||||
|
||||
@@ -13,6 +13,7 @@ import { refreshActions, actionPermissions, actionStore, seedActions } from './t
|
||||
import { refreshCustomWidgets, customWidgetStore, seedCustomWidgets } from './customWidgets.js';
|
||||
import { auditStore } from './audit.js';
|
||||
import { notificationStore, refreshNotifications } from './notifications.js';
|
||||
import { inviteStore, refreshInvites } from './invites.js';
|
||||
import { onTicket } from './ticketHooks.js';
|
||||
import { onTicketEvent } from '../runtime/triggers.js';
|
||||
import { initQueue } from '../runtime/queue.js';
|
||||
@@ -61,6 +62,7 @@ const entities: Array<{
|
||||
// Audit vychozi sadu nema, zaznamy vznikaji az provozem.
|
||||
{ store: auditStore as EntityStore<TenantEntity> },
|
||||
{ store: notificationStore as EntityStore<TenantEntity> },
|
||||
{ store: inviteStore as EntityStore<TenantEntity> },
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -93,6 +95,7 @@ export async function refreshCaches(): Promise<void> {
|
||||
refreshActions(),
|
||||
refreshCustomWidgets(),
|
||||
refreshNotifications(),
|
||||
refreshInvites(),
|
||||
]);
|
||||
|
||||
// Prava k akcim vznikaji z definic akci, takze se registruji az po jejich nacteni.
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Pozvanky do firmy.
|
||||
*
|
||||
* Spravce firmy vytvori odkaz s neodhadnutelnym kodem a posle ho, jak chce -
|
||||
* mailem, na Slacku, diktatem do telefonu. Kdo odkaz otevre, vyplni jmeno
|
||||
* a heslo a je uvnitr.
|
||||
*
|
||||
* Proc odkaz a ne "zaloz uzivatele a posli mu heslo":
|
||||
* - **heslo nikdy nikam neposilame**. Kdo si ho nastavi sam, ma ho jen on.
|
||||
* - pozvanka **vyprsi** a da se zrusit, ucet ne,
|
||||
* - kdo uz ucet ma, se jen pripoji k dalsi firme misto zakladani druheho.
|
||||
*
|
||||
* Kod je jedina ochrana, proto je dlouhy a nahodny - stejne jako u webhooku.
|
||||
*/
|
||||
|
||||
import { randomBytes, randomUUID } from 'node:crypto';
|
||||
import { defineStore, nowIso, type TenantEntity } from './store/index.js';
|
||||
import { withCache } from './store/cached.js';
|
||||
|
||||
export interface Invite extends TenantEntity {
|
||||
/** Firma, do ktere pozvanka zve. */
|
||||
tenantId: string;
|
||||
/** Neodhadnutelny kod v odkazu. */
|
||||
code: string;
|
||||
/**
|
||||
* Komu je urcena. Prazdne = komukoliv, kdo odkaz dostane.
|
||||
*
|
||||
* Kdyz je vyplnena, musi souhlasit s tim, co clovek zada. Bez toho by odkaz
|
||||
* poslany dal fungoval komukoliv, coz u pozvanky do firmy neni v poradku.
|
||||
*/
|
||||
email: string;
|
||||
/** Jmeno napovedou, at spravce v seznamu pozna, komu ji poslal. */
|
||||
note: string;
|
||||
/** Jake role dostane v te firme. */
|
||||
roleIds: string[];
|
||||
/**
|
||||
* Ma se z nej stat i resitel, tedy nekdo, komu jde prehodit ticket?
|
||||
*
|
||||
* Uzivatel a resitel nejsou totez: ucetni muze mit pristup do portalu, aniz
|
||||
* by kdy resila ticket. Proto se to pta, misto aby se hadalo.
|
||||
*/
|
||||
asPerson: boolean;
|
||||
expiresAt: string;
|
||||
usedAt: string | null;
|
||||
usedByUserId: string | null;
|
||||
createdByEmail: string;
|
||||
}
|
||||
|
||||
export const inviteStore = defineStore<Invite>('invite');
|
||||
const cache = withCache(inviteStore);
|
||||
|
||||
/** Jak dlouho pozvanka plati. Tyden je dost na dovolenou a malo na zapomenuti. */
|
||||
const VALID_DAYS = 7;
|
||||
|
||||
export async function refreshInvites(): Promise<void> {
|
||||
await cache.refresh();
|
||||
}
|
||||
|
||||
export interface CreateInviteInput {
|
||||
tenantId: string;
|
||||
email?: string;
|
||||
note?: string;
|
||||
roleIds: string[];
|
||||
asPerson?: boolean;
|
||||
createdByEmail: string;
|
||||
}
|
||||
|
||||
export function newInvite(input: CreateInviteInput): Invite {
|
||||
const timestamp = nowIso();
|
||||
return {
|
||||
id: `inv_${randomUUID().slice(0, 12)}`,
|
||||
tenantId: input.tenantId,
|
||||
code: randomBytes(24).toString('base64url'),
|
||||
email: (input.email ?? '').trim().toLowerCase(),
|
||||
note: input.note ?? '',
|
||||
roleIds: input.roleIds,
|
||||
asPerson: input.asPerson ?? true,
|
||||
expiresAt: new Date(Date.now() + VALID_DAYS * 86_400_000).toISOString(),
|
||||
usedAt: null,
|
||||
usedByUserId: null,
|
||||
createdByEmail: input.createdByEmail,
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
/** Pozvanka podle kodu. Nezkoumá platnost, na to je `inviteProblem`. */
|
||||
export function findByCode(code: string): Invite | undefined {
|
||||
if (code.length < 16) return undefined;
|
||||
return cache.find((invite) => invite.code === code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Proc pozvanku nejde pouzit. `null` = je v poradku.
|
||||
*
|
||||
* Duvody jsou zamerne konkretni: "uz byla pouzita" a "vyprsela" vedou k jine
|
||||
* reakci a schovat je za "neplatna pozvanka" znamena, ze clovek nevi, co dal.
|
||||
*/
|
||||
export function inviteProblem(invite: Invite): string | null {
|
||||
if (invite.usedAt) return 'Tahle pozvánka už byla použitá.';
|
||||
if (new Date(invite.expiresAt).getTime() < Date.now()) {
|
||||
return 'Pozvánka vypršela, požádejte o novou.';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function listInvites(tenantIds: string[]): Invite[] {
|
||||
return cache
|
||||
.all()
|
||||
.filter((invite) => tenantIds.includes(invite.tenantId))
|
||||
.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
||||
}
|
||||
|
||||
export async function markUsed(invite: Invite, userId: string): Promise<void> {
|
||||
await inviteStore.update(
|
||||
invite.id,
|
||||
{ usedAt: nowIso(), usedByUserId: userId },
|
||||
{ tenantIds: [invite.tenantId], includeGlobal: true },
|
||||
);
|
||||
await cache.refresh();
|
||||
}
|
||||
@@ -127,6 +127,13 @@ export interface PersonGroup extends TenantEntity {
|
||||
personIds: string[];
|
||||
}
|
||||
|
||||
/** Skupiny, jejichz clenem ten clovek je. */
|
||||
export function groupsOfPerson(personId: string, tenantIds: string[]): PersonGroup[] {
|
||||
return groupCache
|
||||
.all()
|
||||
.filter((group) => tenantIds.includes(group.tenantId) && group.personIds.includes(personId));
|
||||
}
|
||||
|
||||
export const groupStore = defineStore<PersonGroup>('personGroup');
|
||||
const groupCache = withCache(groupStore);
|
||||
|
||||
|
||||
@@ -610,6 +610,41 @@ export const services: Service[] = [
|
||||
{ id: 'created', name: 'created', type: 'boolean', required: true },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'assign-group',
|
||||
name: 'Předat skupině',
|
||||
description:
|
||||
'Ticket se objeví ve frontě skupiny a kdo má čas, si ho převezme. ' +
|
||||
'Když zaškrtnete rovnou přiřadit, dostane ho hned ten, kdo má nejmíň práce - ' +
|
||||
'to se hodí tam, kde se čeká na rychlou reakci.',
|
||||
implementation: 'script',
|
||||
inputs: [
|
||||
{
|
||||
id: 'groupId',
|
||||
label: 'Skupina',
|
||||
kind: 'choice',
|
||||
required: true,
|
||||
options: [],
|
||||
optionsFrom: 'groups',
|
||||
},
|
||||
{
|
||||
id: 'autoAssign',
|
||||
label: 'Rovnou přiřadit nejvolnějšímu',
|
||||
kind: 'choice',
|
||||
required: false,
|
||||
options: [
|
||||
{ value: 'true', label: 'Ano' },
|
||||
{ value: 'false', label: 'Ne, nechat ve frontě skupiny' },
|
||||
],
|
||||
hint: 'Prázdné = nechat ve frontě, ať si to lidé vezmou sami.',
|
||||
},
|
||||
{ id: 'ticketId', label: 'Ticket', kind: 'text', required: false },
|
||||
],
|
||||
outputFields: [
|
||||
{ id: 'groupId', name: 'groupId', type: 'string', required: true },
|
||||
{ id: 'groupName', name: 'groupName', type: 'string', required: true },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'assign-least-busy',
|
||||
name: 'Předat nejvolnějšímu ze skupiny',
|
||||
@@ -623,6 +658,8 @@ export const services: Service[] = [
|
||||
label: 'Skupina',
|
||||
kind: 'choice',
|
||||
required: true,
|
||||
options: [],
|
||||
optionsFrom: 'groups',
|
||||
hint: 'Například sklad nebo IT. Skupiny se spravují v Nastavení.',
|
||||
},
|
||||
{
|
||||
|
||||
+178
-14
@@ -18,6 +18,7 @@
|
||||
|
||||
import { config } from '../config.js';
|
||||
import { publish } from '../events/bus.js';
|
||||
import { currentRun } from '../runtime/context.js';
|
||||
import { onTicketChanged } from './ticketHooks.js';
|
||||
import { notify } from './notifications.js';
|
||||
import { findPerson, type Person } from './people.js';
|
||||
@@ -112,6 +113,17 @@ export interface TicketEvent {
|
||||
/** Cela prijata data. Nic se nezahazuje, at jde dohledat, co presne prislo. */
|
||||
payload: Record<string, unknown>;
|
||||
at: string;
|
||||
/**
|
||||
* Kolikrat prislo totež. Vetsinou 1.
|
||||
*
|
||||
* Odesilatele umi poslat stejnou zpravu i osmdesatkrat za minutu. Zalozit
|
||||
* osmdesat radku znamena, ze v historii nikdo nic nenajde - a pritom se
|
||||
* nestalo nic noveho. Proto se shodna udalost **pocita** u te predchozi.
|
||||
* Zahodit ji nejde: bez pocitadla by nikdo nezjistil, ze odesilatel tluce.
|
||||
*/
|
||||
repeats: number;
|
||||
/** Kdy prisla naposledy. Same jako `at`, dokud se neopakuje. */
|
||||
lastAt: string;
|
||||
}
|
||||
|
||||
export interface Ticket {
|
||||
@@ -312,7 +324,18 @@ export async function initTickets(): Promise<void> {
|
||||
const { trace, events: rowEvents, ...stored } = row;
|
||||
tickets.push(stored);
|
||||
traces.set(row.id, trace ?? []);
|
||||
events.set(row.id, rowEvents ?? []);
|
||||
/*
|
||||
* Pocitadlo opakovani pribylo pozdeji. Ulozene udalosti ho nemaji, takze
|
||||
* se doplni pri nacteni - jinak by se v portalu ukazovalo "undefinedx".
|
||||
*/
|
||||
events.set(
|
||||
row.id,
|
||||
(rowEvents ?? []).map((event) => ({
|
||||
...event,
|
||||
repeats: event.repeats ?? 1,
|
||||
lastAt: event.lastAt ?? event.at,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
// Citac musi pokracovat za nejvyssim ulozenym cislem, jinak by nove tickety
|
||||
@@ -386,7 +409,12 @@ export function appendTrace(ticketId: string, inputs: TraceInput[]): number {
|
||||
return 0;
|
||||
}
|
||||
const before = existing.length;
|
||||
flattenTrace(inputs, null, existing);
|
||||
/*
|
||||
* Kdyz zapis patri behu automatizace, radi se pod jeho radek udalosti.
|
||||
* Diky tomu je v logu videt "prislo tohle -> zmenilo to tohle" misto dvou
|
||||
* vet vedle sebe, u kterych se jen hada, jestli spolu souvisi.
|
||||
*/
|
||||
flattenTrace(inputs, currentRun()?.traceParent.id ?? null, existing);
|
||||
|
||||
// Log je soucast ulozeneho ticketu, takze zapis do logu je zmena ticketu.
|
||||
const ticket = tickets.find((item) => item.id === ticketId);
|
||||
@@ -1030,6 +1058,13 @@ export interface IntakeResult {
|
||||
/** true = ticket teprve ted vznikl, false = udalost se navesila na existujici. */
|
||||
created: boolean;
|
||||
event: TicketEvent;
|
||||
/**
|
||||
* true = prislo presne totez co posledne, takze se jen pricetlo k pocitadlu.
|
||||
* Ticket se **nemenil**: nesmi se kvuli tomu prepsat `updatedAt`, ani
|
||||
* rozeslat zmena - jinak by kazdy duplikat rozblikal dashboard a mohl
|
||||
* spustit automatizaci navazanou na zmenu ticketu.
|
||||
*/
|
||||
repeated: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1053,6 +1088,8 @@ export function intakeEvent(input: IntakeInput): IntakeResult {
|
||||
label: input.label ?? input.type,
|
||||
payload: input.payload ?? {},
|
||||
at: timestamp,
|
||||
repeats: 1,
|
||||
lastAt: timestamp,
|
||||
};
|
||||
|
||||
const existing = input.externalId
|
||||
@@ -1062,6 +1099,18 @@ export function intakeEvent(input: IntakeInput): IntakeResult {
|
||||
: undefined;
|
||||
|
||||
if (existing) {
|
||||
/*
|
||||
* Prislo presne totez co posledne? Pak se to jen pricte. Zadny novy radek,
|
||||
* zadny zapis do logu, zadna zmena ticketu - nic se totiz nestalo.
|
||||
*/
|
||||
const repeat = sameAsLast(existing.id, event);
|
||||
if (repeat) {
|
||||
repeat.repeats += 1;
|
||||
repeat.lastAt = event.at;
|
||||
persist(existing);
|
||||
return { ticket: toTicket(existing), created: false, event: repeat, repeated: true };
|
||||
}
|
||||
|
||||
appendEvent(existing, event);
|
||||
|
||||
if (input.fields) existing.fields = { ...(existing.fields ?? {}), ...input.fields };
|
||||
@@ -1070,15 +1119,31 @@ export function intakeEvent(input: IntakeInput): IntakeResult {
|
||||
}
|
||||
touch(existing);
|
||||
|
||||
/*
|
||||
* Rodic musi byt prazdny, jinak by se radek udalosti zaradil pod udalost
|
||||
* predchozi - `appendTrace` doplnuje rodice z behu.
|
||||
*/
|
||||
const run = currentRun();
|
||||
const parent = run?.traceParent;
|
||||
if (parent) parent.id = null;
|
||||
|
||||
appendTrace(existing.id, [
|
||||
{ kind: 'trigger', status: 'info', label: `Přijata událost: ${event.label}`, response: describePayload(event.payload) },
|
||||
{
|
||||
kind: 'trigger',
|
||||
status: 'info',
|
||||
label: `Přijata událost: ${event.label}${originOf(run, event)}`,
|
||||
response: describePayload(event.payload),
|
||||
},
|
||||
]);
|
||||
|
||||
// Dalsi zapisy tohohle behu uz patri pod tenhle radek.
|
||||
if (parent) parent.id = lastTraceId(existing.id);
|
||||
publish('ticket.updated', `Ticket ${existing.id}: ${event.label}`, {
|
||||
ticketId: existing.id,
|
||||
externalId: existing.externalId,
|
||||
});
|
||||
|
||||
return { ticket: toTicket(existing), created: false, event };
|
||||
return { ticket: toTicket(existing), created: false, event, repeated: false };
|
||||
}
|
||||
|
||||
const created = createTicket({
|
||||
@@ -1101,7 +1166,7 @@ export function intakeEvent(input: IntakeInput): IntakeResult {
|
||||
{
|
||||
kind: 'trigger',
|
||||
status: 'info',
|
||||
label: `Ticket vznikl z události: ${event.label}`,
|
||||
label: `Ticket vznikl z události: ${event.label}${originOf(currentRun(), event)}`,
|
||||
response: describePayload(event.payload),
|
||||
},
|
||||
],
|
||||
@@ -1113,7 +1178,16 @@ export function intakeEvent(input: IntakeInput): IntakeResult {
|
||||
persist(stored);
|
||||
}
|
||||
|
||||
return { ticket: toTicket(stored ?? created as unknown as StoredTicket), created: true, event };
|
||||
// Zmeny, ktere beh udela dal, patri pod radek o vzniku ticketu.
|
||||
const parent = currentRun()?.traceParent;
|
||||
if (parent) parent.id = traces.get(created.id)?.[0]?.id ?? null;
|
||||
|
||||
return {
|
||||
ticket: toTicket(stored ?? (created as unknown as StoredTicket)),
|
||||
created: true,
|
||||
event,
|
||||
repeated: false,
|
||||
};
|
||||
}
|
||||
|
||||
/** Kolik udalosti se u jednoho ticketu drzi. Starsi se odmazavaji. */
|
||||
@@ -1127,6 +1201,28 @@ function appendEvent(ticket: StoredTicket, event: TicketEvent): void {
|
||||
events.set(ticket.id, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Je to totez, co prislo naposledy?
|
||||
*
|
||||
* Porovnava se **jen s posledni** udalosti, ne s celou historii. "Objednavka
|
||||
* pripravena" muze legitimne prijit znovu za hodinu, kdyz se mezitim stalo
|
||||
* neco jineho - to je novy fakt. Dvacet stejnych zprav v rade uz ne.
|
||||
*
|
||||
* Data se srovnavaji pres JSON, protoze na poradi klicu v prijate zprave
|
||||
* nezalezi jen vyjimecne a levnejsi porovnani neni potreba.
|
||||
*/
|
||||
function sameAsLast(ticketId: string, event: TicketEvent): TicketEvent | null {
|
||||
const list = events.get(ticketId);
|
||||
const last = list && list.length > 0 ? list[list.length - 1] : undefined;
|
||||
if (!last) return null;
|
||||
|
||||
if (last.type !== event.type || last.source !== event.source || last.label !== event.label) {
|
||||
return null;
|
||||
}
|
||||
if (JSON.stringify(last.payload) !== JSON.stringify(event.payload)) return null;
|
||||
return last;
|
||||
}
|
||||
|
||||
let eventCounter = 0;
|
||||
|
||||
function nextEventId(): string {
|
||||
@@ -1134,6 +1230,24 @@ function nextEventId(): string {
|
||||
return `tev_${eventCounter.toString(36)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Kdo udalost prinesl.
|
||||
*
|
||||
* V logu je to to nejdulezitejsi: kdyz se ticket zmenil, prvni otazka je "kdo
|
||||
* mi do toho sahl". Jmeno automatizace na to odpovi, `webhook` uz ne.
|
||||
*/
|
||||
function originOf(run: { automationName: string } | undefined, event: TicketEvent): string {
|
||||
if (run) return ` (automatizace ${run.automationName})`;
|
||||
if (event.source && event.source !== 'webhook') return ` (${event.source})`;
|
||||
return '';
|
||||
}
|
||||
|
||||
/** ID posledniho radku logu. Pod nej se radi zmeny, ktere z udalosti plynou. */
|
||||
function lastTraceId(ticketId: string): string | null {
|
||||
const list = traces.get(ticketId);
|
||||
return list && list.length > 0 ? list[list.length - 1].id : null;
|
||||
}
|
||||
|
||||
/** Kratky popis dat do logu. Cela data zustavaji v udalosti. */
|
||||
function describePayload(payload: Record<string, unknown>): string | null {
|
||||
const keys = Object.keys(payload);
|
||||
@@ -1370,13 +1484,20 @@ export function updateTicketStatus(
|
||||
markResponded(ticket);
|
||||
touch(ticket);
|
||||
|
||||
appendTrace(id, [
|
||||
{
|
||||
kind: 'note',
|
||||
label: `Stav změněn z „${previous}" na „${status}".`,
|
||||
status: 'info',
|
||||
},
|
||||
]);
|
||||
/*
|
||||
* Poznamka jen kdyz se neco zmenilo. "Stav zmenen z completed na completed"
|
||||
* je rada, ktera se ctenari pise do historie u kazde prichozi zpravy, a po
|
||||
* dvaceti takovych se v logu neda nic najit.
|
||||
*/
|
||||
const statusChanged = previous !== status;
|
||||
const closedChanged = closed !== undefined && closed !== wasClosed;
|
||||
|
||||
if (statusChanged || closedChanged) {
|
||||
const parts: string[] = [];
|
||||
if (statusChanged) parts.push(`stav ${previous} -> ${status}`);
|
||||
if (closedChanged) parts.push(ticket.closed ? 'oznaceno jako vyrizene' : 'znovu otevreno');
|
||||
appendTrace(id, [{ kind: 'note', label: parts.join(', '), status: 'info' }]);
|
||||
}
|
||||
|
||||
if (ticket.closed) {
|
||||
publish('ticket.resolved', `Ticket ${ticket.id} vyřešen: ${ticket.subject}`, {
|
||||
@@ -1499,12 +1620,14 @@ export function assignTicketGroup(
|
||||
id: string,
|
||||
groupId: string | null,
|
||||
tenantIds: string[],
|
||||
/** false = nechat resitele, jak je. Pouziva to prevzeti ticketu. */
|
||||
clearAssignee = true,
|
||||
): Ticket | undefined {
|
||||
const ticket = findWritable(id, tenantIds);
|
||||
if (!ticket) return undefined;
|
||||
|
||||
ticket.assigneeGroupId = groupId;
|
||||
if (groupId) ticket.assigneeId = null;
|
||||
if (groupId && clearAssignee) ticket.assigneeId = null;
|
||||
touch(ticket);
|
||||
|
||||
appendTrace(id, [
|
||||
@@ -1518,6 +1641,47 @@ export function assignTicketGroup(
|
||||
return toTicket(ticket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevzeti ticketu.
|
||||
*
|
||||
* Clovek si vezme praci sam, misto aby cekal, az mu ji nekdo prideli. Smi to
|
||||
* jen u ticketu **ze sve skupiny**, nebo u takoveho, ktery nema nikoho -
|
||||
* brat cizi rozdelanou praci by znamenalo, ze o ni prijde ten, kdo ji resi.
|
||||
*
|
||||
* Vraci `undefined`, kdyz to nejde; duvod rekne volajici, ktery zna kontext.
|
||||
*/
|
||||
export function claimTicket(
|
||||
id: string,
|
||||
personId: string,
|
||||
tenantIds: string[],
|
||||
): Ticket | undefined {
|
||||
const ticket = findWritable(id, tenantIds);
|
||||
if (!ticket) return undefined;
|
||||
|
||||
const person = findPerson(personId);
|
||||
if (!person || person.tenantId !== ticket.tenantId) return undefined;
|
||||
|
||||
ticket.assigneeId = person.id;
|
||||
markResponded(ticket);
|
||||
touch(ticket);
|
||||
|
||||
appendTrace(id, [
|
||||
{ kind: 'note', status: 'info', label: `${person.name} si ticket převzal` },
|
||||
]);
|
||||
publish('ticket.assigned', `${person.name} si vzal ticket ${ticket.id}`, {
|
||||
ticketId: ticket.id,
|
||||
assigneeId: person.id,
|
||||
});
|
||||
return toTicket(ticket);
|
||||
}
|
||||
|
||||
/** Ma ticket uz nekoho? Pro rozhodnuti, jestli jde prevzit. */
|
||||
export function ticketAssignee(id: string, tenantIds: string[]): string | null | undefined {
|
||||
const ticket = tickets.find((item) => item.id === id);
|
||||
if (!ticket || !tenantIds.includes(ticket.tenantId)) return undefined;
|
||||
return ticket.assigneeId;
|
||||
}
|
||||
|
||||
export function addComment(
|
||||
id: string,
|
||||
author: string,
|
||||
|
||||
@@ -14,6 +14,7 @@ import { buildOpenApiDocument } from './openapi.js';
|
||||
import { authRouter } from './routes/auth.js';
|
||||
import { contactRouter } from './routes/contact.js';
|
||||
import { dashboardRouter } from './routes/dashboard.js';
|
||||
import { publicInviteRouter } from './routes/invites.js';
|
||||
import { webhookRouter } from './routes/webhook.js';
|
||||
import { bootstrapData } from './data/bootstrap.js';
|
||||
import { flushConnectorStore, initConnectorStore, storageStatus } from './data/connectorStore.js';
|
||||
@@ -114,6 +115,8 @@ api.use(
|
||||
|
||||
api.use('/api/auth', authRouter);
|
||||
api.use('/api/dashboard', dashboardRouter);
|
||||
// Verejne: kdo dostal odkaz na pozvanku, neni jeste prihlaseny.
|
||||
api.use('/api/invites', publicInviteRouter);
|
||||
api.use('/api/admin', adminRouter);
|
||||
api.use('/api/contact', contactRouter);
|
||||
api.use('/webhook', webhookRouter);
|
||||
|
||||
@@ -634,6 +634,8 @@ export function buildOpenApiDocument() {
|
||||
kind: { type: 'string', enum: ['workflow', 'voicebot', 'integrace', 'report'] },
|
||||
enabled: { type: 'boolean' },
|
||||
runsToday: { type: 'integer' },
|
||||
runsYesterday: { type: 'integer' },
|
||||
runsTotal: { type: 'integer' },
|
||||
successRate: { type: 'number' },
|
||||
avgDurationMs: { type: 'integer' },
|
||||
lastRunAt: { type: 'string', format: 'date-time' },
|
||||
|
||||
+75
-1
@@ -42,9 +42,10 @@ import { collectScopes } from '../data/flowScope.js';
|
||||
import { widgetCatalog } from '../data/widgets.js';
|
||||
import { listIncidents } from '../data/incidentStore.js';
|
||||
import { getSummary } from '../data/mock.js';
|
||||
import { findPersonByEmail, listGroups, listPeople } from '../data/people.js';
|
||||
import { findPersonByEmail, groupsOfPerson, listGroups, listPeople } from '../data/people.js';
|
||||
import { hasPermission } from '../data/permissions.js';
|
||||
import { listNotifications, markRead, unreadCount } from '../data/notifications.js';
|
||||
import { inviteRouter } from './invites.js';
|
||||
import { queueStats, recentRuns } from '../runtime/queue.js';
|
||||
import { recordAudit } from '../data/audit.js';
|
||||
import { findTenant, generateIntakeToken, refreshTenants, tenantStore } from '../data/tenants.js';
|
||||
@@ -52,6 +53,8 @@ import { listTicketTypes } from '../data/ticketTypes.js';
|
||||
import {
|
||||
addComment,
|
||||
assignTicket,
|
||||
claimTicket,
|
||||
ticketAssignee,
|
||||
getAgentStats,
|
||||
getTicket,
|
||||
getWorkload,
|
||||
@@ -541,6 +544,74 @@ dashboardRouter.get('/tickets', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Prevzeti ticketu.
|
||||
*
|
||||
* Clovek si vezme praci sam, misto aby cekal, az mu ji nekdo prideli. Tohle je
|
||||
* bezny zpusob prace v tymu: ticket lezi ve fronte skupiny a kdo ma cas, si ho
|
||||
* vezme.
|
||||
*
|
||||
* Smi to u ticketu **ze sve skupiny** nebo u takoveho, ktery nema nikoho.
|
||||
* Vzit nekomu rozdelanou praci nejde - to uz je prehozeni a na to je pravo
|
||||
* `ticket.assign.others`.
|
||||
*/
|
||||
dashboardRouter.post('/tickets/:id/claim', (req, res) => {
|
||||
const scope = scopeOrDeny(req, res);
|
||||
if (!scope) return;
|
||||
|
||||
const access = accessFor(req.user!);
|
||||
if (!access.personId) {
|
||||
return res.status(400).json({
|
||||
error: 'not_a_person',
|
||||
message: 'Nejste vedený jako řešitel, takže si ticket nemůžete převzít.',
|
||||
});
|
||||
}
|
||||
|
||||
const tenantIds = writableTenants(req);
|
||||
const current = ticketAssignee(req.params.id, tenantIds);
|
||||
if (current === undefined) {
|
||||
return res.status(404).json({ error: 'not_found', message: 'Ticket neexistuje.' });
|
||||
}
|
||||
|
||||
const ticket = getTicket(req.params.id, tenantIds);
|
||||
if (!ticket) return res.status(404).json({ error: 'not_found', message: 'Ticket neexistuje.' });
|
||||
|
||||
if (current !== null && current !== access.personId) {
|
||||
// Uz to nekdo resi. Prehodit praci nekomu jinemu je jine rozhodnuti.
|
||||
if (!hasPermission(req.user!, 'ticket.assign.others')) {
|
||||
return res.status(409).json({
|
||||
error: 'already_assigned',
|
||||
message: 'Ticket už někdo řeší. Přehodit ho může jen ten, kdo na to má právo.',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const mine = groupsOfPerson(access.personId, tenantIds).map((group) => group.id);
|
||||
const inMyGroup = ticket.assigneeGroupId !== null && mine.includes(ticket.assigneeGroupId);
|
||||
|
||||
if (!inMyGroup && current !== null && current !== access.personId) {
|
||||
return res.status(403).json({
|
||||
error: 'forbidden',
|
||||
message: 'Tenhle ticket není ve vaší skupině.',
|
||||
});
|
||||
}
|
||||
|
||||
const updated = claimTicket(req.params.id, access.personId, tenantIds);
|
||||
if (!updated) {
|
||||
return res.status(400).json({ error: 'failed', message: 'Převzetí se nepodařilo.' });
|
||||
}
|
||||
|
||||
recordAudit({
|
||||
userId: req.user!.id,
|
||||
userEmail: req.user!.email,
|
||||
tenantId: updated.tenantId,
|
||||
action: 'ticket.claim',
|
||||
target: updated.id,
|
||||
});
|
||||
|
||||
return res.json(updated);
|
||||
});
|
||||
|
||||
/** Kdo co ma u sebe. MUSI byt pred /tickets/:id, jinak by to spadlo na detail. */
|
||||
dashboardRouter.get('/tickets/workload', (req, res) => {
|
||||
const scope = scopeOrDeny(req, res);
|
||||
@@ -665,6 +736,9 @@ dashboardRouter.use('/connectors', connectorsRouter);
|
||||
// Nastaveni vsech entit. Cele stoji na fabrice crudRouter.
|
||||
dashboardRouter.use('/settings', settingsRouter);
|
||||
|
||||
// Pozvanky do firmy.
|
||||
dashboardRouter.use('/invites', inviteRouter);
|
||||
|
||||
// Data pro vlastni widgety. Jeden request na cely prehled.
|
||||
dashboardRouter.use('/widget-data', widgetDataRouter);
|
||||
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
/**
|
||||
* Pozvanky do firmy.
|
||||
*
|
||||
* Dva druhy cest v jednom souboru, protoze patri k sobe:
|
||||
* - **za prihlasenim**: spravce firmy pozvanku vytvori, vidi seznam a muze ji
|
||||
* zrusit,
|
||||
* - **verejne**: kdo dostal odkaz, si prohledne, kam ho zve, a prijme ji.
|
||||
*
|
||||
* Verejna cast je zamerne uzka: rekne jen nazev firmy a jestli je pozvanka
|
||||
* platna. Nic o tom, kdo ve firme je, ani co obsahuje - kod v adrese je jedina
|
||||
* ochrana a odkaz se snadno dostane, kam nema.
|
||||
*/
|
||||
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { recordAudit } from '../data/audit.js';
|
||||
import { publicBaseUrl } from '../config.js';
|
||||
import {
|
||||
findByCode,
|
||||
inviteProblem,
|
||||
inviteStore,
|
||||
listInvites,
|
||||
markUsed,
|
||||
newInvite,
|
||||
refreshInvites,
|
||||
} from '../data/invites.js';
|
||||
import { rolesFor, type Role } from '../data/permissions.js';
|
||||
import { personStore, refreshPeople, listPeople } from '../data/people.js';
|
||||
import { findTenant } from '../data/tenants.js';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import {
|
||||
findUserByEmail,
|
||||
hashPassword,
|
||||
listAllUsers,
|
||||
refreshUsers,
|
||||
userStore,
|
||||
} from '../data/users.js';
|
||||
import { nowIso } from '../data/store/index.js';
|
||||
import { requireAuth } from '../middleware/auth.js';
|
||||
import { currentTenant } from './crud.js';
|
||||
|
||||
/** Verejne cesty. Bez prihlaseni, autorizuje kod v adrese. */
|
||||
export const publicInviteRouter = Router();
|
||||
|
||||
/** Cesty za prihlasenim: sprava pozvanek. */
|
||||
export const inviteRouter = Router();
|
||||
|
||||
// ------------------------------------------------------------------ verejne
|
||||
|
||||
/**
|
||||
* Co je za timhle odkazem.
|
||||
*
|
||||
* Vraci minimum: nazev firmy a jestli to jde prijmout. Kdyz uz e-mail znamy,
|
||||
* rekne ho, aby ho clovek nemusel psat - ale nic navic.
|
||||
*/
|
||||
publicInviteRouter.get('/:code', (req, res) => {
|
||||
const invite = findByCode(req.params.code);
|
||||
if (!invite) {
|
||||
return res.status(404).json({ error: 'not_found', message: 'Pozvánka neexistuje.' });
|
||||
}
|
||||
|
||||
const problem = inviteProblem(invite);
|
||||
const tenant = findTenant(invite.tenantId);
|
||||
|
||||
return res.json({
|
||||
tenant: tenant?.name ?? 'Neznámá firma',
|
||||
email: invite.email || null,
|
||||
/** Uz ucet ma? Pak staci heslo, ne registrace. */
|
||||
knownUser: invite.email ? findUserByEmail(invite.email) !== undefined : false,
|
||||
valid: problem === null,
|
||||
problem,
|
||||
});
|
||||
});
|
||||
|
||||
const acceptSchema = z.object({
|
||||
name: z.string().trim().min(2).max(80),
|
||||
email: z.string().trim().email(),
|
||||
password: z.string().min(8, 'Heslo musí mít aspoň 8 znaků.'),
|
||||
});
|
||||
|
||||
/**
|
||||
* Prijeti pozvanky.
|
||||
*
|
||||
* Tri pripady:
|
||||
* - ucet neexistuje: zalozi se a pripoji k firme,
|
||||
* - ucet existuje a heslo sedi: jen se pripoji k firme,
|
||||
* - ucet existuje a heslo nesedi: odmitnuto.
|
||||
*
|
||||
* Heslo se **nikdy neposila** - clovek si ho nastavi sam prave tady.
|
||||
*/
|
||||
publicInviteRouter.post('/:code/accept', (req, res) => {
|
||||
const invite = findByCode(req.params.code);
|
||||
if (!invite) {
|
||||
return res.status(404).json({ error: 'not_found', message: 'Pozvánka neexistuje.' });
|
||||
}
|
||||
|
||||
const problem = inviteProblem(invite);
|
||||
if (problem) return res.status(409).json({ error: 'invalid_invite', message: problem });
|
||||
|
||||
const parsed = acceptSchema.safeParse(req.body ?? {});
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({
|
||||
error: 'validation_error',
|
||||
message: parsed.error.issues[0]?.message ?? 'Neplatné údaje.',
|
||||
});
|
||||
}
|
||||
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
|
||||
/*
|
||||
* Kdyz je pozvanka na konkretni adresu, musi souhlasit. Bez toho by odkaz
|
||||
* poslany dal fungoval komukoliv, coz u pristupu do firmy neni v poradku.
|
||||
*/
|
||||
if (invite.email && invite.email !== email) {
|
||||
return res.status(403).json({
|
||||
error: 'wrong_email',
|
||||
message: 'Tahle pozvánka je pro jinou e-mailovou adresu.',
|
||||
});
|
||||
}
|
||||
|
||||
const existing = findUserByEmail(email);
|
||||
|
||||
return void (async () => {
|
||||
let userId: string;
|
||||
|
||||
if (existing) {
|
||||
// Ucet uz je. Pripojime ho k firme, ale jen kdyz prokaze, ze je jeho.
|
||||
/*
|
||||
* Ucet uz existuje, takze musi prokazat, ze je jeho. Bez toho by kdokoliv
|
||||
* s odkazem pripojil cizi adresu ke sve firme a videl by jeji data.
|
||||
*/
|
||||
const stored = listAllUsers().find((item) => item.id === existing.id);
|
||||
const passwordOk =
|
||||
stored !== undefined && (await bcrypt.compare(parsed.data.password, stored.passwordHash));
|
||||
|
||||
if (!passwordOk) {
|
||||
return res.status(401).json({
|
||||
error: 'wrong_password',
|
||||
message: 'S touhle adresou už účet existuje. Zadejte k němu svoje heslo.',
|
||||
});
|
||||
}
|
||||
|
||||
const already = existing.memberships.some(
|
||||
(membership) => membership.tenantId === invite.tenantId,
|
||||
);
|
||||
if (!already) {
|
||||
await userStore.update(
|
||||
existing.id,
|
||||
{
|
||||
memberships: [
|
||||
...existing.memberships,
|
||||
{ tenantId: invite.tenantId, roleIds: invite.roleIds },
|
||||
],
|
||||
},
|
||||
{ tenantIds: [], includeGlobal: true },
|
||||
);
|
||||
}
|
||||
userId = existing.id;
|
||||
} else {
|
||||
const timestamp = nowIso();
|
||||
const created = await userStore.create({
|
||||
id: `usr_${Math.random().toString(36).slice(2, 10)}`,
|
||||
tenantId: null,
|
||||
email,
|
||||
name: parsed.data.name,
|
||||
passwordHash: hashPassword(parsed.data.password),
|
||||
platformAdmin: false,
|
||||
enabled: true,
|
||||
memberships: [{ tenantId: invite.tenantId, roleIds: invite.roleIds }],
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
});
|
||||
userId = created.id;
|
||||
}
|
||||
|
||||
await refreshUsers();
|
||||
|
||||
/*
|
||||
* Resitel je nekdo, komu jde prehodit ticket. Neni to totez co ucet:
|
||||
* ucetni muze mit pristup do portalu, aniz by kdy resila ticket.
|
||||
*/
|
||||
if (invite.asPerson) {
|
||||
const alreadyPerson = listPeople([invite.tenantId]).some(
|
||||
(person) => person.email.toLowerCase() === email,
|
||||
);
|
||||
if (!alreadyPerson) {
|
||||
const timestamp = nowIso();
|
||||
await personStore.create({
|
||||
id: `ppl_${Math.random().toString(36).slice(2, 10)}`,
|
||||
tenantId: invite.tenantId,
|
||||
name: parsed.data.name,
|
||||
email,
|
||||
role: '',
|
||||
capacity: 8,
|
||||
enabled: true,
|
||||
externalIds: [],
|
||||
createdAt: timestamp,
|
||||
updatedAt: timestamp,
|
||||
});
|
||||
await refreshPeople();
|
||||
}
|
||||
}
|
||||
|
||||
await markUsed(invite, userId);
|
||||
|
||||
recordAudit({
|
||||
userId,
|
||||
userEmail: email,
|
||||
tenantId: invite.tenantId,
|
||||
action: 'invite.accept',
|
||||
target: invite.id,
|
||||
detail: { pozval: invite.createdByEmail },
|
||||
});
|
||||
|
||||
console.info(`[pozvanky] ${email} prijal pozvanku do ${invite.tenantId}`);
|
||||
return res.status(201).json({ ok: true, email });
|
||||
})().catch((err: unknown) => {
|
||||
console.error('[pozvanky] prijeti selhalo:', err);
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: 'internal_error', message: 'Pozvánku se nepodařilo přijmout.' });
|
||||
});
|
||||
});
|
||||
|
||||
// ----------------------------------------------------------------- za prihlasenim
|
||||
|
||||
inviteRouter.use(requireAuth);
|
||||
|
||||
/** Kdo smi zvat. Zvat lidi do firmy je totez co spravovat uzivatele. */
|
||||
const PERMISSION = 'user.manage';
|
||||
|
||||
inviteRouter.get('/', (req, res) => {
|
||||
const tenantId = currentTenant(req, res);
|
||||
if (!tenantId) return;
|
||||
|
||||
const base = `${publicBaseUrl()}/pozvanka/`;
|
||||
return res.json({
|
||||
items: listInvites([tenantId]).map((invite) => ({
|
||||
...invite,
|
||||
/** Cela adresa, at ji jde rovnou zkopirovat a poslat. */
|
||||
url: `${base}${invite.code}`,
|
||||
problem: inviteProblem(invite),
|
||||
})),
|
||||
roles: rolesFor(tenantId).map((role: Role) => ({
|
||||
id: role.id,
|
||||
key: role.key,
|
||||
name: role.name,
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
const createSchema = z.object({
|
||||
email: z.string().trim().email().optional().or(z.literal('')),
|
||||
note: z.string().trim().max(120).optional(),
|
||||
roleIds: z.array(z.string().min(1)).min(1, 'Vyberte aspoň jednu roli.'),
|
||||
asPerson: z.boolean().default(true),
|
||||
});
|
||||
|
||||
inviteRouter.post('/', (req, res) => {
|
||||
const tenantId = currentTenant(req, res);
|
||||
if (!tenantId) return;
|
||||
|
||||
const parsed = createSchema.safeParse(req.body ?? {});
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({
|
||||
error: 'validation_error',
|
||||
message: parsed.error.issues[0]?.message ?? 'Neplatné údaje.',
|
||||
});
|
||||
}
|
||||
|
||||
const invite = newInvite({
|
||||
tenantId,
|
||||
email: parsed.data.email || undefined,
|
||||
note: parsed.data.note,
|
||||
roleIds: parsed.data.roleIds,
|
||||
asPerson: parsed.data.asPerson,
|
||||
createdByEmail: req.user!.email,
|
||||
});
|
||||
|
||||
return void inviteStore
|
||||
.create(invite)
|
||||
.then(async () => {
|
||||
await refreshInvites();
|
||||
recordAudit({
|
||||
userId: req.user!.id,
|
||||
userEmail: req.user!.email,
|
||||
tenantId,
|
||||
action: 'invite.create',
|
||||
target: invite.id,
|
||||
detail: { email: invite.email || '(komukoliv)' },
|
||||
});
|
||||
return res.status(201).json({
|
||||
...invite,
|
||||
url: `${publicBaseUrl()}/pozvanka/${invite.code}`,
|
||||
});
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
console.error('[pozvanky] zalozeni selhalo:', err);
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: 'internal_error', message: 'Pozvánku se nepodařilo vytvořit.' });
|
||||
});
|
||||
});
|
||||
|
||||
/** Zruseni pozvanky. Odkaz okamzite prestane platit. */
|
||||
inviteRouter.delete('/:id', (req, res) => {
|
||||
const tenantId = currentTenant(req, res);
|
||||
if (!tenantId) return;
|
||||
|
||||
return void inviteStore
|
||||
.remove(req.params.id, { tenantIds: [tenantId], includeGlobal: false })
|
||||
.then(async (removed) => {
|
||||
if (!removed) {
|
||||
return res.status(404).json({ error: 'not_found', message: 'Pozvánka neexistuje.' });
|
||||
}
|
||||
await refreshInvites();
|
||||
recordAudit({
|
||||
userId: req.user!.id,
|
||||
userEmail: req.user!.email,
|
||||
tenantId,
|
||||
action: 'invite.delete',
|
||||
target: req.params.id,
|
||||
});
|
||||
return res.status(204).end();
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
console.error('[pozvanky] zruseni selhalo:', err);
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: 'internal_error', message: 'Pozvánku se nepodařilo zrušit.' });
|
||||
});
|
||||
});
|
||||
|
||||
export { PERMISSION as invitePermission };
|
||||
@@ -30,6 +30,13 @@ export interface StepContext {
|
||||
tenantId: string;
|
||||
/** Ticket, ke kteremu beh patri. Nekdy vznikne az behem nej. */
|
||||
ticketId: string | null;
|
||||
/**
|
||||
* Data, kterymi beh zacal - u webhooku cele prijate telo.
|
||||
*
|
||||
* Ulozi se do udalosti ticketu, kdyz krok nema vlastni. Bez toho by v historii
|
||||
* bylo prazdno a na otazku "co nam vlastne prislo" by neslo odpovedet.
|
||||
*/
|
||||
trigger?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface StepOutcome {
|
||||
@@ -129,8 +136,21 @@ const handlers: Record<string, Handler> = {
|
||||
externalId: inputs.externalId?.trim() || null,
|
||||
externalSource: inputs.source?.trim() || 'automatizace',
|
||||
type: inputs.event?.trim() || 'automation',
|
||||
label: inputs.label?.trim() || subject || 'Událost',
|
||||
payload: inputs.payload ? safeJson(inputs.payload) : {},
|
||||
/*
|
||||
* Popisek do casove osy. "Udalost" je posledni moznost: kdyz v nem stoji
|
||||
* u vsech radku totez, casova osa nerika nic.
|
||||
*/
|
||||
label:
|
||||
inputs.label?.trim() ||
|
||||
subject ||
|
||||
(inputs.status?.trim() ? `stav ${inputs.status.trim()}` : '') ||
|
||||
inputs.externalId?.trim() ||
|
||||
'Událost',
|
||||
/*
|
||||
* Bez vlastnich dat se ulozi to, cim beh zacal. Prazdna udalost je horsi
|
||||
* nez zadna: v historii se tvari, ze se neco stalo, a nerekne co.
|
||||
*/
|
||||
payload: inputs.payload ? safeJson(inputs.payload) : (context.trigger ?? {}),
|
||||
fields,
|
||||
create: {
|
||||
subject: subject || inputs.externalId || 'Bez předmětu',
|
||||
@@ -180,6 +200,26 @@ const handlers: Record<string, Handler> = {
|
||||
if (closed !== undefined) note += closed ? ', vyřízený' : ', nevyřízený';
|
||||
}
|
||||
|
||||
/*
|
||||
* Odesilatel poslal totez co posledne. Neni to chyba, ale musi to byt videt,
|
||||
* jinak nikdo nepozna, ze proti nam neco tluce ve smycce.
|
||||
*/
|
||||
if (result.repeated) {
|
||||
return {
|
||||
ok: true,
|
||||
summary: `bez změny, ${result.ticket.id} už tuhle zprávu dostal (${result.event.repeats}x)`,
|
||||
detail: null,
|
||||
ticketId: result.ticket.id,
|
||||
outputs: {
|
||||
ticketId: result.ticket.id,
|
||||
created: false,
|
||||
externalId: result.ticket.externalId,
|
||||
repeated: true,
|
||||
repeats: result.event.repeats,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
summary:
|
||||
@@ -194,6 +234,49 @@ const handlers: Record<string, Handler> = {
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Preda ticket skupine.
|
||||
*
|
||||
* Zaskrtnute "rovnou prirad" znamena, ze ho system hned da nejvolnejsimu.
|
||||
* Nezaskrtnute nechava ticket ve **fronte skupiny** a kdo ma cas, si ho
|
||||
* vezme sam - to je pro vetsinu tymu spravne, protoze clovek sam vi, kdy
|
||||
* na dalsi praci ma. Automat se hodi tam, kde se ceka na rychlou reakci
|
||||
* a nikdo nema cas koukat do fronty.
|
||||
*/
|
||||
'ticket/assign-group': (inputs, context) => {
|
||||
const ticketId = inputs.ticketId?.trim() || context.ticketId;
|
||||
if (!ticketId) return missing('ticket');
|
||||
|
||||
const groupId = inputs.groupId?.trim();
|
||||
if (!groupId) return missing('skupina');
|
||||
|
||||
const group = findGroup(groupId);
|
||||
if (!group || group.tenantId !== context.tenantId) {
|
||||
return { ok: false, summary: `skupina ${groupId} neexistuje`, outputs: {} };
|
||||
}
|
||||
|
||||
const updated = assignTicketGroup(ticketId, groupId, [context.tenantId]);
|
||||
if (!updated) return { ok: false, summary: 'přiřazení skupině se nepodařilo', outputs: {} };
|
||||
|
||||
if (boolInput(inputs.autoAssign) !== true) {
|
||||
return {
|
||||
ok: true,
|
||||
summary: `${group.name}, čeká ve frontě skupiny`,
|
||||
detail: null,
|
||||
outputs: { groupId: group.id, groupName: group.name },
|
||||
};
|
||||
}
|
||||
|
||||
// Zaskrtnuto: rovnou tomu, kdo ma nejmene prace.
|
||||
const handler = handlers['ticket/assign-least-busy'];
|
||||
const outcome = handler({ ticketId, groupId }, context) as StepOutcome;
|
||||
return {
|
||||
...outcome,
|
||||
summary: `${group.name}: ${outcome.summary}`,
|
||||
outputs: { groupId: group.id, groupName: group.name, ...outcome.outputs },
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Preda ticket **nejvolnejsimu** cloveku ze skupiny.
|
||||
*
|
||||
|
||||
@@ -15,7 +15,17 @@ import { AsyncLocalStorage } from 'node:async_hooks';
|
||||
export interface RunMarker {
|
||||
runId: string;
|
||||
automationId: string;
|
||||
/** Jmeno, ne ID. V logu ticketu ho cte clovek, ne stroj. */
|
||||
automationName: string;
|
||||
tenantId: string;
|
||||
/**
|
||||
* Radek logu, pod ktery patri zmeny tohohle behu.
|
||||
*
|
||||
* Log ma byt citelny jako "prislo tohle a zpusobilo to tuhle zmenu". Bez
|
||||
* spolecneho rodice je z nej seznam nesouvisejicich vet a nikdo nepozna, co
|
||||
* zpusobilo co. Menitelna schranka proto, ze radek vznika az v prubehu behu.
|
||||
*/
|
||||
traceParent: { id: string | null };
|
||||
}
|
||||
|
||||
const storage = new AsyncLocalStorage<RunMarker>();
|
||||
|
||||
@@ -47,6 +47,13 @@ export interface RunOptions {
|
||||
idempotencyKey: string | null;
|
||||
/** Kolik kroku se nejvys vykona. Pojistka proti nechtene dlouhemu stromu. */
|
||||
maxSteps?: number;
|
||||
/**
|
||||
* Data, kterymi beh zacal. Doplni si je `runFlow` samo.
|
||||
*
|
||||
* Kroky si je predavaji jako kontext, ale ten se cestou plni vystupy. Pro
|
||||
* zapis do udalosti ticketu je potreba puvodni prijata zprava, ne smes.
|
||||
*/
|
||||
trigger?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface StepResult {
|
||||
@@ -100,6 +107,8 @@ export async function runFlow(
|
||||
const startedAt = Date.now();
|
||||
const results: StepResult[] = [];
|
||||
const working: RunContext = { ...context };
|
||||
// Puvodni data se drzi zvlast: `working` se cestou plni vystupy kroku.
|
||||
options.trigger = { ...context };
|
||||
const limit = options.maxSteps ?? MAX_STEPS;
|
||||
|
||||
let error: string | null = null;
|
||||
@@ -182,6 +191,7 @@ async function runAction(
|
||||
const outcome = await builtin(inputsForStep, {
|
||||
tenantId: options.tenantId,
|
||||
ticketId: options.ticketId,
|
||||
trigger: options.trigger,
|
||||
});
|
||||
|
||||
if (outcome.ticketId && !options.ticketId) {
|
||||
|
||||
@@ -106,7 +106,13 @@ async function runOne(item: QueueItem): Promise<void> {
|
||||
* ticketu ji spusti, a tak porad.
|
||||
*/
|
||||
const result = await withRun(
|
||||
{ runId: item.id, automationId: item.automationId, tenantId: item.tenantId },
|
||||
{
|
||||
runId: item.id,
|
||||
automationId: item.automationId,
|
||||
automationName: automation.name,
|
||||
tenantId: item.tenantId,
|
||||
traceParent: { id: null },
|
||||
},
|
||||
() =>
|
||||
runFlow(automation.flow.steps, item.payload, {
|
||||
tenantId: item.tenantId,
|
||||
|
||||
Reference in New Issue
Block a user