@quad-education/sdk 0.1.3 → 0.2.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.
Files changed (54) hide show
  1. package/README.md +2 -0
  2. package/dist/contract-tests/apps.d.ts +105 -0
  3. package/dist/contract-tests/apps.js +167 -0
  4. package/dist/contract-tests/harness.d.ts +11 -0
  5. package/dist/contract-tests/harness.js +40 -0
  6. package/dist/contract-tests/helpers.d.ts +5 -0
  7. package/dist/contract-tests/helpers.js +25 -0
  8. package/dist/contract-tests/index.d.ts +4 -2
  9. package/dist/contract-tests/index.js +3 -1
  10. package/dist/contract-tests/standalone-apps.d.ts +183 -0
  11. package/dist/contract-tests/standalone-apps.js +178 -0
  12. package/dist/contract-tests/standalone.d.ts +2 -0
  13. package/dist/contract-tests/standalone.js +709 -0
  14. package/dist/contract-tests/suite.d.ts +10 -3
  15. package/dist/contract-tests/suite.js +536 -44
  16. package/dist/src/client/connect.d.ts +13 -3
  17. package/dist/src/client/connect.js +21 -3
  18. package/dist/src/client/handshake-client.d.ts +2 -0
  19. package/dist/src/client/handshake-client.js +2 -0
  20. package/dist/src/client/http-transport.d.ts +18 -0
  21. package/dist/src/client/http-transport.js +76 -0
  22. package/dist/src/client/live.d.ts +39 -0
  23. package/dist/src/client/live.js +122 -0
  24. package/dist/src/contract/errors.d.ts +1 -1
  25. package/dist/src/contract/principals.d.ts +30 -0
  26. package/dist/src/contract/principals.js +12 -0
  27. package/dist/src/contract/recipients.d.ts +8 -2
  28. package/dist/src/contract/types.d.ts +18 -2
  29. package/dist/src/contract/types.js +1 -1
  30. package/dist/src/engine/audit.d.ts +2 -2
  31. package/dist/src/engine/audit.js +8 -6
  32. package/dist/src/engine/db-indexes.js +6 -1
  33. package/dist/src/engine/engine.d.ts +60 -22
  34. package/dist/src/engine/engine.js +281 -162
  35. package/dist/src/engine/host.d.ts +2 -2
  36. package/dist/src/engine/host.js +1 -1
  37. package/dist/src/engine/in-process.d.ts +2 -2
  38. package/dist/src/engine/index.d.ts +3 -1
  39. package/dist/src/engine/index.js +1 -0
  40. package/dist/src/engine/memory-store.d.ts +32 -0
  41. package/dist/src/engine/memory-store.js +123 -0
  42. package/dist/src/engine/runner.d.ts +2 -2
  43. package/dist/src/engine/runner.js +31 -26
  44. package/dist/src/engine/state.d.ts +21 -15
  45. package/dist/src/engine/state.js +31 -3
  46. package/dist/src/engine/store.d.ts +48 -0
  47. package/dist/src/engine/store.js +1 -0
  48. package/dist/src/engine/tasks.d.ts +4 -6
  49. package/dist/src/engine/tasks.js +27 -24
  50. package/dist/src/index.d.ts +1 -0
  51. package/dist/src/index.js +1 -0
  52. package/dist/src/jobs/define.d.ts +2 -2
  53. package/dist/src/platform-types.d.ts +2 -2
  54. package/package.json +4 -4
@@ -10,8 +10,11 @@ export function taskView(ctx, t) {
10
10
  const { appId: _appId, purpose: _purpose, assignees, ...rest } = t;
11
11
  return {
12
12
  ...rest,
13
- assignedToMe: ctx.principal.kind === 'user' && assignees.includes(ctx.actorId),
14
- ...(isStaffOrSystem(ctx) ? { assignees: [...assignees] } : {}),
13
+ assignedToMe: ctx.principal.kind !== 'system' && assignees.includes(ctx.actorId),
14
+ // The creator sees who was assigned, as staff do (an account has no school role to be staff by).
15
+ ...(isStaffOrSystem(ctx) || (ctx.principal.kind === 'account' && t.createdBy === ctx.actorId)
16
+ ? { assignees: [...assignees] }
17
+ : {}),
15
18
  };
16
19
  }
