@diagonal-hq/cli 0.33.2 → 0.33.4

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
@@ -549,7 +549,7 @@ email-layouts/
549
549
  default.json
550
550
  ```
551
551
 
552
- The type sets the recipient resource, the variables, and the default template. The recipient resource maps the email and phone fields. A type without `recipient` sends to email addresses. A variable uses a `field` builder other than `field.relation`, or `{ resource }` for one record of a resource. Templates read a record variable's fields, for example `{{ first_event.name }}`.
552
+ The type sets the recipient resource, the variables, and the default template. The recipient resource maps the email and phone fields. A type without `recipient` sends to email addresses. `recipient.relations` lists the relation paths from the recipient resource whose records a send loads with the recipient. Templates read them below `record`, for example `{{ record.account.name }}`. A variable uses a `field` builder other than `field.relation`, or `{ resource }` for one record of a resource. Templates read a record variable's fields, for example `{{ first_event.name }}`.
553
553
 
554
554
  ```ts
555
555
  import { NotificationTypes } from '@app/generated/notification-types';
@@ -559,7 +559,7 @@ import { defineNotificationType, field } from '@diagonal-hq/cli';
559
559
  export default defineNotificationType({
560
560
  uid: '019a4d6e-3c2f-7b8a-9d1e-5f6a7b8c9d0e',
561
561
  name: 'Member welcome',
562
- recipient: { resource: Resources.Members.slug },
562
+ recipient: { resource: Resources.Members.slug, relations: ['account'] },
563
563
  variables: {
564
564
  joined_on: field.date({ label: 'Joined on', required: true }),
565
565
  first_event: { resource: Resources.Events.slug, label: 'First event' }
@@ -661,6 +661,7 @@ export default defineNotificationSchedule({
661
661
  This sends the Standard welcome 7 days after each active member's `joined_on`.
662
662
 
663
663
  - `offsets` holds one to 30 distinct ISO 8601 durations in days, hours, or minutes: `'-P30D'` is 30 days before, `'P0D'` is the day, `'-PT2H'` is 2 hours before in exact timing. A duration without a sign counts after the date, so `'P7D'` and `'+P7D'` are the same offset.
664
+ - `stageIds` is optional: one distinct id for each offset, in the order of `offsets`, with letters, digits, `_`, and `-` and at most 100 characters each. A notification keeps its stage id when its offset changes, so it keeps its edits, hold, and history. Without `stageIds`, an offset that stays keeps its id, and a changed offset takes the id of a removed offset. A save from the schedule page writes `stageIds`.
664
665
  - `timing` defaults to `'day'`, which sends at 8 a.m. in the workspace timezone on the day, so `'-PT2H'` sends at 6 a.m. `'exact'` sends at the time of a datetime field or the start of an event schedule field, plus the offset.
665
666
  - `filters` is optional. It is a filter tree, `{ operator, conditions }`, with the shape of a policy `filter`. See [Policies](#policies).
666
667
  - `automaticFilters` is optional and has the shape of `filters`. Of the records that match `filters`, the schedule makes and sends notifications on its own only for the records that also match `automaticFilters`. Staff can still send to the other records from the schedule page. Leave it out to send on its own to every record that matches `filters`. For example, `automaticFilters: { operator: 'and', conditions: [{ field: 'renewal_notice_mode', operator: '=', value: 'automatic' }] }` sends on its own only to the members who chose automatic renewal notices.
@@ -669,7 +670,7 @@ This sends the Standard welcome 7 days after each active member's `joined_on`.
669
670
 
670
671
  A deployed schedule is a System Notification. It sends with no staff identity. `filters` decides who gets it, and `automaticFilters` decides who gets it without a staff action. A file cannot name a person to send as. Staff actions on one generated notification, such as **Send now** or an edit, still act as the person who clicks.
671
672
 
672
- A deploy writes the schedule into the workspace and makes its upcoming notifications. A later deploy updates the same schedule. An offset that stays in the list keeps its notifications, with their edits and holds. When the file is gone, the publish deletes the schedule, as **Delete** on the schedule page does: the notifications that were not sent are archived, and the send history stays. A publish never pauses or enables a schedule. Staff can still pause it on the schedule page, which shows the other fields as read-only, with the path of the file.
673
+ A deploy writes the schedule into the workspace and makes its upcoming notifications. A later deploy updates the same schedule. An offset that stays in the list keeps its notifications, with their edits and holds. When the file is gone, the publish deletes the schedule, as **Delete** on the schedule page does: the notifications that were not sent are archived, and the send history stays. A publish never pauses or enables a schedule. Staff pause and resume it on the schedule page, and that changes no file. A save or a delete on the schedule page, through the API, or through MCP commits the schedule file and deploys it.
673
674
 
674
675
  ### Send from a workflow
675
676
 
@@ -719,6 +720,7 @@ The editor checks the shape of each definition. `diagonal check` and the build a
719
720
  - Each type, template, and layout has a `uid` and a slug of at most 64 characters: its `slug` option, or the name of its folder or file. Each schedule has a `uid`. No two definitions of one kind have the same uid or the same slug, and no two templates of one type have the same slug. No two layouts have the same name. A `name` has 1 to 255 characters.
720
721
  - A `{ file }` Liquid source names a `.liquid` file in the folder of the definition, and the file exists.
721
722
  - A type's `recipient` resource and the resource of each record variable are resources of the workspace. A type has at most 100 variables. A variable key starts with a letter or `_` and has only letters, digits, and `_`. A variable cannot be named `record` or `ref`: templates read the recipient as `{{ record.* }}` and the generators as `{{ ref.* }}`. A variable is a value that the sender passes, so a `field` builder cannot have `default`, `unique`, `index`, `guard`, `computed`, `getter`, `setter`, or `setterWhenEmpty`.
723
+ - Each path in a type's `recipient.relations` follows relation fields from the recipient resource, for example `'account'` or `'account.owner'`. A path has at most 5 relations and passes through a resource only once; only its last relation can return to a resource that the path already reached. A type has at most 100 paths, and no path twice.
722
724
  - A type's `recipient` resource declares the Notifiable capability. When a template of the type has an email, the resource maps `email_field`. When a template has an SMS, the resource maps `phone_field`. The resource check makes sure that `email_field` names an `email` field and `phone_field` names a `phone` or `text` field.
723
725
  - A type has at least one template, and its `defaultTemplate` names one of them.
724
726
  - A template's folder has a `type.ts`. A template has no `type`. A template has an email, an SMS, or both. An email `subject` has 1 to 10,000 characters. `email.layout` names a layout of the workspace files, or `'default'`. An email's `fields` match the fields of its layout and fill each required field. A layout field key differs from `record` and from each variable key of the template's type. The built-in layout has the field `content`. An SMS `body` is not empty.
@@ -739,6 +741,7 @@ A deploy writes each type, template, layout, and schedule into the workspace, ma
739
741
  These rules need server data, so the check does not check them yet:
740
742
 
741
743
  - A built-in `recipient` resource can receive notifications.
744
+ - The relation paths of `recipient.relations` past a built-in resource. A send checks them.
742
745
  - The fields of a built-in resource. The deploy checks them.
743
746
  - The field paths of a schedule's `filters`. The deploy does not check them either: a filter that names a field that does not exist deploys.
744
747
 
package/dist/cli.mjs CHANGED
@@ -307773,6 +307773,7 @@ init_define_globalThis_DIAGONAL_SDK_RUNTIME();
307773
307773
 
307774
307774
  // scripts/lib/runtime-build/notification-values.mjs
307775
307775
  init_define_globalThis_DIAGONAL_SDK_RUNTIME();
307776
+ var RELATION_PATH_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*$/;
307776
307777
  function isPlainObject3(value) {
307777
307778
  return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof FieldBuilderValue);
307778
307779
  }
@@ -312519,9 +312520,13 @@ var GENERATED_NOTIFICATION_TYPES_MODULE = "@app/generated/notification-types";
312519
312520
  var NOTIFICATION_TYPES_EXPORT = "NotificationTypes";
312520
312521
  var TYPE_PATH = /^notifications\/([^/]+)\/type\.ts$/;
312521
312522
  var TEMPLATE_PATH = /^notifications\/([^/]+)\/([^/]+)\.template\.ts$/;
312523
+ var SCHEDULE_PATH = /^notifications\/[^/]+\/[^/]+\.schedule\.ts$/;
312522
312524
  function notificationFolder(path) {
312523
312525
  return path.split("/")[1];
312524
312526
  }
312527
+ function isNotificationSchedulePath(path) {
312528
+ return SCHEDULE_PATH.test(path);
312529
+ }
312525
312530
  function templateSlugOf(path) {
312526
312531
  return TEMPLATE_PATH.exec(path)[2];
312527
312532
  }
@@ -315067,6 +315072,7 @@ var SCHEDULE_PROPERTIES = [
315067
315072
  "dateField",
315068
315073
  "timing",
315069
315074
  "offsets",
315075
+ "stageIds",
315070
315076
  "filters",
315071
315077
  "automaticFilters",
315072
315078
  "recipient",
@@ -315078,7 +315084,7 @@ var MAX_VARIABLES = 100;
315078
315084
  var RECIPIENT_MAX_LENGTH = 255;
315079
315085
  var VARIABLE_PATH_MAX_LENGTH = 500;
315080
315086
  var VARIABLE_PATH_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_.-]*$/;
315081
- var RELATION_PATH_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*$/;
315087
+ var STAGE_ID_PATTERN = /^[a-zA-Z0-9_-]{1,100}$/;
315082
315088
  var DATE_FIELD_TYPES = ["date", "datetime", "event_schedule"];
315083
315089
  var EXACT_TIMING_FIELD_TYPES2 = ["datetime", "event_schedule"];
315084
315090
  var SINGULAR_RELATIONS = ["belongs_to", "has_one"];
@@ -315139,6 +315145,26 @@ function readVariablePaths(value, node, report) {
315139
315145
  }
315140
315146
  return Object.keys(variables).length === entries.length ? variables : null;
315141
315147
  }
315148
+ function readStageIds(value, offsets, node, report) {
315149
+ if (value.stageIds === void 0) {
315150
+ return null;
315151
+ }
315152
+ const stageIdsNode = nodeAt(node, "stageIds");
315153
+ const stageIds = value.stageIds;
315154
+ if (!Array.isArray(stageIds) || stageIds.some((id) => typeof id !== "string" || !STAGE_ID_PATTERN.test(id))) {
315155
+ report(stageIdsNode, '"stageIds" must be a list of ids of letters, digits, "_", and "-", with at most 100 characters each');
315156
+ return null;
315157
+ }
315158
+ if (new Set(stageIds).size !== stageIds.length) {
315159
+ report(stageIdsNode, "The stage ids must be distinct");
315160
+ return null;
315161
+ }
315162
+ if (offsets !== null && stageIds.length !== offsets.length) {
315163
+ report(stageIdsNode, `"stageIds" must have one id for each offset: ${offsets.length}`);
315164
+ return null;
315165
+ }
315166
+ return stageIds;
315167
+ }
315142
315168
  function readFilters(value, key, node, report) {
315143
315169
  const filters = value[key] ?? null;
315144
315170
  if (filters !== null) {
@@ -315174,6 +315200,10 @@ function buildSchedule(file2, value, node, report, context) {
315174
315200
  `"recipient" must be a path of relations from the source record, for example 'member'`
315175
315201
  );
315176
315202
  }
315203
+ const offsets = readOffsets(value.offsets, nodeAt(node, "offsets"), report, {
315204
+ max: MAX_OFFSETS,
315205
+ unsignedAfter: true
315206
+ });
315177
315207
  return {
315178
315208
  path: file2.path,
315179
315209
  uid: checkedUid(file2),
@@ -315185,10 +315215,8 @@ function buildSchedule(file2, value, node, report, context) {
315185
315215
  resource: readSource(value, node, report, context),
315186
315216
  dateField,
315187
315217
  timing,
315188
- offsets: readOffsets(value.offsets, nodeAt(node, "offsets"), report, {
315189
- max: MAX_OFFSETS,
315190
- unsignedAfter: true
315191
- }),
315218
+ offsets,
315219
+ stageIds: readStageIds(value, offsets, node, report),
315192
315220
  filters,
315193
315221
  automaticFilters,
315194
315222
  recipient,
@@ -315377,6 +315405,7 @@ function resolveSchedules(schedules, definitions, context, diagnostics) {
315377
315405
  date_field: schedule.dateField,
315378
315406
  timing: schedule.timing,
315379
315407
  offsets: schedule.offsets,
315408
+ stage_ids: schedule.stageIds,
315380
315409
  filters: schedule.filters,
315381
315410
  automatic_filters: schedule.automaticFilters,
315382
315411
  recipient_path: schedule.recipient,
@@ -315404,6 +315433,8 @@ var LABEL_MAX_LENGTH = 255;
315404
315433
  var SUBJECT_MAX_LENGTH = 1e4;
315405
315434
  var MAX_VARIABLES2 = 100;
315406
315435
  var MAX_REFERENCES = 50;
315436
+ var MAX_RELATIONS = 100;
315437
+ var MAX_RELATION_PATH_DEPTH = 5;
315407
315438
  var VARIABLE_KEY_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
315408
315439
  var RESERVED_VARIABLE_KEYS = /* @__PURE__ */ new Set(["record", "ref"]);
315409
315440
  var REFERENCE_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
@@ -315723,13 +315754,79 @@ function readVariables(value, node, report, context) {
315723
315754
  }
315724
315755
  return variables;
315725
315756
  }
315757
+ function relationPathProblem2(path, resource, resourcesBySlug) {
315758
+ const segments = path.split(".");
315759
+ if (segments.length > MAX_RELATION_PATH_DEPTH) {
315760
+ return `"${path}" has more than ${MAX_RELATION_PATH_DEPTH} relations`;
315761
+ }
315762
+ const visited = [resource.slug];
315763
+ let current = resource;
315764
+ for (const [position, segment] of segments.entries()) {
315765
+ if (current === void 0) {
315766
+ return null;
315767
+ }
315768
+ const prefix2 = segments.slice(0, position + 1).join(".");
315769
+ const field2 = current.fields.find((item) => item.key === segment);
315770
+ if (field2 === void 0 || field2.type !== "relation") {
315771
+ return `"${prefix2}" is not a relation of "${current.slug}"`;
315772
+ }
315773
+ const next = field2.settings.resource;
315774
+ if (position < segments.length - 1 && visited.includes(next)) {
315775
+ return `"${prefix2}" returns to "${next}". A relation path passes through a resource only once`;
315776
+ }
315777
+ visited.push(next);
315778
+ current = resourcesBySlug.get(next);
315779
+ }
315780
+ return null;
315781
+ }
315782
+ function readRelations(recipient, resource, node, report, context) {
315783
+ if (recipient.relations === void 0) {
315784
+ return [];
315785
+ }
315786
+ const relationsNode = nodeAt(node, "relations");
315787
+ if (!Array.isArray(recipient.relations) || recipient.relations.length > MAX_RELATIONS) {
315788
+ report(
315789
+ relationsNode,
315790
+ `"recipient.relations" must be a list of at most ${MAX_RELATIONS} relation paths, for example ['company']`
315791
+ );
315792
+ return [];
315793
+ }
315794
+ const elements = unwrap(relationsNode)?.elements ?? [];
315795
+ const recipientResource = resource === null || context.resourcesBySlug === null ? void 0 : context.resourcesBySlug.get(resource);
315796
+ const relations = [];
315797
+ for (const [index3, path] of recipient.relations.entries()) {
315798
+ const pathNode = elements[index3] ?? relationsNode;
315799
+ if (typeof path !== "string" || !RELATION_PATH_PATTERN.test(path)) {
315800
+ report(
315801
+ pathNode,
315802
+ `"recipient.relations" holds paths of relation fields from the recipient, for example 'company.owner'`
315803
+ );
315804
+ continue;
315805
+ }
315806
+ if (relations.includes(path)) {
315807
+ report(pathNode, `"recipient.relations" has "${path}" twice`);
315808
+ continue;
315809
+ }
315810
+ const problem2 = recipientResource === void 0 ? null : relationPathProblem2(path, recipientResource, context.resourcesBySlug);
315811
+ if (problem2 !== null) {
315812
+ report(pathNode, `"recipient.relations": ${problem2}`);
315813
+ continue;
315814
+ }
315815
+ relations.push(path);
315816
+ }
315817
+ return relations;
315818
+ }
315726
315819
  function buildType(file2, value, node, report, context) {
315727
315820
  const identity = readIdentity(file2, "type", value, node, report);
315728
315821
  let recipientResource = null;
315822
+ let recipientRelations = [];
315729
315823
  if (value.recipient !== void 0) {
315730
315824
  const recipientNode = nodeAt(node, "recipient");
315731
- if (!isPlainObject3(value.recipient) || Object.keys(value.recipient).join() !== "resource") {
315732
- report(recipientNode, '"recipient" must be { resource: Resources.<Name>.slug }');
315825
+ if (!isPlainObject3(value.recipient) || !Object.hasOwn(value.recipient, "resource") || Object.keys(value.recipient).some((key) => key !== "resource" && key !== "relations")) {
315826
+ report(
315827
+ recipientNode,
315828
+ '"recipient" must be { resource: Resources.<Name>.slug, relations?: [<relation path>] }'
315829
+ );
315733
315830
  } else {
315734
315831
  recipientResource = readResource(
315735
315832
  value.recipient.resource,
@@ -315738,6 +315835,7 @@ function buildType(file2, value, node, report, context) {
315738
315835
  '"recipient.resource"',
315739
315836
  context
315740
315837
  );
315838
+ recipientRelations = readRelations(value.recipient, recipientResource, recipientNode, report, context);
315741
315839
  }
315742
315840
  }
315743
315841
  return {
@@ -315746,6 +315844,7 @@ function buildType(file2, value, node, report, context) {
315746
315844
  ...identity,
315747
315845
  recipient_resource: recipientResource,
315748
315846
  recipient_resource_uid: recipientResource === null ? null : resourceUid(recipientResource, context),
315847
+ recipient_relations: recipientRelations,
315749
315848
  variables: readVariables(value, node, report, context),
315750
315849
  defaultTemplate: readNamed(
315751
315850
  value,
@@ -316235,6 +316334,7 @@ function checkNotificationFiles(files, {
316235
316334
  name: type.name,
316236
316335
  recipient_resource: type.recipient_resource,
316237
316336
  recipient_resource_uid: type.recipient_resource_uid,
316337
+ recipient_relations: type.recipient_relations,
316238
316338
  variables: type.variables,
316239
316339
  default_template: defaultTemplate.uid
316240
316340
  });
@@ -321903,7 +322003,6 @@ function declaredHost(domainKey, primaryDomain) {
321903
322003
  var CONFIG_PATH2 = "diagonal.config.ts";
321904
322004
  var NOTIFICATION_TYPE_PATTERN = /^notifications\/[^/]+\/type\.ts$/;
321905
322005
  var NOTIFICATION_TEMPLATE_PATTERN = /^notifications\/[^/]+\/[^/]+\.template\.ts$/;
321906
- var NOTIFICATION_SCHEDULE_PATTERN = /^notifications\/[^/]+\/[^/]+\.schedule\.ts$/;
321907
322006
  var EMAIL_LAYOUT_PATTERN = /^email-layouts\/[^/]+\/layout\.ts$/;
321908
322007
  var IGNORED_DIRECTORIES = /* @__PURE__ */ new Set(["vendor"]);
321909
322008
  var COMPONENT_NAME_PATTERN = /^[A-Z][A-Za-z0-9]*$/;
@@ -322123,7 +322222,7 @@ function classify(path) {
322123
322222
  if (NOTIFICATION_TEMPLATE_PATTERN.test(path)) {
322124
322223
  return "notification_template";
322125
322224
  }
322126
- return NOTIFICATION_SCHEDULE_PATTERN.test(path) ? "notification_schedule" : "ignored";
322225
+ return isNotificationSchedulePath(path) ? "notification_schedule" : "ignored";
322127
322226
  case "email-layouts":
322128
322227
  return EMAIL_LAYOUT_PATTERN.test(path) ? "email_layout" : "ignored";
322129
322228
  case "record-bindings":
package/dist/index.d.ts CHANGED
@@ -1137,7 +1137,15 @@ export type NotificationTypeDefinition = UidOption & {
1137
1137
  * The resource that receives the notification. The resource maps the email
1138
1138
  * and phone fields. Leave it out to send to email addresses.
1139
1139
  */
1140
- recipient?: { resource: ResourceReference };
1140
+ recipient?: {
1141
+ resource: ResourceReference;
1142
+ /**
1143
+ * The relation paths from the recipient resource whose records a send
1144
+ * loads with the recipient, for example `'company'` or `'company.owner'`.
1145
+ * Templates read them as `{{ record.company.name }}`.
1146
+ */
1147
+ relations?: readonly string[];
1148
+ };
1141
1149
  variables?: { readonly [key: string]: NotificationVariableDefinition };
1142
1150
  defaultTemplate: NotificationTemplateReference;
1143
1151
  };
@@ -1259,6 +1267,15 @@ export type NotificationScheduleDefinition = UidOption & {
1259
1267
  * before in exact timing). A duration without a sign counts after the date.
1260
1268
  */
1261
1269
  offsets: readonly string[];
1270
+ /**
1271
+ * One distinct stable id for each offset, in the order of `offsets`: letters,
1272
+ * digits, `_`, and `-`, up to 100 characters. A notification keeps its stage
1273
+ * id when you change its offset, so it keeps its edits, hold, and history.
1274
+ * Without `stageIds`, an offset that stays keeps its id, and a changed offset
1275
+ * takes the id of a removed offset. The app writes `stageIds` when it saves
1276
+ * the schedule.
1277
+ */
1278
+ stageIds?: readonly string[];
1262
1279
  /** Only the source records that match, for automatic and staff sends. */
1263
1280
  filters?: FilterGroup;
1264
1281
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagonal-hq/cli",
3
- "version": "0.33.2",
3
+ "version": "0.33.4",
4
4
  "description": "The diagonal CLI and the configuration helpers for file-based Diagonal workspaces.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {