@7365admin1/core 3.127.2-staging.427 → 3.127.2-staging.428

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/dist/index.d.ts CHANGED
@@ -518,6 +518,7 @@ declare function useMemberRepo(): {
518
518
  _id: string | ObjectId;
519
519
  name: string;
520
520
  } | null) => Promise<number>;
521
+ detachUnitsOfOrgAtSite: (org: string | ObjectId, site: string | ObjectId) => Promise<number>;
521
522
  detachUnit: (unit: string | ObjectId) => Promise<number>;
522
523
  getByUserIdTypeOrg: (user: string | ObjectId, type: string, org: string | ObjectId) => Promise<TMember>;
523
524
  completeOnboardingById: (_id: string | ObjectId, session?: ClientSession) => Promise<string>;
@@ -2891,7 +2892,7 @@ declare function MServiceProvider(value: TServiceProvider): {
2891
2892
  };
2892
2893
 
2893
2894
  declare function useServiceProviderRepo(): {
2894
- getProviderMembersBySite: (site: string | ObjectId, orgIds: string[]) => Promise<bson.Document[]>;
2895
+ getProviderMembersBySite: (site: string | ObjectId, orgIds: string[], unit?: string) => Promise<bson.Document[]>;
2895
2896
  createTextIndex: () => Promise<void>;
2896
2897
  createUniqueIndex: () => Promise<void>;
2897
2898
  createServiceProvider: (value: TServiceProvider, session?: ClientSession) => Promise<ObjectId>;
@@ -2943,6 +2944,7 @@ declare function useServiceProviderRepo(): {
2943
2944
  declare function useServiceProviderController(): {
2944
2945
  createServiceProvider: (req: Request, res: Response, next: NextFunction) => Promise<void>;
2945
2946
  getSiteProviderMembers: (req: Request, res: Response, next: NextFunction) => Promise<void>;
2947
+ updateSiteMemberUnit: (req: Request, res: Response, next: NextFunction) => Promise<void>;
2946
2948
  getServiceProviders: (req: Request, res: Response, next: NextFunction) => Promise<void>;
2947
2949
  getByProviderOrg: (req: Request, res: Response, next: NextFunction) => Promise<void>;
2948
2950
  getByEmail: (req: Request, res: Response, next: NextFunction) => Promise<void>;
package/dist/index.js CHANGED
@@ -5523,6 +5523,19 @@ function useMemberRepo() {
5523
5523
  }
5524
5524
  return res.modifiedCount;
5525
5525
  }
5526
+ async function detachUnitsOfOrgAtSite(org, site) {
5527
+ const both2 = (id) => import_mongodb15.ObjectId.isValid(id) ? [new import_mongodb15.ObjectId(id), id.toString()] : [id.toString()];
5528
+ const res = await collection.updateMany(
5529
+ { org: { $in: both2(org) }, siteId: { $in: both2(site) }, unit: { $nin: [null, ""] } },
5530
+ { $set: { unit: null, unitName: "", updatedAt: /* @__PURE__ */ new Date() } }
5531
+ );
5532
+ if (res.modifiedCount) {
5533
+ delNamespace().catch((err) => {
5534
+ import_node_server_utils16.logger.error(`Failed to clear cache for namespace: ${namespace_collection3}`, err);
5535
+ });
5536
+ }
5537
+ return res.modifiedCount;
5538
+ }
5526
5539
  async function updateSiteById(_id, siteId, siteName) {
5527
5540
  try {
5528
5541
  _id = new import_mongodb15.ObjectId(_id);
@@ -5757,6 +5770,7 @@ function useMemberRepo() {
5757
5770
  getByRoles,
5758
5771
  updateSiteById,
5759
5772
  updateUnitById,
5773
+ detachUnitsOfOrgAtSite,
5760
5774
  detachUnit,
5761
5775
  getByUserIdTypeOrg,
5762
5776
  completeOnboardingById,
@@ -13803,7 +13817,7 @@ function useServiceProviderRepo() {
13803
13817
  )
13804
13818
  };
13805
13819
  }
13806
- async function getProviderMembersBySite(site, orgIds) {
13820
+ async function getProviderMembersBySite(site, orgIds, unit = "") {
13807
13821
  try {
13808
13822
  site = new import_mongodb37.ObjectId(site);
13809
13823
  } catch {
@@ -13823,7 +13837,8 @@ function useServiceProviderRepo() {
13823
13837
  org: { $in: orgs },
13824
13838
  siteId: { $in: [site, site.toString()] },
13825
13839
  status: { $nin: ["deleted"] },
13826
- deletedAt: { $in: [null, ""] }
13840
+ deletedAt: { $in: [null, ""] },
13841
+ ...unit ? { unit: unitQuery(unit) } : {}
13827
13842
  }
13828
13843
  },
13829
13844
  { $sort: { _id: 1 } },
@@ -13861,7 +13876,9 @@ function useServiceProviderRepo() {
13861
13876
  $ifNull: [{ $arrayElemAt: ["$_user.email", 0] }, "$email"]
13862
13877
  },
13863
13878
  roleName: { $ifNull: [{ $arrayElemAt: ["$_role.name", 0] }, ""] },
13864
- lastLoginAt: { $arrayElemAt: ["$_user.lastLoginAt", 0] }
13879
+ lastLoginAt: { $arrayElemAt: ["$_user.lastLoginAt", 0] },
13880
+ unit: 1,
13881
+ unitName: 1
13865
13882
  }
13866
13883
  }
13867
13884
  ]).toArray();
@@ -37583,7 +37600,10 @@ function useServiceProviderService() {
37583
37600
  }
37584
37601
  return clients;
37585
37602
  }
