@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.
@@ -1,7 +1,7 @@
1
1
  import * as mongoose from 'mongoose';
2
2
  import * as _timardex_cluemart_shared from '@timardex/cluemart-shared';
3
3
  import { PromoCodeType, UserLicenceType, UserType, EnumAffiliateRewardType, EnumUserLicence, DateTimeType, EventListItemType, DateTimeWithPriceType } from '@timardex/cluemart-shared';
4
- import { n as SchemaAffiliateResourceType, S as SchemaOwnerType, i as SchemaRelationType } from '../Affiliate-BxNv9k41.mjs';
4
+ import { n as SchemaAffiliateResourceType, S as SchemaOwnerType, i as SchemaRelationType } from '../Affiliate-B76iZ3fE.mjs';
5
5
  import { G as GraphQLContext, O as ObjectId, f as SchemaCreateBulkNotificationInput } from '../Chat-Wn7yfc3v.mjs';
6
6
  import dayjs from 'dayjs';
7
7
  import { DateTimeType as DateTimeType$1 } from '@timardex/cluemart-shared/types';
@@ -28,8 +28,9 @@ type AwardAffiliateVendorSubscriptionRewardsResult = {
28
28
  * Eligibility per affiliate resource:
29
29
  * - Affiliate is not soft-deleted
30
30
  * - Linked `affiliateResources` entry is vendor, `resourceActive`, and not deleted
31
- * - Vendor is active, not deleted, and `promoCodes` includes the affiliate code
32
- * - Vendor owner has a non-expired Standard / Pro / Pro+ vendor licence
31
+ * - Vendor is active, not deleted, has the affiliate promo code, and
32
+ * `owner.userId` matches `affiliateResources.resourceOwner.userId`
33
+ * - `affiliateResources.resourceOwner` has a non-expired Standard / Pro / Pro+ vendor licence
33
34
  *
34
35
  * Reward type follows the owner's highest valid licence + Stripe paying status:
35
36
  * - Pro / Pro+ with paying Stripe (ACTIVE/TRIALING) → `ACTIVE_VENDOR_PRO_SUBSCRIPTION` (3 pts)
@@ -1,7 +1,7 @@
1
1
  import * as mongoose from 'mongoose';
2
2
  import * as _timardex_cluemart_shared from '@timardex/cluemart-shared';
3
3
  import { PromoCodeType, UserLicenceType, UserType, EnumAffiliateRewardType, EnumUserLicence, DateTimeType, EventListItemType, DateTimeWithPriceType } from '@timardex/cluemart-shared';
4
- import { n as SchemaAffiliateResourceType, S as SchemaOwnerType, i as SchemaRelationType } from '../Affiliate-DRuifOv4.js';
4
+ import { n as SchemaAffiliateResourceType, S as SchemaOwnerType, i as SchemaRelationType } from '../Affiliate-gqV1-654.js';
5
5
  import { G as GraphQLContext, O as ObjectId, f as SchemaCreateBulkNotificationInput } from '../Chat-Wn7yfc3v.js';
6
6
  import dayjs from 'dayjs';
7
7
  import { DateTimeType as DateTimeType$1 } from '@timardex/cluemart-shared/types';
@@ -28,8 +28,9 @@ type AwardAffiliateVendorSubscriptionRewardsResult = {
28
28
  * Eligibility per affiliate resource:
29
29
  * - Affiliate is not soft-deleted
30
30
  * - Linked `affiliateResources` entry is vendor, `resourceActive`, and not deleted
31
- * - Vendor is active, not deleted, and `promoCodes` includes the affiliate code
32
- * - Vendor owner has a non-expired Standard / Pro / Pro+ vendor licence
31
+ * - Vendor is active, not deleted, has the affiliate promo code, and
32
+ * `owner.userId` matches `affiliateResources.resourceOwner.userId`
33
+ * - `affiliateResources.resourceOwner` has a non-expired Standard / Pro / Pro+ vendor licence
33
34
  *
34
35
  * Reward type follows the owner's highest valid licence + Stripe paying status:
35
36
  * - Pro / Pro+ with paying Stripe (ACTIVE/TRIALING) → `ACTIVE_VENDOR_PRO_SUBSCRIPTION` (3 pts)
@@ -22,7 +22,7 @@ import {
22
22
  VendorModel,
23
23
  dateFormat,
24
24
  timeFormat
25
- } from "../chunk-7X5A335O.mjs";
25
+ } from "../chunk-SY7T26IW.mjs";
26
26
  import "../chunk-6PNG5BI5.mjs";
27
27
 
28
28
  // src/service/promoCode/constants.ts
@@ -1428,13 +1428,19 @@ async function findAffiliatesWithActiveVendorReferrals() {
1428
1428
  deletedAt: null
1429
1429
  }).select("_id affiliateCode affiliateResources owner").lean().exec();
