@7365admin1/core 3.34.2 → 3.34.3

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
  # @iservice365/core
2
2
 
3
+ ## 3.34.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 195c268: Fix manpower alert email flood: dedupe daily remark creation per site, skip alerts for sites without recipient emails, send one alert per site per alert time, and enforce a configurable daily alert limit (MANPOWER_ALERT_DAILY_LIMIT, default 50)
8
+
3
9
  ## 3.34.2
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -9208,6 +9208,9 @@ var MAILER_TRANSPORT_SECURE = process.env.MAILER_TRANSPORT_SECURE === "true";
9208
9208
  var MAILER_EMAIL = process.env.MAILER_EMAIL ?? "default@gmail.com";
9209
9209
  var MAILER_PASSWORD = process.env.MAILER_PASSWORD ?? "password";
9210
9210
  var MAILER_FROM_EMAIL = process.env.MAILER_FROM_EMAIL || void 0;
9211
+ var MANPOWER_ALERT_DAILY_LIMIT = Number(
9212
+ process.env.MANPOWER_ALERT_DAILY_LIMIT ?? 50
9213
+ );
9211
9214
  var ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET ?? "access_token_secret";
9212
9215
  var REFRESH_TOKEN_SECRET = process.env.REFRESH_TOKEN_SECRET ?? "refresh_token_secret";
9213
9216
  var ACCESS_TOKEN_EXPIRY = process.env.ACCESS_TOKEN_EXPIRY ?? "15s";
@@ -63818,7 +63821,16 @@ var createManpowerRemarksDaily = async () => {
63818
63821
  serviceProviderId: servideProvider._id,
63819
63822
  status: "active"
63820
63823
  }).toArray();
63824
+ const existingToday = await remarks.find({ createdAtSGT: nowSGT.format("DD-MM-YYYY") }).project({ siteId: 1 }).toArray();
63825
+ const skipSiteIds = new Set(
63826
+ existingToday.map((doc) => doc.siteId.toString())
63827
+ );
63821
63828
  for (const site of remark) {
63829
+ const siteKey = site.siteId.toString();
63830
+ if (skipSiteIds.has(siteKey)) {
63831
+ continue;
63832
+ }
63833
+ skipSiteIds.add(siteKey);
63822
63834
  const existingRemark = await settings.findOne({
63823
63835
  siteId: site.siteId,
63824
63836
  enabled: true
@@ -63885,7 +63897,7 @@ var createManpowerRemarksDaily = async () => {
63885
63897
  }
63886
63898
  }
63887
63899
  if (items.length > 0) {
63888
- await remarks.insertMany(items);
63900
+ await remarks.insertMany(items, { ordered: false });
63889
63901
  }
63890
63902
  console.log("Daily manpower remarks created successfully.");
63891
63903
  } catch (error) {
@@ -63949,12 +63961,30 @@ var updateRemarksisAcknowledged = async () => {
63949
63961
  },
63950
63962
  { $set: { "remarks.2.remark.isAcknowledged": false } }
63951
63963
  );
63964
+ const { getCache, setCache } = (0, import_node_server_utils221.useCache)("manpower-alerts");
63965
+ const capKey = `manpower-alert-count:${nowSGT}`;
63966
+ let sentToday = await getCache(capKey) ?? 0;
63967
+ const emailedSites = /* @__PURE__ */ new Set();
63952
63968
  for (const id of updatedIds) {
63953
63969
  const doc = await remarks.findOne({ _id: id });
63970
+ const docSiteKey = doc?.siteId?.toString() ?? "";
63971
+ if (emailedSites.has(docSiteKey)) {
63972
+ continue;
63973
+ }
63954
63974
  const setting = await settings.findOne(
63955
63975
  { siteId: new import_mongodb131.ObjectId(doc?.siteId) },
63956
63976
  { projection: { emails: 1 } }
63957
63977
  );
63978
+ if (!setting?.emails?.length) {
63979
+ continue;
63980
+ }
63981
+ if (sentToday >= MANPOWER_ALERT_DAILY_LIMIT) {
63982
+ import_node_server_utils221.logger.log({
63983
+ level: "warn",
63984
+ message: `Manpower alert daily limit (${MANPOWER_ALERT_DAILY_LIMIT}) reached, skipping remaining alerts for ${nowSGT}`
63985
+ });
63986
+ break;
63987
+ }
63958
63988
  const payload = {
63959
63989
  startDate: doc?.createdAtSGT,
63960
63990
  endDate: doc?.createdAtSGT,
@@ -63995,6 +64025,9 @@ var updateRemarksisAcknowledged = async () => {
63995
64025
  expected,
63996
64026
  status: newStatus
63997
64027
  });
64028
+ emailedSites.add(docSiteKey);
64029
+ sentToday++;
64030
+ await setCache(capKey, sentToday, 86400);
63998
64031
  }
63999
64032
  } catch (error) {
64000
64033
  console.error("Error updating remarks:", error);