@aglyn/plugins-crm 1.19.0 → 1.20.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.
Files changed (76) hide show
  1. package/package.json +14 -14
  2. package/src/lib/components/log-activity-dialog.d.ts +2 -0
  3. package/src/lib/components/log-activity-dialog.js +11 -2
  4. package/src/lib/components/log-activity-dialog.js.map +1 -1
  5. package/src/lib/components/record-sharing-card.js +8 -7
  6. package/src/lib/components/record-sharing-card.js.map +1 -1
  7. package/src/lib/components/settings-section.d.ts +10 -3
  8. package/src/lib/components/settings-section.js +286 -35
  9. package/src/lib/components/settings-section.js.map +1 -1
  10. package/src/lib/components/share-records-dialog.d.ts +17 -3
  11. package/src/lib/components/share-records-dialog.js +59 -45
  12. package/src/lib/components/share-records-dialog.js.map +1 -1
  13. package/src/lib/components/sharing-rule-drawer.d.ts +13 -3
  14. package/src/lib/components/sharing-rule-drawer.js +51 -40
  15. package/src/lib/components/sharing-rule-drawer.js.map +1 -1
  16. package/src/lib/components/sharing-rules-card.js +2 -1
  17. package/src/lib/components/sharing-rules-card.js.map +1 -1
  18. package/src/lib/declarations.server.js +10 -1
  19. package/src/lib/declarations.server.js.map +1 -1
  20. package/src/lib/hooks/use-campaign-filing-log.js +10 -2
  21. package/src/lib/hooks/use-campaign-filing-log.js.map +1 -1
  22. package/src/lib/hooks/use-crm-sharing.d.ts +8 -0
  23. package/src/lib/hooks/use-crm-sharing.js +20 -0
  24. package/src/lib/hooks/use-crm-sharing.js.map +1 -1
  25. package/src/lib/model/crm-sharing-api.d.ts +5 -2
  26. package/src/lib/model/crm-sharing-api.js +3 -0
  27. package/src/lib/model/crm-sharing-api.js.map +1 -1
  28. package/src/lib/model/crm-sharing.d.ts +153 -5
  29. package/src/lib/model/crm-sharing.js +243 -20
  30. package/src/lib/model/crm-sharing.js.map +1 -1
  31. package/src/lib/model/lead-filters.d.ts +1 -1
  32. package/src/lib/server/add-contact-by-hand.js +11 -4
  33. package/src/lib/server/add-contact-by-hand.js.map +1 -1
  34. package/src/lib/server/api-v1/crm-activities.js +2 -2
  35. package/src/lib/server/api-v1/crm-activities.js.map +1 -1
  36. package/src/lib/server/api-v1/crm-companies.js +2 -2
  37. package/src/lib/server/api-v1/crm-companies.js.map +1 -1
  38. package/src/lib/server/api-v1/crm-deals.js +2 -2
  39. package/src/lib/server/api-v1/crm-deals.js.map +1 -1
  40. package/src/lib/server/api-v1/crm-shared.d.ts +16 -0
  41. package/src/lib/server/api-v1/crm-shared.js +12 -1
  42. package/src/lib/server/api-v1/crm-shared.js.map +1 -1
  43. package/src/lib/server/api-v1/crm-tasks.js +2 -2
  44. package/src/lib/server/api-v1/crm-tasks.js.map +1 -1
  45. package/src/lib/server/associate-company-by-domain.d.ts +1 -1
  46. package/src/lib/server/associate-company-by-domain.js +4 -4
  47. package/src/lib/server/associate-company-by-domain.js.map +1 -1
  48. package/src/lib/server/automation-steps.js +24 -12
  49. package/src/lib/server/automation-steps.js.map +1 -1
  50. package/src/lib/server/campaign-filing-activity.js +7 -3
  51. package/src/lib/server/campaign-filing-activity.js.map +1 -1
  52. package/src/lib/server/convert-host-lead.js +13 -4
  53. package/src/lib/server/convert-host-lead.js.map +1 -1
  54. package/src/lib/server/crm-default-sharing.d.ts +86 -0
  55. package/src/lib/server/crm-default-sharing.js +409 -0
  56. package/src/lib/server/crm-default-sharing.js.map +1 -0
  57. package/src/lib/server/crm-inbound-email.js +2 -2
  58. package/src/lib/server/crm-inbound-email.js.map +1 -1
  59. package/src/lib/server/crm-sharing-runs.d.ts +71 -0
  60. package/src/lib/server/crm-sharing-runs.js +221 -0
  61. package/src/lib/server/crm-sharing-runs.js.map +1 -0
  62. package/src/lib/server/crm-sharing.d.ts +24 -1
  63. package/src/lib/server/crm-sharing.js +183 -31
  64. package/src/lib/server/crm-sharing.js.map +1 -1
  65. package/src/lib/server/email-send.js +5 -2
  66. package/src/lib/server/email-send.js.map +1 -1
  67. package/src/lib/server/record-timeline.d.ts +2 -1
  68. package/src/lib/server/record-timeline.js +11 -6
  69. package/src/lib/server/record-timeline.js.map +1 -1
  70. package/src/lib/server/task-routes.js +10 -6
  71. package/src/lib/server/task-routes.js.map +1 -1
  72. package/src/lib/server.js +23 -1
  73. package/src/lib/server.js.map +1 -1
  74. package/src/lib/transfer/common.d.ts +5 -1
  75. package/src/lib/transfer/common.js +8 -5
  76. package/src/lib/transfer/common.js.map +1 -1
