@cogover/sdk 0.12.0 → 0.12.1
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 +12 -9
- package/dist/crypto.js +89 -46
- package/dist/data.js +4 -1
- package/dist/email.js +5 -3
- package/dist/internal/base64.js +76 -27
- package/dist/internal/bridge.js +41 -3
- package/dist/internal/job-config.js +7 -1
- package/dist/internal/utf8.js +18 -17
- package/dist/locks.js +13 -3
- package/dist/logger.js +47 -1
- package/dist/org.js +14 -14
- package/dist/records.d.ts +13 -0
- package/dist/response.js +21 -1
- package/dist/schema.d.ts +1 -0
- package/dist/script.js +7 -3
- package/docs/en/api-reference/background-jobs.md +288 -0
- package/docs/en/api-reference/cryptography.md +317 -0
- package/docs/en/api-reference/fetch-and-secrets.md +184 -0
- package/docs/en/api-reference/inbound-webhooks.md +65 -0
- package/docs/en/api-reference/notifications-and-email.md +317 -0
- package/docs/en/api-reference/organization-structure.md +304 -0
- package/docs/en/api-reference/overview.md +198 -0
- package/docs/en/api-reference/push-messages.md +115 -0
- package/docs/en/api-reference/record-triggers.md +469 -0
- package/docs/en/api-reference/records.md +447 -0
- package/docs/en/api-reference/scripts-and-routes.md +354 -0
- package/docs/en/api-reference/state-and-locks.md +162 -0
- package/docs/en/usage-guide.md +17 -14
- package/package.json +1 -1
- package/docs/en/api-reference.md +0 -2944
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# `@cogover/sdk` API reference: Notifications and email
|
|
2
|
+
|
|
3
|
+
This page is part of the [`@cogover/sdk` API reference](overview.md).
|
|
4
|
+
|
|
5
|
+
## Notifications
|
|
6
|
+
|
|
7
|
+
`notifications` saves a notification in the notification list (the bell) of each
|
|
8
|
+
recipient and delivers it in the web app, by web push, by mobile push, and as an
|
|
9
|
+
email copy, as the workspace's notification channels decide. Unlike a
|
|
10
|
+
[push message](push-messages.md#push-messages), a notification stays in the list after it was
|
|
11
|
+
delivered, so it suits something a person must act on later, such as an approval
|
|
12
|
+
request. Delivery is asynchronous and best-effort: a call resolves once Cogover has
|
|
13
|
+
accepted the notification, never when it reaches a device or an inbox.
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
type NotificationContentType = "text" | "html";
|
|
17
|
+
type NotificationDisplay = "SIMPLE" | "FULL";
|
|
18
|
+
type NotificationSender = "workspace" | "actor";
|
|
19
|
+
type NotificationLinkTarget = "SAME_TAB" | "NEW_TAB";
|
|
20
|
+
|
|
21
|
+
interface NotificationLink {
|
|
22
|
+
readonly url: string;
|
|
23
|
+
readonly openIn?: NotificationLinkTarget;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface NotificationMessage {
|
|
27
|
+
readonly to: readonly string[];
|
|
28
|
+
readonly exclude?: readonly string[];
|
|
29
|
+
readonly title: string;
|
|
30
|
+
readonly content: string;
|
|
31
|
+
readonly contentType?: NotificationContentType;
|
|
32
|
+
readonly subtitle?: string;
|
|
33
|
+
readonly display?: NotificationDisplay;
|
|
34
|
+
readonly link?: NotificationLink;
|
|
35
|
+
readonly sender?: NotificationSender;
|
|
36
|
+
readonly channel?: string;
|
|
37
|
+
readonly email?: boolean;
|
|
38
|
+
readonly idempotencyKey?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface NotificationResult {
|
|
42
|
+
readonly requestId: string;
|
|
43
|
+
readonly notified: number;
|
|
44
|
+
readonly skippedPersonnelIds: readonly string[];
|
|
45
|
+
readonly duplicate: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface NotificationsApi {
|
|
49
|
+
send(message: NotificationMessage): Promise<NotificationResult>;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const result = await notifications.send({
|
|
55
|
+
to: approverIds,
|
|
56
|
+
exclude: ["actor"],
|
|
57
|
+
title: "Leave request waiting for your approval",
|
|
58
|
+
content: "A leave request of 3 days was submitted and needs your decision.",
|
|
59
|
+
link: { url: `/app/leave_request/${recordId}` },
|
|
60
|
+
channel: approvalChannelId,
|
|
61
|
+
idempotencyKey: `leave-approval:${recordId}`,
|
|
62
|
+
});
|
|
63
|
+
// result.notified: accounts addressed; result.skippedPersonnelIds: personnel without an account
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `notifications.send(message): Promise<NotificationResult>`
|
|
67
|
+
|
|
68
|
+
**Recipients.** `to` lists 1 to 200 personnel IDs; duplicates are removed. The
|
|
69
|
+
notification goes to the user account of each personnel. Personnel without a user
|
|
70
|
+
account are skipped and returned in `skippedPersonnelIds`, and `notified` is the
|
|
71
|
+
number of accounts the notification was addressed to after exclusions. `exclude`
|
|
72
|
+
lists personnel IDs that never receive it; the special value `"actor"` is the user
|
|
73
|
+
whose action started the current execution (the caller of an HTTP route, the user
|
|
74
|
+
whose save ran a trigger, the user whose call enqueued a job run). An execution
|
|
75
|
+
without a user has no actor, so `"actor"` excludes nobody. A personnel ID in `to`
|
|
76
|
+
that does not exist throws `NotFoundError` with `resource` `"personnel"` before
|
|
77
|
+
anything is sent. When every recipient is excluded or skipped, nothing is sent and
|
|
78
|
+
`notified` is `0`.
|
|
79
|
+
|
|
80
|
+
**Content and appearance.**
|
|
81
|
+
|
|
82
|
+
- `title` (required, at most 200 characters) is the heading of the notification and
|
|
83
|
+
the subject of its email copy.
|
|
84
|
+
- `content` (required, at most 16,384 characters) is the body. With
|
|
85
|
+
`contentType: "text"`, the default, it is plain text. With `"html"` it is reduced
|
|
86
|
+
to basic formatting before it is stored — paragraphs, headings, lists, tables,
|
|
87
|
+
links, and emphasis; scripts, styles, and images are removed — and it must still
|
|
88
|
+
contain visible text, otherwise `ValidationError` is thrown.
|
|
89
|
+
- `subtitle` (at most 200 characters) is shown only in the `"FULL"` display.
|
|
90
|
+
`display` defaults to `"FULL"` when a subtitle is given and to `"SIMPLE"`
|
|
91
|
+
otherwise; a subtitle together with `display: "SIMPLE"` throws `ValidationError`.
|
|
92
|
+
- `link` is the page opened when the recipient clicks the notification. `url` is a
|
|
93
|
+
path of the Cogover web app starting with `/`, or an absolute `http` or `https`
|
|
94
|
+
URL, at most 2,048 characters. `openIn` is `"SAME_TAB"` or `"NEW_TAB"`; it defaults
|
|
95
|
+
to `"SAME_TAB"` for a path and to `"NEW_TAB"` for an absolute URL.
|
|
96
|
+
- `sender` is `"workspace"`, the default, or `"actor"`, which shows the name and
|
|
97
|
+
avatar of the user whose action started the execution. `"actor"` in an execution
|
|
98
|
+
without a user throws `ValidationError`.
|
|
99
|
+
|
|
100
|
+
**Channels and the email copy.** `channel` is the record ID of one of the
|
|
101
|
+
workspace's notification channels. The channel decides which deliveries are enabled
|
|
102
|
+
— in the web app, web push, mobile push, and email — and each user may override that
|
|
103
|
+
choice in their own notification settings. Without `channel`, every delivery is
|
|
104
|
+
enabled. An unknown channel throws `NotFoundError` with `resource`
|
|
105
|
+
`"notificationChannel"`; a workspace in which notifications are not set up throws
|
|
106
|
+
`NotFoundError` with `resource` `"object"` and `resourceId` `"notification_channel"`.
|
|
107
|
+
|
|
108
|
+
`email: false` leaves out the email copy even when the channel enables email. The
|
|
109
|
+
copy uses `title` as its subject and `content` as its body, is sent from a Cogover
|
|
110
|
+
notification address rather than from a workspace mailbox, and counts against the
|
|
111
|
+
daily notification-email limit of the workspace's plan. To send an email from a
|
|
112
|
+
workspace mailbox, use [`email.send`](#email).
|
|
113
|
+
|
|
114
|
+
**Idempotency.** `idempotencyKey` starts with a letter or digit and contains at most
|
|
115
|
+
128 letters, digits, `.`, `_`, `:`, or `-`. A call whose key the project already
|
|
116
|
+
used for a notification in the last 7 days sends nothing and returns the first
|
|
117
|
+
call's result with `duplicate: true`. A call made while the first call with the same
|
|
118
|
+
key is still sending throws `CogoverApiError` with `code: "DUPLICATE_IN_PROGRESS"`;
|
|
119
|
+
retry it a moment later. A call that failed does not keep its key, so it can be
|
|
120
|
+
retried. Keys belong to the project and are separate from the keys of `email.send`.
|
|
121
|
+
In an after-change trigger or a job, which may run more than once, derive the key
|
|
122
|
+
from `trigger.changeId` and `record.id`, or from `job.id` or a business key.
|
|
123
|
+
Cogover does not compare the content: a call that reuses a key with different
|
|
124
|
+
recipients or text still returns the first result and sends nothing.
|
|
125
|
+
|
|
126
|
+
### Rules
|
|
127
|
+
|
|
128
|
+
- `notifications` is available in scripts, routes, after-change triggers, background
|
|
129
|
+
jobs, inbound webhook calls, and Development Sessions with write permission. A
|
|
130
|
+
before-change trigger is read-only and receives `PermissionDeniedError` with
|
|
131
|
+
`details.reason` `"TRIGGER_READ_ONLY"`; a read-only preview and a read-only
|
|
132
|
+
Development Session refuse it too.
|
|
133
|
+
- Notifications need no grant in the identity policy: a project may notify any
|
|
134
|
+
personnel of the workspace.
|
|
135
|
+
- A project may address at most 5,000 recipients per hour by default. A call that
|
|
136
|
+
would exceed this throws `RateLimitError` and sends nothing.
|
|
137
|
+
- Inputs are validated by the SDK and again by Cogover; invalid input throws
|
|
138
|
+
`ValidationError` before anything is sent. Each call counts as one capability call.
|
|
139
|
+
- When notifications are switched off for the deployment, every call throws
|
|
140
|
+
`CogoverApiError` with code `NOTIFICATIONS_DISABLED`.
|
|
141
|
+
- A sent notification cannot be recalled. Retrying a failed execution may notify
|
|
142
|
+
again unless the call uses an `idempotencyKey`.
|
|
143
|
+
|
|
144
|
+
## Email
|
|
145
|
+
|
|
146
|
+
`email` sends email from the workspace's mailboxes: a shared workspace mailbox, or
|
|
147
|
+
the default personal mailbox of the user whose action started the execution. The
|
|
148
|
+
Project's approved identity policy decides which mailboxes a project may use. With
|
|
149
|
+
`record`, the email is also logged as an email activity on that record's timeline,
|
|
150
|
+
like an email sent from the record page. Sending is asynchronous: a call resolves
|
|
151
|
+
once Cogover has accepted the email for delivery, and a delivery failure after that
|
|
152
|
+
is not reported back to the script.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
type EmailSender = { readonly mailbox: string } | "actor";
|
|
156
|
+
|
|
157
|
+
type EmailRecipient =
|
|
158
|
+
| string
|
|
159
|
+
| { readonly email: string; readonly name?: string }
|
|
160
|
+
| { readonly personnelId: string };
|
|
161
|
+
|
|
162
|
+
interface EmailRecordLink<ObjectSlug extends string = string> {
|
|
163
|
+
readonly object: ObjectSlug;
|
|
164
|
+
readonly recordId: string;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
interface EmailAttachment<ObjectSlug extends string = string> {
|
|
168
|
+
readonly object: ObjectSlug;
|
|
169
|
+
readonly recordId: string;
|
|
170
|
+
readonly field: string;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
type EmailMessage<ObjectSlug extends string = string> = {
|
|
174
|
+
readonly from: EmailSender;
|
|
175
|
+
readonly to?: readonly EmailRecipient[];
|
|
176
|
+
readonly cc?: readonly EmailRecipient[];
|
|
177
|
+
readonly bcc?: readonly EmailRecipient[];
|
|
178
|
+
readonly subject: string;
|
|
179
|
+
readonly record?: EmailRecordLink<ObjectSlug>;
|
|
180
|
+
readonly recordEmailFields?: readonly string[];
|
|
181
|
+
readonly attachments?: readonly EmailAttachment<ObjectSlug>[];
|
|
182
|
+
readonly appendSignature?: boolean;
|
|
183
|
+
readonly idempotencyKey?: string;
|
|
184
|
+
} & (
|
|
185
|
+
| { readonly html: string; readonly text?: never }
|
|
186
|
+
| { readonly text: string; readonly html?: never }
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
type EmailDelivery = "activity" | "direct";
|
|
190
|
+
|
|
191
|
+
interface EmailSendResult {
|
|
192
|
+
readonly requestId: string;
|
|
193
|
+
readonly delivery: EmailDelivery;
|
|
194
|
+
readonly recipients: number;
|
|
195
|
+
readonly duplicate: boolean;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
type EmailSenderKind = "workspace" | "actor";
|
|
199
|
+
|
|
200
|
+
interface EmailSenderInfo {
|
|
201
|
+
readonly id: string;
|
|
202
|
+
readonly kind: EmailSenderKind;
|
|
203
|
+
readonly email: string;
|
|
204
|
+
readonly displayName: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface EmailApi<Schema extends object = EffectiveWorkspaceObjects> {
|
|
208
|
+
send(message: EmailMessage<StringKeyOf<Schema>>): Promise<EmailSendResult>;
|
|
209
|
+
senders(): Promise<readonly EmailSenderInfo[]>;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
const result = await email.send({
|
|
215
|
+
from: { mailbox: "EMWXXXXXXXXXXXX" },
|
|
216
|
+
bcc: ["sales-archive@example.com"],
|
|
217
|
+
subject: `Quote ${quote.fields.code}`,
|
|
218
|
+
html: "<p>Dear customer,</p><p>Please find our quote attached.</p>",
|
|
219
|
+
record: { object: "quote", recordId: quote.id },
|
|
220
|
+
recordEmailFields: ["contact_email"],
|
|
221
|
+
attachments: [{ object: "quote", recordId: quote.id, field: "quote_pdf" }],
|
|
222
|
+
idempotencyKey: `quote-email:${quote.id}`,
|
|
223
|
+
});
|
|
224
|
+
// result.delivery === "activity": the email appears on the quote's timeline.
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `email.send(message): Promise<EmailSendResult>`
|
|
228
|
+
|
|
229
|
+
**Sender.** `from: { mailbox: id }` sends from a shared workspace mailbox;
|
|
230
|
+
`from: "actor"` sends from the default personal mailbox of the user whose action
|
|
231
|
+
started the execution. The sender must be allowed by the `email` section of the
|
|
232
|
+
Project's approved identity policy: `workspaceMailboxIds` lists the shared mailboxes
|
|
233
|
+
and `allowActorMailbox` allows `"actor"`. Like the rest of the policy, the section
|
|
234
|
+
applies only to the callers selected by `callerPersonnelIds`, and to executions
|
|
235
|
+
without a user only when the policy sets `allowInternalSystem: true`. A project
|
|
236
|
+
whose policy has no `email` section cannot send email. A sender that is not allowed
|
|
237
|
+
throws `PermissionDeniedError` with `details.reason` `"EMAIL_SENDER_NOT_GRANTED"`;
|
|
238
|
+
this check comes before anything else is looked up. A mailbox that does not exist or
|
|
239
|
+
is deactivated, and an actor without an active personal mailbox, throw
|
|
240
|
+
`NotFoundError` with `resource` `"mailbox"`. `"actor"` in an execution without a
|
|
241
|
+
user throws `ValidationError`.
|
|
242
|
+
|
|
243
|
+
**Recipients.**
|
|
244
|
+
|
|
245
|
+
- `to`, `cc`, and `bcc` accept an address string, `{ email, name? }`, or
|
|
246
|
+
`{ personnelId }`. A personnel is resolved to their work email, or to the email of
|
|
247
|
+
their user account when there is no work email; a personnel with neither throws
|
|
248
|
+
`ValidationError`, and an unknown personnel throws `NotFoundError` with `resource`
|
|
249
|
+
`"personnel"`.
|
|
250
|
+
- Duplicates are removed within each list, ignoring case. The three lists together
|
|
251
|
+
accept at most 50 recipients. A `name` is a single line of at most 200 characters.
|
|
252
|
+
- `to` must name at least one recipient unless `recordEmailFields` is given.
|
|
253
|
+
|
|
254
|
+
**Content.** `subject` is required: a single line of at most 255 characters. Provide
|
|
255
|
+
exactly one of `html` and `text`; the body is at most 192 KB (196,608 bytes) in
|
|
256
|
+
UTF-8, measured in its JSON-encoded form: each quote, backslash, or control character
|
|
257
|
+
such as a line break counts as its escape sequence (`\"` is 2 bytes, `\n` is 2 bytes,
|
|
258
|
+
`\u0001` is 6 bytes). A longer body throws `ValidationError` before the call.
|
|
259
|
+
|
|
260
|
+
**Record.** `record: { object, recordId }` logs the email as an email activity on
|
|
261
|
+
that record's timeline: Cogover tracks opens and link clicks and threads replies
|
|
262
|
+
with it, and `result.delivery` is `"activity"`. The record must be readable by the
|
|
263
|
+
default identity of the execution: a record that does not exist throws
|
|
264
|
+
`NotFoundError` with `resource` `"record"`, and a record that the identity may not
|
|
265
|
+
read throws `PermissionDeniedError`. Without `record`, the email is only sent from the mailbox and
|
|
266
|
+
`delivery` is `"direct"`. The following options require `record`; without it they
|
|
267
|
+
throw `ValidationError`:
|
|
268
|
+
|
|
269
|
+
- `recordEmailFields` lists up to 20 email fields of that record whose addresses are
|
|
270
|
+
added to `to`. A field that is not an email field throws `ValidationError`.
|
|
271
|
+
- `attachments` attaches every file stored in the file field `field` of each listed
|
|
272
|
+
record: at most 10 files and 20 MB in total. The records must be readable by the
|
|
273
|
+
default identity; a field that is not a file field throws `ValidationError`.
|
|
274
|
+
- `appendSignature: true` appends the sender's default email signature. It requires a
|
|
275
|
+
personal mailbox, that is `from: "actor"`; otherwise `ValidationError` is thrown.
|
|
276
|
+
|
|
277
|
+
**Result.** `requestId` identifies the send. `recipients` counts the addresses in
|
|
278
|
+
`to`, `cc`, and `bcc` after duplicates were removed; the addresses of
|
|
279
|
+
`recordEmailFields` are resolved later and are not counted. `duplicate` is `true`
|
|
280
|
+
when an earlier call with the same `idempotencyKey` already sent the email.
|
|
281
|
+
|
|
282
|
+
**Idempotency.** `idempotencyKey` follows the same rules as for
|
|
283
|
+
[notifications](#notifications): a retry with the same key within 7 days returns
|
|
284
|
+
the first result with `duplicate: true` instead of sending again, and a retry while
|
|
285
|
+
the first call is still sending throws `CogoverApiError` with
|
|
286
|
+
`code: "DUPLICATE_IN_PROGRESS"`. Email keys are separate from notification keys. As
|
|
287
|
+
for notifications, the content is not compared: reusing a key for a different email
|
|
288
|
+
returns the first result and sends nothing.
|
|
289
|
+
|
|
290
|
+
### `email.senders(): Promise<readonly EmailSenderInfo[]>`
|
|
291
|
+
|
|
292
|
+
Returns the active mailboxes this execution may send from: one `kind: "workspace"`
|
|
293
|
+
entry for each active shared mailbox that the applicable policy lists in
|
|
294
|
+
`workspaceMailboxIds`, to be used as `{ mailbox: id }`, and, when the policy sets
|
|
295
|
+
`allowActorMailbox` and the actor has an active personal mailbox, at most one
|
|
296
|
+
`kind: "actor"` entry describing the mailbox that `from: "actor"` uses. The list is
|
|
297
|
+
empty when the policy grants this execution no sender. Use it to show the available
|
|
298
|
+
senders or to pick one by `email` address instead of hard-coding an ID. It sends
|
|
299
|
+
nothing, so it also works in a read-only preview and a read-only Development
|
|
300
|
+
Session.
|
|
301
|
+
|
|
302
|
+
### Rules
|
|
303
|
+
|
|
304
|
+
- `email` is available in scripts, routes, after-change triggers, background jobs,
|
|
305
|
+
inbound webhook calls, and Development Sessions. A before-change trigger receives
|
|
306
|
+
`PermissionDeniedError` with `details.reason` `"TRIGGER_READ_ONLY"` for both
|
|
307
|
+
methods; a read-only preview and a read-only Development Session refuse
|
|
308
|
+
`email.send`.
|
|
309
|
+
- A project may send at most 500 emails per hour by default; beyond that,
|
|
310
|
+
`RateLimitError` is thrown and nothing is sent. One call is one email, however many
|
|
311
|
+
recipients it has.
|
|
312
|
+
- Inputs are validated by the SDK and again by Cogover; invalid input throws
|
|
313
|
+
`ValidationError` before anything is sent. Each call counts as one capability call.
|
|
314
|
+
- When email is switched off for the deployment, every call throws
|
|
315
|
+
`CogoverApiError` with code `EMAIL_DISABLED`.
|
|
316
|
+
- A sent email cannot be recalled. Retrying a failed execution may send it again
|
|
317
|
+
unless the call uses an `idempotencyKey`.
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# `@cogover/sdk` API reference: Organization structure
|
|
2
|
+
|
|
3
|
+
This page is part of the [`@cogover/sdk` API reference](overview.md).
|
|
4
|
+
|
|
5
|
+
## Organization structure
|
|
6
|
+
|
|
7
|
+
`org` reads the departments, positions and personnel of the workspace: who works in
|
|
8
|
+
which department, which positions apply where, and who manages whom. Every method is
|
|
9
|
+
a read. Structure answers are served from a cached copy of the organization, so they
|
|
10
|
+
are fast; a change made in Cogover is visible within a few seconds. Add
|
|
11
|
+
`withDisplay: true` to also receive department and position names in a language and,
|
|
12
|
+
for personnel, their display name, email, avatar and code. Other personnel fields,
|
|
13
|
+
such as phone numbers or custom fields, are read with `data.object("personnel")`,
|
|
14
|
+
which applies record permissions.
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
interface OrgApi {
|
|
18
|
+
readonly personnel: OrgPersonnelApi;
|
|
19
|
+
readonly departments: OrgDepartmentsApi;
|
|
20
|
+
readonly positions: OrgPositionsApi;
|
|
21
|
+
me(options?: OrgReadOptions): Promise<OrgPersonnel | null>;
|
|
22
|
+
isInDepartment(personnelId: string, departmentId: string,
|
|
23
|
+
options?: OrgIsInDepartmentOptions): Promise<boolean>;
|
|
24
|
+
isManagerOf(managerId: string, personnelId: string,
|
|
25
|
+
options?: OrgIsManagerOfOptions): Promise<boolean>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface OrgPersonnelApi {
|
|
29
|
+
get(id: string, options?: OrgReadOptions): Promise<OrgPersonnel | null>;
|
|
30
|
+
getMany(ids: readonly string[], options?: OrgReadOptions):
|
|
31
|
+
Promise<OrgGetManyResult<OrgPersonnel>>;
|
|
32
|
+
managerChain(id: string, options?: OrgPersonnelChainOptions):
|
|
33
|
+
Promise<readonly OrgManagerChain[]>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface OrgDepartmentsApi {
|
|
37
|
+
get(id: string, options?: OrgReadOptions): Promise<OrgDepartment | null>;
|
|
38
|
+
tree(options?: OrgTreeOptions): Promise<readonly OrgDepartmentNode[]>;
|
|
39
|
+
ancestors(id: string, options?: OrgReadOptions): Promise<readonly OrgDepartment[]>;
|
|
40
|
+
members(id: string, options?: OrgDepartmentMembersOptions): Promise<OrgPage<OrgMember>>;
|
|
41
|
+
managers(id: string, options?: OrgManagerOptions): Promise<readonly OrgManagerTier[]>;
|
|
42
|
+
managerChain(id: string, options?: OrgManagerOptions): Promise<OrgManagerChain>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface OrgPositionsApi {
|
|
46
|
+
list(options?: OrgReadOptions): Promise<readonly OrgPosition[]>;
|
|
47
|
+
get(id: string, options?: OrgReadOptions): Promise<OrgPosition | null>;
|
|
48
|
+
members(id: string, options?: OrgPositionMembersOptions): Promise<OrgPage<OrgMember>>;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Every method that returns data has a second overload: when `options.withDisplay` is
|
|
53
|
+
the literal `true`, the result type is the `<true>` variant of the types below, whose
|
|
54
|
+
display fields are always present. Without it, the display fields do not exist on the
|
|
55
|
+
type.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
interface OrgReadOptions {
|
|
59
|
+
readonly withDisplay?: boolean;
|
|
60
|
+
readonly language?: string; // such as "vi-VN"; defaults to the invocation language
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface OrgPageOptions extends OrgReadOptions {
|
|
64
|
+
readonly includeSubDepartments?: boolean;
|
|
65
|
+
readonly accountOnly?: boolean;
|
|
66
|
+
readonly limit?: number; // default 500, at most 2000; 200 with withDisplay
|
|
67
|
+
readonly cursor?: string;
|
|
68
|
+
}
|
|
69
|
+
interface OrgDepartmentMembersOptions extends OrgPageOptions { readonly positionId?: string; }
|
|
70
|
+
interface OrgPositionMembersOptions extends OrgPageOptions { readonly departmentId?: string; }
|
|
71
|
+
interface OrgManagerOptions extends OrgReadOptions { readonly accountOnly?: boolean; }
|
|
72
|
+
interface OrgPersonnelChainOptions extends OrgManagerOptions { readonly departmentId?: string; }
|
|
73
|
+
interface OrgTreeOptions extends OrgReadOptions {
|
|
74
|
+
readonly rootId?: string;
|
|
75
|
+
readonly depth?: number; // 1 to 100; defaults to every level
|
|
76
|
+
}
|
|
77
|
+
interface OrgIsInDepartmentOptions { readonly includeSubDepartments?: boolean; }
|
|
78
|
+
interface OrgIsManagerOfOptions {
|
|
79
|
+
readonly departmentId?: string;
|
|
80
|
+
readonly directOnly?: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface OrgPersonnelDisplay {
|
|
84
|
+
readonly name: string;
|
|
85
|
+
readonly email: string | null;
|
|
86
|
+
readonly avatar: string | null; // public URL
|
|
87
|
+
readonly code: string | null;
|
|
88
|
+
}
|
|
89
|
+
interface OrgNameDisplay { readonly name: string; }
|
|
90
|
+
|
|
91
|
+
type OrgMembership<D extends boolean = false> = {
|
|
92
|
+
readonly personnelId: string;
|
|
93
|
+
readonly departmentId: string;
|
|
94
|
+
readonly positionId: string | null;
|
|
95
|
+
readonly level: number; // 0 staff, 1 highest manager level, 2 next, ...
|
|
96
|
+
readonly primary: boolean;
|
|
97
|
+
} & (D extends true
|
|
98
|
+
? { readonly departmentName: string | null; readonly positionName: string | null }
|
|
99
|
+
: {});
|
|
100
|
+
|
|
101
|
+
type OrgPersonnel<D extends boolean = false> = {
|
|
102
|
+
readonly id: string;
|
|
103
|
+
readonly accountId: string | null; // null: no workspace account
|
|
104
|
+
readonly memberships: readonly OrgMembership<D>[];
|
|
105
|
+
} & (D extends true ? { readonly display: OrgPersonnelDisplay | null } : {});
|
|
106
|
+
|
|
107
|
+
type OrgMember<D extends boolean = false> = OrgMembership<D> & {
|
|
108
|
+
readonly accountId: string | null;
|
|
109
|
+
} & (D extends true ? { readonly display: OrgPersonnelDisplay | null } : {});
|
|
110
|
+
|
|
111
|
+
type OrgDepartment<D extends boolean = false> = {
|
|
112
|
+
readonly id: string;
|
|
113
|
+
readonly parentId: string | null;
|
|
114
|
+
readonly level: number; // 1 for a root department
|
|
115
|
+
readonly childIds: readonly string[];
|
|
116
|
+
readonly positionIds: readonly string[];
|
|
117
|
+
} & (D extends true ? { readonly display: OrgNameDisplay | null } : {});
|
|
118
|
+
|
|
119
|
+
type OrgDepartmentNode<D extends boolean = false> = OrgDepartment<D> & {
|
|
120
|
+
readonly children: readonly OrgDepartmentNode<D>[];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
type OrgPosition<D extends boolean = false> = {
|
|
124
|
+
readonly id: string;
|
|
125
|
+
readonly departmentOnly: boolean;
|
|
126
|
+
readonly departmentIds: readonly string[];
|
|
127
|
+
readonly excludedDepartmentIds: readonly string[];
|
|
128
|
+
} & (D extends true ? { readonly display: OrgNameDisplay | null } : {});
|
|
129
|
+
|
|
130
|
+
type OrgManagerTier<D extends boolean = false> = {
|
|
131
|
+
readonly departmentId: string;
|
|
132
|
+
readonly level: number;
|
|
133
|
+
readonly distance: number; // 0 starting department, 1 its parent, ...
|
|
134
|
+
readonly personnelIds: readonly string[];
|
|
135
|
+
} & (D extends true ? {
|
|
136
|
+
readonly departmentName: string | null;
|
|
137
|
+
readonly personnel: readonly { readonly id: string; readonly display: OrgPersonnelDisplay | null }[];
|
|
138
|
+
} : {});
|
|
139
|
+
|
|
140
|
+
interface OrgManagerChain<D extends boolean = false> {
|
|
141
|
+
readonly fromDepartmentId: string;
|
|
142
|
+
readonly personnelLevel: number;
|
|
143
|
+
readonly tiers: readonly OrgManagerTier<D>[];
|
|
144
|
+
readonly vacantDepartmentIds: readonly string[];
|
|
145
|
+
readonly truncated: boolean;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface OrgPage<T> {
|
|
149
|
+
readonly items: readonly T[];
|
|
150
|
+
readonly total: number;
|
|
151
|
+
readonly nextCursor?: string;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
interface OrgGetManyResult<T> {
|
|
155
|
+
readonly items: readonly T[];
|
|
156
|
+
readonly missingIds: readonly string[];
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
export default defineScript(async ({ org, input }) => {
|
|
162
|
+
const order = input as { ownerId: string; departmentId: string };
|
|
163
|
+
// Structure only: answered without reading records.
|
|
164
|
+
const [chain] = await org.personnel.managerChain(order.ownerId, {
|
|
165
|
+
departmentId: order.departmentId,
|
|
166
|
+
accountOnly: true,
|
|
167
|
+
});
|
|
168
|
+
const approverIds = chain?.tiers[0]?.personnelIds ?? [];
|
|
169
|
+
|
|
170
|
+
// Names for a screen: one extra read per kind of data.
|
|
171
|
+
const me = await org.me({ withDisplay: true });
|
|
172
|
+
return {
|
|
173
|
+
approverIds,
|
|
174
|
+
greeting: me?.display?.name,
|
|
175
|
+
departments: me?.memberships.map(m => m.departmentName) ?? [],
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Display data
|
|
181
|
+
|
|
182
|
+
- `withDisplay` adds `display` to personnel, departments and positions, and
|
|
183
|
+
`departmentName`/`positionName` to memberships. A value is `null` when the record
|
|
184
|
+
behind it cannot be read, for example because it was deleted a moment ago.
|
|
185
|
+
- `language` selects the translation of department and position names, such as
|
|
186
|
+
`"vi-VN"` or `"en-US"`; a tag without a region (`"vi"`) matches the first
|
|
187
|
+
translation of that language. It defaults to the language of the invocation's user
|
|
188
|
+
membership, then of the user, then of the workspace, and falls back to the stored
|
|
189
|
+
name when there is no matching translation.
|
|
190
|
+
- A personnel `name` is the full name the workspace shows; `email` is the account
|
|
191
|
+
email, else the work email; `avatar` is a public image URL.
|
|
192
|
+
- Display data is limited to these fields and is readable by every member of the
|
|
193
|
+
workspace, as in user pickers. It costs at most one read per kind of data and per
|
|
194
|
+
200 IDs, and recently read values are cached.
|
|
195
|
+
|
|
196
|
+
### `org.me(options?)`
|
|
197
|
+
|
|
198
|
+
Returns the personnel of the invocation's user, or `null` for a `system` or
|
|
199
|
+
`inbound` invocation. It is `org.personnel.get` with
|
|
200
|
+
`invocation.user.membership.personnelId`.
|
|
201
|
+
|
|
202
|
+
### `org.personnel.get(id, options?)` and `org.personnel.getMany(ids, options?)`
|
|
203
|
+
|
|
204
|
+
`get` returns an active personnel with every department membership, primary
|
|
205
|
+
department first, or `null` when the ID is not an active personnel of the workspace.
|
|
206
|
+
`getMany` reads up to 200 IDs in one call: duplicates are removed, `items` follows
|
|
207
|
+
the request order, and unknown IDs are listed in `missingIds`. A personnel without a
|
|
208
|
+
workspace account has `accountId: null`; people who left the workspace are not
|
|
209
|
+
returned.
|
|
210
|
+
|
|
211
|
+
### `org.departments.get(id, options?)`, `tree(options?)` and `ancestors(id, options?)`
|
|
212
|
+
|
|
213
|
+
- `get` returns one active department or `null`. `childIds` are in display order;
|
|
214
|
+
`positionIds` are the positions that apply to the department.
|
|
215
|
+
- `tree` returns the root departments (or `rootId` alone) with their sub-departments.
|
|
216
|
+
`depth: 1` returns only the roots; below the requested depth `children` is empty
|
|
217
|
+
while `childIds` still lists the sub-departments. A tree of more than 5000
|
|
218
|
+
departments throws `ValidationError`; narrow it with `rootId` or `depth`.
|
|
219
|
+
- `ancestors` returns the parent first and the root last.
|
|
220
|
+
- A department whose parent is inactive or deleted is returned as a root
|
|
221
|
+
(`parentId: null`).
|
|
222
|
+
|
|
223
|
+
### `org.departments.members(id, options?)` and `org.positions.members(id, options?)`
|
|
224
|
+
|
|
225
|
+
Return one page of memberships: managers first by level, then staff. A personnel who
|
|
226
|
+
belongs to several listed departments appears once per department.
|
|
227
|
+
`includeSubDepartments` adds every sub-department in tree order, `positionId` keeps
|
|
228
|
+
only the holders of a position, `departmentId` limits position holders to a
|
|
229
|
+
department, and `accountOnly` keeps only personnel with a workspace account. Pass
|
|
230
|
+
`nextCursor` as `cursor` to read the next page; `total` counts every page. The page
|
|
231
|
+
size is 500 by default and at most 2000, or at most 200 with `withDisplay`.
|
|
232
|
+
|
|
233
|
+
### `org.positions.list(options?)` and `org.positions.get(id, options?)`
|
|
234
|
+
|
|
235
|
+
Return active positions, oldest first. A department-only position applies to
|
|
236
|
+
`departmentIds`; any other position applies to every department except
|
|
237
|
+
`excludedDepartmentIds`.
|
|
238
|
+
|
|
239
|
+
### Managers and manager chains
|
|
240
|
+
|
|
241
|
+
A membership's `level` marks managers: `0` is staff, `1` the highest manager level of
|
|
242
|
+
the department, `2` the next one, and so on. Several people may share a level.
|
|
243
|
+
|
|
244
|
+
- `org.departments.managers(id, options?)` returns the managers of that department
|
|
245
|
+
only, one tier per level, level `1` first.
|
|
246
|
+
- `org.departments.managerChain(id, options?)` returns the managers above a staff
|
|
247
|
+
member of the department, nearest first, up to the root department.
|
|
248
|
+
- `org.personnel.managerChain(id, options?)` returns one chain per department of the
|
|
249
|
+
personnel, primary department first; `departmentId` keeps only the chain that
|
|
250
|
+
starts in that department. The result is empty for an unknown personnel.
|
|
251
|
+
|
|
252
|
+
A chain is built with these rules:
|
|
253
|
+
|
|
254
|
+
1. In the starting department, the managers of the personnel are those with a lower
|
|
255
|
+
level number than the personnel's own level (every manager for staff), nearest
|
|
256
|
+
first: level `3`, then `2`, then `1`. A department head has none there.
|
|
257
|
+
2. Then every parent department up to the root adds all its managers, largest level
|
|
258
|
+
number first. Every manager of a parent department ranks above every manager of
|
|
259
|
+
its sub-departments.
|
|
260
|
+
3. A personnel is listed once, at the nearest tier, and the starting personnel is
|
|
261
|
+
never listed.
|
|
262
|
+
4. `accountOnly: true` skips personnel without a workspace account, for example when
|
|
263
|
+
the chain lists approvers.
|
|
264
|
+
5. `vacantDepartmentIds` lists the departments passed without any eligible manager;
|
|
265
|
+
the chain continues with their parent. A department head's own department is not
|
|
266
|
+
vacant.
|
|
267
|
+
6. `truncated` is `true` when a parent department is inactive, so the chain stops
|
|
268
|
+
before the real root.
|
|
269
|
+
|
|
270
|
+
For example, with a company board (CEO level `1`, deputy CEO level `2`), an
|
|
271
|
+
Engineering department under it (director level `1`), and a Testing department under
|
|
272
|
+
Engineering (head `1`, deputy `2`, team lead `3`), the chain of a tester is: team lead,
|
|
273
|
+
deputy, head, director, deputy CEO, CEO. The chain of the Testing deputy is: head,
|
|
274
|
+
director, deputy CEO, CEO.
|
|
275
|
+
|
|
276
|
+
### `org.isInDepartment(personnelId, departmentId, options?)` and `org.isManagerOf(managerId, personnelId, options?)`
|
|
277
|
+
|
|
278
|
+
`isInDepartment` tells whether the personnel belongs to the department, or to one of
|
|
279
|
+
its sub-departments with `includeSubDepartments`. `isManagerOf` tells whether
|
|
280
|
+
`managerId` appears in a manager chain of `personnelId`; `departmentId` considers
|
|
281
|
+
only the chain that starts there and `directOnly` only its nearest tier. Both return
|
|
282
|
+
`false` for unknown IDs.
|
|
283
|
+
|
|
284
|
+
### Rules
|
|
285
|
+
|
|
286
|
+
- `org` is available in scripts, routes, record triggers of both timings, background
|
|
287
|
+
jobs and Development Sessions.
|
|
288
|
+
- It follows the default identity of the execution: an execution with a user or a
|
|
289
|
+
system identity may read it. An execution without a user (a background job, an
|
|
290
|
+
inbound webhook, or a record trigger started by a system write) may read it only
|
|
291
|
+
when the project's approved identity policy sets `allowInternalSystem: true`;
|
|
292
|
+
otherwise every call except `org.me()` throws `PermissionDeniedError` with
|
|
293
|
+
`details.reason` `"IDENTITY_NOT_GRANTED"`. `org.me()` returns `null` there without a
|
|
294
|
+
capability call, because the execution has no user.
|
|
295
|
+
- All calls of one execution read the same version of the structure.
|
|
296
|
+
- IDs are validated by the SDK and again by Cogover; an invalid ID or option throws
|
|
297
|
+
`ValidationError`. Every method returns a promise, and an invalid argument rejects
|
|
298
|
+
that promise rather than throwing when the method is called. `get` methods return `null` for an unknown ID; `members`,
|
|
299
|
+
`managers`, `managerChain`, `ancestors` and `tree({ rootId })` throw
|
|
300
|
+
`NotFoundError` with `resource` `"department"` or `"position"`.
|
|
301
|
+
- When the structure cannot be read, the call throws `CogoverApiError` with code
|
|
302
|
+
`ORGANIZATION_UNAVAILABLE`.
|
|
303
|
+
- Call `getMany` or `members` instead of calling `get` in a loop: each call is one
|
|
304
|
+
capability call.
|