@likewatt/models 1.71.0 → 1.72.1

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.
Files changed (2) hide show
  1. package/dist/core/Site.js +60 -0
  2. package/package.json +1 -1
package/dist/core/Site.js CHANGED
@@ -667,3 +667,63 @@ exports.SiteSchema.pre('save', function (next) {
667
667
  }
668
668
  next();
669
669
  });
670
+ const getCollectiveSiteModel = (conn) => {
671
+ if (!conn || !conn.modelNames().includes('CollectiveSite'))
672
+ return null;
673
+ return conn.model('CollectiveSite');
674
+ };
675
+ exports.SiteSchema.post('save', async function (doc) {
676
+ if (!doc.collectiveSiteId || !doc._id)
677
+ return;
678
+ const CollectiveSiteModel = getCollectiveSiteModel(doc.db);
679
+ if (!CollectiveSiteModel)
680
+ return;
681
+ await CollectiveSiteModel.updateOne({ _id: doc.collectiveSiteId }, { $addToSet: { sites: doc._id } }).exec();
682
+ });
683
+ exports.SiteSchema.pre('findOneAndUpdate', async function () {
684
+ var _a, _b;
685
+ const self = this;
686
+ const update = self.getUpdate();
687
+ if (!update)
688
+ return;
689
+ const set = (_a = update.$set) !== null && _a !== void 0 ? _a : update;
690
+ const touchesCollective = 'collectiveSiteId' in set;
691
+ if (!touchesCollective)
692
+ return;
693
+ const newRaw = set.collectiveSiteId;
694
+ const newId = typeof newRaw === 'string' && newRaw.length > 0 ? newRaw : null;
695
+ const oldDoc = await self.model
696
+ .findOne(self.getQuery())
697
+ .select('_id collectiveSiteId')
698
+ .lean()
699
+ .exec();
700
+ if (!oldDoc)
701
+ return;
702
+ const oldId = (_b = oldDoc.collectiveSiteId) !== null && _b !== void 0 ? _b : null;
703
+ if (oldId === newId)
704
+ return;
705
+ self._collectiveSiteSync = {
706
+ siteId: oldDoc._id,
707
+ oldId: oldId !== null && oldId !== void 0 ? oldId : undefined,
708
+ newId,
709
+ };
710
+ });
711
+ exports.SiteSchema.post('findOneAndUpdate', async function (doc) {
712
+ var _a;
713
+ const self = this;
714
+ const sync = self._collectiveSiteSync;
715
+ if (!sync)
716
+ return;
717
+ const siteId = (_a = sync.siteId) !== null && _a !== void 0 ? _a : doc === null || doc === void 0 ? void 0 : doc._id;
718
+ if (!siteId)
719
+ return;
720
+ const CollectiveSiteModel = getCollectiveSiteModel(self.model.db);
721
+ if (!CollectiveSiteModel)
722
+ return;
723
+ if (sync.oldId) {
724
+ await CollectiveSiteModel.updateOne({ _id: sync.oldId }, { $pull: { sites: siteId } }).exec();
725
+ }
726
+ if (sync.newId) {
727
+ await CollectiveSiteModel.updateOne({ _id: sync.newId }, { $addToSet: { sites: siteId } }).exec();
728
+ }
729
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@likewatt/models",
3
- "version": "1.71.0",
3
+ "version": "1.72.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {