pgkiln 0.50.0 → 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SECURITY.md +7 -0
- package/db/migrations/119_workspace_home.sql +30 -0
- package/db/migrations/120_datasets.sql +87 -0
- package/db/migrations/121_ai_agents.sql +94 -0
- package/db/migrations/122_team_development.sql +186 -0
- package/db/migrations/123_workflow_outcomes.sql +407 -0
- package/db/migrations/124_component_details.sql +51 -0
- package/docs/apex-feature-parity.md +15 -12
- package/docs/guide/01-installation.md +9 -3
- package/docs/guide/03-builder.md +211 -11
- package/docs/guide/04-pages-and-regions.md +96 -22
- package/docs/guide/05-items.md +7 -1
- package/docs/guide/06-processing.md +123 -8
- package/docs/guide/07-dynamic-actions.md +34 -0
- package/docs/guide/09-reference.md +2 -1
- package/docs/guide/11-from-apex.md +2 -1
- package/docs/guide/12-development.md +70 -11
- package/docs/guide/20-ai-agents.md +4 -2
- package/examples/hr/README.md +2 -0
- package/examples/hr/hr_63_datasets.sql +29 -0
- package/examples/hr/hr_64_ai_agent.sql +97 -0
- package/examples/hr/hr_65_ask_reports.sql +21 -0
- package/examples/hr/hr_66_team_development.sql +58 -0
- package/examples/hr/hr_67_workflow_outcomes.sql +113 -0
- package/examples/hr/hr_68_component_details.sql +96 -0
- package/package.json +1 -1
- package/public/app.css +46 -0
- package/public/app.js +134 -20
- package/public/builder.css +116 -0
- package/public/builder.js +47 -0
- package/src/ai/chat.ts +18 -18
- package/src/ai/service.ts +35 -11
- package/src/ai/types.ts +7 -2
- package/src/app.ts +11 -1
- package/src/builder/advisor.ts +6 -3
- package/src/builder/ai-agents.ts +372 -0
- package/src/builder/ai-builder.ts +3 -3
- package/src/builder/ai.ts +13 -9
- package/src/builder/assistant.ts +22 -7
- package/src/builder/components.ts +38 -9
- package/src/builder/datasets.ts +472 -0
- package/src/builder/designer.ts +8 -3
- package/src/builder/feedback.ts +6 -1
- package/src/builder/home.ts +12 -7
- package/src/builder/instance.ts +2 -0
- package/src/builder/locks.ts +7 -1
- package/src/builder/region-help.ts +2 -2
- package/src/builder/reporter.ts +52 -3
- package/src/builder/routes.ts +7 -4
- package/src/builder/shared.ts +1 -1
- package/src/builder/subscriptions.ts +1 -0
- package/src/builder/team.ts +991 -0
- package/src/builder/ui.ts +8 -5
- package/src/builder/workflow-migrate.ts +210 -0
- package/src/builder/workflows.ts +29 -6
- package/src/builder/workspace-home.ts +285 -0
- package/src/builder/workspace-transfer.ts +1 -1
- package/src/builder/workspaces.ts +7 -4
- package/src/cli/replace.ts +7 -1
- package/src/datasets.ts +367 -0
- package/src/i18n/ar.ts +33 -0
- package/src/i18n/cs.ts +33 -0
- package/src/i18n/da.ts +33 -0
- package/src/i18n/de.ts +33 -0
- package/src/i18n/el.ts +33 -0
- package/src/i18n/es.ts +33 -0
- package/src/i18n/fi.ts +33 -0
- package/src/i18n/fr.ts +33 -0
- package/src/i18n/he.ts +33 -0
- package/src/i18n/it.ts +33 -0
- package/src/i18n/ja.ts +33 -0
- package/src/i18n/ko.ts +33 -0
- package/src/i18n/nb.ts +33 -0
- package/src/i18n/pl.ts +33 -0
- package/src/i18n/pt.ts +33 -0
- package/src/i18n/ru.ts +33 -0
- package/src/i18n/sv.ts +33 -0
- package/src/i18n/tr.ts +33 -0
- package/src/i18n/uk.ts +33 -0
- package/src/i18n/zh.ts +33 -0
- package/src/i18n.ts +66 -0
- package/src/instance.ts +3 -0
- package/src/metadata.ts +9 -3
- package/src/release-check.ts +139 -0
- package/src/reporting-app.ts +101 -0
- package/src/runtime/ai-agents.ts +365 -0
- package/src/runtime/ai-filter.ts +385 -107
- package/src/runtime/assistant.ts +64 -11
- package/src/runtime/context.ts +2 -0
- package/src/runtime/data-reporter.ts +199 -32
- package/src/runtime/ir-phrases.ts +812 -0
- package/src/runtime/ir-plan.ts +449 -0
- package/src/runtime/items.ts +1 -1
- package/src/runtime/regions.ts +6 -2
- package/src/runtime/render.ts +1 -1
- package/src/runtime/report.ts +1 -1
- package/src/runtime/tasks.ts +63 -12
- package/src/session.ts +2 -0
- package/src/subscriptions.ts +2 -1
- package/src/task-events.ts +232 -0
- package/src/team-samples.ts +74 -0
- package/src/team.ts +342 -0
- package/src/workflow.ts +135 -18
- package/src/workspace-export.ts +39 -4
package/src/runtime/assistant.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { Region } from '../metadata.ts';
|
|
|
8
8
|
import { logActivity, saveState } from '../session.ts';
|
|
9
9
|
import { fetchSource, loadSource, sourceParamValues, toRows } from '../websources.ts';
|
|
10
10
|
import { aiSubstitute, DATA_NOTE } from './ai.ts';
|
|
11
|
+
import { AGENT_NAME, agentFor, DEFAULT_STEPS, logToolCall, prepareTools, runAgentTool, type AgentOutcome, type TurnExtras } from './ai-agents.ts';
|
|
11
12
|
import { checkPageAccess, computeVisibility, Forbidden, isAuthorized } from './authz.ts';
|
|
12
13
|
import { bindValues, dbg, publicError, stripSemicolon, substitute, type PageContext } from './context.ts';
|
|
13
14
|
import { loadContext, simplePage, txContext, type Req } from './routes.ts';
|
|
@@ -29,6 +30,13 @@ import { loadContext, simplePage, txContext, type Req } from './routes.ts';
|
|
|
29
30
|
// "parameters": {…}, "writes": true, "authz": "EMPLOYEE"}],
|
|
30
31
|
// "max_rounds": 5, "max_turns": 20, "public": false, "error_message": "…"}
|
|
31
32
|
//
|
|
33
|
+
// (121) Or the region names an AI agent of the application (Shared
|
|
34
|
+
// Components → AI Agents, src/runtime/ai-agents.ts): {"agent": "HR_AGENT",
|
|
35
|
+
// "welcome": …, "placeholder": …, "context": […], "max_turns": …, "public": …,
|
|
36
|
+
// "error_message": …}. The agent then sets the service, the system prompt,
|
|
37
|
+
// the reasoning effort, the tools, the steps per question and the output
|
|
38
|
+
// limit; its authorization scheme decides who may use the region.
|
|
39
|
+
//
|
|
32
40
|
// - The conversation is kept per session and region (meta.ai_conversation,
|
|
33
41
|
// owner connection only), in the provider's format; the page shows its
|
|
34
42
|
// transcript. "New conversation" deletes it; so does signing out.
|
|
@@ -73,6 +81,8 @@ export interface ContextQuery {
|
|
|
73
81
|
}
|
|
74
82
|
|
|
75
83
|
export interface AssistantConfig {
|
|
84
|
+
/** (121) an AI agent of the application instead of service, system, tools and max_rounds */
|
|
85
|
+
agent?: string;
|
|
76
86
|
service?: string;
|
|
77
87
|
system?: string;
|
|
78
88
|
welcome?: string;
|
|
@@ -105,7 +115,10 @@ export function assistantProblems(conf: unknown): string[] {
|
|
|
105
115
|
if (!isObj(conf)) return ['The settings are a JSON object.'];
|
|
106
116
|
const c = conf as AssistantConfig;
|
|
107
117
|
const out: string[] = [];
|
|
108
|
-
if (
|
|
118
|
+
if (c.agent !== undefined) {
|
|
119
|
+
if (typeof c.agent !== 'string' || !AGENT_NAME.test(c.agent.toUpperCase())) out.push('"agent": the name of an AI agent, e.g. "SALES_AGENT".');
|
|
120
|
+
for (const k of ['service', 'system', 'tools', 'max_rounds'] as const) if (c[k] !== undefined) out.push(`"${k}": the agent sets it (leave it out).`);
|
|
121
|
+
} else if (typeof c.service !== 'string' || !/^[A-Za-z][A-Za-z0-9_]{0,59}$/.test(c.service)) out.push('"service": the name of an AI service, e.g. "CLAUDE".');
|
|
109
122
|
for (const k of ['system', 'welcome', 'placeholder', 'error_message'] as const)
|
|
110
123
|
if (c[k] !== undefined && typeof c[k] !== 'string') out.push(`"${k}" is text.`);
|
|
111
124
|
for (const [k, max] of [['max_rounds', 10], ['max_turns', 100]] as const)
|
|
@@ -272,6 +285,8 @@ interface Turn {
|
|
|
272
285
|
role: 'user' | 'assistant';
|
|
273
286
|
text: string;
|
|
274
287
|
tools?: { name: string; ok: boolean }[];
|
|
288
|
+
/** (121) links a navigate tool built (URLs with their checksum, made by pgkiln) */
|
|
289
|
+
links?: TurnExtras['links'];
|
|
275
290
|
}
|
|
276
291
|
|
|
277
292
|
interface Conversation {
|
|
@@ -337,8 +352,12 @@ export async function renderAssistant(ctx: PageContext, r: Region): Promise<Raw>
|
|
|
337
352
|
const t = ctx.locale.t;
|
|
338
353
|
const c = configOf(r);
|
|
339
354
|
if (assistantProblems(c).length) return html`<p class="empty">${t('assistant.not_configured')}</p>`;
|
|
355
|
+
const agent = c.agent ? await agentFor(ctx.app.id, c.agent) : null;
|
|
356
|
+
if (c.agent && !agent) return html`<p class="empty">${t('assistant.not_configured')}</p>`;
|
|
357
|
+
if (agent && !(await isAuthorized(ctx, agent.agent.authz))) return html`<p class="muted">${t('assistant.unauthorized')}</p>`;
|
|
358
|
+
const service = agent ? agent.agent.service.toUpperCase() : c.service!.toUpperCase();
|
|
340
359
|
const conv = await loadConversation(ctx, r);
|
|
341
|
-
const turns = conv && conv.service ===
|
|
360
|
+
const turns = conv && conv.service === service ? conv.turns : [];
|
|
342
361
|
const form = `as${r.id}`;
|
|
343
362
|
const clear = `asc${r.id}`;
|
|
344
363
|
const action = `${ctx.base}/${ctx.page.page_no}/assistant/${r.id}`;
|
|
@@ -348,10 +367,19 @@ export async function renderAssistant(ctx: PageContext, r: Region): Promise<Raw>
|
|
|
348
367
|
<form id="${clear}" method="post" action="${action}/clear">
|
|
349
368
|
<input type="hidden" name="__csrf" value="${ctx.session.csrf_token}"><input type="hidden" name="params" value="${keep}"></form>`);
|
|
350
369
|
const draft = ctx.session.state[`__AIDRAFT${r.id}`] ?? '';
|
|
351
|
-
|
|
370
|
+
// (121) the browser actions of the last answer (javascript tools): run once, right after it
|
|
371
|
+
const pending = ctx.session.state[`__AIACT${r.id}`] ?? '';
|
|
372
|
+
if (draft || pending) {
|
|
352
373
|
delete ctx.session.state[`__AIDRAFT${r.id}`];
|
|
374
|
+
delete ctx.session.state[`__AIACT${r.id}`];
|
|
353
375
|
await saveState(ctx.session);
|
|
354
376
|
}
|
|
377
|
+
let actions: TurnExtras['actions'] = [];
|
|
378
|
+
try {
|
|
379
|
+
actions = pending ? JSON.parse(pending) : [];
|
|
380
|
+
} catch {
|
|
381
|
+
// ignored
|
|
382
|
+
}
|
|
355
383
|
const welcome = c.welcome ? substitute(ctx.locale.tr(c.welcome), ctx, (v) => v) : '';
|
|
356
384
|
const usable = mayUse(ctx, r);
|
|
357
385
|
return html`<div class="assistant" data-assistant="${r.id}">
|
|
@@ -361,8 +389,11 @@ export async function renderAssistant(ctx: PageContext, r: Region): Promise<Raw>
|
|
|
361
389
|
<span class="assistant-who">${x.role === 'user' ? t('assistant.you') : t('assistant.assistant')}</span>
|
|
362
390
|
<div class="assistant-text">${x.role === 'user' ? html`<p>${raw(esc(x.text).replace(/\n/g, '<br>'))}</p>` : formatAnswer(x.text)}</div>
|
|
363
391
|
${x.tools?.length ? html`<small class="assistant-tools muted">${t('assistant.used', { tools: [...new Set(x.tools.map((y) => y.name))].join(', ') })}</small>` : ''}
|
|
392
|
+
${x.links?.length ? html`<div class="assistant-links" role="group" aria-label="${t('assistant.links')}">${x.links.map((l) =>
|
|
393
|
+
html`<a class="btn" href="${l.url}"${l.dialog ? raw(' data-dialog') : ''}>${l.label}</a>`)}</div>` : ''}
|
|
364
394
|
</li>`)}
|
|
365
395
|
</ol>
|
|
396
|
+
${actions.map((a) => html`<span hidden class="assistant-action" data-ai-action="${a.action}" data-ai-args="${JSON.stringify(a.args)}"></span>`)}
|
|
366
397
|
${usable
|
|
367
398
|
? html`<div class="assistant-input">
|
|
368
399
|
<label class="label" for="${form}_message">${t('assistant.message')}</label>
|
|
@@ -399,15 +430,23 @@ async function contextData(ctx: PageContext, c: AssistantConfig, question: strin
|
|
|
399
430
|
async function ask(ctx: PageContext, r: Region, message: string) {
|
|
400
431
|
const t = ctx.locale.t;
|
|
401
432
|
const c = configOf(r);
|
|
402
|
-
const
|
|
433
|
+
const agent = c.agent ? await agentFor(ctx.app.id, c.agent) : null;
|
|
434
|
+
if (c.agent && !agent) throw new InputError(t('assistant.not_configured'));
|
|
435
|
+
const service = agent ? agent.agent.service.toUpperCase() : c.service!.toUpperCase();
|
|
403
436
|
// checks, the system prompt, the context and the tools: one short transaction as the app's role
|
|
404
437
|
const prep = await appTx(txContext(ctx), async (client) => {
|
|
405
438
|
ctx.client = client;
|
|
406
439
|
await checkPageAccess(ctx);
|
|
407
440
|
ctx.vis = await computeVisibility(ctx);
|
|
408
441
|
if (!ctx.vis.regions.has(r.id)) throw new Forbidden(t('error.access_denied'));
|
|
409
|
-
|
|
410
|
-
|
|
442
|
+
if (agent && !(await isAuthorized(ctx, agent.agent.authz))) throw new Forbidden(t('error.access_denied'));
|
|
443
|
+
const system = aiSubstitute((agent ? agent.agent.system : c.system) ?? '', ctx);
|
|
444
|
+
return {
|
|
445
|
+
tools: agent ? [] : await offeredTools(ctx, c),
|
|
446
|
+
agentTools: agent ? await prepareTools(ctx, agent.tools) : [],
|
|
447
|
+
context: await contextData(ctx, c, message),
|
|
448
|
+
system,
|
|
449
|
+
};
|
|
411
450
|
});
|
|
412
451
|
ctx.client = undefined;
|
|
413
452
|
let conv = await loadConversation(ctx, r);
|
|
@@ -418,22 +457,35 @@ async function ask(ctx: PageContext, r: Region, message: string) {
|
|
|
418
457
|
conv = undefined;
|
|
419
458
|
}
|
|
420
459
|
if (conv && conv.turns.filter((x) => x.role === 'user').length >= (c.max_turns ?? DEFAULT_TURNS)) throw new InputError(t('assistant.max_turns'));
|
|
421
|
-
const system = [prep.system.text.trim(), DATA_NOTE, prep.tools.length ? TOOLS_NOTE : ''].filter(Boolean).join('\n\n');
|
|
460
|
+
const system = [prep.system.text.trim(), DATA_NOTE, prep.tools.length || prep.agentTools.length ? TOOLS_NOTE : ''].filter(Boolean).join('\n\n');
|
|
422
461
|
const content = [...prep.context, message].join('\n\n');
|
|
423
462
|
dbg(ctx, 9, 'ai', () => `system prompt: ${system}`);
|
|
424
463
|
dbg(ctx, 9, 'ai', () => `message: ${content}`);
|
|
425
464
|
const byName = new Map(prep.tools.map((x) => [x.name, x]));
|
|
465
|
+
const agentTools = new Map(prep.agentTools.map((x) => [x.tool.name, x]));
|
|
466
|
+
const extras: TurnExtras = { links: [], actions: [] };
|
|
467
|
+
// every tool call is logged with its arguments (meta.ai_tool_call, debug)
|
|
468
|
+
const runLogged = async (name: string, input: Record<string, unknown>) => {
|
|
469
|
+
const t0 = performance.now();
|
|
470
|
+
const p = agentTools.get(name);
|
|
471
|
+
const out: AgentOutcome = p
|
|
472
|
+
? await runAgentTool(ctx, p, input, extras, (def, args) => runTool(ctx, def, args))
|
|
473
|
+
: agent ? { content: 'There is no such tool.', isError: true } : await runTool(ctx, byName.get(name), input);
|
|
474
|
+
await logToolCall(ctx, r.id, agent?.agent.name ?? null, name, p?.tool.type ?? byName.get(name)?.type ?? 'sql', input, out, performance.now() - t0);
|
|
475
|
+
return out;
|
|
476
|
+
};
|
|
426
477
|
const res = await chat(service, {
|
|
427
478
|
system,
|
|
428
479
|
history: conv?.messages ?? [],
|
|
429
480
|
message: content,
|
|
430
|
-
tools: prep.tools.map((x): ChatTool => ({ name: x.name, description: x.description, parameters: toolSchema(x) })),
|
|
431
|
-
maxRounds: c.max_rounds ?? DEFAULT_ROUNDS,
|
|
432
|
-
runTool:
|
|
481
|
+
tools: agent ? prep.agentTools.map((x) => x.chat) : prep.tools.map((x): ChatTool => ({ name: x.name, description: x.description, parameters: toolSchema(x) })),
|
|
482
|
+
maxRounds: agent ? (agent.agent.max_steps ?? DEFAULT_STEPS) : (c.max_rounds ?? DEFAULT_ROUNDS),
|
|
483
|
+
runTool: runLogged,
|
|
484
|
+
...(agent ? { effort: agent.agent.effort ?? null, maxTokens: agent.agent.max_tokens ?? null } : {}),
|
|
433
485
|
}, { appId: ctx.app.id, pageNo: ctx.page.page_no, user: ctx.user, source: 'assistant', debug: (l, x) => dbg(ctx, l, 'ai', x) });
|
|
434
486
|
dbg(ctx, 9, 'ai', () => `answer: ${res.text}`);
|
|
435
487
|
const turns: Turn[] = [...(conv?.turns ?? []), { role: 'user', text: message },
|
|
436
|
-
{ role: 'assistant', text: res.text || t('assistant.no_answer'), ...(res.tools.length ? { tools: res.tools } : {}) }];
|
|
488
|
+
{ role: 'assistant', text: res.text || t('assistant.no_answer'), ...(res.tools.length ? { tools: res.tools } : {}), ...(extras.links.length ? { links: extras.links } : {}) }];
|
|
437
489
|
const messages = JSON.stringify(res.history);
|
|
438
490
|
if (messages.length > MAX_HISTORY_BYTES) throw new InputError(t('assistant.max_turns'));
|
|
439
491
|
const saved = conv
|
|
@@ -444,6 +496,7 @@ async function ask(ctx: PageContext, r: Region, message: string) {
|
|
|
444
496
|
[ctx.app.id, r.id, ctx.session.id, ctx.user, service, provider, messages, JSON.stringify(turns)],
|
|
445
497
|
);
|
|
446
498
|
if (!saved.rowCount) throw new InputError(t('assistant.busy'));
|
|
499
|
+
if (extras.actions.length) ctx.session.state[`__AIACT${r.id}`] = JSON.stringify(extras.actions);
|
|
447
500
|
}
|
|
448
501
|
|
|
449
502
|
export async function assistantRoutes(app: FastifyInstance) {
|
package/src/runtime/context.ts
CHANGED
|
@@ -62,6 +62,8 @@ export interface PageContext {
|
|
|
62
62
|
locale: Locale;
|
|
63
63
|
/** set when a link with a per-user checksum was rendered (region caching for all users skips such regions) */
|
|
64
64
|
userBound?: boolean;
|
|
65
|
+
/** (sprint 112) reports whose settings show as chips in their "Ask in your own words" box (not in the report's own chip row) */
|
|
66
|
+
askChips?: Set<number>;
|
|
65
67
|
/** render cached regions anew (a dynamic action's Refresh region), storing the new result */
|
|
66
68
|
cacheRefresh?: boolean;
|
|
67
69
|
/** the lazy region the request renders (GET …/region/:id), not as a placeholder */
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import pg from 'pg';
|
|
2
2
|
import type { FastifyInstance, FastifyReply } from 'fastify';
|
|
3
|
-
import {
|
|
3
|
+
import { appDatasets, DATASET_NAME, datasetSql } from '../datasets.ts';
|
|
4
|
+
import { appTx, owner, runtime, savepoint } from '../db.ts';
|
|
4
5
|
import { html, raw, type Raw } from '../html.ts';
|
|
6
|
+
import { english } from '../i18n.ts';
|
|
5
7
|
import type { Region } from '../metadata.ts';
|
|
6
8
|
import { logActivity, saveState } from '../session.ts';
|
|
7
9
|
import { checkPageAccess, computeVisibility, Forbidden, isAuthorized } from './authz.ts';
|
|
@@ -21,7 +23,13 @@ import { loadContext, simplePage, txContext, type Req } from './routes.ts';
|
|
|
21
23
|
// "columns": [{"name": "ename", "label": "Name"}, {"name": "sal", "label": "Salary", "format": "FML999G990D00"}]}],
|
|
22
24
|
// "sharing": true, // users may share reports with everyone (default true)
|
|
23
25
|
// "share_authz": "SCHEME", // …but only users that pass this scheme (default: every signed-in user)
|
|
24
|
-
// "page_size": 25
|
|
26
|
+
// "page_size": 25,
|
|
27
|
+
// // (sprint 110) a workspace dataset as a source (the builder's Data Reporter): its joined tables, columns,
|
|
28
|
+
// // labels, masks and row filter come from meta.dataset; label and description may be given here too
|
|
29
|
+
// // "sources": [{"id": "hr", "dataset": "hr"}]
|
|
30
|
+
// "edit_authz": "SCHEME", // who may build, change and save reports; others only run shared ones (default: everyone)
|
|
31
|
+
// "admin_authz": "SCHEME", // who may unpublish and delete anyone's reports of the region
|
|
32
|
+
// "publishing": true // shared reports are "published": a stable URL (/a/<alias>/reports/<id>), pinning
|
|
25
33
|
// }
|
|
26
34
|
//
|
|
27
35
|
// A report is a definition (Def): the source, the columns, filters, group by
|
|
@@ -39,6 +47,7 @@ export interface SourceColumn {
|
|
|
39
47
|
name: string;
|
|
40
48
|
label?: string;
|
|
41
49
|
format?: string;
|
|
50
|
+
description?: string;
|
|
42
51
|
}
|
|
43
52
|
export interface Source {
|
|
44
53
|
id: string;
|
|
@@ -47,6 +56,10 @@ export interface Source {
|
|
|
47
56
|
schema: string;
|
|
48
57
|
table: string;
|
|
49
58
|
columns: SourceColumn[];
|
|
59
|
+
/** (sprint 110) the name of a workspace dataset: schema and table are empty until resolveSources() */
|
|
60
|
+
dataset?: string;
|
|
61
|
+
/** (sprint 110) the dataset's SELECT (built by datasetSql from the checked definition) */
|
|
62
|
+
sql?: string;
|
|
50
63
|
}
|
|
51
64
|
export interface Def {
|
|
52
65
|
source: string;
|
|
@@ -73,6 +86,10 @@ export function sourcesOf(r: { config: Record<string, any> }): Source[] {
|
|
|
73
86
|
const list = Array.isArray(r.config.sources) ? r.config.sources : [];
|
|
74
87
|
const out: Source[] = [];
|
|
75
88
|
for (const s of list) {
|
|
89
|
+
if (s && typeof s === 'object' && SOURCE_ID.test(str(s.id)) && DATASET_NAME.test(str(s.dataset)) && !out.some((x) => x.id === s.id)) {
|
|
90
|
+
out.push({ id: s.id, label: str(s.label) || undefined, description: str(s.description) || undefined, schema: '', table: '', columns: [], dataset: s.dataset });
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
76
93
|
if (!s || typeof s !== 'object' || !SOURCE_ID.test(str(s.id)) || !str(s.schema) || !str(s.table) || out.some((x) => x.id === s.id)) continue;
|
|
77
94
|
// never pgkiln's own or the system's tables
|
|
78
95
|
if (RESERVED_SCHEMAS.test(s.schema)) continue;
|
|
@@ -82,15 +99,41 @@ export function sourcesOf(r: { config: Record<string, any> }): Source[] {
|
|
|
82
99
|
return out;
|
|
83
100
|
}
|
|
84
101
|
|
|
102
|
+
/**
|
|
103
|
+
* (sprint 110) The region's sources with its datasets filled in (schema, columns with labels, masks and descriptions,
|
|
104
|
+
* the dataset's SELECT); a dataset source whose dataset is gone is left out.
|
|
105
|
+
*/
|
|
106
|
+
export async function resolveSources(r: { config: Record<string, any> }, appId: number): Promise<Source[]> {
|
|
107
|
+
const list = sourcesOf(r);
|
|
108
|
+
const names = [...new Set(list.map((s) => s.dataset).filter((x): x is string => !!x))];
|
|
109
|
+
if (!names.length) return list;
|
|
110
|
+
const sets = await appDatasets(runtime, appId, names);
|
|
111
|
+
const out: Source[] = [];
|
|
112
|
+
for (const s of list) {
|
|
113
|
+
if (!s.dataset) {
|
|
114
|
+
out.push(s);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const d = sets.get(s.dataset);
|
|
118
|
+
if (!d || !d.columns.length) continue;
|
|
119
|
+
out.push({
|
|
120
|
+
...s, label: s.label ?? d.label, description: s.description ?? d.description, schema: d.schema, table: d.name, sql: datasetSql(d),
|
|
121
|
+
columns: d.columns.map((c) => ({ name: c.name, ...(c.label ? { label: c.label } : {}), ...(c.format ? { format: c.format } : {}), ...(c.description ? { description: c.description } : {}) })),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
85
127
|
/** A column a user may use: offered by the developer and readable now. */
|
|
86
128
|
export interface Col {
|
|
87
129
|
name: string;
|
|
88
130
|
label: string;
|
|
89
131
|
format?: string;
|
|
132
|
+
description?: string;
|
|
90
133
|
type: number;
|
|
91
134
|
}
|
|
92
135
|
|
|
93
|
-
const objectSql = (s: Source) => `${pg.escapeIdentifier(s.schema)}.${pg.escapeIdentifier(s.table)}
|
|
136
|
+
const objectSql = (s: Source) => (s.sql ? `(${s.sql})` : `${pg.escapeIdentifier(s.schema)}.${pg.escapeIdentifier(s.table)}`);
|
|
94
137
|
const q = (col: string) => `"__q".${pg.escapeIdentifier(col)}`;
|
|
95
138
|
|
|
96
139
|
/**
|
|
@@ -103,7 +146,7 @@ export async function availableColumns(c: pg.PoolClient, s: Source, tr: (x: stri
|
|
|
103
146
|
const out = new Map<string, Col>();
|
|
104
147
|
for (const sc of s.columns)
|
|
105
148
|
if (types.has(sc.name) && !out.has(sc.name))
|
|
106
|
-
out.set(sc.name, { name: sc.name, label: tr(sc.label || heading(sc.name)), format: sc.format || undefined, type: types.get(sc.name)! });
|
|
149
|
+
out.set(sc.name, { name: sc.name, label: tr(sc.label || heading(sc.name)), format: sc.format || undefined, description: sc.description ? tr(sc.description) : undefined, type: types.get(sc.name)! });
|
|
107
150
|
return out;
|
|
108
151
|
}
|
|
109
152
|
|
|
@@ -239,6 +282,35 @@ export async function mayShare(ctx: PageContext, r: Region) {
|
|
|
239
282
|
return typeof r.config.share_authz === 'string' && r.config.share_authz ? isAuthorized(ctx, r.config.share_authz) : true;
|
|
240
283
|
}
|
|
241
284
|
|
|
285
|
+
/** (sprint 110) May the current user build, change and save reports of this region (config.edit_authz)? */
|
|
286
|
+
export async function mayEdit(ctx: PageContext, r: Region) {
|
|
287
|
+
const a = r.config.edit_authz;
|
|
288
|
+
return typeof a === 'string' && a ? isAuthorized(ctx, a) : true;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** (sprint 110) May the current user unpublish and delete anyone's reports of this region (config.admin_authz)? */
|
|
292
|
+
export async function mayModerate(ctx: PageContext, r: Region) {
|
|
293
|
+
const a = r.config.admin_authz;
|
|
294
|
+
return ctx.user !== 'nobody' && typeof a === 'string' && a ? isAuthorized(ctx, a) : false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** (sprint 110) Shared reports are called published (a stable URL, pins). */
|
|
298
|
+
const publishing = (r: Region) => r.config.publishing === true;
|
|
299
|
+
|
|
300
|
+
/** (sprint 110) A report's stable URL: /a/<alias>/reports/<id> (it opens the report on its page). */
|
|
301
|
+
export const reportLink = (ctx: PageContext, id: number) => `${ctx.base}/reports/${id}`;
|
|
302
|
+
|
|
303
|
+
const MAX_PINS = 20;
|
|
304
|
+
const pinsName = (r: Region) => `reporter.pins.${r.id}`;
|
|
305
|
+
|
|
306
|
+
/** (sprint 110) The published reports the current user pinned in this region (a user preference; with config.publishing). */
|
|
307
|
+
async function pinsOf(ctx: PageContext, r: Region): Promise<number[]> {
|
|
308
|
+
if (ctx.user === 'nobody') return [];
|
|
309
|
+
const c = ctx.client!;
|
|
310
|
+
const v = (await savepoint(c, () => c.query<{ v: string | null }>('select meta.get_preference($1) as v', [pinsName(r)]))).rows[0]?.v ?? '';
|
|
311
|
+
return v.split(',').filter((x) => /^\d{1,9}$/.test(x)).map(Number).slice(0, MAX_PINS);
|
|
312
|
+
}
|
|
313
|
+
|
|
242
314
|
const pageSize = (r: Region) => {
|
|
243
315
|
const n = Number(r.config.page_size);
|
|
244
316
|
return Number.isInteger(n) && n >= 5 && n <= 200 ? n : DEFAULT_PAGE_SIZE;
|
|
@@ -261,27 +333,29 @@ export async function renderDataReporter(ctx: PageContext, r: Region): Promise<R
|
|
|
261
333
|
const t = ctx.locale.t;
|
|
262
334
|
const c = ctx.client!;
|
|
263
335
|
const prefix = prefixOf(r);
|
|
264
|
-
const sources =
|
|
336
|
+
const sources = await resolveSources(r, ctx.app.id);
|
|
265
337
|
if (!sources.length) return html`<p class="empty">${t('reporter.no_sources')}</p>`;
|
|
338
|
+
// (sprint 110) users who may not edit only run saved reports: no editor, no reports of their own
|
|
339
|
+
const edit = await mayEdit(ctx, r);
|
|
266
340
|
const openId = Number(ctx.params.get(`${prefix}open`));
|
|
267
341
|
let saved: Saved | undefined;
|
|
268
342
|
if (Number.isInteger(openId) && openId > 0) {
|
|
269
343
|
saved = (await savepoint(c, () => c.query<Saved>('select * from meta.data_reports where id = $1 and region_id = $2', [openId, r.id]))).rows[0];
|
|
270
|
-
if (!saved) return html`<div class="alert alert-error" role="alert">${t('reporter.not_found')}</div>${await home(ctx, r, sources)}`;
|
|
344
|
+
if (!saved) return html`<div class="alert alert-error" role="alert">${t('reporter.not_found')}</div>${await home(ctx, r, sources, edit)}`;
|
|
271
345
|
}
|
|
272
|
-
const edited = ctx.params.has(`${prefix}src`);
|
|
346
|
+
const edited = edit && ctx.params.has(`${prefix}src`);
|
|
273
347
|
const sourceId = edited ? ctx.params.get(`${prefix}src`)! : saved ? str(saved.definition?.source) : '';
|
|
274
|
-
if (!sourceId) return home(ctx, r, sources);
|
|
348
|
+
if (!sourceId) return home(ctx, r, sources, edit);
|
|
275
349
|
const source = sources.find((s) => s.id === sourceId);
|
|
276
|
-
if (!source) return html`<div class="alert alert-error" role="alert">${t('reporter.unknown_source')}</div>${await home(ctx, r, sources)}`;
|
|
350
|
+
if (!source) return html`<div class="alert alert-error" role="alert">${t('reporter.unknown_source')}</div>${await home(ctx, r, sources, edit)}`;
|
|
277
351
|
let cols: Map<string, Col>;
|
|
278
352
|
try {
|
|
279
353
|
cols = await availableColumns(c, source, ctx.locale.tr);
|
|
280
354
|
} catch (e) {
|
|
281
|
-
return html`<div class="alert alert-error" role="alert">${await publicError(ctx, e, `data reporter "${r.title ?? r.id}" source ${source.id}`)}</div>${await home(ctx, r, sources)}`;
|
|
355
|
+
return html`<div class="alert alert-error" role="alert">${await publicError(ctx, e, `data reporter "${r.title ?? r.id}" source ${source.id}`)}</div>${await home(ctx, r, sources, edit)}`;
|
|
282
356
|
}
|
|
283
357
|
const def = checkDef(edited ? defFromParams(ctx.params, prefix) : saved!.definition, source.id, cols);
|
|
284
|
-
return reportView(ctx, r, source, cols, def, saved, edited);
|
|
358
|
+
return reportView(ctx, r, source, cols, def, saved, edited, edit);
|
|
285
359
|
}
|
|
286
360
|
|
|
287
361
|
/**
|
|
@@ -291,12 +365,12 @@ export async function renderDataReporter(ctx: PageContext, r: Region): Promise<R
|
|
|
291
365
|
* keeps the compact form (a list of sources and a Start button); welcome_title
|
|
292
366
|
* and welcome_text replace the greeting.
|
|
293
367
|
*/
|
|
294
|
-
async function home(ctx: PageContext, r: Region, sources: Source[]): Promise<Raw> {
|
|
368
|
+
async function home(ctx: PageContext, r: Region, sources: Source[], edit = true): Promise<Raw> {
|
|
295
369
|
const t = ctx.locale.t;
|
|
296
370
|
const c = ctx.client!;
|
|
297
371
|
const prefix = prefixOf(r);
|
|
298
372
|
const q = (ctx.params.get(`${prefix}q`) ?? '').trim().slice(0, 100);
|
|
299
|
-
const
|
|
373
|
+
const all = (
|
|
300
374
|
await savepoint(c, () =>
|
|
301
375
|
c.query<Saved & { updated_at: string }>(
|
|
302
376
|
`select id, name, description, username, shared, own, definition, updated_at from meta.data_reports
|
|
@@ -304,6 +378,11 @@ async function home(ctx: PageContext, r: Region, sources: Source[]): Promise<Raw
|
|
|
304
378
|
order by own desc, case when own then updated_at end desc, lower(name), id`, [r.id, q]),
|
|
305
379
|
)
|
|
306
380
|
).rows;
|
|
381
|
+
// (sprint 110) users who may not edit see the shared (published) reports only
|
|
382
|
+
const reports = edit ? all : all.filter((x) => x.shared);
|
|
383
|
+
const pub = publishing(r);
|
|
384
|
+
const pins = pub ? await pinsOf(ctx, r) : [];
|
|
385
|
+
const sharedTag = pub ? t('reporter.published_tag') : t('reporter.shared_tag');
|
|
307
386
|
const labelOf = (id: string) => {
|
|
308
387
|
const s = sources.find((x) => x.id === id);
|
|
309
388
|
return s ? ctx.locale.tr(s.label || heading(s.table)) : id;
|
|
@@ -311,51 +390,54 @@ async function home(ctx: PageContext, r: Region, sources: Source[]): Promise<Raw
|
|
|
311
390
|
const list = (rows: (Saved & { updated_at: string })[]) =>
|
|
312
391
|
html`<ul class="reporter-list">${rows.map((x) => html`<li>
|
|
313
392
|
<a href="${url(ctx, r, new URLSearchParams([[`${prefix}open`, String(x.id)]]))}">${x.name}</a>
|
|
314
|
-
<span class="muted">${labelOf(str(x.definition?.source))}${x.own ? html` · ${t('reporter.changed', { date: String(x.updated_at).slice(0, 10) })}` : html` · ${t('reporter.by', { user: x.username })}`}</span>${x.definition?.chart ? html` <span class="tag">${t('reporter.chart_tag')}</span>` : ''}${x.own && x.shared ? html` <span class="tag">${
|
|
393
|
+
<span class="muted">${labelOf(str(x.definition?.source))}${x.own ? html` · ${t('reporter.changed', { date: String(x.updated_at).slice(0, 10) })}` : html` · ${t('reporter.by', { user: x.username })}`}</span>${x.definition?.chart ? html` <span class="tag">${t('reporter.chart_tag')}</span>` : ''}${x.own && x.shared ? html` <span class="tag">${sharedTag}</span>` : ''}
|
|
315
394
|
${x.description ? html`<p class="muted reporter-desc">${x.description}</p>` : ''}
|
|
316
395
|
</li>`)}</ul>`;
|
|
317
|
-
const
|
|
318
|
-
const
|
|
396
|
+
const pinned = pins.map((id) => reports.find((x) => x.id === id)).filter((x): x is (typeof reports)[number] => !!x);
|
|
397
|
+
const mine = edit ? reports.filter((x) => x.own) : [];
|
|
398
|
+
const others = edit ? reports.filter((x) => !x.own) : reports;
|
|
319
399
|
const keep = [...ctx.params.entries()].filter(([k]) => !k.startsWith(prefix) && !['clear', 'cs'].includes(k));
|
|
320
400
|
const hidden = keep.map(([k, v]) => html`<input type="hidden" name="${k}" value="${v}">`);
|
|
321
|
-
const lists = html`${
|
|
322
|
-
${
|
|
401
|
+
const lists = html`${pinned.length ? html`<h3 class="reporter-h">${t('reporter.pinned')}</h3>${list(pinned)}` : ''}
|
|
402
|
+
${edit && ctx.user !== 'nobody' ? html`<h3 class="reporter-h">${t('reporter.my_reports')}</h3>${mine.length ? list(mine) : html`<p class="muted">${q ? t('reporter.no_match') : t('reporter.no_reports')}</p>`}` : ''}
|
|
403
|
+
${others.length ? html`<h3 class="reporter-h">${pub ? t('reporter.published_reports') : t('reporter.shared_reports')}</h3>${list(others)}`
|
|
404
|
+
: edit ? '' : html`<h3 class="reporter-h">${pub ? t('reporter.published_reports') : t('reporter.shared_reports')}</h3><p class="muted">${q ? t('reporter.no_match') : t('reporter.no_published')}</p>`}`;
|
|
323
405
|
|
|
324
406
|
if (r.config.welcome === false) {
|
|
325
407
|
const form = `drn${r.id}`;
|
|
326
408
|
ctx.detached.push(html`<form id="${form}" method="get" action="${ctx.base}/${ctx.page.page_no}">${hidden}</form>`);
|
|
327
409
|
return html`<div class="reporter">
|
|
328
|
-
|
|
410
|
+
${edit ? html`<div class="reporter-new">
|
|
329
411
|
<label class="label" for="${form}_src">${t('reporter.new')}</label>
|
|
330
412
|
<div class="reporter-row">
|
|
331
413
|
${sel(`${prefix}src`, form, t('reporter.source'), sources.map((s) => [s.id, ctx.locale.tr(s.label || heading(s.table))]), sources[0].id, `${form}_src`)}
|
|
332
414
|
<button class="btn btn-hot" form="${form}">${t('reporter.create')}</button>
|
|
333
415
|
</div>
|
|
334
416
|
${sources.some((s) => s.description) ? html`<dl class="reporter-sources">${sources.filter((s) => s.description).map((s) => html`<dt>${ctx.locale.tr(s.label || heading(s.table))}</dt><dd>${ctx.locale.tr(s.description!)}</dd>`)}</dl>` : ''}
|
|
335
|
-
</div
|
|
417
|
+
</div>` : ''}
|
|
336
418
|
${lists}
|
|
337
419
|
</div>`;
|
|
338
420
|
}
|
|
339
421
|
|
|
340
422
|
const title = typeof r.config.welcome_title === 'string' && r.config.welcome_title.trim() ? ctx.locale.tr(r.config.welcome_title) : t('reporter.welcome_title');
|
|
341
|
-
const text = typeof r.config.welcome_text === 'string' && r.config.welcome_text.trim() ? ctx.locale.tr(r.config.welcome_text) : t('reporter.welcome_text');
|
|
423
|
+
const text = typeof r.config.welcome_text === 'string' && r.config.welcome_text.trim() ? ctx.locale.tr(r.config.welcome_text) : edit ? t('reporter.welcome_text') : t('reporter.welcome_viewer');
|
|
342
424
|
// the search: a GET form of its own (detached: the region may sit inside the page's form)
|
|
343
425
|
const search = `drq${r.id}`;
|
|
344
426
|
const searchable = q !== '' || reports.length > 6;
|
|
345
427
|
if (searchable) ctx.detached.push(html`<form id="${search}" method="get" action="${ctx.base}/${ctx.page.page_no}" role="search">${hidden}</form>`);
|
|
346
428
|
return html`<div class="reporter reporter-welcome">
|
|
347
429
|
<div class="reporter-hello"><h3 class="reporter-title">${title}</h3><p class="muted">${text}</p></div>
|
|
348
|
-
|
|
430
|
+
${edit ? html`<h3 class="reporter-h">${t('reporter.new')}</h3>
|
|
349
431
|
<ul class="reporter-cards">${sources.map((s) => html`<li><a class="reporter-card" href="${url(ctx, r, new URLSearchParams([[`${prefix}src`, s.id]]))}">
|
|
350
432
|
<strong>${ctx.locale.tr(s.label || heading(s.table))}</strong>
|
|
351
433
|
${s.description ? html`<span class="muted">${ctx.locale.tr(s.description)}</span>` : ''}
|
|
352
|
-
<span class="reporter-card-meta">${t('reporter.columns_count', { n: s.columns.length })}</span></a></li>`)}</ul
|
|
434
|
+
<span class="reporter-card-meta">${t('reporter.columns_count', { n: s.columns.length })}</span></a></li>`)}</ul>` : ''}
|
|
353
435
|
${searchable ? html`<div class="reporter-row reporter-search"><input type="search" name="${prefix}q" value="${q}" form="${search}" aria-label="${t('reporter.search')}" placeholder="${t('reporter.search')}"><button class="btn" form="${search}">${t('reporter.search')}</button></div>` : ''}
|
|
354
436
|
${lists}
|
|
355
437
|
</div>`;
|
|
356
438
|
}
|
|
357
439
|
|
|
358
|
-
async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map<string, Col>, def: Def, saved: Saved | undefined, edited: boolean): Promise<Raw> {
|
|
440
|
+
async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map<string, Col>, def: Def, saved: Saved | undefined, edited: boolean, edit = true): Promise<Raw> {
|
|
359
441
|
const t = ctx.locale.t;
|
|
360
442
|
const prefix = prefixOf(r);
|
|
361
443
|
const form = `dre${r.id}`;
|
|
@@ -374,7 +456,7 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
|
|
|
374
456
|
const editor = html`<details class="reporter-editor"${!saved || edited ? raw(' open') : ''}>
|
|
375
457
|
<summary>${t('reporter.edit')}</summary>
|
|
376
458
|
<fieldset class="reporter-set"><legend>${t('reporter.columns')}</legend>
|
|
377
|
-
<div class="reporter-checks">${[...cols.values()].map((x) => html`<label class="check"><input type="checkbox" name="${prefix}col" value="${x.name}" form="${form}"${shownCols.includes(x.name) ? raw(' checked') : ''}> ${x.label}</label>`)}</div>
|
|
459
|
+
<div class="reporter-checks">${[...cols.values()].map((x) => html`<label class="check"><input type="checkbox" name="${prefix}col" value="${x.name}" form="${form}"${shownCols.includes(x.name) ? raw(' checked') : ''}> ${x.label}${x.description ? html` <small class="muted reporter-coldesc">${x.description}</small>` : ''}</label>`)}</div>
|
|
378
460
|
</fieldset>
|
|
379
461
|
<fieldset class="reporter-set"><legend>${t('reporter.filters')}</legend>
|
|
380
462
|
${rows(def.filters, LIMITS.filters, { column: '', op: 'eq', value: '' }).map((f) => html`<div class="reporter-row">
|
|
@@ -412,7 +494,8 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
|
|
|
412
494
|
|
|
413
495
|
// ---- saving (signed-in users)
|
|
414
496
|
let save: Raw | '' = '';
|
|
415
|
-
|
|
497
|
+
const pub = publishing(r);
|
|
498
|
+
if (edit && ctx.user !== 'nobody') {
|
|
416
499
|
const share = await mayShare(ctx, r);
|
|
417
500
|
const own = saved?.own ? saved : undefined;
|
|
418
501
|
const sform = `drs${r.id}`;
|
|
@@ -429,7 +512,7 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
|
|
|
429
512
|
<label class="label" for="${fid('desc')}">${t('reporter.description')}</label>
|
|
430
513
|
<input id="${fid('desc')}" name="description" form="${sform}" maxlength="500" value="${own?.description ?? ''}">
|
|
431
514
|
</div>
|
|
432
|
-
${share ? html`<label class="check"><input type="checkbox" name="shared" value="true" form="${sform}"${own?.shared ? raw(' checked') : ''}> ${t('reporter.share')}</label>` : ''}
|
|
515
|
+
${share ? html`<label class="check"><input type="checkbox" name="shared" value="true" form="${sform}"${own?.shared ? raw(' checked') : ''}> ${pub ? t('reporter.publish_on_save') : t('reporter.share')}</label>` : ''}
|
|
433
516
|
<div class="buttons">
|
|
434
517
|
<button class="btn btn-hot" form="${sform}" name="mode" value="save">${t('report.save')}</button>
|
|
435
518
|
${own ? html`<button class="btn" form="${sform}" name="mode" value="new">${t('reporter.save_as_new')}</button>
|
|
@@ -438,14 +521,40 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
|
|
|
438
521
|
</fieldset>`;
|
|
439
522
|
}
|
|
440
523
|
|
|
524
|
+
// (sprint 110) a saved report's actions: its stable URL, pin, publish / unpublish, an administrator's unpublish and delete
|
|
525
|
+
let actions: Raw | '' = '';
|
|
526
|
+
if (saved && ctx.user !== 'nobody') {
|
|
527
|
+
const base = `${ctx.base}/${ctx.page.page_no}/reporter/${r.id}`;
|
|
528
|
+
const csrf = html`<input type="hidden" name="__csrf" value="${ctx.session.csrf_token}">`;
|
|
529
|
+
const post = (action: string, mode: string, label: string, cls = 'btn', confirm = '') => {
|
|
530
|
+
const fid2 = `dra${r.id}_${action}_${mode}`;
|
|
531
|
+
ctx.detached.push(html`<form id="${fid2}" method="post" action="${base}/${action}">${csrf}<input type="hidden" name="rep" value="${saved.id}"><input type="hidden" name="mode" value="${mode}"></form>`);
|
|
532
|
+
return html`<button class="${cls}" form="${fid2}"${confirm ? html` data-confirm="${confirm}"` : ''}>${label}</button>`;
|
|
533
|
+
};
|
|
534
|
+
const pins = await pinsOf(ctx, r);
|
|
535
|
+
const moderate = !saved.own && (await mayModerate(ctx, r));
|
|
536
|
+
const buttons: Raw[] = [];
|
|
537
|
+
if (pub && saved.shared) buttons.push(post('pin', pins.includes(saved.id) ? 'unpin' : 'pin', pins.includes(saved.id) ? t('reporter.unpin') : t('reporter.pin')));
|
|
538
|
+
if (pub && saved.own && edit && (saved.shared || (await mayShare(ctx, r)))) buttons.push(post('publish', saved.shared ? 'unpublish' : 'publish', saved.shared ? t('reporter.unpublish') : t('reporter.publish'), saved.shared ? 'btn' : 'btn btn-hot'));
|
|
539
|
+
if (moderate && saved.shared) buttons.push(post('publish', 'unpublish', t('reporter.unpublish')));
|
|
540
|
+
if (moderate) buttons.push(post('delete', 'any', t('reporter.delete'), 'btn btn-danger', t('reporter.delete_confirm', { name: saved.name })));
|
|
541
|
+
const link = pub && saved.shared ? reportLink(ctx, saved.id) : '';
|
|
542
|
+
if (buttons.length || link)
|
|
543
|
+
actions = html`<div class="reporter-actions">
|
|
544
|
+
${link ? html`<label class="label" for="${fid('link')}">${t('reporter.link')}</label><input id="${fid('link')}" class="reporter-link" readonly value="${link}"> <a href="${link}">${t('reporter.open_link')}</a>` : ''}
|
|
545
|
+
${buttons.length ? html`<div class="buttons">${buttons}</div>` : ''}
|
|
546
|
+
</div>`;
|
|
547
|
+
}
|
|
548
|
+
|
|
441
549
|
return html`<div class="reporter">
|
|
442
550
|
<div class="reporter-head">
|
|
443
551
|
<a href="${url(ctx, r, null)}">‹ ${t('reporter.back')}</a>
|
|
444
552
|
<h3 class="reporter-title">${title}</h3>
|
|
445
|
-
<span class="muted">${saved ? html`${ctx.locale.tr(source.label || heading(source.table))}${saved.own ? '' : html` · ${t('reporter.by', { user: saved.username })}`}${saved.shared ? html` <span class="tag">${t('reporter.shared_tag')}</span>` : ''}` : ''}</span>
|
|
553
|
+
<span class="muted">${saved ? html`${ctx.locale.tr(source.label || heading(source.table))}${saved.own ? '' : html` · ${t('reporter.by', { user: saved.username })}`}${saved.shared ? html` <span class="tag">${pub ? t('reporter.published_tag') : t('reporter.shared_tag')}</span>` : ''}` : ''}</span>
|
|
446
554
|
${saved?.description ? html`<p class="muted reporter-desc">${saved.description}</p>` : ''}
|
|
447
555
|
</div>
|
|
448
|
-
${
|
|
556
|
+
${actions}
|
|
557
|
+
${edit ? editor : ''}
|
|
449
558
|
${result}
|
|
450
559
|
${save}
|
|
451
560
|
</div>`;
|
|
@@ -514,6 +623,12 @@ async function runReport(ctx: PageContext, r: Region, source: Source, cols: Map<
|
|
|
514
623
|
|
|
515
624
|
// ---------------------------------------------------------------- saving and deleting
|
|
516
625
|
|
|
626
|
+
/** A report of the region the current user can see (own or shared), through meta.data_reports. */
|
|
627
|
+
async function reportOf(ctx: PageContext, r: Region, id: number) {
|
|
628
|
+
if (!id) return undefined;
|
|
629
|
+
return (await ctx.client!.query<{ id: number; name: string; own: boolean; shared: boolean }>('select id, name, own, shared from meta.data_reports where id = $1 and region_id = $2', [id, r.id])).rows[0];
|
|
630
|
+
}
|
|
631
|
+
|
|
517
632
|
export async function dataReporterRoutes(app: FastifyInstance) {
|
|
518
633
|
/** Shared checks: CSRF, a signed-in user, the region visible on the page; back to the page afterwards. */
|
|
519
634
|
const action = async (req: Req, reply: FastifyReply, run: (ctx: PageContext, r: Region) => Promise<{ message: string; back: URLSearchParams }>) => {
|
|
@@ -558,7 +673,8 @@ export async function dataReporterRoutes(app: FastifyInstance) {
|
|
|
558
673
|
const b = req.body ?? {};
|
|
559
674
|
const prefix = prefixOf(r);
|
|
560
675
|
const params = new URLSearchParams(typeof b.params === 'string' ? b.params.slice(0, 8000) : '');
|
|
561
|
-
|
|
676
|
+
if (!(await mayEdit(ctx, r))) throw new Forbidden('edit');
|
|
677
|
+
const source = (await resolveSources(r, ctx.app.id)).find((s) => s.id === params.get(`${prefix}src`));
|
|
562
678
|
if (!source) throw new Forbidden('source');
|
|
563
679
|
const cols = await availableColumns(ctx.client!, source);
|
|
564
680
|
const def = checkDef(defFromParams(params, prefix), source.id, cols);
|
|
@@ -579,10 +695,61 @@ export async function dataReporterRoutes(app: FastifyInstance) {
|
|
|
579
695
|
);
|
|
580
696
|
|
|
581
697
|
app.post('/a/:alias/:page/reporter/:id/delete', async (req: Req, reply) =>
|
|
582
|
-
action(req, reply, async (ctx) => {
|
|
698
|
+
action(req, reply, async (ctx, r) => {
|
|
583
699
|
const id = /^\d{1,9}$/.test(String(req.body?.rep ?? '')) ? Number(req.body!.rep) : 0;
|
|
584
|
-
const
|
|
700
|
+
const rep = await reportOf(ctx, r, id);
|
|
701
|
+
let done = false;
|
|
702
|
+
if (rep?.own) {
|
|
703
|
+
// (sprint 110) users who may not edit don't delete either
|
|
704
|
+
if (!(await mayEdit(ctx, r))) throw new Forbidden('edit');
|
|
705
|
+
done = (await ctx.client!.query<{ d: boolean }>('select meta.delete_data_report($1) as d', [id])).rows[0].d;
|
|
706
|
+
} else if (rep) {
|
|
707
|
+
// (sprint 110) an administrator of the region deletes anyone's (shared) report
|
|
708
|
+
if (!(await mayModerate(ctx, r))) throw new Forbidden('moderate');
|
|
709
|
+
done = !!(await owner.query('delete from meta.data_report where id = $1 and app_id = $2 and region_id = $3', [id, ctx.app.id, r.id])).rowCount;
|
|
710
|
+
}
|
|
585
711
|
return { message: ctx.locale.t(done ? 'reporter.deleted' : 'reporter.not_found'), back: new URLSearchParams() };
|
|
586
712
|
}),
|
|
587
713
|
);
|
|
714
|
+
|
|
715
|
+
// (sprint 110) publish or unpublish a saved report: the owner (who may edit and share), or an administrator of the region (unpublish)
|
|
716
|
+
app.post('/a/:alias/:page/reporter/:id/publish', async (req: Req, reply) =>
|
|
717
|
+
action(req, reply, async (ctx, r) => {
|
|
718
|
+
const t = ctx.locale.t;
|
|
719
|
+
const id = /^\d{1,9}$/.test(String(req.body?.rep ?? '')) ? Number(req.body!.rep) : 0;
|
|
720
|
+
const on = req.body?.mode === 'publish';
|
|
721
|
+
const rep = await reportOf(ctx, r, id);
|
|
722
|
+
if (!rep) return { message: t('reporter.not_found'), back: new URLSearchParams() };
|
|
723
|
+
const allowed = rep.own ? (await mayEdit(ctx, r)) && (!on || (await mayShare(ctx, r))) : !on && (await mayModerate(ctx, r));
|
|
724
|
+
if (!allowed) throw new Forbidden('publish');
|
|
725
|
+
await owner.query('update meta.data_report set shared = $4, updated_at = now() where id = $1 and app_id = $2 and region_id = $3', [id, ctx.app.id, r.id, on]);
|
|
726
|
+
return { message: t(on ? 'reporter.published' : 'reporter.unpublished', { name: rep.name }), back: new URLSearchParams([[`${prefixOf(r)}open`, String(id)]]) };
|
|
727
|
+
}),
|
|
728
|
+
);
|
|
729
|
+
|
|
730
|
+
// (sprint 110) pin a shared report to the top of the region's list, or unpin it (a preference of the user)
|
|
731
|
+
app.post('/a/:alias/:page/reporter/:id/pin', async (req: Req, reply) =>
|
|
732
|
+
action(req, reply, async (ctx, r) => {
|
|
733
|
+
const id = /^\d{1,9}$/.test(String(req.body?.rep ?? '')) ? Number(req.body!.rep) : 0;
|
|
734
|
+
const rep = await reportOf(ctx, r, id);
|
|
735
|
+
if (!rep?.shared || !publishing(r)) return { message: ctx.locale.t('reporter.not_found'), back: new URLSearchParams() };
|
|
736
|
+
const pins = (await pinsOf(ctx, r)).filter((x) => x !== id);
|
|
737
|
+
if (req.body?.mode === 'pin') pins.unshift(id);
|
|
738
|
+
await ctx.client!.query('select meta.set_preference($1, $2)', [pinsName(r), pins.slice(0, MAX_PINS).join(',')]);
|
|
739
|
+
return { message: ctx.locale.t(req.body?.mode === 'pin' ? 'reporter.pinned_msg' : 'reporter.unpinned_msg', { name: rep.name }), back: new URLSearchParams([[`${prefixOf(r)}open`, String(id)]]) };
|
|
740
|
+
}),
|
|
741
|
+
);
|
|
742
|
+
|
|
743
|
+
// (sprint 110) a report's stable URL: its page with the report open (the page checks who may see it)
|
|
744
|
+
app.get('/a/:alias/reports/:id', async (req: Req, reply) => {
|
|
745
|
+
const id = /^\d{1,9}$/.test(String(req.params.id ?? '')) ? Number(req.params.id) : 0;
|
|
746
|
+
const hit = id
|
|
747
|
+
? await owner.one<{ page_no: number; region_id: number; alias: string }>(
|
|
748
|
+
`select p.page_no, d.region_id, a.alias from meta.data_report d join meta.app a on a.id = d.app_id
|
|
749
|
+
join meta.region g on g.id = d.region_id join meta.page p on p.id = g.page_id
|
|
750
|
+
where d.id = $1 and a.alias = $2 and d.shared`, [id, req.params.alias])
|
|
751
|
+
: undefined;
|
|
752
|
+
if (!hit) return simplePage(reply, 404, english('error.not_found'), english('reporter.not_found'));
|
|
753
|
+
return reply.redirect(`/a/${encodeURIComponent(hit.alias)}/${hit.page_no}?dr${hit.region_id}_open=${id}`, 303);
|
|
754
|
+
});
|
|
588
755
|
}
|