17
20
  /**
@@ -24,8 +27,8 @@ function canSee(k, ctx, t) {
24
27
  const involved = isStaffOrSystem(ctx) || t.assignees.includes(ctx.actorId) || t.createdBy === ctx.actorId;
25
28
  return involved && seesSubject(k, ctx, t.regardingStudentId);
26
29
  }
27
- function visibleTask(k, ctx, id) {
28
- const t = ctx.app.tasks.get(str(id, 'id'));
30
+ async function visibleTask(k, ctx, id) {
31
+ const t = await k.store.getPlatform(ctx.app.manifest.id, 'task', str(id, 'id'));
29
32
  if (!t || !canSee(k, ctx, t))
30
33
  throw new QuadError('NOT_FOUND', `no task ${String(id)}`);
31
34
  return t;
@@ -37,14 +40,10 @@ export async function assignTask(k, ctx, input) {
37
40
  throw new QuadError('UNDECLARED_TYPE', `task type '${type}' isn't declared in app.manifest.json`);
38
41
  }
39
42
  const key = k.idempotencyKey(input, 'tasks.assign');
40
- if (key && ctx.app.keys.has(key)) {
41
- const ids = ctx.app.keys.get(key);
42
- return {
43
- tasks: ids
44
- .map((id) => ctx.app.tasks.get(id))
45
- .filter((t) => t !== undefined)
46
- .map((t) => taskView(ctx, t)),
47
- };
43
+ const remembered = key ? await k.store.getKey(ctx.app.manifest.id, key) : undefined;
44
+ if (Array.isArray(remembered)) {
45
+ const found = await Promise.all(remembered.map((id) => k.store.getPlatform(ctx.app.manifest.id, 'task', id)));
46
+ return { tasks: found.filter((t) => t !== undefined).map((t) => taskView(ctx, t)) };
48
47
  }
49
48
  const title = str(input.title, 'title');
50
49
  if (title.length > 200)
@@ -57,8 +56,12 @@ export async function assignTask(k, ctx, input) {
57
56
  const dueAt = input.dueAt === undefined ? undefined : instant(input.dueAt, 'dueAt');
58
57
  const link = appLink(input.link);
59
58
  const data = jsonObject(input.data, 'data');
59
+ if (ctx.principal.kind === 'guest')
60
+ throw new QuadError('FORBIDDEN', 'guests cannot assign tasks: nobody is signed in to be accountable');
60
61
  let regarding;
61
62
  if (input.regarding !== undefined) {
63
+ if (!ctx.school)
64
+ throw new QuadError('NOT_AVAILABLE', 'regarding is for tasks about a student, which needs a school');
62
65
  if (!isObject(input.regarding))
63
66
  throw new QuadError('VALIDATION', 'regarding must be { studentId }');
64
67
  regarding = str(input.regarding.studentId, 'regarding.studentId');
@@ -66,7 +69,7 @@ export async function assignTask(k, ctx, input) {
66
69
  if (regarding !== undefined && !seesSubject(k, ctx, regarding)) {
67
70
  throw new QuadError('NOT_PERMITTED', 'regarding: not a student you can see');
68
71
  }
69
- const resolved = k.resolveRecipients(ctx, input.assignee, regarding);
72
+ const resolved = await k.resolveRecipients(ctx, input.assignee, regarding);
70
73
  const { suppressed } = resolved;
71
74
  // A task about a student only goes to people who can see that student (D25); parents
72
75
  // were already limited to eligible links (D17).
@@ -111,8 +114,6 @@ export async function assignTask(k, ctx, input) {
111
114
  id: `task_${crypto.randomUUID()}`,
112
115
  assignees,
113
116
  }));
114
- for (const t of tasks)
115
- ctx.app.tasks.set(t.id, t);
116
117
  await k.persistPlatform(ctx.app, 'task', tasks);
117
118
  for (const t of tasks) {
118
119
  await appendAudit(k, ctx.app, {
@@ -147,10 +148,10 @@ export async function assignTask(k, ctx, input) {
147
148
  await k.remember(ctx, key, tasks.map((t) => t.id));
148
149
  return { tasks: tasks.map((t) => taskView(ctx, t)) };
149
150
  }
150
- export function getTask(k, ctx, id) {
151
- return taskView(ctx, visibleTask(k, ctx, id));
151
+ export async function getTask(k, ctx, id) {
152
+ return taskView(ctx, await visibleTask(k, ctx, id));
152
153
  }
153
- export function listTasks(k, ctx, options) {
154
+ export async function listTasks(k, ctx, options) {
154
155
  const o = isObject(options) ? options : {};
155
156
  const where = {};
156
157
  if (o.status !== undefined) {
@@ -165,7 +166,8 @@ export function listTasks(k, ctx, options) {
165
166
  where.regardingStudentId = str(o.regardingStudentId, 'regardingStudentId');
166
167
  if (o.groupId !== undefined)
167
168
  where.groupId = str(o.groupId, 'groupId');
168
- let tasks = [...ctx.app.tasks.values()].filter((t) => canSee(k, ctx, t));
169
+ const all = await k.store.allPlatform(ctx.app.manifest.id, 'task');
170
+ let tasks = all.filter((t) => canSee(k, ctx, t));
169
171
  if (o.assignedToMe !== undefined) {
170
172
  if (typeof o.assignedToMe !== 'boolean')
171
173
  throw new QuadError('VALIDATION', 'assignedToMe must be true or false');
@@ -183,12 +185,12 @@ export function listTasks(k, ctx, options) {
183
185
  };
184
186
  }
185
187
  export async function completeTask(k, ctx, id, options) {
186
- const t = visibleTask(k, ctx, id);
188
+ const t = await visibleTask(k, ctx, id);
187
189
  if (ctx.principal.kind === 'system' || !t.assignees.includes(ctx.actorId)) {
188
190
  throw new QuadError('NOT_ASSIGNEE', 'only an assignee can complete this task, from the app');
189
191
  }
190
192
  // D17 is re-checked now, not just at assignment.
191
- if (ctx.principal.role === 'parent' && t.regardingStudentId) {
193
+ if (ctx.principal.kind === 'user' && ctx.principal.role === 'parent' && t.regardingStudentId) {
192
194
  if (!eligibleParents(k.dataset, t.regardingStudentId, t.purpose ?? 'correspondence').includes(ctx.actorId)) {
193
195
  throw new QuadError('NOT_ASSIGNEE', 'you can no longer complete this task');
194
196
  }
@@ -215,7 +217,7 @@ export async function completeTask(k, ctx, id, options) {
215
217
  return taskView(ctx, t);
216
218
  }
217
219
  export async function cancelTask(k, ctx, id) {
218
- const t = visibleTask(k, ctx, id);
220
+ const t = await visibleTask(k, ctx, id);
219
221
  if (!isStaffOrSystem(ctx) && t.createdBy !== ctx.actorId) {
220
222
  throw new QuadError('NOT_PERMITTED', 'only staff, the system or the task’s creator can cancel it');
221
223
  }
@@ -238,8 +240,9 @@ export async function cancelTask(k, ctx, id) {
238
240
  return taskView(ctx, t);
239
241
  }
240
242
  /** Open tasks assigned to a person (the shell's inbox). */