1430
1430
  }
1431
- async function findEligibleReferredVendor(resourceId, affiliateCode) {
1432
- return await VendorModel.findOne({
1431
+ async function isEligibleReferredVendor({
1432
+ affiliateCode,
1433
+ resourceId,
1434
+ resourceOwnerUserId
1435
+ }) {
1436
+ const match = await VendorModel.exists({
1433
1437
  _id: resourceId,
1434
1438
  active: true,
1435
1439
  deletedAt: null,
1440
+ "owner.userId": resourceOwnerUserId,
1436
1441
  promoCodes: affiliateCode
1437
- }).select("_id name owner.userId").lean().exec();
1442
+ }).exec();
1443
+ return Boolean(match);
1438
1444
  }
1439
1445
  async function resolveSubscriptionRewardTypeForVendorOwner(vendorOwnerUserId, now) {
1440
1446
  const vendorOwner = await UserModel.findById(vendorOwnerUserId).select("licences stripe").lean().exec();
@@ -1476,8 +1482,7 @@ async function tryAwardSubscriptionReward({
1476
1482
  createdAt,
1477
1483
  period,
1478
1484
  resource,
1479
- rewardType,
1480
- vendor
1485
+ rewardType
1481
1486
  }) {
1482
1487
  const reward = createAffiliateReward(rewardType, createdAt);
1483
1488
  const updateResult = await AffiliateModel.updateOne(
@@ -1487,7 +1492,7 @@ async function tryAwardSubscriptionReward({
1487
1492
  $elemMatch: {
1488
1493
  resourceActive: true,
1489
1494
  resourceDeletedAt: null,
1490
- resourceId: vendor._id,
1495
+ resourceId: resource.resourceId,
1491
1496
  resourceType: EnumResourceType.VENDOR,
1492
1497
  rewards: {
1493
1498
  $not: {
@@ -1512,15 +1517,12 @@ async function tryAwardSubscriptionReward({
1512
1517
  if (updateResult.modifiedCount === 0) {
1513
1518
  return "skipped";
1514
1519
  }
1515
- const affiliateOwnerUserId = affiliate.owner?.userId;
1516
- if (affiliateOwnerUserId) {
1517
- await notifyAffiliateOwnerOfSubscriptionReward({
1518
- affiliateId: affiliate._id,
1519
- affiliateOwnerUserId,
1520
- resourceName: resource.resourceName || vendor.name || "vendor",
1521
- rewardValue: reward.rewardValue
1522
- });
1523
- }
1520
+ await notifyAffiliateOwnerOfSubscriptionReward({
1521
+ affiliateId: affiliate._id,
1522
+ affiliateOwnerUserId: affiliate.owner.userId,
1523
+ resourceName: resource.resourceName,
1524
+ rewardValue: reward.rewardValue
1525
+ });
1524
1526
  return "awarded";
1525
1527
  }
1526
1528
  async function processAffiliateVendorReferral({
@@ -1531,15 +1533,16 @@ async function processAffiliateVendorReferral({
1531
1533
  period,
1532
1534
  resource
1533
1535
  }) {
1534
- const vendor = await findEligibleReferredVendor(
1535
- resource.resourceId,
1536
- affiliateCode
1537
- );
1538
- if (!vendor?.owner?.userId) {
1536
+ const vendorEligible = await isEligibleReferredVendor({
1537
+ affiliateCode,
1538
+ resourceId: resource.resourceId,
1539
+ resourceOwnerUserId: resource.resourceOwner.userId
1540
+ });
1541
+ if (!vendorEligible) {
1539
1542
  return "skipped";
1540
1543
  }
1541
1544
  const rewardType = await resolveSubscriptionRewardTypeForVendorOwner(
1542
- vendor.owner.userId,
1545
+ resource.resourceOwner.userId,
1543
1546
  now
1544
1547
  );
1545
1548
  if (!rewardType) {
@@ -1550,8 +1553,7 @@ async function processAffiliateVendorReferral({
1550
1553
  createdAt,
1551
1554
  period,
1552
1555
  resource,
1553
- rewardType,
1554
- vendor
1556
+ rewardType
1555
1557
  });
1556
1558
  }
1557
1559
  async function awardAffiliateVendorSubscriptionRewards(now = /* @__PURE__ */ new Date()) {