@quad-education/sdk 0.1.4 → 0.3.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/README.md +3 -1
- package/dist/contract-tests/apps.d.ts +1 -1
- package/dist/contract-tests/apps.js +1 -1
- package/dist/contract-tests/harness.d.ts +11 -0
- package/dist/contract-tests/harness.js +40 -0
- package/dist/contract-tests/helpers.d.ts +5 -0
- package/dist/contract-tests/helpers.js +25 -0
- package/dist/contract-tests/index.d.ts +1 -0
- package/dist/contract-tests/index.js +1 -0
- package/dist/contract-tests/suite.js +3 -24
- package/dist/src/contract/types.d.ts +1 -1
- package/dist/src/contract/types.js +1 -1
- package/dist/src/engine/audit.d.ts +2 -2
- package/dist/src/engine/audit.js +8 -6
- package/dist/src/engine/db-indexes.js +6 -1
- package/dist/src/engine/engine.d.ts +14 -12
- package/dist/src/engine/engine.js +100 -135
- package/dist/src/engine/host.js +1 -1
- package/dist/src/engine/index.d.ts +3 -1
- package/dist/src/engine/index.js +1 -0
- package/dist/src/engine/memory-store.d.ts +32 -0
- package/dist/src/engine/memory-store.js +123 -0
- package/dist/src/engine/runner.d.ts +2 -2
- package/dist/src/engine/runner.js +27 -24
- package/dist/src/engine/state.d.ts +4 -9
- package/dist/src/engine/store.d.ts +48 -0
- package/dist/src/engine/store.js +1 -0
- package/dist/src/engine/tasks.d.ts +4 -6
- package/dist/src/engine/tasks.js +17 -21
- package/package.json +4 -4
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { EngineStorage, } from "./storage.js";
|
|
2
|
+
const emptyState = () => ({
|
|
3
|
+
records: new Map(),
|
|
4
|
+
keys: new Map(),
|
|
5
|
+
platform: {
|
|
6
|
+
task: new Map(),
|
|
7
|
+
schedule: new Map(),
|
|
8
|
+
run: new Map(),
|
|
9
|
+
cron: new Map(),
|
|
10
|
+
audit: new Map(),
|
|
11
|
+
},
|
|
12
|
+
auditSeq: 0,
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* The in-browser store (D15): an app's state in memory, written through to IndexedDB. Reads hand out the stored
|
|
16
|
+
* objects themselves, and the engine changes them and puts them back, which is how it has always worked.
|
|
17
|
+
*/
|
|
18
|
+
export class MemoryStore {
|
|
19
|
+
storage;
|
|
20
|
+
apps = new Map();
|
|
21
|
+
constructor(storage) {
|
|
22
|
+
this.storage = storage;
|
|
23
|
+
}
|
|
24
|
+
static async open(indexedDB, dbName) {
|
|
25
|
+
return new MemoryStore(await EngineStorage.open(indexedDB, dbName));
|
|
26
|
+
}
|
|
27
|
+
state(appId) {
|
|
28
|
+
let s = this.apps.get(appId);
|
|
29
|
+
if (!s)
|
|
30
|
+
this.apps.set(appId, (s = emptyState()));
|
|
31
|
+
return s;
|
|
32
|
+
}
|
|
33
|
+
async open(appId, schemaHash) {
|
|
34
|
+
const meta = await this.storage.getMeta(appId);
|
|
35
|
+
if (meta && meta.schemaHash !== schemaHash)
|
|
36
|
+
await this.storage.clearApp(appId, { schemaChangeOnly: true });
|
|
37
|
+
await this.storage.put('meta', [{ appId, schemaHash }]);
|
|
38
|
+
const state = emptyState();
|
|
39
|
+
for (const r of await this.storage.getAll('records', appId)) {
|
|
40
|
+
let rows = state.records.get(r.entity);
|
|
41
|
+
if (!rows)
|
|
42
|
+
state.records.set(r.entity, (rows = new Map()));
|
|
43
|
+
rows.set(r.id, r.data);
|
|
44
|
+
}
|
|
45
|
+
for (const k of await this.storage.getAll('keys', appId))
|
|
46
|
+
state.keys.set(k.key, k.result);
|
|
47
|
+
for (const row of await this.storage.getAll('platform', appId)) {
|
|
48
|
+
let data = row.data;
|
|
49
|
+
// Rows stored before run-as rules (D7) have no runAs; those were system-scheduled.
|
|
50
|
+
if (row.kind === 'schedule' || row.kind === 'run')
|
|
51
|
+
data = { ...data, runAs: data.runAs ?? { kind: 'system', appId } };
|
|
52
|
+
state.platform[row.kind].set(row.id, data);
|
|
53
|
+
if (row.kind === 'audit')
|
|
54
|
+
state.auditSeq = Math.max(state.auditSeq, data.seq ?? 0);
|
|
55
|
+
}
|
|
56
|
+
this.apps.set(appId, state);
|
|
57
|
+
}
|
|
58
|
+
async clear(appId) {
|
|
59
|
+
await this.storage.clearApp(appId);
|
|
60
|
+
this.apps.set(appId, emptyState());
|
|
61
|
+
}
|
|
62
|
+
async getRecord(appId, entity, id) {
|
|
63
|
+
return this.state(appId).records.get(entity)?.get(id);
|
|
64
|
+
}
|
|
65
|
+
async putRecord(appId, entity, record) {
|
|
66
|
+
const s = this.state(appId);
|
|
67
|
+
let rows = s.records.get(entity);
|
|
68
|
+
if (!rows)
|
|
69
|
+
s.records.set(entity, (rows = new Map()));
|
|
70
|
+
rows.set(record.id, record);
|
|
71
|
+
await this.storage.put('records', [{ appId, entity, id: record.id, data: record }]);
|
|
72
|
+
}
|
|
73
|
+
async deleteRecord(appId, entity, record) {
|
|
74
|
+
this.state(appId)
|
|
75
|
+
.records.get(entity)
|
|
76
|
+
?.delete(record.id);
|
|
77
|
+
await this.storage.deleteRecord(appId, entity, record.id);
|
|
78
|
+
}
|
|
79
|
+
async candidates(appId, entity, _hint) {
|
|
80
|
+
return [...(this.state(appId).records.get(entity)?.values() ?? [])];
|
|
81
|
+
}
|
|
82
|
+
async recordStamps(appId) {
|
|
83
|
+
const out = {};
|
|
84
|
+
for (const [entity, rows] of this.state(appId).records) {
|
|
85
|
+
out[entity] = Object.fromEntries([...rows.values()].map((r) => [r.id, r.updatedAt]));
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
async getKey(appId, key) {
|
|
90
|
+
return this.state(appId).keys.get(key);
|
|
91
|
+
}
|
|
92
|
+
async putKey(appId, key, result) {
|
|
93
|
+
this.state(appId).keys.set(key, result);
|
|
94
|
+
await this.storage.put('keys', [{ appId, key, result }]);
|
|
95
|
+
}
|
|
96
|
+
async getPlatform(appId, kind, id) {
|
|
97
|
+
return this.state(appId).platform[kind].get(id);
|
|
98
|
+
}
|
|
99
|
+
async allPlatform(appId, kind) {
|
|
100
|
+
return [...this.state(appId).platform[kind].values()];
|
|
101
|
+
}
|
|
102
|
+
async putPlatform(appId, kind, items) {
|
|
103
|
+
if (items.length === 0)
|
|
104
|
+
return;
|
|
105
|
+
const map = this.state(appId).platform[kind];
|
|
106
|
+
for (const item of items)
|
|
107
|
+
map.set(item.id, item);
|
|
108
|
+
await this.storage.putPlatform(appId, kind, items);
|
|
109
|
+
}
|
|
110
|
+
async nextAuditSeq(appId) {
|
|
111
|
+
return ++this.state(appId).auditSeq;
|
|
112
|
+
}
|
|
113
|
+
async putOutbox(_appId, entries) {
|
|
114
|
+
if (entries.length > 0)
|
|
115
|
+
await this.storage.put('outbox', entries);
|
|
116
|
+
}
|
|
117
|
+
async outbox(appId) {
|
|
118
|
+
return this.storage.getAll('outbox', appId);
|
|
119
|
+
}
|
|
120
|
+
close() {
|
|
121
|
+
this.storage.close();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -4,8 +4,8 @@ import { type App, type Ctx, type Kernel } from './state.ts';
|
|
|
4
4
|
export type { RunRequest, RunResult } from '../jobs/define.ts';
|
|
5
5
|
export declare function scheduleAt(k: Kernel, ctx: Ctx, input: Record<string, unknown>): Promise<ScheduledRun>;
|
|
6
6
|
export declare function scheduleCancel(k: Kernel, ctx: Ctx, id: unknown): Promise<ScheduledRun>;
|
|
7
|
-
export declare function scheduleList(ctx: Ctx, options: unknown): ScheduledRun[]
|
|
8
|
-
export declare function cronList(k: Kernel, ctx: Ctx): CronJobInfo[]
|
|
7
|
+
export declare function scheduleList(k: Kernel, ctx: Ctx, options: unknown): Promise<ScheduledRun[]>;
|
|
8
|
+
export declare function cronList(k: Kernel, ctx: Ctx): Promise<CronJobInfo[]>;
|
|
9
9
|
/**
|
|
10
10
|
* Start cron baselines at install, so a new job doesn't backfill from the distant past.
|
|
11
11
|
* If the demo clock is now behind a baseline (it was moved back, D14), the baseline moves
|
|
@@ -21,8 +21,9 @@ export async function scheduleAt(k, ctx, input) {
|
|
|
21
21
|
throw new QuadError('UNDECLARED_TYPE', `job '${job}' isn't in the manifest's scheduled list`);
|
|
22
22
|
}
|
|
23
23
|
const key = k.idempotencyKey(input, 'schedule.at');
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const remembered = key ? await k.store.getKey(ctx.app.manifest.id, key) : undefined;
|
|
25
|
+
if (typeof remembered === 'string') {
|
|
26
|
+
const existing = await k.store.getPlatform(ctx.app.manifest.id, 'schedule', remembered);
|
|
26
27
|
if (existing)
|
|
27
28
|
return scheduleView(existing);
|
|
28
29
|
}
|
|
@@ -37,14 +38,13 @@ export async function scheduleAt(k, ctx, input) {
|
|
|
37
38
|
createdBy: ctx.actorId,
|
|
38
39
|
runAs: ctx.principal,
|
|
39
40
|
};
|
|
40
|
-
ctx.app.schedules.set(s.id, s);
|
|
41
41
|
await k.persistPlatform(ctx.app, 'schedule', [s]);
|
|
42
42
|
if (key)
|
|
43
43
|
await k.remember(ctx, key, s.id);
|
|
44
44
|
return scheduleView(s);
|
|
45
45
|
}
|
|
46
46
|
export async function scheduleCancel(k, ctx, id) {
|
|
47
|
-
const s = ctx.app.
|
|
47
|
+
const s = await k.store.getPlatform(ctx.app.manifest.id, 'schedule', str(id, 'id'));
|
|
48
48
|
if (!s || !ownsSchedule(ctx, s))
|
|
49
49
|
throw new QuadError('NOT_FOUND', `no scheduled job ${String(id)}`);
|
|
50
50
|
if (s.status !== 'pending')
|
|
@@ -53,18 +53,20 @@ export async function scheduleCancel(k, ctx, id) {
|
|
|
53
53
|
await k.persistPlatform(ctx.app, 'schedule', [s]);
|
|
54
54
|
return scheduleView(s);
|
|
55
55
|
}
|
|
56
|
-
export function scheduleList(ctx, options) {
|
|
56
|
+
export async function scheduleList(k, ctx, options) {
|
|
57
57
|
const status = isObject(options) ? options.status : undefined;
|
|
58
58
|
if (status !== undefined && !['pending', 'done', 'cancelled'].includes(status)) {
|
|
59
59
|
throw new QuadError('VALIDATION', "status must be 'pending', 'done' or 'cancelled'");
|
|
60
60
|
}
|
|
61
|
-
|
|
61
|
+
const all = await k.store.allPlatform(ctx.app.manifest.id, 'schedule');
|
|
62
|
+
return all
|
|
62
63
|
.filter((s) => ownsSchedule(ctx, s) && (status === undefined || s.status === status))
|
|
63
64
|
.sort((a, b) => a.when.localeCompare(b.when))
|
|
64
65
|
.map(scheduleView);
|
|
65
66
|
}
|
|
66
|
-
export function cronList(k, ctx) {
|
|
67
|
+
export async function cronList(k, ctx) {
|
|
67
68
|
const tz = k.timezone();
|
|
69
|
+
const states = await k.store.allPlatform(ctx.app.manifest.id, 'cron');
|
|
68
70
|
return ctx.app.manifest.cron.map((job) => {
|
|
69
71
|
const next = nextOccurrence(job.schedule, tz, k.nowMs());
|
|
70
72
|
return {
|
|
@@ -72,7 +74,7 @@ export function cronList(k, ctx) {
|
|
|
72
74
|
schedule: job.schedule,
|
|
73
75
|
description: job.description,
|
|
74
76
|
nextRunAt: next === null ? null : new Date(next).toISOString(),
|
|
75
|
-
lastScheduledFor:
|
|
77
|
+
lastScheduledFor: states.find((s) => s.id === job.name)?.lastScheduledFor ?? null,
|
|
76
78
|
};
|
|
77
79
|
});
|
|
78
80
|
}
|
|
@@ -85,12 +87,10 @@ export async function initCron(k, app) {
|
|
|
85
87
|
const changed = [];
|
|
86
88
|
const now = k.nowMs();
|
|
87
89
|
for (const job of app.manifest.cron) {
|
|
88
|
-
const state = app.cron
|
|
90
|
+
const state = await k.store.getPlatform(app.manifest.id, 'cron', job.name);
|
|
89
91
|
if (state && Date.parse(state.lastScheduledFor) <= now)
|
|
90
92
|
continue;
|
|
91
|
-
|
|
92
|
-
app.cron.set(job.name, fresh);
|
|
93
|
-
changed.push(fresh);
|
|
93
|
+
changed.push({ id: job.name, lastScheduledFor: new Date(now).toISOString() });
|
|
94
94
|
}
|
|
95
95
|
await k.persistPlatform(app, 'cron', changed);
|
|
96
96
|
}
|
|
@@ -104,7 +104,10 @@ export async function startDueRuns(k, app) {
|
|
|
104
104
|
const changedRuns = new Map();
|
|
105
105
|
const changedSchedules = [];
|
|
106
106
|
const changedCron = [];
|
|
107
|
-
|
|
107
|
+
const id = app.manifest.id;
|
|
108
|
+
const schedules = await k.store.allPlatform(id, 'schedule');
|
|
109
|
+
const runs = new Map((await k.store.allPlatform(id, 'run')).map((r) => [r.id, r]));
|
|
110
|
+
for (const s of schedules) {
|
|
108
111
|
if (s.status !== 'pending' || Date.parse(s.when) > now)
|
|
109
112
|
continue;
|
|
110
113
|
const run = {
|
|
@@ -120,15 +123,15 @@ export async function startDueRuns(k, app) {
|
|
|
120
123
|
attempts: 0,
|
|
121
124
|
deliveries: 0,
|
|
122
125
|
};
|
|
123
|
-
if (!
|
|
124
|
-
|
|
126
|
+
if (!runs.has(run.id)) {
|
|
127
|
+
runs.set(run.id, run);
|
|
125
128
|
changedRuns.set(run.id, run);
|
|
126
129
|
}
|
|
127
130
|
s.status = 'done';
|
|
128
131
|
changedSchedules.push(s);
|
|
129
132
|
}
|
|
130
133
|
for (const job of app.manifest.cron) {
|
|
131
|
-
const state =
|
|
134
|
+
const state = await k.store.getPlatform(id, 'cron', job.name);
|
|
132
135
|
if (!state)
|
|
133
136
|
continue;
|
|
134
137
|
const latest = latestOccurrence(job.schedule, k.timezone(), Date.parse(state.lastScheduledFor), now);
|
|
@@ -147,15 +150,15 @@ export async function startDueRuns(k, app) {
|
|
|
147
150
|
attempts: 0,
|
|
148
151
|
deliveries: 0,
|
|
149
152
|
};
|
|
150
|
-
if (!
|
|
151
|
-
|
|
153
|
+
if (!runs.has(run.id)) {
|
|
154
|
+
runs.set(run.id, run);
|
|
152
155
|
changedRuns.set(run.id, run);
|
|
153
156
|
}
|
|
154
157
|
state.lastScheduledFor = scheduledFor;
|
|
155
158
|
changedCron.push(state);
|
|
156
159
|
}
|
|
157
160
|
const wall = Date.now();
|
|
158
|
-
for (const run of
|
|
161
|
+
for (const run of runs.values()) {
|
|
159
162
|
if (run.status === 'running' &&
|
|
160
163
|
run.startedAtMs !== undefined &&
|
|
161
164
|
wall - run.startedAtMs > RUN_TIMEOUT_MS) {
|
|
@@ -163,9 +166,9 @@ export async function startDueRuns(k, app) {
|
|
|
163
166
|
changedRuns.set(run.id, run);
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
|
-
const busy = new Set([...
|
|
169
|
+
const busy = new Set([...runs.values()].filter((r) => r.status === 'running').map((r) => r.jobName));
|
|
167
170
|
const started = [];
|
|
168
|
-
const pending = [...
|
|
171
|
+
const pending = [...runs.values()]
|
|
169
172
|
.filter((r) => r.status === 'pending')
|
|
170
173
|
.sort((a, b) => a.scheduledFor.localeCompare(b.scheduledFor) || a.id.localeCompare(b.id));
|
|
171
174
|
for (const run of pending) {
|
|
@@ -194,7 +197,7 @@ export async function startDueRuns(k, app) {
|
|
|
194
197
|
}
|
|
195
198
|
/** Records a run's result. Failures are retried up to three attempts, then marked failed. */
|
|
196
199
|
export async function finishRun(k, app, runId, result) {
|
|
197
|
-
const run = app.
|
|
200
|
+
const run = await k.store.getPlatform(app.manifest.id, 'run', runId);
|
|
198
201
|
if (!run || run.status !== 'running')
|
|
199
202
|
return; // a late or duplicate report
|
|
200
203
|
run.finishedAt = k.nowIso();
|
|
@@ -228,7 +231,7 @@ export async function finishRun(k, app, runId, result) {
|
|
|
228
231
|
}
|
|
229
232
|
/** A run handed out but never dispatched (e.g. no jobs host yet) goes back to pending. */
|
|
230
233
|
export async function releaseRun(k, app, runId) {
|
|
231
|
-
const run = app.
|
|
234
|
+
const run = await k.store.getPlatform(app.manifest.id, 'run', runId);
|
|
232
235
|
if (!run || run.status !== 'running')
|
|
233
236
|
return;
|
|
234
237
|
run.status = 'pending';
|
|
@@ -239,7 +242,7 @@ export async function releaseRun(k, app, runId) {
|
|
|
239
242
|
}
|
|
240
243
|
/** Delivers a finished run again with the same runId (D20: handlers must be idempotent). */
|
|
241
244
|
export async function redeliverRun(k, app, runId) {
|
|
242
|
-
const run = app.
|
|
245
|
+
const run = await k.store.getPlatform(app.manifest.id, 'run', runId);
|
|
243
246
|
if (!run)
|
|
244
247
|
throw new QuadError('NOT_FOUND', `no run ${runId}`);
|
|
245
248
|
if (run.status === 'running' || run.status === 'pending')
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Dataset, type Principal, type Role } from '@quad-education/canonical';
|
|
2
2
|
import type { AppSchema, Manifest } from '@quad-education/schema';
|
|
3
3
|
import type { AuditEntry, JsonValue, ScheduledRun, Task } from '../contract/types.ts';
|
|
4
|
-
import type { PlatformKind } from './
|
|
4
|
+
import type { AppStore, PlatformKind } from './store.ts';
|
|
5
5
|
export type StoredTask = Omit<Task, 'assignedToMe' | 'assignees'> & {
|
|
6
6
|
readonly appId: string;
|
|
7
7
|
assignees: string[];
|
|
@@ -51,16 +51,10 @@ export type StoredAudit = AuditEntry & {
|
|
|
51
51
|
readonly seq: number;
|
|
52
52
|
readonly visibility: 'app' | 'cio';
|
|
53
53
|
};
|
|
54
|
+
/** An installed app: what it is. Its state lives in the engine's `AppStore`. */
|
|
54
55
|
export type App = {
|
|
55
56
|
manifest: Manifest;
|
|
56
57
|
schema: AppSchema;
|
|
57
|
-
records: Map<string, Map<string, Record<string, unknown>>>;
|
|
58
|
-
keys: Map<string, unknown>;
|
|
59
|
-
tasks: Map<string, StoredTask>;
|
|
60
|
-
schedules: Map<string, StoredSchedule>;
|
|
61
|
-
runs: Map<string, StoredRun>;
|
|
62
|
-
cron: Map<string, CronState>;
|
|
63
|
-
audit: StoredAudit[];
|
|
64
58
|
};
|
|
65
59
|
export type Ctx = {
|
|
66
60
|
app: App;
|
|
@@ -89,6 +83,7 @@ export type ResolvedRecipients = {
|
|
|
89
83
|
/** The engine internals the Slice B modules use. `Engine` implements it. */
|
|
90
84
|
export interface Kernel {
|
|
91
85
|
readonly dataset: Dataset;
|
|
86
|
+
readonly store: AppStore;
|
|
92
87
|
nowIso(): string;
|
|
93
88
|
nowMs(): number;
|
|
94
89
|
timezone(): string;
|
|
@@ -97,7 +92,7 @@ export interface Kernel {
|
|
|
97
92
|
}[]): Promise<void>;
|
|
98
93
|
remember(ctx: Ctx, key: string, result: unknown): Promise<void>;
|
|
99
94
|
idempotencyKey(options: unknown, scope: string): string | undefined;
|
|
100
|
-
resolveRecipients(ctx: Ctx, to: unknown, regarding: string | undefined): ResolvedRecipients
|
|
95
|
+
resolveRecipients(ctx: Ctx, to: unknown, regarding: string | undefined): Promise<ResolvedRecipients>;
|
|
101
96
|
}
|
|
102
97
|
/**
|
|
103
98
|
* Whether the current principal can see a subject student, under the school's visibility
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { AppSchema } from '@quad-education/schema';
|
|
2
|
+
import type { OutboxEntry } from '../platform-types.ts';
|
|
3
|
+
import type { PlatformKind } from './storage.ts';
|
|
4
|
+
export type { PlatformKind };
|
|
5
|
+
/** A stored record: the app's fields plus the system fields (id, createdAt, createdBy, updatedAt, updatedBy). */
|
|
6
|
+
export type StoredRecordData = Record<string, unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* What a read needs from a store. A store may return a superset of the matching records and never fewer: the
|
|
9
|
+
* engine applies the access rules, the filters, the order and the page itself. The hints only let a store that
|
|
10
|
+
* has indexes read less.
|
|
11
|
+
*/
|
|
12
|
+
export type ReadHint = {
|
|
13
|
+
/** Every record the caller may read was created by this actor (the only rule is `own`). */
|
|
14
|
+
readonly ownedBy?: string;
|
|
15
|
+
readonly where?: unknown;
|
|
16
|
+
readonly orderBy?: unknown;
|
|
17
|
+
/** The entity's declared indexes, each a list of field names. */
|
|
18
|
+
readonly indexes: readonly (readonly string[])[];
|
|
19
|
+
};
|
|
20
|
+
export interface AppStore {
|
|
21
|
+
/**
|
|
22
|
+
* Get the app's state ready. When the stored schema hash differs from `schemaHash`, records and idempotency keys
|
|
23
|
+
* are cleared first (D12), and the new hash is recorded. A store with indexes needs the schema to lay them out.
|
|
24
|
+
*/
|
|
25
|
+
open(appId: string, schemaHash: string, schema: AppSchema): Promise<void>;
|
|
26
|
+
/** Remove everything the app has stored: records, keys, outbox and platform state. */
|
|
27
|
+
clear(appId: string): Promise<void>;
|
|
28
|
+
getRecord(appId: string, entity: string, id: string): Promise<StoredRecordData | undefined>;
|
|
29
|
+
/** Write a record. `previous` is the version being replaced, so indexes can be kept in step. */
|
|
30
|
+
putRecord(appId: string, entity: string, record: StoredRecordData, previous?: StoredRecordData): Promise<void>;
|
|
31
|
+
deleteRecord(appId: string, entity: string, record: StoredRecordData): Promise<void>;
|
|
32
|
+
/** Records of one entity that may match a read (see `ReadHint`). */
|
|
33
|
+
candidates(appId: string, entity: string, hint: ReadHint): Promise<StoredRecordData[]>;
|
|
34
|
+
/** Every record id and update time, by entity (for the run-twice effects snapshot). */
|
|
35
|
+
recordStamps(appId: string): Promise<Record<string, Record<string, string>>>;
|
|
36
|
+
getKey(appId: string, key: string): Promise<unknown>;
|
|
37
|
+
putKey(appId: string, key: string, result: unknown): Promise<void>;
|
|
38
|
+
getPlatform<T>(appId: string, kind: PlatformKind, id: string): Promise<T | undefined>;
|
|
39
|
+
allPlatform<T>(appId: string, kind: PlatformKind): Promise<T[]>;
|
|
40
|
+
putPlatform(appId: string, kind: PlatformKind, items: readonly {
|
|
41
|
+
id: string;
|
|
42
|
+
}[]): Promise<void>;
|
|
43
|
+
/** The next audit sequence number for the app: 1, then 2 and so on, never repeated. */
|
|
44
|
+
nextAuditSeq(appId: string): Promise<number>;
|
|
45
|
+
putOutbox(appId: string, entries: readonly OutboxEntry[]): Promise<void>;
|
|
46
|
+
outbox(appId: string): Promise<OutboxEntry[]>;
|
|
47
|
+
close(): void | Promise<void>;
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -4,14 +4,12 @@ export declare function taskView(ctx: Ctx, t: StoredTask): Task;
|
|
|
4
4
|
export declare function assignTask(k: Kernel, ctx: Ctx, input: Record<string, unknown>): Promise<{
|
|
5
5
|
tasks: Task[];
|
|
6
6
|
}>;
|
|
7
|
-
export declare function getTask(k: Kernel, ctx: Ctx, id: unknown): Task
|
|
8
|
-
export declare function listTasks(k: Kernel, ctx: Ctx, options: unknown): {
|
|
7
|
+
export declare function getTask(k: Kernel, ctx: Ctx, id: unknown): Promise<Task>;
|
|
8
|
+
export declare function listTasks(k: Kernel, ctx: Ctx, options: unknown): Promise<{
|
|
9
9
|
items: Task[];
|
|
10
10
|
nextCursor: string | null;
|
|
11
|
-
}
|
|
11
|
+
}>;
|
|
12
12
|
export declare function completeTask(k: Kernel, ctx: Ctx, id: unknown, options: unknown): Promise<Task>;
|
|
13
13
|
export declare function cancelTask(k: Kernel, ctx: Ctx, id: unknown): Promise<Task>;
|
|
14
14
|
/** Open tasks assigned to a person (the shell's inbox). */
|
|
15
|
-
export declare function inbox(
|
|
16
|
-
tasks: Map<string, StoredTask>;
|
|
17
|
-
}, personId: string): StoredTask[];
|
|
15
|
+
export declare function inbox(k: Kernel, appId: string, personId: string): Promise<StoredTask[]>;
|
package/dist/src/engine/tasks.js
CHANGED
|
@@ -24,8 +24,8 @@ function canSee(k, ctx, t) {
|
|
|
24
24
|
const involved = isStaffOrSystem(ctx) || t.assignees.includes(ctx.actorId) || t.createdBy === ctx.actorId;
|
|
25
25
|
return involved && seesSubject(k, ctx, t.regardingStudentId);
|
|
26
26
|
}
|
|
27
|
-
function visibleTask(k, ctx, id) {
|
|
28
|
-
const t = ctx.app.
|
|
27
|
+
async function visibleTask(k, ctx, id) {
|
|
28
|
+
const t = await k.store.getPlatform(ctx.app.manifest.id, 'task', str(id, 'id'));
|
|
29
29
|
if (!t || !canSee(k, ctx, t))
|
|
30
30
|
throw new QuadError('NOT_FOUND', `no task ${String(id)}`);
|
|
31
31
|
return t;
|
|
@@ -37,14 +37,10 @@ export async function assignTask(k, ctx, input) {
|
|
|
37
37
|
throw new QuadError('UNDECLARED_TYPE', `task type '${type}' isn't declared in app.manifest.json`);
|
|
38
38
|
}
|
|
39
39
|
const key = k.idempotencyKey(input, 'tasks.assign');
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
.map((id) => ctx.app.tasks.get(id))
|
|
45
|
-
.filter((t) => t !== undefined)
|
|
46
|
-
.map((t) => taskView(ctx, t)),
|
|
47
|
-
};
|
|
40
|
+
const remembered = key ? await k.store.getKey(ctx.app.manifest.id, key) : undefined;
|
|
41
|
+
if (Array.isArray(remembered)) {
|
|
42
|
+
const found = await Promise.all(remembered.map((id) => k.store.getPlatform(ctx.app.manifest.id, 'task', id)));
|
|
43
|
+
return { tasks: found.filter((t) => t !== undefined).map((t) => taskView(ctx, t)) };
|
|
48
44
|
}
|
|
49
45
|
const title = str(input.title, 'title');
|
|
50
46
|
if (title.length > 200)
|
|
@@ -66,7 +62,7 @@ export async function assignTask(k, ctx, input) {
|
|
|
66
62
|
if (regarding !== undefined && !seesSubject(k, ctx, regarding)) {
|
|
67
63
|
throw new QuadError('NOT_PERMITTED', 'regarding: not a student you can see');
|
|
68
64
|
}
|
|
69
|
-
const resolved = k.resolveRecipients(ctx, input.assignee, regarding);
|
|
65
|
+
const resolved = await k.resolveRecipients(ctx, input.assignee, regarding);
|
|
70
66
|
const { suppressed } = resolved;
|
|
71
67
|
// A task about a student only goes to people who can see that student (D25); parents
|
|
72
68
|
// were already limited to eligible links (D17).
|
|
@@ -111,8 +107,6 @@ export async function assignTask(k, ctx, input) {
|
|
|
111
107
|
id: `task_${crypto.randomUUID()}`,
|
|
112
108
|
assignees,
|
|
113
109
|
}));
|
|
114
|
-
for (const t of tasks)
|
|
115
|
-
ctx.app.tasks.set(t.id, t);
|
|
116
110
|
await k.persistPlatform(ctx.app, 'task', tasks);
|
|
117
111
|
for (const t of tasks) {
|
|
118
112
|
await appendAudit(k, ctx.app, {
|
|
@@ -147,10 +141,10 @@ export async function assignTask(k, ctx, input) {
|
|
|
147
141
|
await k.remember(ctx, key, tasks.map((t) => t.id));
|
|
148
142
|
return { tasks: tasks.map((t) => taskView(ctx, t)) };
|
|
149
143
|
}
|
|
150
|
-
export function getTask(k, ctx, id) {
|
|
151
|
-
return taskView(ctx, visibleTask(k, ctx, id));
|
|
144
|
+
export async function getTask(k, ctx, id) {
|
|
145
|
+
return taskView(ctx, await visibleTask(k, ctx, id));
|
|
152
146
|
}
|
|
153
|
-
export function listTasks(k, ctx, options) {
|
|
147
|
+
export async function listTasks(k, ctx, options) {
|
|
154
148
|
const o = isObject(options) ? options : {};
|
|
155
149
|
const where = {};
|
|
156
150
|
if (o.status !== undefined) {
|
|
@@ -165,7 +159,8 @@ export function listTasks(k, ctx, options) {
|
|
|
165
159
|
where.regardingStudentId = str(o.regardingStudentId, 'regardingStudentId');
|
|
166
160
|
if (o.groupId !== undefined)
|
|
167
161
|
where.groupId = str(o.groupId, 'groupId');
|
|
168
|
-
|
|
162
|
+
const all = await k.store.allPlatform(ctx.app.manifest.id, 'task');
|
|
163
|
+
let tasks = all.filter((t) => canSee(k, ctx, t));
|
|
169
164
|
if (o.assignedToMe !== undefined) {
|
|
170
165
|
if (typeof o.assignedToMe !== 'boolean')
|
|
171
166
|
throw new QuadError('VALIDATION', 'assignedToMe must be true or false');
|
|
@@ -183,7 +178,7 @@ export function listTasks(k, ctx, options) {
|
|
|
183
178
|
};
|
|
184
179
|
}
|
|
185
180
|
export async function completeTask(k, ctx, id, options) {
|
|
186
|
-
const t = visibleTask(k, ctx, id);
|
|
181
|
+
const t = await visibleTask(k, ctx, id);
|
|
187
182
|
if (ctx.principal.kind === 'system' || !t.assignees.includes(ctx.actorId)) {
|
|
188
183
|
throw new QuadError('NOT_ASSIGNEE', 'only an assignee can complete this task, from the app');
|
|
189
184
|
}
|
|
@@ -215,7 +210,7 @@ export async function completeTask(k, ctx, id, options) {
|
|
|
215
210
|
return taskView(ctx, t);
|
|
216
211
|
}
|
|
217
212
|
export async function cancelTask(k, ctx, id) {
|
|
218
|
-
const t = visibleTask(k, ctx, id);
|
|
213
|
+
const t = await visibleTask(k, ctx, id);
|
|
219
214
|
if (!isStaffOrSystem(ctx) && t.createdBy !== ctx.actorId) {
|
|
220
215
|
throw new QuadError('NOT_PERMITTED', 'only staff, the system or the task’s creator can cancel it');
|
|
221
216
|
}
|
|
@@ -238,8 +233,9 @@ export async function cancelTask(k, ctx, id) {
|
|
|
238
233
|
return taskView(ctx, t);
|
|
239
234
|
}
|
|
240
235
|
/** Open tasks assigned to a person (the shell's inbox). */
|
|
241
|
-
export function inbox(
|
|
242
|
-
|
|
236
|
+
export async function inbox(k, appId, personId) {
|
|
237
|
+
const all = await k.store.allPlatform(appId, 'task');
|
|
238
|
+
return all
|
|
243
239
|
.filter((t) => t.status === 'open' && t.assignees.includes(personId))
|
|
244
240
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
245
241
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quad-education/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The @quad-education/sdk contract, its in-browser engine, transports and contract tests",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"fake-indexeddb": "^6.2.5",
|
|
50
|
-
"@quad-education/canonical": "^0.
|
|
51
|
-
"@quad-education/fictional-school": "^0.
|
|
52
|
-
"@quad-education/schema": "^0.
|
|
50
|
+
"@quad-education/canonical": "^0.3.0",
|
|
51
|
+
"@quad-education/fictional-school": "^0.3.0",
|
|
52
|
+
"@quad-education/schema": "^0.3.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"vitest": "^5.0.3"
|