37586
- async function getSiteProviderMembers({ siteId }) {
37603
+ async function getSiteProviderMembers({
37604
+ siteId,
37605
+ unit = ""
37606
+ }) {
37587
37607
  const [engaged, storedRows, via] = await Promise.all([
37588
37608
  _getEngaged(siteId),
37589
37609
  _getServiceProviders({ siteId, status: "active", page: 1, limit: 200 }),
@@ -37618,7 +37638,7 @@ function useServiceProviderService() {
37618
37638
  invitedVia: ""
37619
37639
  });
37620
37640
  }
37621
- const people = await _getProviderMembers(siteId, [...companies.keys()]);
37641
+ const people = await _getProviderMembers(siteId, [...companies.keys()], unit);
37622
37642
  const counts = /* @__PURE__ */ new Map();
37623
37643
  const items = people.map((m) => {
37624
37644
  const orgId = m.org?.toString?.() ?? "";
@@ -37637,7 +37657,9 @@ function useServiceProviderService() {
37637
37657
  status: m.status ?? "active",
37638
37658
  addedAt: m.createdAt ?? null,
37639
37659
  lastLoginAt: m.lastLoginAt ?? null,
37640
- invitedVia: company.invitedVia ?? ""
37660
+ invitedVia: company.invitedVia ?? "",
37661
+ unit: m.unit ?? null,
37662
+ unitName: m.unitName ?? ""
37641
37663
  };
37642
37664
  });
37643
37665
  return {
@@ -37702,7 +37724,9 @@ function useServiceProviderController() {
37702
37724
  type: import_joi47.default.string().max(60).optional().allow("", null),
37703
37725
  company: import_joi47.default.string().hex().length(24).optional().allow("", null),
37704
37726
  search: import_joi47.default.string().max(200).optional().allow("", null),
37705
- format: import_joi47.default.string().valid("json", "xlsx").optional().allow("", null)
37727
+ format: import_joi47.default.string().valid("json", "xlsx").optional().allow("", null),
37728
+ // A building unit id, or "none" (people on no unit yet).
37729
+ unit: import_joi47.default.alternatives().try(import_joi47.default.string().hex().length(24), import_joi47.default.string().valid(NO_UNIT)).optional().allow("", null)
37706
37730
  }).validate(req.query);
37707
37731
  if (error)
37708
37732
  throw new import_node_server_utils109.BadRequestError(`Invalid input: ${error.message}`);
@@ -37712,7 +37736,10 @@ function useServiceProviderController() {
37712
37736
  "Only the site's own organisation can see its service providers' people."
37713
37737
  );
37714
37738
  }
37715
- const data = await _getSiteProviderMembers({ siteId: value.siteId });
37739
+ const data = await _getSiteProviderMembers({
37740
+ siteId: value.siteId,
37741
+ unit: value.unit || ""
37742
+ });
37716
37743
  if (value.format !== "xlsx") {
37717
37744
  res.json(data);
37718
37745
  return;
@@ -38007,6 +38034,10 @@ function useServiceProviderController() {
38007
38034
  const existing = await _getServiceProviderById(value.id);
38008
38035
  await requireSiteReach(req, existing?.siteId?.toString());
38009
38036
  await _updateStatusById(value.id, value.status);
38037
+ const row = existing;
38038
+ if (value.status !== "active" && row?.serviceProviderOrgId && row?.siteId) {
38039
+ await useMemberRepo().detachUnitsOfOrgAtSite(row.serviceProviderOrgId, row.siteId);
38040
+ }
38010
38041
  res.json({ message: "Service provider status updated successfully." });
38011
38042
  return;
38012
38043
  } catch (error2) {
@@ -38043,6 +38074,47 @@ function useServiceProviderController() {
38043
38074
  next(error2);
38044
38075
  }
38045
38076
  }
38077
+ async function updateSiteMemberUnit(req, res, next) {
38078
+ const { error, value } = import_joi47.default.object({
38079
+ id: import_joi47.default.string().hex().length(24).required(),
38080
+ unit: import_joi47.default.string().hex().length(24).optional().allow("", null)
38081
+ }).validate({ id: req.params.id, unit: req.body?.unit });
38082
+ if (error) {
38083
+ next(new import_node_server_utils109.BadRequestError(error.message));
38084
+ return;
38085
+ }
38086
+ try {
38087
+ const members = useMemberRepo();
38088
+ const existing = await members.getById(value.id);
38089
+ const siteId = existing?.siteId?.toString() ?? "";
38090
+ if (!siteId)
38091
+ throw new import_node_server_utils109.BadRequestError("This person is not pinned to a site.");
38092
+ await requireSiteReach(req, siteId);
38093
+ if (!await isSiteClient(req, siteId)) {
38094
+ throw new import_node_server_utils109.UnauthorizedError(
38095
+ "Only the site's own organisation can assign its service providers' people."
38096
+ );
38097
+ }
38098
+ const { items } = await _getSiteProviderMembers({ siteId });
38099
+ if (!items.some((p) => p._id?.toString() === value.id)) {
38100
+ throw new import_node_server_utils109.BadRequestError("This person is not one of the site's service providers.");
38101
+ }
38102
+ if (!value.unit) {
38103
+ await members.updateUnitById(value.id, null);
38104
+ res.json({ message: "Service provider removed from unit." });
38105
+ return;
38106
+ }
38107
+ const unit = await useBuildingUnitRepo().getById(value.unit);
38108
+ const refusal = unitAssignmentRefusal(existing, unit, "service provider");
38109
+ if (refusal)
38110
+ throw new import_node_server_utils109.BadRequestError(refusal);
38111
+ await members.updateUnitById(value.id, { _id: value.unit, name: unit.name ?? "" });
38112
+ res.json({ message: "Service provider assigned to unit." });
38113
+ } catch (error2) {
38114
+ import_node_server_utils109.logger.log({ level: "error", message: error2.message });
38115
+ next(error2);
38116
+ }
38117
+ }
38046
38118
  async function getByEmail(req, res, next) {
38047
38119
  const validation = import_joi47.default.object({
38048
38120
  email: import_joi47.default.string().email().required(),
@@ -38069,6 +38141,7 @@ function useServiceProviderController() {
38069
38141
  return {
38070
38142
  createServiceProvider,
38071
38143
  getSiteProviderMembers,
38144
+ updateSiteMemberUnit,
38072
38145
  getServiceProviders,
38073
38146
  getByProviderOrg,
38074
38147
  getByEmail,