241
- export function inbox(app, personId) {
242
- return [...app.tasks.values()]
243
+ export async function inbox(k, appId, personId) {
244
+ const all = await k.store.allPlatform(appId, 'task');
245
+ return all
243
246
  .filter((t) => t.status === 'open' && t.assignees.includes(personId))
244
247
  .sort((a, b) => a.createdAt.localeCompare(b.createdAt));
245
248
  }
@@ -1,3 +1,4 @@
1
1
  export * from './contract/errors.ts';
2
+ export * from './contract/principals.ts';
2
3
  export * from './contract/recipients.ts';
3
4
  export * from './contract/types.ts';
package/dist/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // @quad-education/sdk: the contract. Apps get an instance from `connect()` in '@quad-education/sdk/client'.
2
2
  export * from "./contract/errors.js";
3
+ export * from "./contract/principals.js";
3
4
  export * from "./contract/recipients.js";
4
5
  export * from "./contract/types.js";
@@ -1,4 +1,4 @@
1
- import type { Principal } from '@quad-education/canonical';
1
+ import type { ActingAs } from '../contract/principals.ts';
2
2
  import type { AppConfig, CurrentApp, JsonValue, QuadSdk } from '../contract/types.ts';
3
3
  export type JobContext<A extends AppConfig = CurrentApp> = {
4
4
  /**
@@ -29,7 +29,7 @@ export type RunRequest = {
29
29
  readonly payload: JsonValue;
30
30
  readonly attempt: number;
31
31
  /** Who the run acts as. The host must connect the job's SDK as this principal (D7). */
32
- readonly runAs: Principal;
32
+ readonly runAs: ActingAs;
33
33
  };
34
34
  export type RunResult = {
35
35
  ok: true;
@@ -1,4 +1,3 @@
1
- import type { Role } from '@quad-education/canonical';
2
1
  import type { Channel } from './contract/types.ts';
3
2
  /** A simulated outbox entry (D10). Visible to the demo shell and tests, never to apps. */
4
3
  export type OutboxEntry = {
@@ -7,7 +6,8 @@ export type OutboxEntry = {
7
6
  readonly type: string;
8
7
  readonly channel: Channel;
9
8
  readonly recipientId: string;
10
- readonly recipientRole: Role;
9
+ /** A school role, or an app-defined role (V2c, D100). */
10
+ readonly recipientRole: string;
11
11
  readonly subject: string;
12
12
  readonly body: string;
13
13
  readonly link?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quad-education/sdk",
3
- "version": "0.1.3",
3
+ "version": "0.2.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/schema": "^0.1.3",
51
- "@quad-education/canonical": "^0.1.3",
52
- "@quad-education/fictional-school": "^0.1.3"
50
+ "@quad-education/fictional-school": "^0.2.0",
51
+ "@quad-education/canonical": "^0.2.0",
52
+ "@quad-education/schema": "^0.2.0"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "vitest": "^5.0.3"