@goweekdays/core 2.11.16 → 2.11.17

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,11 @@
1
1
  # @goweekdays/core
2
2
 
3
+ ## 2.11.17
4
+
5
+ ### Patch Changes
6
+
7
+ - 029e07e: Add getByUserOrg to member repo and update verification logic
8
+
3
9
  ## 2.11.16
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -345,6 +345,7 @@ declare function useMemberRepo(): {
345
345
  deleteById: (_id: string | ObjectId, session?: ClientSession) => Promise<string>;
346
346
  updateStatusByOrg: (org: string | ObjectId, status: string, session?: ClientSession) => Promise<string>;
347
347
  countUserByOrg: (org: string | ObjectId) => Promise<any>;
348
+ getByUserOrg: (user: string | ObjectId, org: string | ObjectId) => Promise<TMember | null>;
348
349
  };
349
350
 
350
351
  declare function useMemberController(): {
package/dist/index.js CHANGED
@@ -2066,6 +2066,49 @@ function useMemberRepo() {
2066
2066
  );
2067
2067
  }
2068
2068
  }
2069
+ async function getByUserOrg(user, org) {
2070
+ try {
2071
+ org = new import_mongodb7.ObjectId(org);
2072
+ } catch (error) {
2073
+ throw new import_utils7.BadRequestError("Invalid organization ID.");
2074
+ }
2075
+ try {
2076
+ user = new import_mongodb7.ObjectId(user);
2077
+ } catch (error) {
2078
+ throw new import_utils7.BadRequestError("Invalid user ID.");
2079
+ }
2080
+ try {
2081
+ const cacheKey = (0, import_utils7.makeCacheKey)(namespace_collection, {
2082
+ user: String(user),
2083
+ org: String(org)
2084
+ });
2085
+ const cached = await getCache(cacheKey);
2086
+ if (cached) {
2087
+ import_utils7.logger.log({
2088
+ level: "info",
2089
+ message: `Cache hit for getByUserOrg member: ${cacheKey}`
2090
+ });
2091
+ return cached;
2092
+ }
2093
+ const data = await collection.findOne({ user, org });
2094
+ setCache(cacheKey, data, 300).then(() => {
2095
+ import_utils7.logger.log({
2096
+ level: "info",
2097
+ message: `Cache set for member by user and org: ${cacheKey}`
2098
+ });
2099
+ }).catch((err) => {
2100
+ import_utils7.logger.log({
2101
+ level: "error",
2102
+ message: `Failed to set cache for member by user and org: ${err.message}`
2103
+ });
2104
+ });
2105
+ return data;
2106
+ } catch (error) {
2107
+ throw new import_utils7.InternalServerError(
2108
+ "Internal server error, failed to retrieve member."
2109
+ );
2110
+ }
2111
+ }
2069
2112
  return {
2070
2113
  createIndexes,
2071
2114
  add,
@@ -2085,7 +2128,8 @@ function useMemberRepo() {
2085
2128
  updateStatusById,
2086
2129
  deleteById,
2087
2130
  updateStatusByOrg,
2088
- countUserByOrg
2131
+ countUserByOrg,
2132
+ getByUserOrg
2089
2133
  };
2090
2134
  }
2091
2135
 
@@ -10781,7 +10825,11 @@ function useVerificationService() {
10781
10825
  getVerifications: _getVerifications
10782
10826
  } = useVerificationRepo();
10783
10827
  const { getUserByEmail } = useUserRepo();
10784
- const { add: addMember, countUserByOrg } = useMemberRepo();
10828
+ const {
10829
+ add: addMember,
10830
+ countUserByOrg,
10831
+ getByUserOrg: getMemberByUserOrg
10832
+ } = useMemberRepo();
10785
10833
  const { getById: getOrgById } = useOrgRepo();
10786
10834
  const { getById: getRoleById } = useRoleRepo();
10787
10835
  async function createUserInvite({
@@ -11113,8 +11161,17 @@ function useVerificationService() {
11113
11161
  );
11114
11162
  }
11115
11163
  }
11164
+ const user = await getUserByEmail(value.email);
11165
+ let userId = "";
11166
+ if (user) {
11167
+ userId = String(user._id);
11168
+ }
11116
11169
  const memberCount = await countUserByOrg(String(value.org));
11117
- if (subscription.seats <= memberCount) {
11170
+ const existingMember = await getMemberByUserOrg(
11171
+ userId,
11172
+ String(value.org)
11173
+ );
11174
+ if (subscription.seats <= memberCount && !existingMember) {
11118
11175
  throw new import_utils53.BadRequestError(
11119
11176
  "Organization has reached the maximum number of members for its subscription plan."
11120
11177
  );