@cogover/sdk 0.16.0 → 0.17.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
CHANGED
|
@@ -654,6 +654,7 @@ router.post("/quotes/:id/send", async ({ request, notifications, email }) => {
|
|
|
654
654
|
title: "Quote sent to the customer",
|
|
655
655
|
content: "The quote was emailed and logged on its timeline.",
|
|
656
656
|
link: { url: `/app/quote/${quoteId}` },
|
|
657
|
+
categories: ["sales"],
|
|
657
658
|
idempotencyKey: `quote-sent:${quoteId}`,
|
|
658
659
|
});
|
|
659
660
|
return { requestId: sent.requestId };
|
|
@@ -663,7 +664,9 @@ router.post("/quotes/:id/send", async ({ request, notifications, email }) => {
|
|
|
663
664
|
Both are available in scripts, routes, after-change triggers, and jobs, and both are
|
|
664
665
|
asynchronous and best-effort: a successful call means Cogover accepted the message.
|
|
665
666
|
`idempotencyKey` makes a retry within 7 days return the first result with
|
|
666
|
-
`duplicate: true` instead of sending again.
|
|
667
|
+
`duplicate: true` instead of sending again. `categories` files a notification under
|
|
668
|
+
notification categories of the workspace by category code (such as `"sales"`) or
|
|
669
|
+
record ID, so recipients can filter their list. Notifications need no approval; email
|
|
667
670
|
may only be sent from the mailboxes allowed by the `email` section of the Project's
|
|
668
671
|
approved identity policy, otherwise `PermissionDeniedError` is thrown with
|
|
669
672
|
`details.reason === "EMAIL_SENDER_NOT_GRANTED"`. `email.senders()` lists the
|
package/dist/notifications.d.ts
CHANGED
|
@@ -36,6 +36,15 @@ export interface NotificationMessage {
|
|
|
36
36
|
* mobile push, email) are enabled; without one, all of them are.
|
|
37
37
|
*/
|
|
38
38
|
readonly channel?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Notification categories the notification is filed under in the recipients' notification list, at most 10. Each
|
|
41
|
+
* entry is the `code` of a category (for example `"sales"`), which stays the same across workspaces, or a
|
|
42
|
+
* category record ID. Duplicates are ignored, and so are entries that do not name an active category of the
|
|
43
|
+
* workspace; when none is left, the `system` category is used. Without categories, the workspace's own category
|
|
44
|
+
* settings apply. Categories only group notifications in the list, unlike `channel`, which decides how they are
|
|
45
|
+
* delivered.
|
|
46
|
+
*/
|
|
47
|
+
readonly categories?: readonly string[];
|
|
39
48
|
/** Whether to include an email copy for channels that enable email. Defaults to `true`. */
|
|
40
49
|
readonly email?: boolean;
|
|
41
50
|
/** A retry with the same key returns the first result instead of notifying again. */
|
package/dist/notifications.js
CHANGED
|
@@ -8,12 +8,14 @@ const MAX_SUBTITLE_CHARS = 200;
|
|
|
8
8
|
const MAX_CONTENT_CHARS = 16_384;
|
|
9
9
|
const MAX_LINK_CHARS = 2_048;
|
|
10
10
|
const CHANNEL_ID = /^[A-Za-z0-9_-]{1,64}$/u;
|
|
11
|
+
const MAX_CATEGORIES = 10;
|
|
12
|
+
const CATEGORY = /^[A-Za-z0-9_-]{1,64}$/u;
|
|
11
13
|
// The sandbox has no WHATWG `URL` global, so the shape is checked with a pattern; Cogover parses it again.
|
|
12
14
|
const HTTP_URL = /^https?:\/\/[^\s/?#\\@]+(?:[/?#]\S*)?$/iu;
|
|
13
15
|
const APP_PATH = /^\/(?!\/)[^\s\\]*$/u;
|
|
14
16
|
const MESSAGE_KEYS = new Set([
|
|
15
17
|
"to", "exclude", "title", "content", "contentType", "subtitle", "display", "link", "sender", "channel",
|
|
16
|
-
"email", "idempotencyKey",
|
|
18
|
+
"categories", "email", "idempotencyKey",
|
|
17
19
|
]);
|
|
18
20
|
const LINK_KEYS = new Set(["url", "openIn"]);
|
|
19
21
|
const CONTENT_TYPES = new Set(["text", "html"]);
|
|
@@ -73,6 +75,11 @@ function notificationPayload(message) {
|
|
|
73
75
|
}
|
|
74
76
|
payload.channel = input.channel;
|
|
75
77
|
}
|
|
78
|
+
if (input.categories !== undefined) {
|
|
79
|
+
const categories = notificationCategories(input.categories);
|
|
80
|
+
if (categories.length > 0)
|
|
81
|
+
payload.categories = categories;
|
|
82
|
+
}
|
|
76
83
|
const email = optionalBoolean(input.email, "email");
|
|
77
84
|
if (email !== undefined)
|
|
78
85
|
payload.email = email;
|
|
@@ -81,6 +88,23 @@ function notificationPayload(message) {
|
|
|
81
88
|
payload.idempotencyKey = idempotencyKey;
|
|
82
89
|
return payload;
|
|
83
90
|
}
|
|
91
|
+
/** Category codes or record IDs in order without duplicates. */
|
|
92
|
+
function notificationCategories(value) {
|
|
93
|
+
if (!Array.isArray(value))
|
|
94
|
+
throw new ValidationError("categories must be a list of category codes or record IDs");
|
|
95
|
+
if (value.length > MAX_CATEGORIES) {
|
|
96
|
+
throw new ValidationError(`categories must not list more than ${MAX_CATEGORIES} entries`);
|
|
97
|
+
}
|
|
98
|
+
const categories = new Set();
|
|
99
|
+
for (const entry of value) {
|
|
100
|
+
if (typeof entry !== "string" || !CATEGORY.test(entry)) {
|
|
101
|
+
throw new ValidationError("categories must only contain category codes or record IDs of 1 to 64 letters, "
|
|
102
|
+
+ "digits, '_' or '-'");
|
|
103
|
+
}
|
|
104
|
+
categories.add(entry);
|
|
105
|
+
}
|
|
106
|
+
return [...categories];
|
|
107
|
+
}
|
|
84
108
|
function notificationLink(value) {
|
|
85
109
|
const link = requireKnownKeys(value, LINK_KEYS, "link");
|
|
86
110
|
const url = requiredText(link.url, "link.url", MAX_LINK_CHARS);
|
|
@@ -34,6 +34,7 @@ interface NotificationMessage {
|
|
|
34
34
|
readonly link?: NotificationLink;
|
|
35
35
|
readonly sender?: NotificationSender;
|
|
36
36
|
readonly channel?: string;
|
|
37
|
+
readonly categories?: readonly string[];
|
|
37
38
|
readonly email?: boolean;
|
|
38
39
|
readonly idempotencyKey?: string;
|
|
39
40
|
}
|
|
@@ -58,6 +59,7 @@ const result = await notifications.send({
|
|
|
58
59
|
content: "A leave request of 3 days was submitted and needs your decision.",
|
|
59
60
|
link: { url: `/app/leave_request/${recordId}` },
|
|
60
61
|
channel: approvalChannelId,
|
|
62
|
+
categories: ["process"],
|
|
61
63
|
idempotencyKey: `leave-approval:${recordId}`,
|
|
62
64
|
});
|
|
63
65
|
// result.notified: accounts addressed; result.skippedPersonnelIds: personnel without an account
|
|
@@ -105,6 +107,20 @@ enabled. An unknown channel throws `NotFoundError` with `resource`
|
|
|
105
107
|
`"notificationChannel"`; a workspace in which notifications are not set up throws
|
|
106
108
|
`NotFoundError` with `resource` `"object"` and `resourceId` `"notification_channel"`.
|
|
107
109
|
|
|
110
|
+
**Categories.** `categories` files the notification under up to 10 notification
|
|
111
|
+
categories of the workspace, which recipients use to filter their notification list.
|
|
112
|
+
Each entry is the `code` of a category, such as `"process"`, `"sales"`, or `"system"`,
|
|
113
|
+
or the record ID of a category; a code stays the same in every workspace that has
|
|
114
|
+
the category, while a record ID belongs to one workspace. Each entry has 1 to 64
|
|
115
|
+
letters, digits, `_`, or `-`; duplicates are removed, and an empty list is the same as
|
|
116
|
+
leaving the option out. Categories only group notifications in the list and do not
|
|
117
|
+
change how they are delivered, which is what `channel` decides. Cogover does not
|
|
118
|
+
check the entries when you call `send`: entries that do not name an active category
|
|
119
|
+
of the workspace are ignored, and when none is left the notification is filed under
|
|
120
|
+
the active `system` category. Without `categories`, the categories the workspace
|
|
121
|
+
assigns to notifications of this kind are used, again falling back to `system`. A
|
|
122
|
+
workspace without notification categories saves the notification without one.
|
|
123
|
+
|
|
108
124
|
`email: false` leaves out the email copy even when the channel enables email. The
|
|
109
125
|
copy uses `title` as its subject and `content` as its body, is sent from a Cogover
|
|
110
126
|
notification address rather than from a workspace mailbox, and counts against the
|
package/docs/en/usage-guide.md
CHANGED
|
@@ -770,6 +770,8 @@ export const triggers = [
|
|
|
770
770
|
content: "A leave request was submitted and needs your decision.",
|
|
771
771
|
link: { url: `/app/leave_request/${record.id}` },
|
|
772
772
|
channel: APPROVAL_CHANNEL,
|
|
773
|
+
// Category code: the same in every workspace, unlike the channel record ID.
|
|
774
|
+
categories: ["process"],
|
|
773
775
|
// The same change of the same record always produces the same key.
|
|
774
776
|
idempotencyKey: `leave-approval:${trigger.changeId}:${record.id}`,
|
|
775
777
|
});
|
|
@@ -787,6 +789,11 @@ export const triggers = [
|
|
|
787
789
|
`email: false` to never send the email copy. The copy comes from a Cogover
|
|
788
790
|
notification address and counts against the workspace plan's daily
|
|
789
791
|
notification-email limit.
|
|
792
|
+
- `categories` files the notification under up to 10 notification categories, which
|
|
793
|
+
recipients use to filter their notification list. Pass category codes such as
|
|
794
|
+
`"process"` or `"sales"` (or category record IDs); unknown or inactive categories
|
|
795
|
+
are ignored, and without usable categories the notification goes to `system`.
|
|
796
|
+
Categories do not change delivery, which `channel` decides.
|
|
790
797
|
- `link.url` is a path of the web app starting with `/` or an absolute `http(s)`
|
|
791
798
|
URL. `contentType: "html"` keeps only basic formatting such as paragraphs, lists,
|
|
792
799
|
tables, and links.
|