@quad-education/sdk 0.0.0-stage → 0.1.3
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/LICENSE.md +15 -0
- package/README.md +106 -2
- package/dist/contract-tests/apps.d.ts +373 -0
- package/dist/contract-tests/apps.js +309 -0
- package/dist/contract-tests/index.d.ts +3 -0
- package/dist/contract-tests/index.js +2 -0
- package/dist/contract-tests/suite.d.ts +57 -0
- package/dist/contract-tests/suite.js +1405 -0
- package/dist/src/client/connect.d.ts +10 -0
- package/dist/src/client/connect.js +12 -0
- package/dist/src/client/create-client.d.ts +14 -0
- package/dist/src/client/create-client.js +76 -0
- package/dist/src/client/handshake-client.d.ts +16 -0
- package/dist/src/client/handshake-client.js +37 -0
- package/dist/src/contract/errors.d.ts +8 -0
- package/dist/src/contract/errors.js +13 -0
- package/dist/src/contract/recipients.d.ts +28 -0
- package/dist/src/contract/recipients.js +12 -0
- package/dist/src/contract/types.d.ts +442 -0
- package/dist/src/contract/types.js +1 -0
- package/dist/src/engine/audit.d.ts +27 -0
- package/dist/src/engine/audit.js +105 -0
- package/dist/src/engine/cron.d.ts +17 -0
- package/dist/src/engine/cron.js +143 -0
- package/dist/src/engine/db-indexes.d.ts +11 -0
- package/dist/src/engine/db-indexes.js +82 -0
- package/dist/src/engine/engine.d.ts +156 -0
- package/dist/src/engine/engine.js +953 -0
- package/dist/src/engine/host.d.ts +39 -0
- package/dist/src/engine/host.js +73 -0
- package/dist/src/engine/in-process.d.ts +5 -0
- package/dist/src/engine/in-process.js +18 -0
- package/dist/src/engine/index.d.ts +10 -0
- package/dist/src/engine/index.js +8 -0
- package/dist/src/engine/query.d.ts +16 -0
- package/dist/src/engine/query.js +106 -0
- package/dist/src/engine/runner.d.ts +26 -0
- package/dist/src/engine/runner.js +260 -0
- package/dist/src/engine/state.d.ts +107 -0
- package/dist/src/engine/state.js +16 -0
- package/dist/src/engine/storage.d.ts +46 -0
- package/dist/src/engine/storage.js +86 -0
- package/dist/src/engine/tasks.d.ts +17 -0
- package/dist/src/engine/tasks.js +245 -0
- package/dist/src/engine/validate-input.d.ts +9 -0
- package/dist/src/engine/validate-input.js +76 -0
- package/dist/src/engine/values.d.ts +10 -0
- package/dist/src/engine/values.js +58 -0
- package/dist/src/handshake.d.ts +16 -0
- package/dist/src/handshake.js +9 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +4 -0
- package/dist/src/jobs/define.d.ts +41 -0
- package/dist/src/jobs/define.js +28 -0
- package/dist/src/jobs/host.d.ts +13 -0
- package/dist/src/jobs/host.js +22 -0
- package/dist/src/jobs/index.d.ts +1 -0
- package/dist/src/jobs/index.js +2 -0
- package/dist/src/jobs/worker.d.ts +17 -0
- package/dist/src/jobs/worker.js +25 -0
- package/dist/src/platform-types.d.ts +19 -0
- package/dist/src/platform-types.js +1 -0
- package/dist/src/port-rpc.d.ts +42 -0
- package/dist/src/port-rpc.js +85 -0
- package/dist/src/protocol.d.ts +41 -0
- package/dist/src/protocol.js +45 -0
- package/dist/src/testing/index.d.ts +78 -0
- package/dist/src/testing/index.js +118 -0
- package/package.json +71 -3
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import type { CanonicalRecord, ContactPurpose, Entities, EntityName, RecordWithFields, RecordWithTiers, Role, TiersFor } from '@quad-education/canonical';
|
|
2
|
+
import type { AppEntityDef, AppInput, AppSchema, FieldValue, SystemFields } from '@quad-education/schema';
|
|
3
|
+
import type { Recipient } from './recipients.ts';
|
|
4
|
+
export declare const SDK_VERSION = "0.1.3";
|
|
5
|
+
/**
|
|
6
|
+
* Describes the current app to the type system. `quad gen` augments it from
|
|
7
|
+
* app.manifest.json and schema/ (D23); without that, nothing is readable.
|
|
8
|
+
*/
|
|
9
|
+
export interface QuadApp {
|
|
10
|
+
}
|
|
11
|
+
/** The resolved shape of an app's configuration. */
|
|
12
|
+
export type AppConfig = {
|
|
13
|
+
/** The roles the app serves (D2). Fields hidden from any of them are optional (D25). */
|
|
14
|
+
roles: string;
|
|
15
|
+
scopes: string;
|
|
16
|
+
/** The varying fields a version 1 manifest declares, per entity (V2s D85). Absent for version 0. */
|
|
17
|
+
data?: Record<string, {
|
|
18
|
+
required: string;
|
|
19
|
+
optional: string;
|
|
20
|
+
}> | undefined;
|
|
21
|
+
notifications: string;
|
|
22
|
+
tasks: string;
|
|
23
|
+
/** Jobs that `schedule.at` may name (the manifest's `scheduled` list, D7). */
|
|
24
|
+
scheduled: string;
|
|
25
|
+
schema: AppSchema;
|
|
26
|
+
};
|
|
27
|
+
type Field<T, K extends string, Fallback> = T extends {
|
|
28
|
+
[P in K]: infer V;
|
|
29
|
+
} ? V : Fallback;
|
|
30
|
+
/** QuadApp with defaults filled in. */
|
|
31
|
+
export type CurrentApp = {
|
|
32
|
+
roles: Field<QuadApp, 'roles', Role> & string;
|
|
33
|
+
scopes: Field<QuadApp, 'scopes', never> & string;
|
|
34
|
+
data: Field<QuadApp, 'data', undefined>;
|
|
35
|
+
notifications: Field<QuadApp, 'notifications', never> & string;
|
|
36
|
+
tasks: Field<QuadApp, 'tasks', never> & string;
|
|
37
|
+
scheduled: Field<QuadApp, 'scheduled', never> & string;
|
|
38
|
+
schema: Field<QuadApp, 'schema', Record<never, never>> & AppSchema;
|
|
39
|
+
};
|
|
40
|
+
type Scalar = string | number | boolean;
|
|
41
|
+
export type Condition<V> = V | {
|
|
42
|
+
readonly in: readonly V[];
|
|
43
|
+
} | {
|
|
44
|
+
readonly ne: V;
|
|
45
|
+
} | {
|
|
46
|
+
readonly gt?: V;
|
|
47
|
+
readonly gte?: V;
|
|
48
|
+
readonly lt?: V;
|
|
49
|
+
readonly lte?: V;
|
|
50
|
+
};
|
|
51
|
+
/** Field equality, `in`, `ne` and range conditions, combined with AND. */
|
|
52
|
+
export type Where<R> = {
|
|
53
|
+
readonly [K in keyof R]?: R[K] extends Scalar | undefined ? Condition<Exclude<R[K], undefined>> : never;
|
|
54
|
+
};
|
|
55
|
+
export type OrderBy<R> = {
|
|
56
|
+
readonly field: keyof R & string;
|
|
57
|
+
readonly direction?: 'asc' | 'desc';
|
|
58
|
+
};
|
|
59
|
+
export type ListOptions<R> = {
|
|
60
|
+
readonly where?: Where<R>;
|
|
61
|
+
readonly orderBy?: OrderBy<R> | readonly OrderBy<R>[];
|
|
62
|
+
/** 1–200, default 50. */
|
|
63
|
+
readonly limit?: number;
|
|
64
|
+
readonly cursor?: string;
|
|
65
|
+
};
|
|
66
|
+
export type Page<R> = {
|
|
67
|
+
readonly items: readonly R[];
|
|
68
|
+
readonly nextCursor: string | null;
|
|
69
|
+
};
|
|
70
|
+
export type PersonSummary = {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly givenName: string;
|
|
73
|
+
readonly familyName: string;
|
|
74
|
+
readonly displayName: string;
|
|
75
|
+
};
|
|
76
|
+
/** The current principal. Jobs (Slice B) run as `system` (D8). */
|
|
77
|
+
export type Me = {
|
|
78
|
+
readonly kind: 'user';
|
|
79
|
+
readonly role: Role;
|
|
80
|
+
readonly person: PersonSummary;
|
|
81
|
+
} | {
|
|
82
|
+
readonly kind: 'system';
|
|
83
|
+
readonly appId: string;
|
|
84
|
+
};
|
|
85
|
+
export type AuthApi = {
|
|
86
|
+
me(): Promise<Me>;
|
|
87
|
+
};
|
|
88
|
+
/** What an app declared for one entity: required and optional field names (V2s D85). */
|
|
89
|
+
type DeclaredFor<A, N extends string, K extends 'required' | 'optional'> = A extends {
|
|
90
|
+
data: infer D;
|
|
91
|
+
} ? D extends {
|
|
92
|
+
[P in N]: {
|
|
93
|
+
[Q in K]: infer F extends string;
|
|
94
|
+
};
|
|
95
|
+
} ? F : never : never;
|
|
96
|
+
/**
|
|
97
|
+
* A school record as this app sees it. A version 1 manifest (with `data`) gets the core fields and the
|
|
98
|
+
* fields it declared, optional ones typed `?`; a version 0 manifest gets every field of its granted
|
|
99
|
+
* tiers (D23).
|
|
100
|
+
*/
|
|
101
|
+
export type SchoolRecord<A extends AppConfig, N extends EntityName> = A extends {
|
|
102
|
+
data: infer D;
|
|
103
|
+
} ? [D] extends [undefined] ? RecordWithTiers<Entities[N], TiersFor<A['scopes'], N>> : RecordWithFields<Entities[N], TiersFor<A['scopes'], N>, DeclaredFor<A, N, 'required'>, DeclaredFor<A, N, 'optional'>> : RecordWithTiers<Entities[N], TiersFor<A['scopes'], N>>;
|
|
104
|
+
export type Collection<R> = {
|
|
105
|
+
/** A record the current user can see; NOT_FOUND otherwise. */
|
|
106
|
+
get(id: string): Promise<R>;
|
|
107
|
+
list(options?: ListOptions<R>): Promise<Page<R>>;
|
|
108
|
+
};
|
|
109
|
+
export type SchoolInfo = {
|
|
110
|
+
readonly id: string;
|
|
111
|
+
readonly name: string;
|
|
112
|
+
readonly state: CanonicalRecord<'schools'>['state'];
|
|
113
|
+
/** IANA timezone. Dates, "today" and schedules follow it, never the browser's (D21). */
|
|
114
|
+
readonly timezone: string;
|
|
115
|
+
readonly yearLevelLabels: Readonly<Record<string, string>>;
|
|
116
|
+
};
|
|
117
|
+
type CohortHelpers<A extends AppConfig> = {
|
|
118
|
+
/** The cohort with this per-school code, e.g. `deputy-principal` (D18), or null. */
|
|
119
|
+
byCode(code: string): Promise<SchoolRecord<A, 'cohorts'> | null>;
|
|
120
|
+
/** Memberships of a cohort. Needs a `cohortMemberships` scope. */
|
|
121
|
+
members(cohortId: string, options?: {
|
|
122
|
+
memberType?: 'staff' | 'student';
|
|
123
|
+
}): Promise<readonly SchoolRecord<A, 'cohortMemberships'>[]>;
|
|
124
|
+
};
|
|
125
|
+
type StudentHelpers<A extends AppConfig> = {
|
|
126
|
+
/** Linked parents the current user can see. Needs a `parents` scope. Not a contact list: use `parentsOf` to contact parents. */
|
|
127
|
+
parents(studentId: string): Promise<readonly SchoolRecord<A, 'parents'>[]>;
|
|
128
|
+
};
|
|
129
|
+
type ParentHelpers<A extends AppConfig> = {
|
|
130
|
+
/** Children of a parent that the current user can see. Needs a `students` scope. */
|
|
131
|
+
children(parentId: string): Promise<readonly SchoolRecord<A, 'students'>[]>;
|
|
132
|
+
};
|
|
133
|
+
type Helpers<A extends AppConfig, N extends EntityName> = N extends 'cohorts' ? CohortHelpers<A> : N extends 'students' ? StudentHelpers<A> : N extends 'parents' ? ParentHelpers<A> : unknown;
|
|
134
|
+
type Readable<A extends AppConfig> = {
|
|
135
|
+
[N in Exclude<EntityName, 'schools'>]: [TiersFor<A['scopes'], N>] extends [never] ? never : N;
|
|
136
|
+
}[Exclude<EntityName, 'schools'>];
|
|
137
|
+
/** Canonical school data, filtered by the app's scopes and the user's visibility. Read-only. */
|
|
138
|
+
export type SchoolApi<A extends AppConfig> = {
|
|
139
|
+
info(): Promise<SchoolInfo>;
|
|
140
|
+
} & {
|
|
141
|
+
readonly [N in Readable<A>]: Collection<SchoolRecord<A, N>> & Helpers<A, N>;
|
|
142
|
+
} & ('calendarEvents' extends Readable<A> ? {
|
|
143
|
+
readonly calendar: {
|
|
144
|
+
/** Events overlapping [from, to); with `cohortId`, that cohort's events plus whole-school ones. */
|
|
145
|
+
list(options: {
|
|
146
|
+
from: string;
|
|
147
|
+
to: string;
|
|
148
|
+
cohortId?: string;
|
|
149
|
+
}): Promise<readonly SchoolRecord<A, 'calendarEvents'>[]>;
|
|
150
|
+
};
|
|
151
|
+
} : unknown);
|
|
152
|
+
type Simplify<T> = {
|
|
153
|
+
[K in keyof T]: T[K];
|
|
154
|
+
};
|
|
155
|
+
/** True when every role the app serves may see the field (D25). */
|
|
156
|
+
type SeenByAll<D, R extends string> = D extends {
|
|
157
|
+
visibleTo: readonly (infer V)[];
|
|
158
|
+
} ? [Exclude<R, V>] extends [never] ? true : false : true;
|
|
159
|
+
type AlwaysPresent<A extends AppConfig, D> = D extends {
|
|
160
|
+
optional: true;
|
|
161
|
+
} ? false : SeenByAll<D, A['roles']>;
|
|
162
|
+
/**
|
|
163
|
+
* An app record as this app's users receive it. A field with `visibleTo` is optional unless
|
|
164
|
+
* every role the app serves may see it, since other roles never receive it (D25).
|
|
165
|
+
*/
|
|
166
|
+
export type RecordFor<A extends AppConfig, E extends AppEntityDef> = Simplify<SystemFields & {
|
|
167
|
+
readonly [K in keyof E['fields'] as AlwaysPresent<A, E['fields'][K]> extends true ? K : never]: FieldValue<E['fields'][K]>;
|
|
168
|
+
} & {
|
|
169
|
+
readonly [K in keyof E['fields'] as AlwaysPresent<A, E['fields'][K]> extends true ? never : K]?: FieldValue<E['fields'][K]>;
|
|
170
|
+
}>;
|
|
171
|
+
type RefFields<E extends AppEntityDef> = {
|
|
172
|
+
[K in keyof E['fields']]: E['fields'][K] extends {
|
|
173
|
+
kind: 'ref';
|
|
174
|
+
} ? K : never;
|
|
175
|
+
}[keyof E['fields']] & string;
|
|
176
|
+
type RefTarget<E extends AppEntityDef, K extends keyof E['fields']> = E['fields'][K] extends {
|
|
177
|
+
to: infer T;
|
|
178
|
+
} ? T : never;
|
|
179
|
+
type Resolved<A extends AppConfig, T> = T extends `school.${infer N extends EntityName}` ? SchoolRecord<A, N> : T extends keyof A['schema'] ? RecordFor<A, A['schema'][T]> : never;
|
|
180
|
+
export type WithIncluded<A extends AppConfig, E extends AppEntityDef, I extends string> = RecordFor<A, E> & {
|
|
181
|
+
/** Included refs; null when the target doesn't exist or isn't visible to the current user. */
|
|
182
|
+
readonly $included: {
|
|
183
|
+
readonly [K in I]: K extends keyof E['fields'] ? Resolved<A, RefTarget<E, K>> | null : never;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Fields an app entity may sort on or range-filter (D37): any field in a declared index,
|
|
188
|
+
* plus `id` and `createdAt`, which are always indexed. The engine also checks the exact
|
|
189
|
+
* rule at runtime: index order, an equality prefix, one range field and one sort direction.
|
|
190
|
+
*/
|
|
191
|
+
export type IndexedField<E extends AppEntityDef> = 'id' | 'createdAt' | (E['indexes'][number][number] & string);
|
|
192
|
+
type EqualityCondition<V> = V | {
|
|
193
|
+
readonly in: readonly V[];
|
|
194
|
+
} | {
|
|
195
|
+
readonly ne: V;
|
|
196
|
+
};
|
|
197
|
+
/** Like `Where`, but range conditions only on indexed fields (D37). */
|
|
198
|
+
export type DbWhere<R, IX extends string> = {
|
|
199
|
+
readonly [K in keyof R]?: R[K] extends Scalar | undefined ? K extends IX ? Condition<Exclude<R[K], undefined>> : EqualityCondition<Exclude<R[K], undefined>> : never;
|
|
200
|
+
};
|
|
201
|
+
export type DbOrderBy<R, IX extends string> = {
|
|
202
|
+
readonly field: keyof R & IX & string;
|
|
203
|
+
readonly direction?: 'asc' | 'desc';
|
|
204
|
+
};
|
|
205
|
+
export type DbListOptions<A extends AppConfig, E extends AppEntityDef, I extends RefFields<E>> = Omit<ListOptions<RecordFor<A, E>>, 'where' | 'orderBy'> & {
|
|
206
|
+
readonly where?: DbWhere<RecordFor<A, E>, IndexedField<E>>;
|
|
207
|
+
readonly orderBy?: DbOrderBy<RecordFor<A, E>, IndexedField<E>> | readonly DbOrderBy<RecordFor<A, E>, IndexedField<E>>[];
|
|
208
|
+
readonly include?: readonly I[];
|
|
209
|
+
};
|
|
210
|
+
/** Operations on one app entity. A future `db.transaction(tx => …)` hands out the same interface (D12). */
|
|
211
|
+
export type DbOps<A extends AppConfig, E extends AppEntityDef> = {
|
|
212
|
+
/** NOT_PERMITTED if the access rules deny it, or the subject is a student you can't see (D25). */
|
|
213
|
+
create(input: AppInput<E>, options?: {
|
|
214
|
+
idempotencyKey?: string;
|
|
215
|
+
}): Promise<RecordFor<A, E>>;
|
|
216
|
+
update(id: string, patch: Partial<AppInput<E>>): Promise<RecordFor<A, E>>;
|
|
217
|
+
/** NOT_FOUND if the record doesn't exist or you may not read it (D25). */
|
|
218
|
+
get(id: string): Promise<RecordFor<A, E>>;
|
|
219
|
+
/** Only records you may read; filters and pages apply after access checks (D25). */
|
|
220
|
+
list<const I extends RefFields<E> = never>(options?: DbListOptions<A, E, I>): Promise<Page<[I] extends [never] ? RecordFor<A, E> : WithIncluded<A, E, I>>>;
|
|
221
|
+
delete(id: string): Promise<void>;
|
|
222
|
+
};
|
|
223
|
+
/** The app's own data, stored by the platform (IndexedDB in V1). */
|
|
224
|
+
export type DbApi<A extends AppConfig> = {
|
|
225
|
+
entity<N extends keyof A['schema'] & string>(name: N): DbOps<A, A['schema'][N]>;
|
|
226
|
+
};
|
|
227
|
+
export type Channel = 'inApp' | 'email';
|
|
228
|
+
export type NotifyInput<A extends AppConfig> = {
|
|
229
|
+
readonly type: A['notifications'];
|
|
230
|
+
readonly to: Recipient | readonly Recipient[];
|
|
231
|
+
/** Required whenever a recipient is a parent: the student they're being contacted about (D17). */
|
|
232
|
+
readonly regarding?: {
|
|
233
|
+
readonly studentId: string;
|
|
234
|
+
};
|
|
235
|
+
readonly channel?: Channel;
|
|
236
|
+
readonly subject: string;
|
|
237
|
+
readonly body: string;
|
|
238
|
+
/** An app-relative link, e.g. `/greetings/abc`. */
|
|
239
|
+
readonly link?: string;
|
|
240
|
+
readonly idempotencyKey?: string;
|
|
241
|
+
};
|
|
242
|
+
export type ResolvedRecipient = {
|
|
243
|
+
readonly personId: string;
|
|
244
|
+
readonly role: Role;
|
|
245
|
+
};
|
|
246
|
+
export type NotifyApi<A extends AppConfig> = {
|
|
247
|
+
/**
|
|
248
|
+
* Queue a notification. In V1 it lands in the simulated outbox and is never sent (D10).
|
|
249
|
+
* Returns the recipients it resolved to; ineligible parents are left out without saying why (D17).
|
|
250
|
+
*/
|
|
251
|
+
send(input: NotifyInput<A>): Promise<{
|
|
252
|
+
readonly recipients: readonly ResolvedRecipient[];
|
|
253
|
+
}>;
|
|
254
|
+
};
|
|
255
|
+
export type JsonValue = string | number | boolean | null | readonly JsonValue[] | {
|
|
256
|
+
readonly [key: string]: JsonValue;
|
|
257
|
+
};
|
|
258
|
+
export type TaskStatus = 'open' | 'completed' | 'cancelled';
|
|
259
|
+
export type TaskMode = 'each' | 'any';
|
|
260
|
+
export type Task = {
|
|
261
|
+
readonly id: string;
|
|
262
|
+
readonly type: string;
|
|
263
|
+
readonly title: string;
|
|
264
|
+
readonly description?: string;
|
|
265
|
+
readonly status: TaskStatus;
|
|
266
|
+
readonly mode: TaskMode;
|
|
267
|
+
/** Tasks created together by one `assign` call share a group. */
|
|
268
|
+
readonly groupId: string;
|
|
269
|
+
/** Whether the current user is one of the assignees. */
|
|
270
|
+
readonly assignedToMe: boolean;
|
|
271
|
+
/** Who it's assigned to. Only staff and the system principal see this. */
|
|
272
|
+
readonly assignees?: readonly string[];
|
|
273
|
+
readonly regardingStudentId?: string;
|
|
274
|
+
readonly dueAt?: string;
|
|
275
|
+
/** An app-relative link; the shell's inbox opens the app there. */
|
|
276
|
+
readonly link?: string;
|
|
277
|
+
readonly data?: {
|
|
278
|
+
readonly [key: string]: JsonValue;
|
|
279
|
+
};
|
|
280
|
+
readonly createdAt: string;
|
|
281
|
+
readonly createdBy: string;
|
|
282
|
+
readonly completedAt?: string;
|
|
283
|
+
readonly completedBy?: string;
|
|
284
|
+
readonly outcome?: string;
|
|
285
|
+
readonly cancelledAt?: string;
|
|
286
|
+
readonly cancelledBy?: string;
|
|
287
|
+
};
|
|
288
|
+
export type AssignInput<A extends AppConfig> = {
|
|
289
|
+
readonly type: A['tasks'];
|
|
290
|
+
readonly title: string;
|
|
291
|
+
readonly description?: string;
|
|
292
|
+
readonly assignee: Recipient | readonly Recipient[];
|
|
293
|
+
/** `each` (default): one task per person. `any`: one shared task; the first to complete closes it. */
|
|
294
|
+
readonly mode?: TaskMode;
|
|
295
|
+
/** Required when an assignee is a parent (D17). */
|
|
296
|
+
readonly regarding?: {
|
|
297
|
+
readonly studentId: string;
|
|
298
|
+
};
|
|
299
|
+
readonly dueAt?: string;
|
|
300
|
+
readonly link?: string;
|
|
301
|
+
readonly data?: {
|
|
302
|
+
readonly [key: string]: JsonValue;
|
|
303
|
+
};
|
|
304
|
+
readonly idempotencyKey?: string;
|
|
305
|
+
};
|
|
306
|
+
export type TaskListOptions = {
|
|
307
|
+
readonly assignedToMe?: boolean;
|
|
308
|
+
readonly status?: TaskStatus;
|
|
309
|
+
readonly type?: string;
|
|
310
|
+
readonly regardingStudentId?: string;
|
|
311
|
+
readonly groupId?: string;
|
|
312
|
+
readonly limit?: number;
|
|
313
|
+
readonly cursor?: string;
|
|
314
|
+
};
|
|
315
|
+
export type TasksApi<A extends AppConfig> = {
|
|
316
|
+
/** Assign a task. Ineligible parents are left out without saying why (D17). */
|
|
317
|
+
assign(input: AssignInput<A>): Promise<{
|
|
318
|
+
readonly tasks: readonly Task[];
|
|
319
|
+
}>;
|
|
320
|
+
/** NOT_FOUND unless you're an assignee, its creator, or staff. */
|
|
321
|
+
get(id: string): Promise<Task>;
|
|
322
|
+
list(options?: TaskListOptions): Promise<Page<Task>>;
|
|
323
|
+
/** Only an assignee can complete a task, from the app. NOT_ASSIGNEE otherwise (D6). */
|
|
324
|
+
complete(id: string, options?: {
|
|
325
|
+
outcome?: string;
|
|
326
|
+
}): Promise<Task>;
|
|
327
|
+
/** Its creator, staff or the system principal can cancel an open task. */
|
|
328
|
+
cancel(id: string): Promise<Task>;
|
|
329
|
+
};
|
|
330
|
+
export type ScheduledRun = {
|
|
331
|
+
readonly id: string;
|
|
332
|
+
readonly job: string;
|
|
333
|
+
readonly when: string;
|
|
334
|
+
readonly payload: JsonValue;
|
|
335
|
+
readonly status: 'pending' | 'done' | 'cancelled';
|
|
336
|
+
readonly createdAt: string;
|
|
337
|
+
readonly createdBy: string;
|
|
338
|
+
/** `user`: runs as whoever scheduled it, with their access. `system`: scheduled by a system job. */
|
|
339
|
+
readonly runsAs: 'user' | 'system';
|
|
340
|
+
};
|
|
341
|
+
export type ScheduleApi<A extends AppConfig> = {
|
|
342
|
+
/**
|
|
343
|
+
* Run a job from `jobs/` once, at or after `when` (D7). It runs as the caller: a job
|
|
344
|
+
* scheduled in a user's session runs with that user's permissions and visibility, and a
|
|
345
|
+
* job scheduled from inside another job runs as that job's principal.
|
|
346
|
+
*/
|
|
347
|
+
at(input: {
|
|
348
|
+
readonly when: string;
|
|
349
|
+
readonly job: A['scheduled'];
|
|
350
|
+
readonly payload?: JsonValue;
|
|
351
|
+
readonly idempotencyKey?: string;
|
|
352
|
+
}): Promise<ScheduledRun>;
|
|
353
|
+
/** Your own scheduled jobs; the system principal can cancel any. */
|
|
354
|
+
cancel(id: string): Promise<ScheduledRun>;
|
|
355
|
+
/** Jobs you scheduled; the system principal sees all. */
|
|
356
|
+
list(options?: {
|
|
357
|
+
readonly status?: ScheduledRun['status'];
|
|
358
|
+
}): Promise<readonly ScheduledRun[]>;
|
|
359
|
+
};
|
|
360
|
+
export type CronJobInfo = {
|
|
361
|
+
readonly name: string;
|
|
362
|
+
readonly schedule: string;
|
|
363
|
+
readonly description: string;
|
|
364
|
+
/** The next run, in UTC, computed in the school's timezone (D21). */
|
|
365
|
+
readonly nextRunAt: string | null;
|
|
366
|
+
readonly lastScheduledFor: string | null;
|
|
367
|
+
};
|
|
368
|
+
export type CronApi = {
|
|
369
|
+
list(): Promise<readonly CronJobInfo[]>;
|
|
370
|
+
};
|
|
371
|
+
export type AuditEntry = {
|
|
372
|
+
readonly id: string;
|
|
373
|
+
readonly appId: string;
|
|
374
|
+
readonly source: 'app' | 'platform';
|
|
375
|
+
readonly action: string;
|
|
376
|
+
/** Who did it: a person id, or `system:<appId>`. Stamped by the platform. */
|
|
377
|
+
readonly actor: string;
|
|
378
|
+
/** When, stamped by the platform. */
|
|
379
|
+
readonly at: string;
|
|
380
|
+
readonly entity?: string;
|
|
381
|
+
readonly recordId?: string;
|
|
382
|
+
/** The student the entry is about; it then follows that student's visibility (D25). */
|
|
383
|
+
readonly regardingStudentId?: string;
|
|
384
|
+
readonly summary: string;
|
|
385
|
+
readonly data?: {
|
|
386
|
+
readonly [key: string]: JsonValue;
|
|
387
|
+
};
|
|
388
|
+
};
|
|
389
|
+
export type AuditApi = {
|
|
390
|
+
/** Record an app event. The platform stamps actor and time; entries are append-only. */
|
|
391
|
+
record(input: {
|
|
392
|
+
readonly action: string;
|
|
393
|
+
readonly summary: string;
|
|
394
|
+
readonly entity?: string;
|
|
395
|
+
readonly recordId?: string;
|
|
396
|
+
/** The student this is about. NOT_PERMITTED unless you can see them. */
|
|
397
|
+
readonly regarding?: {
|
|
398
|
+
readonly studentId: string;
|
|
399
|
+
};
|
|
400
|
+
readonly data?: {
|
|
401
|
+
readonly [key: string]: JsonValue;
|
|
402
|
+
};
|
|
403
|
+
readonly idempotencyKey?: string;
|
|
404
|
+
}): Promise<AuditEntry>;
|
|
405
|
+
/**
|
|
406
|
+
* App-recorded entries only: platform entries (sensitive reads, notifications sent and so
|
|
407
|
+
* on) are never exposed through the SDK (D19). Staff see the app's entries; parents and
|
|
408
|
+
* students only their own. Entries about a student follow that student's visibility, for
|
|
409
|
+
* staff too (D25).
|
|
410
|
+
*/
|
|
411
|
+
list(options?: {
|
|
412
|
+
readonly entity?: string;
|
|
413
|
+
readonly recordId?: string;
|
|
414
|
+
readonly action?: string;
|
|
415
|
+
readonly limit?: number;
|
|
416
|
+
readonly cursor?: string;
|
|
417
|
+
}): Promise<Page<AuditEntry>>;
|
|
418
|
+
};
|
|
419
|
+
export type ClockApi = {
|
|
420
|
+
/** The current instant (ISO 8601, UTC). Follows the demo clock, not the wall clock (D14). */
|
|
421
|
+
now(): string;
|
|
422
|
+
/** Today's date (YYYY-MM-DD) in the school's timezone (D21). */
|
|
423
|
+
today(): string;
|
|
424
|
+
};
|
|
425
|
+
export type QuadSdk<A extends AppConfig = CurrentApp> = {
|
|
426
|
+
readonly version: string;
|
|
427
|
+
readonly appId: string;
|
|
428
|
+
readonly auth: AuthApi;
|
|
429
|
+
readonly school: SchoolApi<A>;
|
|
430
|
+
readonly db: DbApi<A>;
|
|
431
|
+
readonly notify: NotifyApi<A>;
|
|
432
|
+
readonly tasks: TasksApi<A>;
|
|
433
|
+
readonly schedule: ScheduleApi<A>;
|
|
434
|
+
readonly cron: CronApi;
|
|
435
|
+
readonly audit: AuditApi;
|
|
436
|
+
readonly clock: ClockApi;
|
|
437
|
+
};
|
|
438
|
+
export type { ContactPurpose, Recipient, Role };
|
|
439
|
+
/** A canonical record as this app sees it, e.g. `SchoolRecordOf<'students'>` (D23). */
|
|
440
|
+
export type SchoolRecordOf<N extends EntityName> = SchoolRecord<CurrentApp, N>;
|
|
441
|
+
/** A stored record of one of this app's own entities, e.g. `AppRecordOf<'greeting'>`. */
|
|
442
|
+
export type AppRecordOf<N extends keyof CurrentApp['schema'] & string> = RecordFor<CurrentApp, CurrentApp['schema'][N]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SDK_VERSION = '0.1.3';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AuditEntry } from '../contract/types.ts';
|
|
2
|
+
import { type App, type Ctx, type Kernel, type StoredAudit } from './state.ts';
|
|
3
|
+
export type AuditInput = {
|
|
4
|
+
source: 'app' | 'platform';
|
|
5
|
+
action: string;
|
|
6
|
+
actor: string;
|
|
7
|
+
summary: string;
|
|
8
|
+
visibility: 'app' | 'cio';
|
|
9
|
+
entity?: string;
|
|
10
|
+
recordId?: string;
|
|
11
|
+
/** The student an entry is about; it then follows that student's visibility (D25). */
|
|
12
|
+
regardingStudentId?: string;
|
|
13
|
+
data?: Record<string, unknown>;
|
|
14
|
+
};
|
|
15
|
+
export declare function appendAudit(k: Kernel, app: App, input: AuditInput): Promise<StoredAudit>;
|
|
16
|
+
export declare function auditView(e: StoredAudit): AuditEntry;
|
|
17
|
+
export declare function recordAudit(k: Kernel, ctx: Ctx, input: unknown): Promise<AuditEntry>;
|
|
18
|
+
/**
|
|
19
|
+
* App-recorded entries only (D19): platform entries (tasks, notifications, jobs, sensitive
|
|
20
|
+
* reads, suppressions) are never exposed through the SDK. Staff see the app's entries;
|
|
21
|
+
* parents and students only their own. An entry about a student also needs that student
|
|
22
|
+
* to be visible, staff included (D25). The system principal sees all app entries.
|
|
23
|
+
*/
|
|
24
|
+
export declare function listAudit(k: Kernel, ctx: Ctx, options: unknown): {
|
|
25
|
+
items: AuditEntry[];
|
|
26
|
+
nextCursor: string | null;
|
|
27
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { QuadError } from "../contract/errors.js";
|
|
2
|
+
import { runQuery } from "./query.js";
|
|
3
|
+
import { isStaffOrSystem, seesSubject, } from "./state.js";
|
|
4
|
+
import { isObject, jsonObject, onlyKeys, optionalStr, str } from "./values.js";
|
|
5
|
+
export async function appendAudit(k, app, input) {
|
|
6
|
+
const seq = (app.audit[app.audit.length - 1]?.seq ?? 0) + 1;
|
|
7
|
+
const entry = {
|
|
8
|
+
id: `aud-${String(seq).padStart(7, '0')}`,
|
|
9
|
+
seq,
|
|
10
|
+
appId: app.manifest.id,
|
|
11
|
+
at: k.nowIso(),
|
|
12
|
+
source: input.source,
|
|
13
|
+
action: input.action,
|
|
14
|
+
actor: input.actor,
|
|
15
|
+
summary: input.summary,
|
|
16
|
+
visibility: input.visibility,
|
|
17
|
+
...(input.entity !== undefined ? { entity: input.entity } : {}),
|
|
18
|
+
...(input.recordId !== undefined ? { recordId: input.recordId } : {}),
|
|
19
|
+
...(input.regardingStudentId !== undefined
|
|
20
|
+
? { regardingStudentId: input.regardingStudentId }
|
|
21
|
+
: {}),
|
|
22
|
+
...(input.data !== undefined ? { data: input.data } : {}),
|
|
23
|
+
};
|
|
24
|
+
app.audit.push(entry);
|
|
25
|
+
await k.persistPlatform(app, 'audit', [entry]);
|
|
26
|
+
return entry;
|
|
27
|
+
}
|
|
28
|
+
export function auditView(e) {
|
|
29
|
+
const { seq: _seq, visibility: _visibility, ...view } = e;
|
|
30
|
+
return view;
|
|
31
|
+
}
|
|
32
|
+
const ACTION_RE = /^[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*)*$/;
|
|
33
|
+
export async function recordAudit(k, ctx, input) {
|
|
34
|
+
if (!isObject(input))
|
|
35
|
+
throw new QuadError('VALIDATION', 'audit.record takes an object');
|
|
36
|
+
onlyKeys(input, ['action', 'summary', 'entity', 'recordId', 'regarding', 'data', 'idempotencyKey'], 'audit.record');
|
|
37
|
+
const key = k.idempotencyKey(input, 'audit.record');
|
|
38
|
+
if (key && ctx.app.keys.has(key)) {
|
|
39
|
+
const existing = ctx.app.audit.find((e) => e.id === ctx.app.keys.get(key));
|
|
40
|
+
if (existing)
|
|
41
|
+
return auditView(existing);
|
|
42
|
+
}
|
|
43
|
+
const action = str(input.action, 'action');
|
|
44
|
+
if (!ACTION_RE.test(action) || action.length > 100) {
|
|
45
|
+
throw new QuadError('VALIDATION', 'action must be dotted lowercase, e.g. greeting.approved');
|
|
46
|
+
}
|
|
47
|
+
const summary = str(input.summary, 'summary');
|
|
48
|
+
if (summary.length > 500)
|
|
49
|
+
throw new QuadError('VALIDATION', 'summary must be at most 500 characters');
|
|
50
|
+
const entity = optionalStr(input.entity, 'entity', 100);
|
|
51
|
+
const recordId = optionalStr(input.recordId, 'recordId', 200);
|
|
52
|
+
const data = jsonObject(input.data, 'data');
|
|
53
|
+
let regardingStudentId;
|
|
54
|
+
if (input.regarding !== undefined) {
|
|
55
|
+
if (!isObject(input.regarding))
|
|
56
|
+
throw new QuadError('VALIDATION', 'regarding must be { studentId }');
|
|
57
|
+
regardingStudentId = str(input.regarding.studentId, 'regarding.studentId');
|
|
58
|
+
if (!seesSubject(k, ctx, regardingStudentId)) {
|
|
59
|
+
throw new QuadError('NOT_PERMITTED', 'regarding: not a student you can see');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const entry = await appendAudit(k, ctx.app, {
|
|
63
|
+
source: 'app',
|
|
64
|
+
action,
|
|
65
|
+
actor: ctx.actorId,
|
|
66
|
+
summary,
|
|
67
|
+
visibility: 'app',
|
|
68
|
+
...(entity !== undefined ? { entity } : {}),
|
|
69
|
+
...(regardingStudentId !== undefined ? { regardingStudentId } : {}),
|
|
70
|
+
...(recordId !== undefined ? { recordId } : {}),
|
|
71
|
+
...(data !== undefined ? { data } : {}),
|
|
72
|
+
});
|
|
73
|
+
if (key)
|
|
74
|
+
await k.remember(ctx, key, entry.id);
|
|
75
|
+
return auditView(entry);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* App-recorded entries only (D19): platform entries (tasks, notifications, jobs, sensitive
|
|
79
|
+
* reads, suppressions) are never exposed through the SDK. Staff see the app's entries;
|
|
80
|
+
* parents and students only their own. An entry about a student also needs that student
|
|
81
|
+
* to be visible, staff included (D25). The system principal sees all app entries.
|
|
82
|
+
*/
|
|
83
|
+
export function listAudit(k, ctx, options) {
|
|
84
|
+
const opts = (isObject(options) ? options : {});
|
|
85
|
+
const { entity, recordId, action, limit, cursor } = opts;
|
|
86
|
+
const where = {};
|
|
87
|
+
if (entity !== undefined)
|
|
88
|
+
where.entity = str(entity, 'entity');
|
|
89
|
+
if (recordId !== undefined)
|
|
90
|
+
where.recordId = str(recordId, 'recordId');
|
|
91
|
+
if (action !== undefined)
|
|
92
|
+
where.action = str(action, 'action');
|
|
93
|
+
const visible = ctx.app.audit.filter((e) => e.source === 'app' &&
|
|
94
|
+
(isStaffOrSystem(ctx) || e.actor === ctx.actorId) &&
|
|
95
|
+
seesSubject(k, ctx, e.regardingStudentId));
|
|
96
|
+
const page = runQuery(visible, {
|
|
97
|
+
where,
|
|
98
|
+
...(limit !== undefined ? { limit } : {}),
|
|
99
|
+
...(cursor !== undefined ? { cursor } : {}),
|
|
100
|
+
}, new Set(['entity', 'recordId', 'action']));
|
|
101
|
+
return {
|
|
102
|
+
items: page.items.map(auditView),
|
|
103
|
+
nextCursor: page.nextCursor,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type Spec = {
|
|
2
|
+
minutes: number[];
|
|
3
|
+
hours: number[];
|
|
4
|
+
dom: Set<number>;
|
|
5
|
+
months: Set<number>;
|
|
6
|
+
dow: Set<number>;
|
|
7
|
+
domAny: boolean;
|
|
8
|
+
dowAny: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare function parseCron(expr: string): Spec;
|
|
11
|
+
/** UTC instant for a local wall-clock time in `tz`. */
|
|
12
|
+
export declare function zonedToUtc(y: number, mo: number, d: number, h: number, mi: number, tz: string): number;
|
|
13
|
+
/** The latest occurrence in (afterMs, upToMs], or null. Missed runs collapse to this one (D8). */
|
|
14
|
+
export declare function latestOccurrence(expr: string, tz: string, afterMs: number, upToMs: number): number | null;
|
|
15
|
+
/** The first occurrence strictly after `afterMs`, or null within a year and a bit. */
|
|
16
|
+
export declare function nextOccurrence(expr: string, tz: string, afterMs: number): number | null;
|
|
17
|
+
export {};
|