@aglyn/tenant-data-admin 1.0.0-beta.277 → 1.0.0-beta.283

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 (34) hide show
  1. package/package.json +7 -7
  2. package/src/lib/server/account-emails.js +18 -4
  3. package/src/lib/server/account-emails.js.map +1 -1
  4. package/src/lib/server/bundle-links.d.ts +5 -0
  5. package/src/lib/server/bundle-links.js +4 -2
  6. package/src/lib/server/bundle-links.js.map +1 -1
  7. package/src/lib/server/erase.js +9 -0
  8. package/src/lib/server/erase.js.map +1 -1
  9. package/src/lib/server/media-strong-digest.js +33 -5
  10. package/src/lib/server/media-strong-digest.js.map +1 -1
  11. package/src/lib/server/organizations.js +20 -0
  12. package/src/lib/server/organizations.js.map +1 -1
  13. package/src/lib/server/owner-handoff-emails.d.ts +43 -0
  14. package/src/lib/server/owner-handoff-emails.js +134 -0
  15. package/src/lib/server/owner-handoff-emails.js.map +1 -0
  16. package/src/lib/server/personal-data-export.js +18 -4
  17. package/src/lib/server/personal-data-export.js.map +1 -1
  18. package/src/lib/server/render-monitor.d.ts +8 -4
  19. package/src/lib/server/render-monitor.js +12 -8
  20. package/src/lib/server/render-monitor.js.map +1 -1
  21. package/src/lib/server/site-care-inbound.d.ts +75 -4
  22. package/src/lib/server/site-care-inbound.js +236 -26
  23. package/src/lib/server/site-care-inbound.js.map +1 -1
  24. package/src/lib/server/site-care-reconcile.d.ts +36 -2
  25. package/src/lib/server/site-care-reconcile.js +219 -54
  26. package/src/lib/server/site-care-reconcile.js.map +1 -1
  27. package/src/lib/server/site-care-requests.d.ts +43 -1
  28. package/src/lib/server/site-care-requests.js +117 -6
  29. package/src/lib/server/site-care-requests.js.map +1 -1
  30. package/src/lib/server/upgrade-proposal.d.ts +6 -1
  31. package/src/lib/server/upgrade-proposal.js.map +1 -1
  32. package/src/lib/server/upload-cors-reconcile.d.ts +2 -2
  33. package/src/lib/server/upload-cors-reconcile.js +2 -2
  34. package/src/lib/server/upload-cors-reconcile.js.map +1 -1
@@ -18,7 +18,7 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
18
18
  import { createResourceUid } from "@aglyn/aglyn/app-utils/create-resource-uid";
19
19
  import { describeHours, ownerApprovalSentence, readOwnerApprovalInput, readOwnerApprovalRecord } from "@aglyn/aglyn/app-utils/owner-approval-record";
20
20
  import { hasSiteCare, resolveEffectivePlan, SITE_CARE_EXTRA_HOUR_USD } from "@aglyn/aglyn/app-utils/plan-entitlements";
21
- import { allocateSiteCareMinutes, closeMinutesFromTimer, formatSiteCareDate, formatSiteCareMinutes, formatSiteCareUsd, isSiteCareMonthKey, isSiteCarePublishAction, readSiteCareMinutes, SITE_CARE_BODY_MAX, SITE_CARE_LIVE_CHARGE_STATUSES, SITE_CARE_RESPONSE_BUSINESS_DAYS, SITE_CARE_SUBJECT_MAX, SITE_CARE_TICKET_KIND, siteCareChargeWords, siteCareCloseVerdict, siteCareExtraTimeCents, siteCareExtraTimeDescription, siteCareIncludedPool, siteCareMonthKey, siteCareMonthSentence, siteCareMonthStatement, siteCareOrgMonth, siteCarePublishSentence, siteCareStatementRequest } from "@aglyn/aglyn/app-utils/site-care";
21
+ import { allocateSiteCareMinutes, closeMinutesFromTimer, formatSiteCareDate, formatSiteCareMinutes, formatSiteCareUsd, isSiteCareMonthKey, isSiteCarePublishAction, otherSiteCareCandidates, readSiteCareCandidates, readSiteCareMinutes, SITE_CARE_BODY_MAX, SITE_CARE_LIVE_CHARGE_STATUSES, SITE_CARE_RESPONSE_BUSINESS_DAYS, SITE_CARE_STAFF_COLLECTION, SITE_CARE_SUBJECT_MAX, SITE_CARE_TICKET_KIND, siteCareCandidatesSentence, siteCareChargeWords, siteCareCloseVerdict, siteCareExtraTimeCents, siteCareExtraTimeDescription, siteCareIncludedPool, siteCareMonthKey, siteCareMonthSentence, siteCareMonthStatement, siteCareMoveBlockedReason, siteCareOrgMonth, siteCarePublishSentence, siteCareStatementRequest } from "@aglyn/aglyn/app-utils/site-care";
22
22
  import { addBusinessDays } from "@aglyn/aglyn/app-utils/support-tiers";
