@cogover/sdk 0.11.0 → 0.12.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 +53 -12
- package/dist/email.d.ts +77 -0
- package/dist/email.js +190 -0
- package/dist/index.d.ts +2 -0
- package/dist/internal/job-runtime.js +4 -0
- package/dist/internal/messaging.js +75 -0
- package/dist/internal/trigger-runtime.js +5 -0
- package/dist/jobs.d.ts +4 -0
- package/dist/notifications.d.ts +56 -0
- package/dist/notifications.js +92 -0
- package/dist/router.js +2 -0
- package/dist/script.d.ts +4 -0
- package/dist/script.js +4 -0
- package/dist/trigger.d.ts +6 -0
- package/docs/en/api-reference.md +341 -16
- package/docs/en/usage-guide.md +203 -7
- package/package.json +1 -1
package/dist/router.js
CHANGED
package/dist/script.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ import type { EffectiveWorkspaceObjects } from "./workspace.js";
|
|
|
7
7
|
import type { ProjectState } from "./state.js";
|
|
8
8
|
import type { DistributedLocks } from "./locks.js";
|
|
9
9
|
import type { PushApi } from "./push.js";
|
|
10
|
+
import type { NotificationsApi } from "./notifications.js";
|
|
11
|
+
import type { EmailApi } from "./email.js";
|
|
10
12
|
import type { InvocationContext } from "./invocation.js";
|
|
11
13
|
import type { JobsApi } from "./jobs.js";
|
|
12
14
|
import type { SecretsApi } from "./secrets.js";
|
|
@@ -23,6 +25,8 @@ export interface ScriptContext<TInput, TSchema extends object = EffectiveWorkspa
|
|
|
23
25
|
readonly state: ProjectState;
|
|
24
26
|
readonly locks: DistributedLocks;
|
|
25
27
|
readonly push: PushApi<TSchema>;
|
|
28
|
+
readonly notifications: NotificationsApi;
|
|
29
|
+
readonly email: EmailApi<TSchema>;
|
|
26
30
|
readonly jobs: JobsApi;
|
|
27
31
|
readonly secrets: SecretsApi;
|
|
28
32
|
readonly crypto: CryptoApi;
|
package/dist/script.js
CHANGED
|
@@ -8,6 +8,8 @@ import { createSchemaApi } from "./schema.js";
|
|
|
8
8
|
import { createStateApi } from "./state.js";
|
|
9
9
|
import { createLocksApi } from "./locks.js";
|
|
10
10
|
import { createPushApi } from "./push.js";
|
|
11
|
+
import { createNotificationsApi } from "./notifications.js";
|
|
12
|
+
import { createEmailApi } from "./email.js";
|
|
11
13
|
import { installCogoverFetch } from "./fetch.js";
|
|
12
14
|
import { invocationContext } from "./internal/invocation.js";
|
|
13
15
|
import { createJobsApi } from "./internal/jobs-api.js";
|
|
@@ -49,6 +51,8 @@ export function createSandboxHandler(handler) {
|
|
|
49
51
|
state: createStateApi(),
|
|
50
52
|
locks: createLocksApi(),
|
|
51
53
|
push: createPushApi(),
|
|
54
|
+
notifications: createNotificationsApi(),
|
|
55
|
+
email: createEmailApi(),
|
|
52
56
|
jobs: createJobsApi(),
|
|
53
57
|
secrets: createSecretsApi(),
|
|
54
58
|
crypto,
|
package/dist/trigger.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import type { SecretsApi } from "./secrets.js";
|
|
|
6
6
|
import type { InvocationContext } from "./invocation.js";
|
|
7
7
|
import type { ScriptLogger } from "./logger.js";
|
|
8
8
|
import type { PushApi } from "./push.js";
|
|
9
|
+
import type { NotificationsApi } from "./notifications.js";
|
|
10
|
+
import type { EmailApi } from "./email.js";
|
|
9
11
|
import type { FilterOperator } from "./query.js";
|
|
10
12
|
import type { CogoverRecordId, UpdateFields } from "./records.js";
|
|
11
13
|
import type { SchemaApi } from "./schema.js";
|
|
@@ -121,6 +123,10 @@ export interface TriggerContext<TObject extends string = string, TOperation exte
|
|
|
121
123
|
readonly log: ScriptLogger;
|
|
122
124
|
/** Refused with `TRIGGER_READ_ONLY` in a before-change trigger. */
|
|
123
125
|
readonly push: PushApi<TSchema>;
|
|
126
|
+
/** Refused with `TRIGGER_READ_ONLY` in a before-change trigger. */
|
|
127
|
+
readonly notifications: NotificationsApi;
|
|
128
|
+
/** Refused with `TRIGGER_READ_ONLY` in a before-change trigger. */
|
|
129
|
+
readonly email: EmailApi<TSchema>;
|
|
124
130
|
/** Available in after-change triggers; a before-change trigger is refused with `PermissionDeniedError`. */
|
|
125
131
|
readonly jobs: JobsApi;
|
|
126
132
|
/** Available in after-change triggers; a before-change trigger is refused with `PermissionDeniedError`. */
|
package/docs/en/api-reference.md
CHANGED
|
@@ -9,7 +9,8 @@ configured for the project.
|
|
|
9
9
|
|
|
10
10
|
`defineScript<Input, Output>(handler)` wraps an async Cogover script. It parses
|
|
11
11
|
the input JSON, supplies `input`, `request`, `invocation`, `response`, `data`, `schema`, `log`, `state`, `locks`,
|
|
12
|
-
`push`, `jobs`, `secrets`, `crypto`, and `org`, awaits the handler, and serializes its
|
|
12
|
+
`push`, `notifications`, `email`, `jobs`, `secrets`, `crypto`, and `org`, awaits the handler, and serializes its
|
|
13
|
+
result.
|
|
13
14
|
|
|
14
15
|
```typescript
|
|
15
16
|
function defineScript<
|
|
@@ -40,6 +41,8 @@ interface ScriptContext<Input, Schema extends object = EffectiveWorkspaceObjects
|
|
|
40
41
|
readonly state: ProjectState;
|
|
41
42
|
readonly locks: DistributedLocks;
|
|
42
43
|
readonly push: PushApi<Schema>;
|
|
44
|
+
readonly notifications: NotificationsApi;
|
|
45
|
+
readonly email: EmailApi<Schema>;
|
|
43
46
|
readonly jobs: JobsApi;
|
|
44
47
|
readonly secrets: SecretsApi;
|
|
45
48
|
readonly crypto: CryptoApi;
|
|
@@ -54,7 +57,8 @@ Returning `null` produces a null handler result; returning `undefined` is invali
|
|
|
54
57
|
## HTTP routers and request context
|
|
55
58
|
|
|
56
59
|
`ScriptContext<TInput, TSchema>` provides `input`, `request`, `invocation`, `response`, `data`, `schema`,
|
|
57
|
-
`log`, `state`, `locks`, `push`, `jobs`, `secrets`, `crypto`, and `org` to both script
|
|
60
|
+
`log`, `state`, `locks`, `push`, `notifications`, `email`, `jobs`, `secrets`, `crypto`, and `org` to both script
|
|
61
|
+
and route handlers. `input`
|
|
58
62
|
preserves the invocation input. Prefer `request.body` for business data: it excludes invocation
|
|
59
63
|
metadata and transport/authentication fields removed by Cogover.
|
|
60
64
|
|
|
@@ -538,6 +542,8 @@ interface TriggerContext<
|
|
|
538
542
|
readonly schema: SchemaApi<Schema>;
|
|
539
543
|
readonly log: ScriptLogger;
|
|
540
544
|
readonly push: PushApi<Schema>;
|
|
545
|
+
readonly notifications: NotificationsApi;
|
|
546
|
+
readonly email: EmailApi<Schema>;
|
|
541
547
|
readonly jobs: JobsApi;
|
|
542
548
|
readonly secrets: SecretsApi;
|
|
543
549
|
readonly crypto: CryptoApi;
|
|
@@ -577,9 +583,10 @@ receives all records of the same change that run the trigger, up to 200 records
|
|
|
577
583
|
once per record. Keep `fields` short, and read related data for the whole list with
|
|
578
584
|
one `records.getMany` call rather than one read per record.
|
|
579
585
|
|
|
580
|
-
`data`, `schema`, `log`, `invocation`, `push`, `
|
|
581
|
-
the same APIs that scripts receive. A trigger context has no `input`,
|
|
582
|
-
`state`, or `locks`; `push`
|
|
586
|
+
`data`, `schema`, `log`, `invocation`, `push`, `notifications`, `email`, `jobs`, `secrets`,
|
|
587
|
+
`crypto`, and `org` are the same APIs that scripts receive. A trigger context has no `input`,
|
|
588
|
+
`request`, `response`, `state`, or `locks`; `push`, `notifications`, and `email` are refused in a
|
|
589
|
+
before-change trigger. `trigger.operation` is
|
|
583
590
|
the operation of the current call, `trigger.id` identifies the trigger registration, and
|
|
584
591
|
`trigger.changeId` identifies the record change that caused the call.
|
|
585
592
|
|
|
@@ -658,8 +665,9 @@ merges, and cascading deletes — do not run before-change triggers, and neither
|
|
|
658
665
|
a request that only validates input without saving.
|
|
659
666
|
|
|
660
667
|
- **Read-only.** A before-change trigger may read records and schema, but every
|
|
661
|
-
record write, `fetch`, lock, state write, push message,
|
|
662
|
-
and every `crypto` operation with a
|
|
668
|
+
record write, `fetch`, lock, state write, push message, notification, email (including
|
|
669
|
+
`email.senders()`), `jobs.enqueue`, `secrets.get`, and every `crypto` operation with a
|
|
670
|
+
`{ secret }` key is refused with
|
|
663
671
|
`PermissionDeniedError` whose `details.reason` is `"TRIGGER_READ_ONLY"`. This
|
|
664
672
|
applies to every identity, including `data.asSystem()`. Every `crypto` operation
|
|
665
673
|
with a key given in the call remains available, as do `crypto.sha256`,
|
|
@@ -726,9 +734,9 @@ export const triggers = [
|
|
|
726
734
|
were saved, including the record ID and values assigned while saving, such as an
|
|
727
735
|
auto number. For a deleted record `record.new` is `null` and `record.old` holds the
|
|
728
736
|
last values.
|
|
729
|
-
- **May write and call out.** Record writes, `fetch`, `push`, `
|
|
730
|
-
`secrets.get`, and `crypto` work as they do in a script, within the
|
|
731
|
-
limits. `data.object()` acts as the user who made the change, and the other identity
|
|
737
|
+
- **May write and call out.** Record writes, `fetch`, `push`, `notifications`, `email`,
|
|
738
|
+
`jobs.enqueue`, `secrets.get`, and `crypto` work as they do in a script, within the
|
|
739
|
+
same runtime limits. `data.object()` acts as the user who made the change, and the other identity
|
|
732
740
|
rules of before-change triggers apply. The trigger context has no `state` or `locks`;
|
|
733
741
|
`push` is the usual way to refresh the records the handler changed for everyone who
|
|
734
742
|
has them open, and longer work, or work that needs `state` or `locks`, belongs in a
|
|
@@ -911,6 +919,8 @@ interface JobContext<Payload = unknown, Schema extends object = EffectiveWorkspa
|
|
|
911
919
|
readonly log: ScriptLogger;
|
|
912
920
|
readonly state: ProjectState;
|
|
913
921
|
readonly locks: DistributedLocks;
|
|
922
|
+
readonly notifications: NotificationsApi;
|
|
923
|
+
readonly email: EmailApi<Schema>;
|
|
914
924
|
readonly jobs: JobsApi;
|
|
915
925
|
readonly secrets: SecretsApi;
|
|
916
926
|
readonly crypto: CryptoApi;
|
|
@@ -929,9 +939,9 @@ interface JobInfo {
|
|
|
929
939
|
```
|
|
930
940
|
|
|
931
941
|
A handler may be synchronous or asynchronous; its return value is ignored. `data`,
|
|
932
|
-
`schema`, `log`, `invocation`, `state`, `locks`, `
|
|
933
|
-
are the same APIs that scripts receive. A job context has
|
|
934
|
-
`response`.
|
|
942
|
+
`schema`, `log`, `invocation`, `state`, `locks`, `notifications`, `email`, `jobs`,
|
|
943
|
+
`secrets`, `crypto`, and `org` are the same APIs that scripts receive. A job context has
|
|
944
|
+
no `input`, `request`, `response`, or `push`.
|
|
935
945
|
|
|
936
946
|
- `job.id` identifies the run and stays the same across its attempts; use it as the
|
|
937
947
|
idempotency key of external calls. `job.attempt` starts at 1.
|
|
@@ -997,9 +1007,9 @@ Workspace, the error is `CogoverApiError` with `code: "JOBS_DISABLED"`.
|
|
|
997
1007
|
policy sets `allowInternalSystem: true`; otherwise its calls throw
|
|
998
1008
|
`PermissionDeniedError` with `details.reason` `"IDENTITY_NOT_GRANTED"`.
|
|
999
1009
|
`data.asUser()` and `data.asSystem()` follow the identity policy as usual.
|
|
1000
|
-
- **Capabilities.** A job may
|
|
1001
|
-
|
|
1002
|
-
|
|
1010
|
+
- **Capabilities.** A job may read and write records, call `fetch`, use `state` and
|
|
1011
|
+
`locks`, send notifications and email, enqueue jobs, read secrets, and use `crypto`,
|
|
1012
|
+
within the runtime limits of the job lane.
|
|
1003
1013
|
- **Time.** Each attempt must finish within `timeoutMs`. Work that needs longer is
|
|
1004
1014
|
split: process one page, save the cursor, and enqueue the same job again with the
|
|
1005
1015
|
cursor in the payload, as in the example above.
|
|
@@ -2106,6 +2116,313 @@ custom component listening for it. `content` is required (up to 16,384 character
|
|
|
2106
2116
|
- A sent message cannot be recalled, and retrying a failed execution may send it
|
|
2107
2117
|
again.
|
|
2108
2118
|
|
|
2119
|
+
## Notifications
|
|
2120
|
+
|
|
2121
|
+
`notifications` saves a notification in the notification list (the bell) of each
|
|
2122
|
+
recipient and delivers it in the web app, by web push, by mobile push, and as an
|
|
2123
|
+
email copy, as the workspace's notification channels decide. Unlike a
|
|
2124
|
+
[push message](#push-messages), a notification stays in the list after it was
|
|
2125
|
+
delivered, so it suits something a person must act on later, such as an approval
|
|
2126
|
+
request. Delivery is asynchronous and best-effort: a call resolves once Cogover has
|
|
2127
|
+
accepted the notification, never when it reaches a device or an inbox.
|
|
2128
|
+
|
|
2129
|
+
```typescript
|
|
2130
|
+
type NotificationContentType = "text" | "html";
|
|
2131
|
+
type NotificationDisplay = "SIMPLE" | "FULL";
|
|
2132
|
+
type NotificationSender = "workspace" | "actor";
|
|
2133
|
+
type NotificationLinkTarget = "SAME_TAB" | "NEW_TAB";
|
|
2134
|
+
|
|
2135
|
+
interface NotificationLink {
|
|
2136
|
+
readonly url: string;
|
|
2137
|
+
readonly openIn?: NotificationLinkTarget;
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
interface NotificationMessage {
|
|
2141
|
+
readonly to: readonly string[];
|
|
2142
|
+
readonly exclude?: readonly string[];
|
|
2143
|
+
readonly title: string;
|
|
2144
|
+
readonly content: string;
|
|
2145
|
+
readonly contentType?: NotificationContentType;
|
|
2146
|
+
readonly subtitle?: string;
|
|
2147
|
+
readonly display?: NotificationDisplay;
|
|
2148
|
+
readonly link?: NotificationLink;
|
|
2149
|
+
readonly sender?: NotificationSender;
|
|
2150
|
+
readonly channel?: string;
|
|
2151
|
+
readonly email?: boolean;
|
|
2152
|
+
readonly idempotencyKey?: string;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
interface NotificationResult {
|
|
2156
|
+
readonly requestId: string;
|
|
2157
|
+
readonly notified: number;
|
|
2158
|
+
readonly skippedPersonnelIds: readonly string[];
|
|
2159
|
+
readonly duplicate: boolean;
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
interface NotificationsApi {
|
|
2163
|
+
send(message: NotificationMessage): Promise<NotificationResult>;
|
|
2164
|
+
}
|
|
2165
|
+
```
|
|
2166
|
+
|
|
2167
|
+
```typescript
|
|
2168
|
+
const result = await notifications.send({
|
|
2169
|
+
to: approverIds,
|
|
2170
|
+
exclude: ["actor"],
|
|
2171
|
+
title: "Leave request waiting for your approval",
|
|
2172
|
+
content: "A leave request of 3 days was submitted and needs your decision.",
|
|
2173
|
+
link: { url: `/app/leave_request/${recordId}` },
|
|
2174
|
+
channel: approvalChannelId,
|
|
2175
|
+
idempotencyKey: `leave-approval:${recordId}`,
|
|
2176
|
+
});
|
|
2177
|
+
// result.notified: accounts addressed; result.skippedPersonnelIds: personnel without an account
|
|
2178
|
+
```
|
|
2179
|
+
|
|
2180
|
+
### `notifications.send(message): Promise<NotificationResult>`
|
|
2181
|
+
|
|
2182
|
+
**Recipients.** `to` lists 1 to 200 personnel IDs; duplicates are removed. The
|
|
2183
|
+
notification goes to the user account of each personnel. Personnel without a user
|
|
2184
|
+
account are skipped and returned in `skippedPersonnelIds`, and `notified` is the
|
|
2185
|
+
number of accounts the notification was addressed to after exclusions. `exclude`
|
|
2186
|
+
lists personnel IDs that never receive it; the special value `"actor"` is the user
|
|
2187
|
+
whose action started the current execution (the caller of an HTTP route, the user
|
|
2188
|
+
whose save ran a trigger, the user whose call enqueued a job run). An execution
|
|
2189
|
+
without a user has no actor, so `"actor"` excludes nobody. A personnel ID in `to`
|
|
2190
|
+
that does not exist throws `NotFoundError` with `resource` `"personnel"` before
|
|
2191
|
+
anything is sent. When every recipient is excluded or skipped, nothing is sent and
|
|
2192
|
+
`notified` is `0`.
|
|
2193
|
+
|
|
2194
|
+
**Content and appearance.**
|
|
2195
|
+
|
|
2196
|
+
- `title` (required, at most 200 characters) is the heading of the notification and
|
|
2197
|
+
the subject of its email copy.
|
|
2198
|
+
- `content` (required, at most 16,384 characters) is the body. With
|
|
2199
|
+
`contentType: "text"`, the default, it is plain text. With `"html"` it is reduced
|
|
2200
|
+
to basic formatting before it is stored — paragraphs, headings, lists, tables,
|
|
2201
|
+
links, and emphasis; scripts, styles, and images are removed — and it must still
|
|
2202
|
+
contain visible text, otherwise `ValidationError` is thrown.
|
|
2203
|
+
- `subtitle` (at most 200 characters) is shown only in the `"FULL"` display.
|
|
2204
|
+
`display` defaults to `"FULL"` when a subtitle is given and to `"SIMPLE"`
|
|
2205
|
+
otherwise; a subtitle together with `display: "SIMPLE"` throws `ValidationError`.
|
|
2206
|
+
- `link` is the page opened when the recipient clicks the notification. `url` is a
|
|
2207
|
+
path of the Cogover web app starting with `/`, or an absolute `http` or `https`
|
|
2208
|
+
URL, at most 2,048 characters. `openIn` is `"SAME_TAB"` or `"NEW_TAB"`; it defaults
|
|
2209
|
+
to `"SAME_TAB"` for a path and to `"NEW_TAB"` for an absolute URL.
|
|
2210
|
+
- `sender` is `"workspace"`, the default, or `"actor"`, which shows the name and
|
|
2211
|
+
avatar of the user whose action started the execution. `"actor"` in an execution
|
|
2212
|
+
without a user throws `ValidationError`.
|
|
2213
|
+
|
|
2214
|
+
**Channels and the email copy.** `channel` is the record ID of one of the
|
|
2215
|
+
workspace's notification channels. The channel decides which deliveries are enabled
|
|
2216
|
+
— in the web app, web push, mobile push, and email — and each user may override that
|
|
2217
|
+
choice in their own notification settings. Without `channel`, every delivery is
|
|
2218
|
+
enabled. An unknown channel throws `NotFoundError` with `resource`
|
|
2219
|
+
`"notificationChannel"`; a workspace in which notifications are not set up throws
|
|
2220
|
+
`NotFoundError` with `resource` `"object"` and `resourceId` `"notification_channel"`.
|
|
2221
|
+
|
|
2222
|
+
`email: false` leaves out the email copy even when the channel enables email. The
|
|
2223
|
+
copy uses `title` as its subject and `content` as its body, is sent from a Cogover
|
|
2224
|
+
notification address rather than from a workspace mailbox, and counts against the
|
|
2225
|
+
daily notification-email limit of the workspace's plan. To send an email from a
|
|
2226
|
+
workspace mailbox, use [`email.send`](#email).
|
|
2227
|
+
|
|
2228
|
+
**Idempotency.** `idempotencyKey` starts with a letter or digit and contains at most
|
|
2229
|
+
128 letters, digits, `.`, `_`, `:`, or `-`. A call whose key the project already
|
|
2230
|
+
used for a notification in the last 7 days sends nothing and returns the first
|
|
2231
|
+
call's result with `duplicate: true`. A call made while the first call with the same
|
|
2232
|
+
key is still sending throws `CogoverApiError` with `code: "DUPLICATE_IN_PROGRESS"`;
|
|
2233
|
+
retry it a moment later. A call that failed does not keep its key, so it can be
|
|
2234
|
+
retried. Keys belong to the project and are separate from the keys of `email.send`.
|
|
2235
|
+
In an after-change trigger or a job, which may run more than once, derive the key
|
|
2236
|
+
from `trigger.changeId` and `record.id`, or from `job.id` or a business key.
|
|
2237
|
+
|
|
2238
|
+
### Rules
|
|
2239
|
+
|
|
2240
|
+
- `notifications` is available in scripts, routes, after-change triggers, background
|
|
2241
|
+
jobs, inbound webhook calls, and Development Sessions with write permission. A
|
|
2242
|
+
before-change trigger is read-only and receives `PermissionDeniedError` with
|
|
2243
|
+
`details.reason` `"TRIGGER_READ_ONLY"`; a read-only preview and a read-only
|
|
2244
|
+
Development Session refuse it too.
|
|
2245
|
+
- Notifications need no grant in the identity policy: a project may notify any
|
|
2246
|
+
personnel of the workspace.
|
|
2247
|
+
- A project may address at most 5,000 recipients per hour by default. A call that
|
|
2248
|
+
would exceed this throws `RateLimitError` and sends nothing.
|
|
2249
|
+
- Inputs are validated by the SDK and again by Cogover; invalid input throws
|
|
2250
|
+
`ValidationError` before anything is sent. Each call counts as one capability call.
|
|
2251
|
+
- When notifications are switched off for the deployment, every call throws
|
|
2252
|
+
`CogoverApiError` with code `NOTIFICATIONS_DISABLED`.
|
|
2253
|
+
- A sent notification cannot be recalled. Retrying a failed execution may notify
|
|
2254
|
+
again unless the call uses an `idempotencyKey`.
|
|
2255
|
+
|
|
2256
|
+
## Email
|
|
2257
|
+
|
|
2258
|
+
`email` sends email from the workspace's mailboxes: a shared workspace mailbox, or
|
|
2259
|
+
the default personal mailbox of the user whose action started the execution. The
|
|
2260
|
+
Project's approved identity policy decides which mailboxes a project may use. With
|
|
2261
|
+
`record`, the email is also logged as an email activity on that record's timeline,
|
|
2262
|
+
like an email sent from the record page. Sending is asynchronous: a call resolves
|
|
2263
|
+
once Cogover has accepted the email for delivery, and a delivery failure after that
|
|
2264
|
+
is not reported back to the script.
|
|
2265
|
+
|
|
2266
|
+
```typescript
|
|
2267
|
+
type EmailSender = { readonly mailbox: string } | "actor";
|
|
2268
|
+
|
|
2269
|
+
type EmailRecipient =
|
|
2270
|
+
| string
|
|
2271
|
+
| { readonly email: string; readonly name?: string }
|
|
2272
|
+
| { readonly personnelId: string };
|
|
2273
|
+
|
|
2274
|
+
interface EmailRecordLink<ObjectSlug extends string = string> {
|
|
2275
|
+
readonly object: ObjectSlug;
|
|
2276
|
+
readonly recordId: string;
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
interface EmailAttachment<ObjectSlug extends string = string> {
|
|
2280
|
+
readonly object: ObjectSlug;
|
|
2281
|
+
readonly recordId: string;
|
|
2282
|
+
readonly field: string;
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2285
|
+
type EmailMessage<ObjectSlug extends string = string> = {
|
|
2286
|
+
readonly from: EmailSender;
|
|
2287
|
+
readonly to?: readonly EmailRecipient[];
|
|
2288
|
+
readonly cc?: readonly EmailRecipient[];
|
|
2289
|
+
readonly bcc?: readonly EmailRecipient[];
|
|
2290
|
+
readonly subject: string;
|
|
2291
|
+
readonly record?: EmailRecordLink<ObjectSlug>;
|
|
2292
|
+
readonly recordEmailFields?: readonly string[];
|
|
2293
|
+
readonly attachments?: readonly EmailAttachment<ObjectSlug>[];
|
|
2294
|
+
readonly appendSignature?: boolean;
|
|
2295
|
+
readonly idempotencyKey?: string;
|
|
2296
|
+
} & (
|
|
2297
|
+
| { readonly html: string; readonly text?: never }
|
|
2298
|
+
| { readonly text: string; readonly html?: never }
|
|
2299
|
+
);
|
|
2300
|
+
|
|
2301
|
+
type EmailDelivery = "activity" | "direct";
|
|
2302
|
+
|
|
2303
|
+
interface EmailSendResult {
|
|
2304
|
+
readonly requestId: string;
|
|
2305
|
+
readonly delivery: EmailDelivery;
|
|
2306
|
+
readonly recipients: number;
|
|
2307
|
+
readonly duplicate: boolean;
|
|
2308
|
+
}
|
|
2309
|
+
|
|
2310
|
+
type EmailSenderKind = "workspace" | "actor";
|
|
2311
|
+
|
|
2312
|
+
interface EmailSenderInfo {
|
|
2313
|
+
readonly id: string;
|
|
2314
|
+
readonly kind: EmailSenderKind;
|
|
2315
|
+
readonly email: string;
|
|
2316
|
+
readonly displayName: string;
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
interface EmailApi<Schema extends object = EffectiveWorkspaceObjects> {
|
|
2320
|
+
send(message: EmailMessage<StringKeyOf<Schema>>): Promise<EmailSendResult>;
|
|
2321
|
+
senders(): Promise<readonly EmailSenderInfo[]>;
|
|
2322
|
+
}
|
|
2323
|
+
```
|
|
2324
|
+
|
|
2325
|
+
```typescript
|
|
2326
|
+
const result = await email.send({
|
|
2327
|
+
from: { mailbox: "EMWXXXXXXXXXXXX" },
|
|
2328
|
+
bcc: ["sales-archive@example.com"],
|
|
2329
|
+
subject: `Quote ${quote.fields.code}`,
|
|
2330
|
+
html: "<p>Dear customer,</p><p>Please find our quote attached.</p>",
|
|
2331
|
+
record: { object: "quote", recordId: quote.id },
|
|
2332
|
+
recordEmailFields: ["contact_email"],
|
|
2333
|
+
attachments: [{ object: "quote", recordId: quote.id, field: "quote_pdf" }],
|
|
2334
|
+
idempotencyKey: `quote-email:${quote.id}`,
|
|
2335
|
+
});
|
|
2336
|
+
// result.delivery === "activity": the email appears on the quote's timeline.
|
|
2337
|
+
```
|
|
2338
|
+
|
|
2339
|
+
### `email.send(message): Promise<EmailSendResult>`
|
|
2340
|
+
|
|
2341
|
+
**Sender.** `from: { mailbox: id }` sends from a shared workspace mailbox;
|
|
2342
|
+
`from: "actor"` sends from the default personal mailbox of the user whose action
|
|
2343
|
+
started the execution. The sender must be allowed by the `email` section of the
|
|
2344
|
+
Project's approved identity policy: `workspaceMailboxIds` lists the shared mailboxes
|
|
2345
|
+
and `allowActorMailbox` allows `"actor"`. Like the rest of the policy, the section
|
|
2346
|
+
applies only to the callers selected by `callerPersonnelIds`, and to executions
|
|
2347
|
+
without a user only when the policy sets `allowInternalSystem: true`. A project
|
|
2348
|
+
whose policy has no `email` section cannot send email. A sender that is not allowed
|
|
2349
|
+
throws `PermissionDeniedError` with `details.reason` `"EMAIL_SENDER_NOT_GRANTED"`;
|
|
2350
|
+
this check comes before anything else is looked up. A mailbox that does not exist or
|
|
2351
|
+
is deactivated, and an actor without an active personal mailbox, throw
|
|
2352
|
+
`NotFoundError` with `resource` `"mailbox"`. `"actor"` in an execution without a
|
|
2353
|
+
user throws `ValidationError`.
|
|
2354
|
+
|
|
2355
|
+
**Recipients.**
|
|
2356
|
+
|
|
2357
|
+
- `to`, `cc`, and `bcc` accept an address string, `{ email, name? }`, or
|
|
2358
|
+
`{ personnelId }`. A personnel is resolved to their work email, or to the email of
|
|
2359
|
+
their user account when there is no work email; a personnel with neither throws
|
|
2360
|
+
`ValidationError`, and an unknown personnel throws `NotFoundError` with `resource`
|
|
2361
|
+
`"personnel"`.
|
|
2362
|
+
- Duplicates are removed within each list, ignoring case. The three lists together
|
|
2363
|
+
accept at most 50 recipients. A `name` is a single line of at most 200 characters.
|
|
2364
|
+
- `to` must name at least one recipient unless `recordEmailFields` is given.
|
|
2365
|
+
|
|
2366
|
+
**Content.** `subject` is required: a single line of at most 255 characters. Provide
|
|
2367
|
+
exactly one of `html` and `text`; the body is at most 192 KB (196,608 bytes) in
|
|
2368
|
+
UTF-8.
|
|
2369
|
+
|
|
2370
|
+
**Record.** `record: { object, recordId }` logs the email as an email activity on
|
|
2371
|
+
that record's timeline: Cogover tracks opens and link clicks and threads replies
|
|
2372
|
+
with it, and `result.delivery` is `"activity"`. The record must be readable by the
|
|
2373
|
+
default identity of the execution, otherwise `NotFoundError` with `resource`
|
|
2374
|
+
`"record"` is thrown. Without `record`, the email is only sent from the mailbox and
|
|
2375
|
+
`delivery` is `"direct"`. The following options require `record`; without it they
|
|
2376
|
+
throw `ValidationError`:
|
|
2377
|
+
|
|
2378
|
+
- `recordEmailFields` lists up to 20 email fields of that record whose addresses are
|
|
2379
|
+
added to `to`. A field that is not an email field throws `ValidationError`.
|
|
2380
|
+
- `attachments` attaches every file stored in the file field `field` of each listed
|
|
2381
|
+
record: at most 10 files and 20 MB in total. The records must be readable by the
|
|
2382
|
+
default identity; a field that is not a file field throws `ValidationError`.
|
|
2383
|
+
- `appendSignature: true` appends the sender's default email signature. It requires a
|
|
2384
|
+
personal mailbox, that is `from: "actor"`; otherwise `ValidationError` is thrown.
|
|
2385
|
+
|
|
2386
|
+
**Result.** `requestId` identifies the send. `recipients` counts the addresses in
|
|
2387
|
+
`to`, `cc`, and `bcc` after duplicates were removed; the addresses of
|
|
2388
|
+
`recordEmailFields` are resolved later and are not counted. `duplicate` is `true`
|
|
2389
|
+
when an earlier call with the same `idempotencyKey` already sent the email.
|
|
2390
|
+
|
|
2391
|
+
**Idempotency.** `idempotencyKey` follows the same rules as for
|
|
2392
|
+
[notifications](#notifications): a retry with the same key within 7 days returns
|
|
2393
|
+
the first result with `duplicate: true` instead of sending again, and a retry while
|
|
2394
|
+
the first call is still sending throws `CogoverApiError` with
|
|
2395
|
+
`code: "DUPLICATE_IN_PROGRESS"`. Email keys are separate from notification keys.
|
|
2396
|
+
|
|
2397
|
+
### `email.senders(): Promise<readonly EmailSenderInfo[]>`
|
|
2398
|
+
|
|
2399
|
+
Returns the active mailboxes this execution may send from: one `kind: "workspace"`
|
|
2400
|
+
entry for each active shared mailbox that the applicable policy lists in
|
|
2401
|
+
`workspaceMailboxIds`, to be used as `{ mailbox: id }`, and, when the policy sets
|
|
2402
|
+
`allowActorMailbox` and the actor has an active personal mailbox, at most one
|
|
2403
|
+
`kind: "actor"` entry describing the mailbox that `from: "actor"` uses. The list is
|
|
2404
|
+
empty when the policy grants this execution no sender. Use it to show the available
|
|
2405
|
+
senders or to pick one by `email` address instead of hard-coding an ID. It sends
|
|
2406
|
+
nothing, so it also works in a read-only preview and a read-only Development
|
|
2407
|
+
Session.
|
|
2408
|
+
|
|
2409
|
+
### Rules
|
|
2410
|
+
|
|
2411
|
+
- `email` is available in scripts, routes, after-change triggers, background jobs,
|
|
2412
|
+
inbound webhook calls, and Development Sessions. A before-change trigger receives
|
|
2413
|
+
`PermissionDeniedError` with `details.reason` `"TRIGGER_READ_ONLY"` for both
|
|
2414
|
+
methods; a read-only preview and a read-only Development Session refuse
|
|
2415
|
+
`email.send`.
|
|
2416
|
+
- A project may send at most 500 emails per hour by default; beyond that,
|
|
2417
|
+
`RateLimitError` is thrown and nothing is sent. One call is one email, however many
|
|
2418
|
+
recipients it has.
|
|
2419
|
+
- Inputs are validated by the SDK and again by Cogover; invalid input throws
|
|
2420
|
+
`ValidationError` before anything is sent. Each call counts as one capability call.
|
|
2421
|
+
- When email is switched off for the deployment, every call throws
|
|
2422
|
+
`CogoverApiError` with code `EMAIL_DISABLED`.
|
|
2423
|
+
- A sent email cannot be recalled. Retrying a failed execution may send it again
|
|
2424
|
+
unless the call uses an `idempotencyKey`.
|
|
2425
|
+
|
|
2109
2426
|
## Organization structure
|
|
2110
2427
|
|
|
2111
2428
|
`org` reads the departments, positions and personnel of the workspace: who works in
|
|
@@ -2526,6 +2843,14 @@ the feature is not enabled for the Workspace. `crypto.aesDecrypt` and
|
|
|
2526
2843
|
`crypto.rsaDecrypt` throw `CogoverApiError` with `code: "DECRYPTION_FAILED"` when
|
|
2527
2844
|
the ciphertext cannot be decrypted.
|
|
2528
2845
|
|
|
2846
|
+
`notifications.send` and `email.send` fail with `ValidationError`, `NotFoundError`,
|
|
2847
|
+
`PermissionDeniedError`, or `RateLimitError` as described in their sections, with
|
|
2848
|
+
`CogoverApiError` whose `code` is `NOTIFICATIONS_DISABLED` or `EMAIL_DISABLED` when
|
|
2849
|
+
the feature is switched off, and with `CogoverApiError` whose `code` is
|
|
2850
|
+
`DUPLICATE_IN_PROGRESS` while an earlier call with the same `idempotencyKey` is
|
|
2851
|
+
still sending. A sender mailbox that the Project policy does not allow is reported
|
|
2852
|
+
as `PermissionDeniedError` with `details.reason` `"EMAIL_SENDER_NOT_GRANTED"`.
|
|
2853
|
+
|
|
2529
2854
|
Scripts can catch `CogoverApiError`, inspect `error.r`, and return their own client
|
|
2530
2855
|
error, such as `{ r: 1001, msg: "You cannot update this order." }`, without exposing
|
|
2531
2856
|
the original code or message. A script's returned JSON remains its own response
|