scdashboard 0.0.0-stage → 1.0.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/sc.icon.png ADDED
Binary file
package/server.js ADDED
@@ -0,0 +1,130 @@
1
+ import express from 'express';
2
+ import path from 'node:path';
3
+ import fs from 'node:fs';
4
+ import { initConfig, loadConfig, saveConfig, maskKey, DATA_DIR } from './lib/config.js';
5
+ import { testConnection } from './lib/runlayer.js';
6
+ import { slackDm } from './lib/slack.js';
7
+ import { dateWindow, loadSnapshots, refreshSc, searchUsers, lookupUserIds, queries } from './lib/snapshot.js';
8
+
9
+ initConfig();
10
+
11
+ const app = express();
12
+ const PORT = process.env.PORT || 4000;
13
+ const ROSTER_FILE = path.join(DATA_DIR, 'roster.json');
14
+
15
+ app.use(express.json());
16
+
17
+ // App icon: served straight from sc.icon.png in the project root (also answers /favicon.ico).
18
+ const ICON_FILE = path.join(import.meta.dirname, 'sc.icon.png');
19
+ app.get(['/icon.png', '/favicon.ico'], (req, res) => res.type('png').sendFile(ICON_FILE));
20
+ app.use(express.static(path.join(import.meta.dirname, 'public')));
21
+
22
+ const wrap = (fn) => (req, res) => fn(req, res).catch((e) => res.status(500).json({ error: e.message }));
23
+
24
+ // Persisted list of SC names shown in the picker (starts empty). Only names are
25
+ // stored; Salesforce user Ids are looked up on load and whenever the list changes.
26
+ function rosterNames() {
27
+ try {
28
+ return JSON.parse(fs.readFileSync(ROSTER_FILE, 'utf8')).map((r) => (typeof r === 'string' ? r : r.name));
29
+ } catch {
30
+ return [];
31
+ }
32
+ }
33
+
34
+ function saveRosterNames(names) {
35
+ fs.mkdirSync(DATA_DIR, { recursive: true, mode: 0o700 });
36
+ fs.writeFileSync(ROSTER_FILE, JSON.stringify(names, null, 2), { mode: 0o600 });
37
+ fs.chmodSync(ROSTER_FILE, 0o600);
38
+ idCache = null;
39
+ }
40
+
41
+ let idCache = null; // Map name -> { id, email } for the current list
42
+
43
+ async function roster() {
44
+ const names = rosterNames();
45
+ let error = null;
46
+ if (!idCache) {
47
+ try {
48
+ idCache = await lookupUserIds(names);
49
+ } catch (e) {
50
+ error = e.message; // e.g. no API key yet; retry on the next request
51
+ }
52
+ }
53
+ return { error, users: names.map((name) => ({ name, id: idCache?.get(name)?.id || null, email: idCache?.get(name)?.email || null })) };
54
+ }
55
+
56
+ // Last code change: newest modified time across the app's source files (read per request,
57
+ // so client-only edits show up without a restart).
58
+ const CODE_DIRS = ['public', 'lib'].map((d) => path.join(import.meta.dirname, d));
59
+ function codeUpdatedAt() {
60
+ const files = [path.join(import.meta.dirname, 'server.js')];
61
+ for (const dir of CODE_DIRS) {
62
+ for (const f of fs.readdirSync(dir)) if (/\.(js|html|css)$/.test(f) && !f.startsWith('__')) files.push(path.join(dir, f));
63
+ }
64
+ return new Date(Math.max(...files.map((f) => fs.statSync(f).mtimeMs))).toISOString();
65
+ }
66
+
67
+ app.get('/api/version', (req, res) => res.json({ codeUpdatedAt: codeUpdatedAt() }));
68
+
69
+ app.get('/api/settings', (req, res) => {
70
+ const c = loadConfig();
71
+ res.json({ runlayerHost: c.runlayerHost, salesforceServerId: c.salesforceServerId, apiKeySet: !!c.apiKey, apiKeyMasked: maskKey(c.apiKey) });
72
+ });
73
+
74
+ app.post('/api/settings', (req, res) => {
75
+ const { apiKey, runlayerHost, salesforceServerId } = req.body || {};
76
+ const patch = {};
77
+ if (typeof apiKey === 'string' && apiKey.trim()) patch.apiKey = apiKey.trim();
78
+ if (typeof runlayerHost === 'string' && /^https:\/\//.test(runlayerHost)) patch.runlayerHost = runlayerHost.trim();
79
+ if (typeof salesforceServerId === 'string' && /^[0-9a-f-]{36}$/i.test(salesforceServerId)) patch.salesforceServerId = salesforceServerId;
80
+ const c = saveConfig(patch);
81
+ res.json({ ok: true, apiKeySet: !!c.apiKey, apiKeyMasked: maskKey(c.apiKey) });
82
+ });
83
+
84
+ app.delete('/api/settings/key', (req, res) => {
85
+ saveConfig({ apiKey: '' });
86
+ res.json({ ok: true });
87
+ });
88
+
89
+ app.post('/api/settings/test', wrap(async (req, res) => res.json(await testConnection())));
90
+
91
+ app.get('/api/roster', wrap(async (req, res) => res.json(await roster())));
92
+
93
+ app.get('/api/users/search', wrap(async (req, res) => {
94
+ const q = String(req.query.q || '').trim();
95
+ if (q.length < 3) return res.json([]);
96
+ const users = await searchUsers(q);
97
+ res.json(users.map((u) => ({ id: u.Id, name: u.Name })));
98
+ }));
99
+
100
+ app.post('/api/roster', wrap(async (req, res) => {
101
+ const name = String(req.body?.name || '').trim();
102
+ if (!name) return res.status(400).json({ error: 'name required' });
103
+ const names = rosterNames();
104
+ if (!names.includes(name)) saveRosterNames([...names, name].sort((a, b) => a.localeCompare(b)));
105
+ res.json(await roster());
106
+ }));
107
+
108
+ app.delete('/api/roster/:name', wrap(async (req, res) => {
109
+ saveRosterNames(rosterNames().filter((n) => n !== req.params.name));
110
+ res.json(await roster());
111
+ }));
112
+
113
+ // Draft (default) or send a Slack DM to an SC with the review text.
114
+ app.post('/api/slack/dm', wrap(async (req, res) => {
115
+ const { email, name, message, send } = req.body || {};
116
+ res.json(await slackDm({ email, name, message, send: send === true }));
117
+ }));
118
+
119
+ app.get('/api/snapshot/:scId', (req, res) => {
120
+ const window = dateWindow();
121
+ let soql = '';
122
+ try {
123
+ soql = Object.entries(queries(req.params.scId, window)).map(([k, q]) => `-- ${k}\n${q.replace(/\s+/g, ' ')}`).join('\n\n');
124
+ } catch {}
125
+ res.json({ snapshot: loadSnapshots()[req.params.scId] || null, window, soql });
126
+ });
127
+
128
+ app.post('/api/refresh/:scId', wrap(async (req, res) => res.json(await refreshSc(req.params.scId))));
129
+
130
+ app.listen(PORT, '127.0.0.1', () => console.log(`SC Record Hygiene running at http://localhost:${PORT}`));