23
23
  import { SITE_CARE_APPROVAL_EMAIL_KEY, SITE_CARE_CHARGE_REVERSED_EMAIL_KEY, SITE_CARE_QUOTE_EMAIL_KEY, SITE_CARE_REQUEST_CORRECTED_EMAIL_KEY, SITE_CARE_REQUEST_DONE_EMAIL_KEY, SITE_CARE_REQUEST_RECEIVED_EMAIL_KEY, SITE_CARE_STATEMENT_EMAIL_KEY } from "@aglyn/shared-util-email";
24
24
  import { createHash } from "node:crypto";
@@ -38,7 +38,11 @@ import { createExtraTimeItem, creditExtraTimeItem, deleteExtraTimeItem, extraTim
38
38
  * the publish that shipped it. Beside it, an append-only history
39
39
  * (`supportTickets/{id}/siteCareHistory`), and per workspace and month a
40
40
  * document for goodwill minutes with its own history
41
- * (`orgs/{orgId}/siteCareMonths/{YYYY-MM}`). None of it is in the Firestore
41
+ * (`orgs/{orgId}/siteCareMonths/{YYYY-MM}`). An emailed request from a
42
+ * sender in several Site Care workspaces also keeps every one of them, for
43
+ * staff only, at `supportTickets/{id}/siteCareStaff/intake` (AGL-4031): the
44
+ * ticket itself is returned to the workspace's members, and which other
45
+ * workspaces the sender belongs to is not theirs to read. None of it is in the Firestore
42
46
  * rules: every read and write passes the console's routes, as the rest of
43
47
  * the support system does.
44
48
  *
@@ -55,6 +59,7 @@ import { createExtraTimeItem, creditExtraTimeItem, deleteExtraTimeItem, extraTim
55
59
  */ const TICKETS = 'supportTickets';
56
60
  export const SITE_CARE_HISTORY_COLLECTION = 'siteCareHistory';
57
61
  export const SITE_CARE_MONTHS_COLLECTION = 'siteCareMonths';
62
+ const SITE_CARE_INTAKE_DOC = 'intake';
58
63
  /** A refusal the caller turns into a response. */ export class SiteCareError extends Error {
59
64
  constructor(message, status = 400, detail = null){
60
65
  super(message);
@@ -77,6 +82,9 @@ function ticketRef(firestore, ticketId) {
77
82
  function ticketHistory(firestore, ticketId) {
78
83
  return ticketRef(firestore, ticketId).collection(SITE_CARE_HISTORY_COLLECTION);
79
84
  }
85
+ function intakeRef(firestore, ticketId) {
86
+ return ticketRef(firestore, ticketId).collection(SITE_CARE_STAFF_COLLECTION).doc(SITE_CARE_INTAKE_DOC);
87
+ }
80
88
  function monthRef(firestore, orgId, month) {
81
89
  return firestore.collection('orgs').doc(orgId).collection(SITE_CARE_MONTHS_COLLECTION).doc(month);
82
90
  }
@@ -214,6 +222,8 @@ function loadedTicket(snapshot) {
214
222
  dueAtMs,
215
223
  duplicate: true
216
224
  };
225
+ const candidates = readSiteCareCandidates(input.candidates);
226
+ const others = candidates.length > 1 ? otherSiteCareCandidates(candidates, input.orgId) : [];
217
227
  const now = FieldValue.serverTimestamp();
218
228
  const batch = firestore.batch();
219
229
  batch.create(ref, {
@@ -247,6 +257,13 @@ function loadedTicket(snapshot) {
247
257
  source: input.source,
248
258
  createdAt: now
249
259
  });
260
+ if (others.length) {
261
+ batch.create(ref.collection(SITE_CARE_STAFF_COLLECTION).doc(SITE_CARE_INTAKE_DOC), {
262
+ candidates,
263
+ filedOnOrgId: input.orgId,
264
+ createdAt: now
265
+ });
266
+ }
250
267
  appendHistoryRow(batch, firestore, ref.collection(SITE_CARE_HISTORY_COLLECTION), historyRow({
251
268
  atMs: nowMs,
252
269
  by: input.requester,
@@ -263,7 +280,9 @@ function loadedTicket(snapshot) {
263
280
  await notifyStaff({
264
281
  type: 'support.ticketOpened',
265
282
  title: `New Site Care request: ${subject}`,
266
- body: `${(_input_requester_email1 = input.requester.email) != null ? _input_requester_email1 : 'An owner'} asked ${input.source === 'email' ? 'by email' : 'from the console'} ` + `for workspace ${String((_input_org_name = input.org['name']) != null ? _input_org_name : input.orgId)} (${input.orgId}). ` + `It is due done by ${formatSiteCareDate(dueAtMs)}.`,
283
+ body: `${(_input_requester_email1 = input.requester.email) != null ? _input_requester_email1 : 'An owner'} asked ${input.source === 'email' ? 'by email' : 'from the console'} ` + `for workspace ${String((_input_org_name = input.org['name']) != null ? _input_org_name : input.orgId)} (${input.orgId}). ` + `It is due done by ${formatSiteCareDate(dueAtMs)}.` + (others.length ? ` The sender is in more than one Site Care workspace. ${siteCareCandidatesSentence(others.map((other)=>_extends({}, other, {
284
+ name: `${other.name} (${other.orgId})`
285
+ })))}. If it belongs to one of those, move it from the Site Care card on the request.` : ''),
267
286
  link: `/admin/support?ticketId=${ref.id}`,
268
287
  orgId: input.orgId
269
288
  });
@@ -376,6 +395,94 @@ function loadedTicket(snapshot) {
376
395
  return request;
377
396
  });
378
397
  }
398
+ // ── Moving to another workspace ─────────────────────────────────────────────
399
+ /** The candidates recorded for a request, or none. */ export async function readSiteCareCandidatesFor(firestore, ticketId) {
400
+ const snapshot = await intakeRef(firestore, ticketId).get();
401
+ return readSiteCareCandidates(snapshot.get('candidates'));
402
+ }
403
+ /**
404
+ * Moves an open request to another Site Care workspace its sender belongs
405
+ * to (AGL-4031): an emailed request from a sender in several is filed on a
406
+ * default one, and staff move it when it belongs to another.
407
+ *
408
+ * Refused for a closed request, one with a charge or an owner's approval on
409
+ * it ({@link siteCareMoveBlockedReason}), and for any workspace that is not
410
+ * one of the candidates recorded at intake, no longer has Site Care, or no
411
+ * longer has the sender as a member.
412
+ *
413
+ * What moves is everything the request holds against a workspace: the
414
+ * ticket's `orgId` and plan, and its month key (`siteCareOrgMonth`), which
415
+ * is what puts a request in a workspace's month. A month's totals are not
416
+ * stored: they are read from the requests that carry its key, and an open
417
+ * request has logged no minutes yet (they are logged at close), so changing
418
+ * the key in this one write takes it out of the old workspace's month and
419
+ * into the new one's at once. A site it named is cleared, since that site
420
+ * is the old workspace's. The `request.moved` row and the staff audit row
421
+ * commit in the same transaction; nothing earlier is rewritten.
422
+ */ export async function moveSiteCareRequest(firestore, input, deps = {}) {
423
+ const targetOrgId = typeof input.orgId === 'string' ? input.orgId.trim() : '';
424
+ if (!targetOrgId) throw new SiteCareError('Choose the workspace to move it to.', 400);
425
+ const target = await readSiteCareOrg(firestore, targetOrgId);
426
+ if (!orgHasSiteCare(target)) {
427
+ throw new SiteCareError('That workspace does not have Site Care, so the request cannot move there.', 409);
428
+ }
429
+ const nowMs = nowOf(deps);
430
+ return firestore.runTransaction(async (tx)=>{
431
+ var _ref;
432
+ const [ticketSnap, intakeSnap] = await Promise.all([
433
+ tx.get(ticketRef(firestore, input.ticketId)),
434
+ tx.get(intakeRef(firestore, input.ticketId))
435
+ ]);
436
+ const ticket = loadedTicket(ticketSnap);
437
+ const candidates = readSiteCareCandidates(intakeSnap.get('candidates'));
438
+ const others = otherSiteCareCandidates(candidates, ticket.orgId);
439
+ const blocked = siteCareMoveBlockedReason(ticket.request, others);
440
+ if (blocked) throw new SiteCareError(blocked, 409);
441
+ const candidate = others.find((other)=>other.orgId === targetOrgId);
442
+ if (!candidate) {
443
+ throw new SiteCareError(targetOrgId === ticket.orgId ? 'The request is already on that workspace.' : 'The sender is not recorded as a member of that workspace, so the request cannot move there.', 409);
444
+ }
445
+ const member = await tx.get(firestore.collection('orgs').doc(targetOrgId).collection('members').doc(candidate.uid));
446
+ if (!member.exists) {
447
+ throw new SiteCareError('The sender is no longer a member of that workspace, so the request cannot move there.', 409);
448
+ }
449
+ const fromOrgId = ticket.orgId;
450
+ const toName = String((_ref = target == null ? void 0 : target['name']) != null ? _ref : '') || candidate.name;
451
+ const request = _extends({}, ticket.request, {
452
+ hostId: null
453
+ });
454
+ tx.update(ticket.ref, {
455
+ orgId: targetOrgId,
456
+ plan: resolveEffectivePlan(target),
457
+ siteCare: request,
458
+ siteCareOrgMonth: siteCareOrgMonth(targetOrgId, request.month),
459
+ updatedAt: FieldValue.serverTimestamp()
460
+ });
461
+ appendHistoryRow(tx, firestore, ticketHistory(firestore, input.ticketId), historyRow({
462
+ atMs: nowMs,
463
+ by: input.actor,
464
+ kind: 'request.moved',
465
+ // The new workspace's statement and export read this row, so it
466
+ // names only that workspace; the one it left is an id in `before`.
467
+ summary: `Moved to ${toName}, the workspace it belongs to.`,
468
+ before: {
469
+ orgId: fromOrgId,
470
+ hostId: ticket.request.hostId
471
+ },
472
+ after: {
473
+ orgId: targetOrgId,
474
+ hostId: null,
475
+ month: request.month
476
+ },
477
+ subjectId: input.ticketId
478
+ }), requestAudit(targetOrgId, input.ticketId, 'request.moved'));
479
+ return {
480
+ request,
481
+ fromOrgId,
482
+ orgId: targetOrgId
483
+ };
484
+ });
485
+ }
379
486
  async function readMonthContext(reader, firestore, orgId, month, excludeTicketId) {
380
487
  var _monthSnap_get;
381
488
  const query = firestore.collection(TICKETS).where('siteCareOrgMonth', '==', siteCareOrgMonth(orgId, month));
@@ -1395,10 +1502,12 @@ async function emailDoneSummary(firestore, ticket, request, statement, deps) {
1395
1502
  const ticket = loadedTicket(await ticketRef(firestore, ticketId).get());
1396
1503
  const nowMs = nowOf(deps);
1397
1504
  const month = ticket.request.state === 'closed' ? ticket.request.month : siteCareMonthKey(nowMs);
1398
- const [history, context] = await Promise.all([
1505
+ const [history, context, candidates] = await Promise.all([
1399
1506
  readHistoryRows(ticketHistory(firestore, ticketId)),
1400
- readMonthContext(null, firestore, ticket.orgId, month, ticketId)
1507
+ readMonthContext(null, firestore, ticket.orgId, month, ticketId),
1508
+ readSiteCareCandidatesFor(firestore, ticketId)
1401
1509
  ]);
1510
+ const otherWorkspaces = otherSiteCareCandidates(candidates, ticket.orgId);
1402
1511
  const pool = siteCareIncludedPool(context.goodwillMinutes);
1403
1512
  return {
1404
1513
  ticketId,
@@ -1407,7 +1516,9 @@ async function emailDoneSummary(firestore, ticket, request, statement, deps) {
1407
1516
  request: ticket.request,
1408
1517
  history,
1409
1518
  suggestedMinutes: closeMinutesFromTimer(ticket.request.timerStartedAtMs, nowMs),
1410
- monthMinutesLeft: Math.max(0, pool - context.otherIncludedUsed)
1519
+ monthMinutesLeft: Math.max(0, pool - context.otherIncludedUsed),
1520
+ otherWorkspaces,
1521
+ moveBlockedReason: otherWorkspaces.length ? siteCareMoveBlockedReason(ticket.request, otherWorkspaces) : null
1411
1522
  };
1412
1523
  }
1413
1524