@7365admin1/core 3.107.0 → 3.109.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @iservice365/core
2
2
 
3
+ ## 3.109.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 2724925: Invitations record who sent them: the Seven365 console lists only its own (`GET /verifications?origin=seven365`), a client's invitation stays in that client's list, only Seven365 staff may send an invitation naming no organisation, and an organisation invitation must name a role.
8
+
9
+ ## 3.108.0
10
+
11
+ ### Minor Changes
12
+
13
+ - a96a08f: Only the owning organisation (a member whose role holds `*` or `sites:delete-site`) or Seven365 Super Admin may delete a site through `DELETE /api/sites/:id`. A service provider that serves the site is refused with a message pointing at Remove from your Sites.
14
+
3
15
  ## 3.107.0
4
16
 
5
17
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -1057,6 +1057,8 @@ type TVerificationMetadata = {
1057
1057
  siteId?: string | ObjectId;
1058
1058
  siteName?: string;
1059
1059
  serviceProviderOrgId?: string | ObjectId;
1060
+ /** "seven365" when Seven365 sent it - see `invite-origin.util.ts`. */
1061
+ origin?: string;
1060
1062
  };
1061
1063
  type TVerification = {
1062
1064
  _id?: ObjectId;
@@ -1091,7 +1093,7 @@ declare function useVerificationRepo(): {
1091
1093
  createTextIndex: () => Promise<void>;
1092
1094
  add: (value: TVerification, session?: ClientSession) => Promise<ObjectId>;
1093
1095
  getById: (_id: string | ObjectId) => Promise<TVerification | null>;
1094
- getVerifications: ({ search, page, limit, sort, status, app, type, email, siteId, orgId, }: {
1096
+ getVerifications: ({ search, page, limit, sort, status, app, type, email, siteId, orgId, origin, }: {
1095
1097
  search?: string | undefined;
1096
1098
  page?: number | undefined;
1097
1099
  limit?: number | undefined;
@@ -1102,6 +1104,7 @@ declare function useVerificationRepo(): {
1102
1104
  email?: string | undefined;
1103
1105
  siteId?: string | undefined;
1104
1106
  orgId?: string | undefined;
1107
+ origin?: string | undefined;
1105
1108
  }) => Promise<{}>;
1106
1109
  getByIdByType: (type: string) => Promise<TVerification[]>;
1107
1110
  updateStatusById: (_id: string | ObjectId, status: string, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
@@ -14931,6 +14934,8 @@ type TVerificationMetadataV2 = {
14931
14934
  siteId?: string | ObjectId;
14932
14935
  siteName?: string;
14933
14936
  serviceProviderOrgId?: string | ObjectId;
14937
+ /** "seven365" when Seven365 sent it - see `invite-origin.util.ts`. */
14938
+ origin?: string;
14934
14939
  verificationCode?: string;
14935
14940
  /** Who sent the invitation. Taken from the session, never from the body. */
14936
14941
  invitedBy?: string | ObjectId;
package/dist/index.js CHANGED
@@ -5729,6 +5729,22 @@ var MVerification = class {
5729
5729
  // src/repositories/verification.repo.ts
5730
5730
  var import_node_server_utils19 = require("@7365admin1/node-server-utils");
5731
5731
  var import_mongodb17 = require("mongodb");
5732
+
5733
+ // src/utils/invite-origin.util.ts
5734
+ var SEVEN365_INVITE_ORIGIN = "seven365";
5735
+ function seven365InviteQuery() {
5736
+ return {
5737
+ $or: [
5738
+ { "metadata.origin": SEVEN365_INVITE_ORIGIN },
5739
+ {
5740
+ "metadata.origin": { $exists: false },
5741
+ "metadata.org": { $in: ["", null] }
5742
+ }
5743
+ ]
5744
+ };
5745
+ }
5746
+
5747
+ // src/repositories/verification.repo.ts
5732
5748
  function useVerificationRepo() {
5733
5749
  const db2 = import_node_server_utils19.useAtlas.getDb();
5734
5750
  if (!db2) {
@@ -5815,7 +5831,8 @@ function useVerificationRepo() {
5815
5831
  type = {},
5816
5832
  email = "",
5817
5833
  siteId = "",
5818
- orgId = ""
5834
+ orgId = "",
5835
+ origin = ""
5819
5836
  }) {
5820
5837
  page = page > 0 ? page - 1 : 0;
5821
5838
  const query2 = { status };
@@ -5860,6 +5877,10 @@ function useVerificationRepo() {
5860
5877
  query2["metadata.org"] = { $in: orgCandidates };
5861
5878
  cacheOptions.orgId = orgId;
5862
5879
  }
5880
+ if (origin === SEVEN365_INVITE_ORIGIN) {
5881
+ Object.assign(query2, seven365InviteQuery());
5882
+ cacheOptions.origin = origin;
5883
+ }
5863
5884
  const cacheKey = (0, import_node_server_utils19.makeCacheKey)(namespace_collection3, cacheOptions);
5864
5885
  const cachedData = await getCache(cacheKey);
5865
5886
  if (cachedData) {
@@ -13908,6 +13929,23 @@ async function requireRoleInvitable(role, org, apps, site) {
13908
13929
  if (refusal)
13909
13930
  throw new import_node_server_utils43.BadRequestError(refusal);
13910
13931
  }
13932
+ async function requireInviteOrigin(req, org, role, fromConsole = false) {
13933
+ if (!org) {
13934
+ await requirePlatformStaff(req);
13935
+ return { origin: SEVEN365_INVITE_ORIGIN };
13936
+ }
13937
+ if (!role)
13938
+ throw new import_node_server_utils43.BadRequestError("Choose a role for this invitation.");
13939
+ if (fromConsole) {
13940
+ const staff = await requirePlatformStaff(req).then(
13941
+ () => true,
13942
+ () => false
13943
+ );
13944
+ if (staff)
13945
+ return { origin: SEVEN365_INVITE_ORIGIN };
13946
+ }
13947
+ return {};
13948
+ }
13911
13949
  async function requireInviteReach(req, org, app, role, site) {
13912
13950
  const apps = Array.isArray(app) ? app : [app];
13913
13951
  if (apps.some((a) => a === PLATFORM_STAFF_MEMBER_TYPE)) {
@@ -27493,6 +27531,7 @@ function useVerificationController() {
27493
27531
  const siteName = req.body.siteName ?? "";
27494
27532
  try {
27495
27533
  await requireInviteReach(req, org, app, role, siteId);
27534
+ const origin = await requireInviteOrigin(req, org, role);
27496
27535
  await _createUserInvite({
27497
27536
  email,
27498
27537
  metadata: {
@@ -27501,7 +27540,8 @@ function useVerificationController() {
27501
27540
  name,
27502
27541
  org,
27503
27542
  siteId,
27504
- siteName
27543
+ siteName,
27544
+ ...origin
27505
27545
  }
27506
27546
  });
27507
27547
  res.status(201).json({ message: "Successfully invited user." });
@@ -27594,6 +27634,7 @@ function useVerificationController() {
27594
27634
  clientModules.residentAppModules = residentAppModules;
27595
27635
  }
27596
27636
  await requireInviteReach(req, org, app, role, siteId);
27637
+ const origin = await requireInviteOrigin(req, org, role, true);
27597
27638
  const verificationIds = await _createSimpleUserInvite({
27598
27639
  email,
27599
27640
  metadata: {
@@ -27603,7 +27644,8 @@ function useVerificationController() {
27603
27644
  org,
27604
27645
  siteId,
27605
27646
  siteName,
27606
- ...clientModules
27647
+ ...clientModules,
27648
+ ...origin
27607
27649
  }
27608
27650
  });
27609
27651
  const responseBody = {
@@ -27659,6 +27701,7 @@ function useVerificationController() {
27659
27701
  const siteName = req.body.siteName ?? "";
27660
27702
  try {
27661
27703
  await requireInviteReach(req, org, app, role, siteId);
27704
+ const origin = await requireInviteOrigin(req, org, role);
27662
27705
  await _createSimpleMemberInvite({
27663
27706
  email,
27664
27707
  metadata: {
@@ -27667,7 +27710,8 @@ function useVerificationController() {
27667
27710
  name,
27668
27711
  org,
27669
27712
  siteId,
27670
- siteName
27713
+ siteName,
27714
+ ...origin
27671
27715
  }
27672
27716
  });
27673
27717
  const responseBody = {
@@ -27837,6 +27881,8 @@ function useVerificationController() {
27837
27881
  email: import_joi29.default.string().optional().allow("", null),
27838
27882
  siteId: import_joi29.default.string().hex().optional().allow("", null),
27839
27883
  orgId: import_joi29.default.string().hex().optional().allow("", null),
27884
+ // "seven365": only the invitations Seven365 sent (the console's lists).
27885
+ origin: import_joi29.default.string().valid(SEVEN365_INVITE_ORIGIN).optional().allow("", null),
27840
27886
  limit: import_joi29.default.number().integer().min(1).allow("", null).default(10)
27841
27887
  });
27842
27888
  const query2 = { ...req.query };
@@ -27854,6 +27900,7 @@ function useVerificationController() {
27854
27900
  const siteId = req.query.siteId ?? "";
27855
27901
  const orgId = req.query.orgId ?? "";
27856
27902
  const limit = parseInt(req.query.limit ?? "10");
27903
+ const origin = req.query.origin ?? "";
27857
27904
  const type = req.query.type ? String(req.query.type).split(",") : "";
27858
27905
  const typeObj = {};
27859
27906
  if (type && Array.isArray(type) && type.length) {
@@ -27862,8 +27909,9 @@ function useVerificationController() {
27862
27909
  });
27863
27910
  }
27864
27911
  try {
27865
- await requireVerificationListReach(req, orgId);
27912
+ await requireVerificationListReach(req, origin ? null : orgId);
27866
27913
  const data = await _getVerifications({
27914
+ origin,
27867
27915
  search,
27868
27916
  page,
27869
27917
  status,
@@ -38750,17 +38798,49 @@ function siteOwnerOnlyMessage(ownerName) {
38750
38798
  async function requireSiteOwnerEdit(actor, staff, siteId) {
38751
38799
  if (staff)
38752
38800
  return;
38801
+ const ownerOrg = await ownerOfSite(siteId);
38802
+ if (mayEditSiteDetails({ staff, actorOrgIds: actor.orgIds, ownerOrg }))
38803
+ return;
38804
+ throw new import_node_server_utils113.AppError(siteOwnerOnlyMessage(await ownerOrgName(ownerOrg)), 403);
38805
+ }
38806
+ async function ownerOfSite(siteId) {
38753
38807
  const id = String(siteId?.toString?.() ?? "");
38754
38808
  const db2 = import_node_server_utils113.useAtlas.getDb();
38755
38809
  const site = db2 && import_mongodb79.ObjectId.isValid(id) ? await db2.collection("sites").findOne({ _id: new import_mongodb79.ObjectId(id) }, { projection: { orgId: 1, org: 1 } }) : null;
38756
- const ownerOrg = site?.orgId ?? site?.org;
38757
- if (mayEditSiteDetails({ staff, actorOrgIds: actor.orgIds, ownerOrg }))
38810
+ return site?.orgId ?? site?.org;
38811
+ }
38812
+ async function ownerOrgName(ownerOrg) {
38813
+ if (!ownerOrg || !import_mongodb79.ObjectId.isValid(String(ownerOrg)))
38814
+ return void 0;
38815
+ const owner = await import_node_server_utils113.useAtlas.getDb()?.collection("organizations").findOne({ _id: new import_mongodb79.ObjectId(String(ownerOrg)) }, { projection: { name: 1 } });
38816
+ return owner?.name;
38817
+ }
38818
+ var DELETE_SITE_PERMISSION = "sites:delete-site";
38819
+ var NO_SITE_DELETE_PERMISSION = "You do not have permission to delete this site.";
38820
+ function siteDeleteOwnerOnlyMessage(ownerName) {
38821
+ const name = String(ownerName ?? "").trim() || "another organisation";
38822
+ return `This site belongs to ${name}. Only they can delete it. You can remove it from your Sites list instead.`;
38823
+ }
38824
+ function mayDeleteSite(permissions) {
38825
+ return permissions.includes("*") || permissions.includes(DELETE_SITE_PERMISSION);
38826
+ }
38827
+ async function requireSiteOwnerDelete(actor, staff, siteId) {
38828
+ if (staff)
38758
38829
  return;
38759
- const owner = ownerOrg && import_mongodb79.ObjectId.isValid(String(ownerOrg)) ? await db2?.collection("organizations").findOne(
38760
- { _id: new import_mongodb79.ObjectId(String(ownerOrg)) },
38761
- { projection: { name: 1 } }
38762
- ) : null;
38763
- throw new import_node_server_utils113.AppError(siteOwnerOnlyMessage(owner?.name), 403);
38830
+ const owner = String(await ownerOfSite(siteId) ?? "");
38831
+ if (owner && import_mongodb79.ObjectId.isValid(owner) && import_mongodb79.ObjectId.isValid(actor.id)) {
38832
+ const member = await import_node_server_utils113.useAtlas.getDb()?.collection("members").findOne(
38833
+ { user: new import_mongodb79.ObjectId(actor.id), org: new import_mongodb79.ObjectId(owner), type: "organization", status: "active" },
38834
+ { projection: { role: 1 } }
38835
+ );
38836
+ if (member) {
38837
+ const permissions = member.role ? await getUserPermissions({ user: actor.id, org: owner, role: member.role }) : [];
38838
+ if (mayDeleteSite(permissions))
38839
+ return;
38840
+ throw new import_node_server_utils113.AppError(NO_SITE_DELETE_PERMISSION, 403);
38841
+ }
38842
+ }
38843
+ throw new import_node_server_utils113.AppError(siteDeleteOwnerOnlyMessage(await ownerOrgName(owner)), 403);
38764
38844
  }
38765
38845
 
38766
38846
  // src/controllers/site.controller.ts
@@ -39071,7 +39151,8 @@ function useSiteController() {
39071
39151
  return;
39072
39152
  }
39073
39153
  try {
39074
- await requireSiteWrite(req, _id);
39154
+ const actor = await requireSiteWrite(req, _id);
39155
+ await requireSiteOwnerDelete(actor, staffMayBypass(actor), _id);
39075
39156
  await _deleteSite(_id);
39076
39157
  res.json({ message: "Successfully deleted site." });
39077
39158
  return;
@@ -93736,6 +93817,7 @@ function useVerificationControllerV2() {
93736
93817
  const { email, app, role, name, org, siteId, siteName } = value;
93737
93818
  try {
93738
93819
  await requireInviteReach(req, org, app, role, siteId);
93820
+ const origin = await requireInviteOrigin(req, org, role);
93739
93821
  await _createUserInvite({
93740
93822
  email,
93741
93823
  metadata: {
@@ -93744,7 +93826,8 @@ function useVerificationControllerV2() {
93744
93826
  name,
93745
93827
  org,
93746
93828
  siteId,
93747
- siteName
93829
+ siteName,
93830
+ ...origin
93748
93831
  }
93749
93832
  });
93750
93833
  res.status(201).json({ message: "Successfully invited user." });