@timardex/cluemart-server-shared 1.0.305 → 1.0.306

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.cjs CHANGED
@@ -10756,13 +10756,19 @@ async function findAffiliatesWithActiveVendorReferrals() {
10756
10756
  deletedAt: null
10757
10757
  }).select("_id affiliateCode affiliateResources owner").lean().exec();
10758
10758
  }
10759
- async function findEligibleReferredVendor(resourceId, affiliateCode) {
10760
- return await VendorModel.findOne({
10759
+ async function isEligibleReferredVendor({
10760
+ affiliateCode,
10761
+ resourceId,
10762
+ resourceOwnerUserId
10763
+ }) {
10764
+ const match = await VendorModel.exists({
10761
10765
  _id: resourceId,
10762
10766
  active: true,
10763
10767
  deletedAt: null,
10768
+ "owner.userId": resourceOwnerUserId,
10764
10769
  promoCodes: affiliateCode
10765
- }).select("_id name owner.userId").lean().exec();
10770
+ }).exec();
10771
+ return Boolean(match);
10766
10772
  }
10767
10773
  async function resolveSubscriptionRewardTypeForVendorOwner(vendorOwnerUserId, now) {
10768
10774
  const vendorOwner = await UserModel.findById(vendorOwnerUserId).select("licences stripe").lean().exec();
@@ -10804,8 +10810,7 @@ async function tryAwardSubscriptionReward({
10804
10810
  createdAt,
10805
10811
  period,
10806
10812
  resource,
10807
- rewardType,
10808
- vendor
10813
+ rewardType
10809
10814
  }) {
10810
10815
  const reward = createAffiliateReward(rewardType, createdAt);
10811
10816
  const updateResult = await AffiliateModel.updateOne(
@@ -10815,7 +10820,7 @@ async function tryAwardSubscriptionReward({
10815
10820
  $elemMatch: {
10816
10821
  resourceActive: true,
10817
10822
  resourceDeletedAt: null,
10818
- resourceId: vendor._id,
10823
+ resourceId: resource.resourceId,
10819
10824
  resourceType: EnumResourceType.VENDOR,
10820
10825
  rewards: {
10821
10826
  $not: {
@@ -10840,15 +10845,12 @@ async function tryAwardSubscriptionReward({
10840
10845
  if (updateResult.modifiedCount === 0) {
10841
10846
  return "skipped";
10842
10847
  }
10843
- const affiliateOwnerUserId = affiliate.owner?.userId;
10844
- if (affiliateOwnerUserId) {
10845
- await notifyAffiliateOwnerOfSubscriptionReward({
10846
- affiliateId: affiliate._id,
10847
- affiliateOwnerUserId,
10848
- resourceName: resource.resourceName || vendor.name || "vendor",
10849
- rewardValue: reward.rewardValue
10850
- });
10851
- }
10848
+ await notifyAffiliateOwnerOfSubscriptionReward({
10849
+ affiliateId: affiliate._id,
10850
+ affiliateOwnerUserId: affiliate.owner.userId,
10851
+ resourceName: resource.resourceName,
10852
+ rewardValue: reward.rewardValue
10853
+ });
10852
10854
  return "awarded";
10853
10855
  }
10854
10856
  async function processAffiliateVendorReferral({
@@ -10859,15 +10861,16 @@ async function processAffiliateVendorReferral({
10859
10861
  period,
10860
10862
  resource
10861
10863
  }) {
10862
- const vendor = await findEligibleReferredVendor(
10863
- resource.resourceId,
10864
- affiliateCode
10865
- );
10866
- if (!vendor?.owner?.userId) {
10864
+ const vendorEligible = await isEligibleReferredVendor({
10865
+ affiliateCode,
10866
+ resourceId: resource.resourceId,
10867
+ resourceOwnerUserId: resource.resourceOwner.userId
10868
+ });
10869
+ if (!vendorEligible) {
10867
10870
  return "skipped";
10868
10871
  }
10869
10872
  const rewardType = await resolveSubscriptionRewardTypeForVendorOwner(
10870
- vendor.owner.userId,
10873
+ resource.resourceOwner.userId,
10871
10874
  now
10872
10875
  );
10873
10876
  if (!rewardType) {
@@ -10878,8 +10881,7 @@ async function processAffiliateVendorReferral({
10878
10881
  createdAt,
10879
10882
  period,
10880
10883
  resource,
10881
- rewardType,
10882
- vendor
10884
+ rewardType
10883
10885
  });
10884
10886
  }
10885
10887
  async function awardAffiliateVendorSubscriptionRewards(now = /* @__PURE__ */ new Date()) {