@goweekdays/core 2.11.5 → 2.11.7

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
  # @goweekdays/core
2
2
 
3
+ ## 2.11.7
4
+
5
+ ### Patch Changes
6
+
7
+ - 635486e: remove finance module as dependency
8
+
9
+ ## 2.11.6
10
+
11
+ ### Patch Changes
12
+
13
+ - 8736673: Add user count check for org member invites
14
+
3
15
  ## 2.11.5
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -344,6 +344,7 @@ declare function useMemberRepo(): {
344
344
  updateStatusById: (_id: string | ObjectId, status: string, session?: ClientSession) => Promise<string>;
345
345
  deleteById: (_id: string | ObjectId, session?: ClientSession) => Promise<string>;
346
346
  updateStatusByOrg: (org: string | ObjectId, status: string, session?: ClientSession) => Promise<string>;
347
+ countUserByOrg: (org: string | ObjectId) => Promise<any>;
347
348
  };
348
349
 
349
350
  declare function useMemberController(): {
package/dist/index.js CHANGED
@@ -1270,6 +1270,7 @@ function useMemberRepo() {
1270
1270
  try {
1271
1271
  await collection.createIndexes([
1272
1272
  { key: { name: 1 } },
1273
+ { key: { user: 1 } },
1273
1274
  { key: { status: 1 } },
1274
1275
  { key: { org: 1 } },
1275
1276
  { key: { name: "text", orgName: "text" }, name: "text_index" },
@@ -2009,6 +2010,55 @@ function useMemberRepo() {
2009
2010
  );
2010
2011
  }
2011
2012
  }
2013
+ async function countUserByOrg(org) {
2014
+ try {
2015
+ org = new import_mongodb7.ObjectId(org);
2016
+ } catch (error) {
2017
+ throw new import_utils7.BadRequestError("Invalid organization ID.");
2018
+ }
2019
+ try {
2020
+ const cacheKey = (0, import_utils7.makeCacheKey)(namespace_collection, {
2021
+ org: String(org),
2022
+ tag: "countUserByOrg"
2023
+ });
2024
+ const cached = await getCache(cacheKey);
2025
+ if (cached) {
2026
+ import_utils7.logger.log({
2027
+ level: "info",
2028
+ message: `Cache hit for countUserByOrg members: ${cacheKey}`
2029
+ });
2030
+ return cached;
2031
+ }
2032
+ const data = await collection.aggregate([
2033
+ { $match: { org, status: "active" } },
2034
+ {
2035
+ $group: {
2036
+ _id: "$user"
2037
+ }
2038
+ },
2039
+ {
2040
+ $count: "memberCount"
2041
+ }
2042
+ ]).toArray();
2043
+ const count = data[0]?.memberCount ?? 0;
2044
+ setCache(cacheKey, count, 300).then(() => {
2045
+ import_utils7.logger.log({
2046
+ level: "info",
2047
+ message: `Cache set for countUserByOrg members: ${cacheKey}`
2048
+ });
2049
+ }).catch((err) => {
2050
+ import_utils7.logger.log({
2051
+ level: "error",
2052
+ message: `Failed to set cache for countUserByOrg members: ${err.message}`
2053
+ });
2054
+ });
2055
+ return count;
2056
+ } catch (error) {
2057
+ throw new import_utils7.InternalServerError(
2058
+ "Internal server error, failed to count users."
2059
+ );
2060
+ }
2061
+ }
2012
2062
  return {
2013
2063
  createIndexes,
2014
2064
  add,
@@ -2027,7 +2077,8 @@ function useMemberRepo() {
2027
2077
  updateRoleById,
2028
2078
  updateStatusById,
2029
2079
  deleteById,
2030
- updateStatusByOrg
2080
+ updateStatusByOrg,
2081
+ countUserByOrg
2031
2082
  };
2032
2083
  }
2033
2084
 
@@ -10532,7 +10583,7 @@ function useVerificationService() {
10532
10583
  getVerifications: _getVerifications
10533
10584
  } = useVerificationRepo();
10534
10585
  const { getUserByEmail } = useUserRepo();
10535
- const { add: addMember } = useMemberRepo();
10586
+ const { add: addMember, countUserByOrg } = useMemberRepo();
10536
10587
  const { getById: getOrgById } = useOrgRepo();
10537
10588
  const { getById: getRoleById } = useRoleRepo();
10538
10589
  async function createUserInvite({
@@ -10831,6 +10882,7 @@ function useVerificationService() {
10831
10882
  }
10832
10883
  }
10833
10884
  const { getByOrg } = useSubscriptionRepo();
10885
+ const { getByCode: getPromoByCode } = usePromoRepo();
10834
10886
  async function inviteMember(value) {
10835
10887
  const { error } = schemaInviteMember.validate(value);
10836
10888
  if (error) {
@@ -10852,6 +10904,23 @@ function useVerificationService() {
10852
10904
  "Organization does not have an active subscription."
10853
10905
  );
10854
10906
  }
10907
+ if (subscription.promoCode) {
10908
+ const promo = await getPromoByCode(subscription.promoCode);
10909
+ if (!promo) {
10910
+ throw new import_utils53.BadRequestError("Promo code not found.");
10911
+ }
10912
+ if (promo.apps && promo.apps.length && !promo.apps.includes(value.app)) {
10913
+ throw new import_utils53.BadRequestError(
10914
+ "Promo code is not valid for the specified app."
10915
+ );
10916
+ }
10917
+ }
10918
+ const memberCount = await countUserByOrg(String(value.org));
10919
+ if (subscription.seats <= memberCount) {
10920
+ throw new import_utils53.BadRequestError(
10921
+ "Organization has reached the maximum number of members for its subscription plan."
10922
+ );
10923
+ }
10855
10924
  }
10856
10925
  let verificationData = {
10857
10926
  type: "user-invite",