@@ -54,6 +54,20 @@
54
54
  * own field, `writeTo`, present only on a shared record: the held tokens
55
55
  * and the tokens of grants whose access is `edit`. The Firestore rules and
56
56
  * the CRM's routes check it; a read-only grant is the default.
57
+ *
58
+ * ## All sites except some (AGL-4047)
59
+ *
60
+ * A grant or a rule can share with every site but the ones it leaves out:
61
+ * `except` beside `tokens` (a rule's `targets`). Readers still enforce one
62
+ * flat list, so the grant's `tokens` are the sites it reaches, worked out
63
+ * from the workspace's sites every time the record's sharing is recomputed,
64
+ * and a site added later joins on the next recompute, which the workspace's
65
+ * own site change starts (`server/crm-sharing-runs.ts`). `planRecordSharing`
66
+ * reads `tokens` as it always has, so `visibleTo`, `added` and `writeTo` come
67
+ * out the same way. A site left out that captured the person still holds
68
+ * them: held tokens are never a grant's to take away. One grant reaches at
69
+ * most {@link CRM_SHARING_MAX_INCLUDED} sites, since a record's search fields
70
+ * grow by site; past that it keeps the sites it has and stops widening.
57
71
  */ import { _ as _extends } from "@swc/helpers/_/_extends";
58
72
  import { heldScopeTokens, hostIdsFromScope, hostScopeToken, isScopeToken, MAX_SCOPE_HOSTS, ORG_SCOPE_TOKEN, parseScopeToken, SCOPE_GRANTS_FIELD } from "@aglyn/aglyn/app-utils/scope-tokens";
59
73
  import { isLeadSourceDirection, LEAD_SOURCE_DIRECTION_LABELS, leadSourceDirectionOf, STANDARD_LEAD_SOURCES } from "./lead-source-direction.js";
@@ -92,6 +106,13 @@ export function isCrmShareAccess(value) {
92
106
  /** The record field its grants live under — the core's name for it. */ export const CRM_SHARING_FIELD = SCOPE_GRANTS_FIELD;
93
107
  /** The record field naming who may WRITE it, present only while it is shared. */ export const CRM_WRITE_SCOPE_FIELD = 'writeTo';
94
108
  /** The array field a rule's removal queries. */ export const CRM_SHARING_RULE_IDS_PATH = `${CRM_SHARING_FIELD}.ruleIds`;
109
+ /** The field a site change queries for records with an all-except grant (AGL-4047). */ export const CRM_SHARING_EXCEPT_PATH = `${CRM_SHARING_FIELD}.hasExcept`;
110
+ /**
111
+ * The most sites one all-except grant reaches (AGL-4047). A record's scoped
112
+ * search fields grow by site (`scopedSearchTokens`), so past this the grant
113
+ * keeps the sites it has and stops widening, and a new share is refused.
114
+ */ export const CRM_SHARING_MAX_INCLUDED = MAX_SCOPE_HOSTS;
115
+ /** The key of a record's one hand share with all sites except some. */ export const MANUAL_EXCEPT_GRANT_KEY = 'manual_except';
95
116
  /** A hand share is one grant per target, so each is unshared on its own. */ export function manualGrantKey(token) {
96
117
  const parsed = parseScopeToken(token);
97
118
  if (!parsed) throw new Error(`not a scope token: ${token}`);
@@ -106,11 +127,21 @@ function tokenList(raw) {
106
127
  ...new Set(raw.filter(isScopeToken))
107
128
  ];
108
129
  }
130
+ /** Host tokens only, de-duplicated: what an `except` list may hold. */ function hostTokenList(raw) {
131
+ return tokenList(raw).filter((token)=>{
132
+ var _parseScopeToken;
133
+ return ((_parseScopeToken = parseScopeToken(token)) == null ? void 0 : _parseScopeToken.kind) === 'host';
134
+ });
135
+ }
109
136
  function readGrant(raw) {
110
137
  if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
111
138
  const value = raw;
112
139
  const tokens = tokenList(value['tokens']);
113
- if (!tokens.length) return null;
140
+ const except = hostTokenList(value['except']);
141
+ // An all-except grant can reach no site for a while, when every site but
142
+ // the ones it leaves out is gone. It still says who it shares with.
143
+ if (!tokens.length && !except.length) return null;
144
+ if (except.length && tokens.includes(ORG_SCOPE_TOKEN)) return null;
114
145
  const source = value['source'] === 'rule' ? 'rule' : value['source'] === 'manual' ? 'manual' : null;
115
146
  if (!source) return null;
116
147
  const ruleId = typeof value['ruleId'] === 'string' ? value['ruleId'] : '';
@@ -120,7 +151,10 @@ function readGrant(raw) {
120
151
  const atMs = typeof value['atMs'] === 'number' && Number.isFinite(value['atMs']) ? value['atMs'] : 0;
121
152
  return _extends({
122
153
  source,
123
- tokens,
154
+ tokens
155
+ }, except.length ? {
156
+ except
157
+ } : {}, {
124
158
  access: value['access'] === 'edit' ? 'edit' : 'read'
125
159
  }, ruleId ? {
126
160
  ruleId
@@ -209,7 +243,7 @@ function readGrant(raw) {
209
243
  ...added
210
244
  ],
211
245
  writeTo,
212
- sharing: {
246
+ sharing: _extends({
213
247
  grants: Object.fromEntries(entries),
214
248
  tokens,
215
249
  added,
@@ -217,13 +251,18 @@ function readGrant(raw) {
217
251
  orgWideHeldBy: added.includes(ORG_SCOPE_TOKEN) ? [
218
252
  ...held
219
253
  ] : []
220
- }
254
+ }, entries.some(([, grant])=>{
255
+ var _grant_except;
256
+ return (_grant_except = grant.except) == null ? void 0 : _grant_except.length;
257
+ }) ? {
258
+ hasExcept: true
259
+ } : {})
221
260
  };
222
261
  }
223
262
  const sameList = (a, b)=>(a != null ? a : null) === null ? (b != null ? b : null) === null : (b != null ? b : null) !== null && a.length === b.length && a.every((entry, at)=>entry === b[at]);
224
263
  const sameGrant = (a, b)=>{
225
- var _a_ruleId, _b_ruleId, _a_byUid, _b_byUid, _a_byName, _b_byName;
226
- return !!a && !!b && a.source === b.source && a.access === b.access && ((_a_ruleId = a.ruleId) != null ? _a_ruleId : '') === ((_b_ruleId = b.ruleId) != null ? _b_ruleId : '') && ((_a_byUid = a.byUid) != null ? _a_byUid : '') === ((_b_byUid = b.byUid) != null ? _b_byUid : '') && ((_a_byName = a.byName) != null ? _a_byName : '') === ((_b_byName = b.byName) != null ? _b_byName : '') && a.atMs === b.atMs && sameList(a.tokens, b.tokens);
264
+ var _a_ruleId, _b_ruleId, _a_byUid, _b_byUid, _a_byName, _b_byName, _a_except, _b_except;
265
+ return !!a && !!b && a.source === b.source && a.access === b.access && ((_a_ruleId = a.ruleId) != null ? _a_ruleId : '') === ((_b_ruleId = b.ruleId) != null ? _b_ruleId : '') && ((_a_byUid = a.byUid) != null ? _a_byUid : '') === ((_b_byUid = b.byUid) != null ? _b_byUid : '') && ((_a_byName = a.byName) != null ? _a_byName : '') === ((_b_byName = b.byName) != null ? _b_byName : '') && a.atMs === b.atMs && sameList(a.tokens, b.tokens) && sameList((_a_except = a.except) != null ? _a_except : [], (_b_except = b.except) != null ? _b_except : []);
227
266
  };
228
267
  /** Whether writing `plan` would change what the record stores. */ export function sharingPlanChanges(record, plan) {
229
268
  if (!sameList(tokenList(record['visibleTo']), plan.visibleTo)) return true;
@@ -236,7 +275,7 @@ const sameGrant = (a, b)=>{
236
275
  if (keys.length !== Object.keys(grants).length) return true;
237
276
  if (!keys.every((key)=>sameGrant(grants[key], plan.sharing.grants[key]))) return true;
238
277
  const sharing = stored != null ? stored : {};
239
- return !sameList(tokenList(sharing['tokens']), plan.sharing.tokens) || !sameList(tokenList(sharing['added']), plan.sharing.added) || !sameList(Array.isArray(sharing['ruleIds']) ? sharing['ruleIds'] : [], plan.sharing.ruleIds) || !sameList(tokenList(sharing['orgWideHeldBy']), plan.sharing.orgWideHeldBy);
278
+ return !sameList(tokenList(sharing['tokens']), plan.sharing.tokens) || !sameList(tokenList(sharing['added']), plan.sharing.added) || !sameList(Array.isArray(sharing['ruleIds']) ? sharing['ruleIds'] : [], plan.sharing.ruleIds) || !sameList(tokenList(sharing['orgWideHeldBy']), plan.sharing.orgWideHeldBy) || sharing['hasExcept'] === true !== (plan.sharing.hasExcept === true);
240
279
  }
241
280
  /*==========================================
242
281
  * SHARING RULES: `crm.sharingRules` on the org document.
@@ -295,7 +334,8 @@ function stringList(raw, max = CRM_SHARING_CRITERION_MAX) {
295
334
  const id = String((_value_id = value['id']) != null ? _value_id : '');
296
335
  if (!RULE_ID.test(id)) return null;
297
336
  if (!isCrmSharingObject(value['object'])) return null;
298
- const targets = normalizeShareTargets(tokenList(value['targets']));
337
+ const except = hostTokenList(value['except']);
338
+ const targets = except.length ? hostTokenList(value['targets']) : normalizeShareTargets(tokenList(value['targets']));
299
339
  if (!targets) return null;
300
340
  const rawCriteria = (_value_criteria = value['criteria']) != null ? _value_criteria : {};
301
341
  const criteria = {};
@@ -313,7 +353,10 @@ function stringList(raw, max = CRM_SHARING_CRITERION_MAX) {
313
353
  enabled: value['enabled'] !== false,
314
354
  sourceHostIds: stringList(value['sourceHostIds'], MAX_SCOPE_HOSTS),
315
355
  criteria,
316
- targets,
356
+ targets
357
+ }, except.length ? {
358
+ except
359
+ } : {}, {
317
360
  access: value['access'] === 'edit' ? 'edit' : 'read'
318
361
  }, value['deleting'] === true ? {
319
362
  deleting: true
@@ -370,6 +413,49 @@ function stringList(raw, max = CRM_SHARING_CRITERION_MAX) {
370
413
  if (!Array.isArray(input)) return null;
371
414
  return normalizeShareTargets(input.map((id)=>String(id != null ? id : '').trim()).filter((id)=>id && !id.includes('/') && !id.includes(':')).map(hostScopeToken));
372
415
  }
416
+ /**
417
+ * All sites except some, from a request (AGL-4047):
418
+ * `{ mode: 'allExcept', exceptHostIds }`, or null when the input is not that
419
+ * choice. Whether the sites are the workspace's, and what the choice reaches,
420
+ * is `sharingFromChoice`'s to answer against the workspace's sites.
421
+ */ export function shareExceptChoiceFrom(input) {
422
+ if (!input || typeof input !== 'object' || Array.isArray(input)) return null;
423
+ const value = input;
424
+ if (value['mode'] !== 'allExcept' || !Array.isArray(value['exceptHostIds'])) return null;
425
+ const exceptHostIds = [
426
+ ...new Set(value['exceptHostIds'].map((id)=>String(id != null ? id : '').trim()).filter((id)=>id && !id.includes('/') && !id.includes(':')))
427
+ ].slice(0, 1000);
428
+ return {
429
+ mode: 'allExcept',
430
+ exceptHostIds
431
+ };
432
+ }
433
+ /**
434
+ * The sites `orgs/{orgId}.hosts` names: the list the sweep keeps every
435
+ * all-except document level with (`orgHostIdsFromDoc`), read the same way
436
+ * here so the CRM's grants and the sweep never disagree about a site.
437
+ */ export function crmOrgHostIds(org) {
438
+ const hosts = org == null ? void 0 : org['hosts'];
439
+ if (!hosts || typeof hosts !== 'object' || Array.isArray(hosts)) return [];
440
+ return Object.entries(hosts).filter(([, on])=>Boolean(on)).map(([hostId])=>hostId);
441
+ }
442
+ /**
443
+ * The sites an all-except grant reaches (AGL-4047): the workspace's sites
444
+ * less the ones it leaves out, sorted. `orgHostIds` unknown keeps what the
445
+ * grant reached last. Past {@link CRM_SHARING_MAX_INCLUDED} it stops
446
+ * widening: it keeps the sites it reached that are still the workspace's.
447
+ */ export function sitesAllExcept(except, orgHostIds, previous) {
448
+ const left = new Set(hostIdsFromScope(except));
449
+ const before = hostIdsFromScope(hostTokenList(previous)).filter((id)=>!left.has(id));
450
+ if (!orgHostIds) return before.map(hostScopeToken);
451
+ const org = [
452
+ ...new Set(orgHostIds.filter(Boolean))
453
+ ];
454
+ const included = org.filter((id)=>!left.has(id)).sort();
455
+ if (included.length <= CRM_SHARING_MAX_INCLUDED) return included.map(hostScopeToken);
456
+ const now = new Set(org);
457
+ return before.filter((id)=>now.has(id)).map(hostScopeToken);
458
+ }
373
459
  const str = (value)=>typeof value === 'string' ? value.trim() : '';
374
460
  const strs = (value)=>Array.isArray(value) ? value.map(str).filter(Boolean) : [];
375
461
  /**
@@ -450,24 +536,51 @@ const anyOf = (wanted, have, fold = false)=>{
450
536
  const current = readRecordGrants(record);
451
537
  const next = {};
452
538
  for (const [key, grant] of Object.entries(current)){
453
- if (grant.source === 'manual') next[key] = grant;
539
+ var _grant_except;
540
+ if (grant.source !== 'manual') continue;
541
+ // A hand share with all sites except some follows the workspace's sites.
542
+ next[key] = ((_grant_except = grant.except) == null ? void 0 : _grant_except.length) ? _extends({}, grant, {
543
+ tokens: sitesAllExcept(grant.except, context.orgHostIds, grant.tokens)
544
+ }) : grant;
454
545
  }
455
546
  for (const rule of activeSharingRules(rules, object)){
547
+ var _rule_except, _before_except;
548
+ var _before_except1;
456
549
  if (!crmSharingRuleMatches(rule, object, record, context)) continue;
457
550
  const key = ruleGrantKey(rule.id);
458
551
  const before = current[key];
459
- next[key] = {
552
+ const except = (_rule_except = rule.except) != null ? _rule_except : [];
553
+ const tokens = except.length ? sitesAllExcept(except, context.orgHostIds, (before == null ? void 0 : (_before_except1 = before.except) == null ? void 0 : _before_except1.length) ? before.tokens : rule.targets) : [
554
+ ...rule.targets
555
+ ];
556
+ next[key] = _extends({
460
557
  source: 'rule',
461
558
  ruleId: rule.id,
462
- tokens: [
463
- ...rule.targets
464
- ],
559
+ tokens
560
+ }, except.length ? {
561
+ except: [
562
+ ...except
563
+ ]
564
+ } : {}, {
465
565
  access: rule.access,
466
- atMs: before && sameList(before.tokens, rule.targets) && before.access === rule.access ? before.atMs : atMs
467
- };
566
+ atMs: before && sameList(before.tokens, tokens) && sameList((_before_except = before.except) != null ? _before_except : [], except) && before.access === rule.access ? before.atMs : atMs
567
+ });
468
568
  }
469
569
  return next;
470
570
  }
571
+ /**
572
+ * Whether working out this record's sharing needs the workspace's sites: a
573
+ * grant on it, or an active rule for its kind, leaves sites out (AGL-4047).
574
+ * Everything else plans from the record and the rules alone.
575
+ */ export function sharingAsksOrgSites(object, record, rules) {
576
+ return Object.values(readRecordGrants(record)).some((grant)=>{
577
+ var _grant_except;
578
+ return Boolean((_grant_except = grant.except) == null ? void 0 : _grant_except.length);
579
+ }) || activeSharingRules(rules, object).some((rule)=>{
580
+ var _rule_except;
581
+ return Boolean((_rule_except = rule.except) == null ? void 0 : _rule_except.length);
582
+ });
583
+ }
471
584
  /** The hand shares after sharing with `targets`, by `by`. */ export function withManualShares(grants, targets, access, by, atMs) {
472
585
  const next = _extends({}, grants);
473
586
  for (const token of targets){
@@ -488,6 +601,34 @@ const anyOf = (wanted, have, fold = false)=>{
488
601
  }
489
602
  return next;
490
603
  }
604
+ /**
605
+ * The hand shares after sharing with all sites except some (AGL-4047): one
606
+ * grant, {@link MANUAL_EXCEPT_GRANT_KEY}, replacing any earlier one. `tokens`
607
+ * are the sites it reaches today.
608
+ */ export function withManualExceptShare(grants, share, access, by, atMs) {
609
+ var _by_name;
610
+ const name = String((_by_name = by.name) != null ? _by_name : '').trim();
611
+ return _extends({}, grants, {
612
+ [MANUAL_EXCEPT_GRANT_KEY]: _extends({
613
+ source: 'manual',
614
+ tokens: [
615
+ ...share.tokens
616
+ ],
617
+ except: [
618
+ ...share.except
619
+ ],
620
+ access,
621
+ byUid: by.uid
622
+ }, name ? {
623
+ byName: name
624
+ } : {}, {
625
+ atMs
626
+ })
627
+ });
628
+ }
629
+ /** The hand shares after taking away the share with all sites except some. */ export function withoutManualExceptShare(grants) {
630
+ return Object.fromEntries(Object.entries(grants).filter(([key])=>key !== MANUAL_EXCEPT_GRANT_KEY));
631
+ }
491
632
  /** The hand shares after unsharing `targets`. Rule grants are the rule's to take away. */ export function withoutManualShares(grants, targets) {
492
633
  const drop = new Set(targets.filter(isScopeToken).map(manualGrantKey));
493
634
  return Object.fromEntries(Object.entries(grants).filter(([key])=>!drop.has(key)));
@@ -551,7 +692,22 @@ const anyOf = (wanted, have, fold = false)=>{
551
692
  ];
552
693
  const grants = Object.entries(readRecordGrants(record));
553
694
  for (const [key, grant] of grants){
695
+ var _grant_except;
554
696
  if (grant.source !== 'manual') continue;
697
+ if ((_grant_except = grant.except) == null ? void 0 : _grant_except.length) {
698
+ lines.push(_extends({
699
+ kind: 'manualExcept',
700
+ key,
701
+ tokens: grant.tokens,
702
+ except: grant.except,
703
+ access: grant.access
704
+ }, grant.byName ? {
705
+ byName: grant.byName
706
+ } : {}, {
707
+ atMs: grant.atMs
708
+ }));
709
+ continue;
710
+ }
555
711
  lines.push(_extends({
556
712
  kind: 'manual',
557
713
  key,
@@ -564,19 +720,31 @@ const anyOf = (wanted, have, fold = false)=>{
564
720
  }));
565
721
  }
566
722
  for (const [key, grant] of grants){
723
+ var _grant_except1;
567
724
  if (grant.source !== 'rule' || !grant.ruleId) continue;
568
- lines.push({
725
+ lines.push(_extends({
569
726
  kind: 'rule',
570
727
  key,
571
728
  ruleId: grant.ruleId,
572
- tokens: grant.tokens,
729
+ tokens: grant.tokens
730
+ }, ((_grant_except1 = grant.except) == null ? void 0 : _grant_except1.length) ? {
731
+ except: grant.except
732
+ } : {}, {
573
733
  access: grant.access,
574
734
  atMs: grant.atMs
575
- });
735
+ }));
576
736
  }
577
737
  return lines;
578
738
  }
579
- /** "All sites", a site's name, or "N sites". */ export function describeShareTargets(tokens, siteName) {
739
+ /**
740
+ * "All sites", a site's name, or "N sites"; for a share with all sites except
741
+ * some (AGL-4047), "All sites except Blog", "All sites except Blog and Shop"
742
+ * or "All sites except 3 sites".
743
+ */ export function describeShareTargets(tokens, siteName, except) {
744
+ const left = except ? hostIdsFromScope(except) : [];
745
+ if (left.length === 1) return `All sites except ${siteName(left[0])}`;
746
+ if (left.length === 2) return `All sites except ${siteName(left[0])} and ${siteName(left[1])}`;
747
+ if (left.length > 2) return `All sites except ${left.length} sites`;
580
748
  if (tokens.includes(ORG_SCOPE_TOKEN)) return 'All sites';
581
749
  const ids = hostIdsFromScope(tokens);
582
750
  if (ids.length === 1) return siteName(ids[0]);
@@ -620,5 +788,60 @@ const anyOf = (wanted, have, fold = false)=>{
620
788
  *=========================================*/ /** `POST /api/crm/sharing` — every sharing act, by `action`. */ export const CRM_SHARING_ROUTE = 'crm/sharing';
621
789
  export const crmSharingRouteUrl = ()=>`/api/${CRM_SHARING_ROUTE}`;
622
790
  /** Records one share or unshare may name. */ export const CRM_SHARING_IDS_MAX = 200;
791
+ /*==========================================
792
+ * "APPLY TO EXISTING RECORDS" (AGL-4047).
793
+ *
794
+ * The CRM default stamps NEW records. Under "All sites except…" a member
795
+ * holding "Change CRM settings" can bring the records the workspace already
796
+ * has to it: every contact, lead, company, deal, task and logged activity is
797
+ * shared with every site but the ones left out, and the sites that captured
798
+ * a person keep them. Only that explicit run changes an existing record,
799
+ * never the setting itself. It walks each collection by a cursor in budgeted
800
+ * passes, its progress mirrored here on the org document.
801
+ *=========================================*/ /** The collections the run walks, in order: the cursor names a place in this list. */ export const CRM_DEFAULT_APPLY_COLLECTIONS = [
802
+ 'contacts',
803
+ 'leads',
804
+ 'companies',
805
+ 'deals',
806
+ 'crmTasks',
807
+ 'crmActivities'
808
+ ];
809
+ export function isCrmDefaultApplyCollection(value) {
810
+ return typeof value === 'string' && CRM_DEFAULT_APPLY_COLLECTIONS.includes(value);
811
+ }
812
+ /**
813
+ * What a browser writes itself that no sharing rule applies to, yet the CRM
814
+ * default "All sites except…" stamps: a logged activity. Its `evaluate`
815
+ * follow-up only marks it, when its sites are exactly the default's, so a
816
+ * site added later sees it (AGL-4047).
817
+ */ export const CRM_DEFAULT_MARK_ONLY_COLLECTIONS = [
818
+ 'crmActivities'
819
+ ];
820
+ export function isCrmDefaultMarkOnlyCollection(value) {
821
+ return typeof value === 'string' && CRM_DEFAULT_MARK_ONLY_COLLECTIONS.includes(value);
822
+ }
823
+ /** Where the run's progress is mirrored on the org document, for the settings card. */ export const CRM_DEFAULT_APPLY_RUN_PATH = 'crm.defaultApplyRun';
824
+ /** The run as the org document mirrors it, cleaned; null when none was ever started. */ export function readCrmDefaultApplyRun(org) {
825
+ const crm = (org != null ? org : {})['crm'];
826
+ const raw = crm && typeof crm === 'object' && !Array.isArray(crm) ? crm['defaultApplyRun'] : undefined;
827
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
828
+ const value = raw;
829
+ const cursor = value['cursor'];
830
+ return _extends({
831
+ status: value['status'] === 'done' || value['status'] === 'failed' ? value['status'] : 'running',
832
+ processed: Number(value['processed']) || 0,
833
+ changed: Number(value['changed']) || 0,
834
+ capped: Number(value['capped']) || 0,
835
+ cursor: cursor && isCrmDefaultApplyCollection(cursor['collection']) ? {
836
+ collection: cursor['collection'],
837
+ afterId: typeof cursor['afterId'] === 'string' ? cursor['afterId'] : null
838
+ } : null,
839
+ startedAtMs: Number(value['startedAtMs']) || 0
840
+ }, typeof value['finishedAtMs'] === 'number' ? {
841
+ finishedAtMs: value['finishedAtMs']
842
+ } : {}, typeof value['error'] === 'string' ? {
843
+ error: value['error']
844
+ } : {});
845
+ }
623
846
 
624
847
  //# sourceMappingURL=crm-sharing.js.map