@7365admin1/core 3.58.1-staging.269 → 3.58.1-staging.270

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
@@ -382,6 +382,15 @@ type TMember = {
382
382
  siteName?: string;
383
383
  status?: string;
384
384
  dateInvited?: string;
385
+ /**
386
+ * Who wrote this membership, when it was not the member themselves.
387
+ *
388
+ * Optional and additive: every existing caller omits it and gets `""`, the
389
+ * same as the other unset references here. `MMember` validates with Joi and
390
+ * REJECTS UNKNOWN KEYS, so a field cannot simply be passed in - it has to be
391
+ * declared here first.
392
+ */
393
+ createdBy?: string | ObjectId | null;
385
394
  onboardingRequired?: boolean;
386
395
  onboardingCompleted?: boolean;
387
396
  onboardingCompletedAt?: string;
package/dist/index.js CHANGED
@@ -9830,6 +9830,7 @@ function MMember(value) {
9830
9830
  siteName: import_joi7.default.string().optional().allow("", null),
9831
9831
  status: import_joi7.default.string().optional().allow("", null),
9832
9832
  dateInvited: import_joi7.default.string().optional().allow("", null),
9833
+ createdBy: import_joi7.default.string().hex().optional().allow("", null),
9833
9834
  onboardingRequired: import_joi7.default.boolean().optional(),
9834
9835
  onboardingCompleted: import_joi7.default.boolean().optional(),
9835
9836
  onboardingCompletedAt: import_joi7.default.string().optional().allow("", null),
@@ -9869,6 +9870,13 @@ function MMember(value) {
9869
9870
  throw new import_node_server_utils13.BadRequestError("Invalid site ID format.");
9870
9871
  }
9871
9872
  }
9873
+ if (value.createdBy) {
9874
+ try {
9875
+ value.createdBy = new import_mongodb12.ObjectId(value.createdBy);
9876
+ } catch (error2) {
9877
+ throw new import_node_server_utils13.BadRequestError("Invalid createdBy ID format.");
9878
+ }
9879
+ }
9872
9880
  return {
9873
9881
  _id: value._id,
9874
9882
  name: value.name,
@@ -9882,6 +9890,7 @@ function MMember(value) {
9882
9890
  siteName: value.siteName ?? "",
9883
9891
  status: value.status || "active",
9884
9892
  dateInvited: value.dateInvited ?? value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
9893
+ createdBy: value.createdBy ?? "",
9885
9894
  onboardingRequired: value.onboardingRequired ?? false,
9886
9895
  onboardingCompleted: value.onboardingCompleted ?? !value.onboardingRequired,
9887
9896
  onboardingCompletedAt: value.onboardingCompletedAt ?? "",
@@ -29334,9 +29343,12 @@ function defaultOwnerRoles(org, nature) {
29334
29343
  return roles;
29335
29344
  }
29336
29345
  async function seedDefaultOrgRoles(addRole, org, nature) {
29346
+ const seeded = [];
29337
29347
  try {
29338
29348
  for (const role of defaultOwnerRoles(org, nature)) {
29339
- await addRole(role);
29349
+ const id = await addRole(role);
29350
+ if (id)
29351
+ seeded.push({ type: String(role.type), id: String(id) });
29340
29352
  }
29341
29353
  } catch (error) {
29342
29354
  import_node_server_utils65.logger.log({
@@ -29344,11 +29356,12 @@ async function seedDefaultOrgRoles(addRole, org, nature) {
29344
29356
  message: `Failed to seed default roles for organization ${org}: ${error?.message}`
29345
29357
  });
29346
29358
  }
29359
+ return seeded;
29347
29360
  }
29348
29361
 
29349
29362
  // src/controllers/organization.controller.ts
29350
29363
  function useOrgController() {
29351
- const { getOrgsByMembership } = useMemberRepo();
29364
+ const { getOrgsByMembership, add: _addMember } = useMemberRepo();
29352
29365
  const { getUserById: _getUserById } = useUserRepo();
29353
29366
  const {
29354
29367
  getByName: _getByName,
@@ -29462,11 +29475,13 @@ function useOrgController() {
29462
29475
  const { inviteId, ...body } = req.body ?? {};
29463
29476
  let invitedFrom = null;
29464
29477
  let invitedRole = null;
29478
+ let self = null;
29465
29479
  if (inviteId) {
29466
- const [invite, self] = await Promise.all([
29480
+ const [invite, caller] = await Promise.all([
29467
29481
  useVerificationRepoV2().getVerificationByIdFresh(inviteId).catch(() => null),
29468
29482
  _getUserById(createdBy).catch(() => null)
29469
29483
  ]);
29484
+ self = caller;
29470
29485
  const samePerson = invite?.email && self?.email && invite.email.toLowerCase() === self.email.toLowerCase();
29471
29486
  if (samePerson) {
29472
29487
  invitedFrom = String(invite._id);
@@ -29479,7 +29494,32 @@ function useOrgController() {
29479
29494
  }
29480
29495
  }
29481
29496
  const _id = await _add({ ...body, createdBy, invitedFrom, invitedRole });
29482
- await seedDefaultRoles(_id, body.nature);
29497
+ const seededRoles = await seedDefaultRoles(_id, body.nature);
29498
+ try {
29499
+ const ownerRole = invitedRole || seededRoles.find((role) => role.type === "organization")?.id || "";
29500
+ const owner = self ?? await _getUserById(createdBy).catch(() => null);
29501
+ const ownerUserId = owner?._id ? String(owner._id) : createdBy;
29502
+ await _addMember({
29503
+ org: String(_id),
29504
+ orgName: body.name,
29505
+ user: ownerUserId,
29506
+ name: owner?.name || owner?.email || "",
29507
+ email: owner?.email || "",
29508
+ createdBy,
29509
+ role: ownerRole,
29510
+ type: "organization",
29511
+ // Deliberately no site: the owner is not a one-site person, and a
29512
+ // `siteId` here would narrow them to it everywhere in the product.
29513
+ siteId: "",
29514
+ siteName: "",
29515
+ onboardingRequired: true
29516
+ });
29517
+ } catch (error2) {
29518
+ import_node_server_utils66.logger.log({
29519
+ level: "error",
29520
+ message: `[organizations/onboarding] could not create the owner membership for ${_id}: ${error2?.message}`
29521
+ });
29522
+ }
29483
29523
  const data = await _getById(_id);
29484
29524
  res.status(201).json({
29485
29525
  message: "Successfully created organization.",