@timardex/cluemart-server-shared 4.1.0 → 4.2.0
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/{Affiliate-CpoM9_Bb.d.mts → Affiliate-Bilg9mgw.d.mts} +43 -4
- package/dist/{Affiliate-YoKkDGxN.d.ts → Affiliate-CfG8ctIo.d.ts} +43 -4
- package/dist/{chunk-U5WL3CZR.mjs → chunk-ABW2L2HY.mjs} +61 -2
- package/dist/chunk-ABW2L2HY.mjs.map +1 -0
- package/dist/index.cjs +645 -169
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +252 -10
- package/dist/index.d.ts +252 -10
- package/dist/index.mjs +538 -70
- package/dist/index.mjs.map +1 -1
- package/dist/mongoose/index.cjs +60 -1
- package/dist/mongoose/index.cjs.map +1 -1
- package/dist/mongoose/index.d.mts +15 -9
- package/dist/mongoose/index.d.ts +15 -9
- package/dist/mongoose/index.mjs +3 -1
- package/dist/service/index.cjs +642 -168
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.mts +200 -3
- package/dist/service/index.d.ts +200 -3
- package/dist/service/index.mjs +477 -68
- package/dist/service/index.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-U5WL3CZR.mjs.map +0 -1
package/dist/service/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
PushTokenModel,
|
|
12
12
|
UserModel,
|
|
13
13
|
VendorModel
|
|
14
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-ABW2L2HY.mjs";
|
|
15
15
|
import "../chunk-F54NB7VH.mjs";
|
|
16
16
|
|
|
17
17
|
// src/service/promoCode/constants.ts
|
|
@@ -452,14 +452,314 @@ function normalizeAffiliatePromoCodes(promoCodes) {
|
|
|
452
452
|
return [...new Set(normalized)];
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
+
// src/service/associates/index.ts
|
|
456
|
+
import {
|
|
457
|
+
EnumAssociateStatus,
|
|
458
|
+
EnumResourceType as EnumResourceType2,
|
|
459
|
+
EnumUserRole
|
|
460
|
+
} from "@timardex/cluemart-shared";
|
|
461
|
+
import mongoose from "mongoose";
|
|
462
|
+
function isAssociableResourceType(resourceType) {
|
|
463
|
+
return resourceType === EnumResourceType2.EVENT || resourceType === EnumResourceType2.PARTNER || resourceType === EnumResourceType2.VENDOR;
|
|
464
|
+
}
|
|
465
|
+
function getAssociateAccessMeta(resourceType) {
|
|
466
|
+
if (resourceType === EnumResourceType2.EVENT) {
|
|
467
|
+
return {
|
|
468
|
+
associatedField: "events",
|
|
469
|
+
associatedPath: "associatedResources.events",
|
|
470
|
+
associateRole: EnumUserRole.ASSOCIATED_EVENT
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
if (resourceType === EnumResourceType2.PARTNER) {
|
|
474
|
+
return {
|
|
475
|
+
associatedField: "partners",
|
|
476
|
+
associatedPath: "associatedResources.partners",
|
|
477
|
+
associateRole: EnumUserRole.ASSOCIATED_PARTNER
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
associatedField: "vendors",
|
|
482
|
+
associatedPath: "associatedResources.vendors",
|
|
483
|
+
associateRole: EnumUserRole.ASSOCIATED_VENDOR
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
var ASSOCIABLE_MODELS = {
|
|
487
|
+
[EnumResourceType2.EVENT]: EventModel,
|
|
488
|
+
[EnumResourceType2.PARTNER]: PartnerModel,
|
|
489
|
+
[EnumResourceType2.VENDOR]: VendorModel
|
|
490
|
+
};
|
|
491
|
+
function acceptedAssociateUserIds(associates) {
|
|
492
|
+
return (associates ?? []).filter((associate) => associate.status === EnumAssociateStatus.ACCEPTED).map((associate) => associate.userId);
|
|
493
|
+
}
|
|
494
|
+
async function detachUsersFromResource({
|
|
495
|
+
resourceId,
|
|
496
|
+
resourceType,
|
|
497
|
+
userIds
|
|
498
|
+
}) {
|
|
499
|
+
if (userIds.length === 0) return;
|
|
500
|
+
const { associateRole, associatedPath: path } = getAssociateAccessMeta(resourceType);
|
|
501
|
+
const removedResourceId = typeof resourceId === "string" ? new mongoose.Types.ObjectId(resourceId) : resourceId;
|
|
502
|
+
await UserModel.updateMany({ _id: { $in: userIds } }, [
|
|
503
|
+
{
|
|
504
|
+
$set: {
|
|
505
|
+
[path]: {
|
|
506
|
+
$setDifference: [{ $ifNull: [`$${path}`, []] }, [removedResourceId]]
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
{
|
|
511
|
+
$set: {
|
|
512
|
+
roles: {
|
|
513
|
+
$cond: [
|
|
514
|
+
{ $eq: [{ $size: { $ifNull: [`$${path}`, []] } }, 0] },
|
|
515
|
+
{
|
|
516
|
+
$setDifference: [{ $ifNull: ["$roles", []] }, [associateRole]]
|
|
517
|
+
},
|
|
518
|
+
"$roles"
|
|
519
|
+
]
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
]).exec();
|
|
524
|
+
}
|
|
525
|
+
async function acceptedUserIdsForResource({
|
|
526
|
+
resourceId,
|
|
527
|
+
resourceType
|
|
528
|
+
}) {
|
|
529
|
+
const model = ASSOCIABLE_MODELS[resourceType];
|
|
530
|
+
const resource = await model.findOne({ _id: resourceId, deletedAt: null }).select("associates").lean().exec();
|
|
531
|
+
return new Set(
|
|
532
|
+
acceptedAssociateUserIds(resource?.associates).map(
|
|
533
|
+
(userId) => String(userId)
|
|
534
|
+
)
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
async function grantAssociateGrants({
|
|
538
|
+
resourceId,
|
|
539
|
+
resourceType,
|
|
540
|
+
userIds
|
|
541
|
+
}) {
|
|
542
|
+
if (userIds.length === 0) return;
|
|
543
|
+
const acceptedUserIds = await acceptedUserIdsForResource({
|
|
544
|
+
resourceId,
|
|
545
|
+
resourceType
|
|
546
|
+
});
|
|
547
|
+
const grantUserIds = userIds.filter(
|
|
548
|
+
(userId) => acceptedUserIds.has(String(userId))
|
|
549
|
+
);
|
|
550
|
+
if (grantUserIds.length === 0) return;
|
|
551
|
+
const { associateRole, associatedPath } = getAssociateAccessMeta(resourceType);
|
|
552
|
+
await UserModel.updateMany(
|
|
553
|
+
{ _id: { $in: grantUserIds } },
|
|
554
|
+
{
|
|
555
|
+
$addToSet: {
|
|
556
|
+
[associatedPath]: resourceId,
|
|
557
|
+
roles: associateRole
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
).exec();
|
|
561
|
+
}
|
|
562
|
+
async function detachUnlessStillAccepted({
|
|
563
|
+
resourceId,
|
|
564
|
+
resourceType,
|
|
565
|
+
userIds
|
|
566
|
+
}) {
|
|
567
|
+
if (userIds.length === 0) return [];
|
|
568
|
+
await detachUsersFromResource({ resourceId, resourceType, userIds });
|
|
569
|
+
const acceptedAfterDetach = await acceptedUserIdsForResource({
|
|
570
|
+
resourceId,
|
|
571
|
+
resourceType
|
|
572
|
+
});
|
|
573
|
+
const restoredUserIds = userIds.filter(
|
|
574
|
+
(userId) => acceptedAfterDetach.has(String(userId))
|
|
575
|
+
);
|
|
576
|
+
if (restoredUserIds.length === 0) return userIds;
|
|
577
|
+
await grantAssociateGrants({
|
|
578
|
+
resourceId,
|
|
579
|
+
resourceType,
|
|
580
|
+
userIds: restoredUserIds
|
|
581
|
+
});
|
|
582
|
+
const acceptedAfterRestore = await acceptedUserIdsForResource({
|
|
583
|
+
resourceId,
|
|
584
|
+
resourceType
|
|
585
|
+
});
|
|
586
|
+
const regressedUserIds = restoredUserIds.filter(
|
|
587
|
+
(userId) => !acceptedAfterRestore.has(String(userId))
|
|
588
|
+
);
|
|
589
|
+
if (regressedUserIds.length > 0) {
|
|
590
|
+
await detachUsersFromResource({
|
|
591
|
+
resourceId,
|
|
592
|
+
resourceType,
|
|
593
|
+
userIds: regressedUserIds
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
return userIds.filter((userId) => !acceptedAfterRestore.has(String(userId)));
|
|
597
|
+
}
|
|
598
|
+
async function detachStaleAssociateGrants({
|
|
599
|
+
acceptedUserIds,
|
|
600
|
+
resourceId,
|
|
601
|
+
resourceType
|
|
602
|
+
}) {
|
|
603
|
+
const { associatedPath } = getAssociateAccessMeta(resourceType);
|
|
604
|
+
const stale = await UserModel.find({
|
|
605
|
+
_id: { $nin: acceptedUserIds },
|
|
606
|
+
[associatedPath]: resourceId
|
|
607
|
+
}).select("_id").lean().exec();
|
|
608
|
+
const staleUserIds = stale.map((user) => user._id);
|
|
609
|
+
return detachUnlessStillAccepted({
|
|
610
|
+
resourceId,
|
|
611
|
+
resourceType,
|
|
612
|
+
userIds: staleUserIds
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
async function detachAssociateGrantsForResources({
|
|
616
|
+
resources
|
|
617
|
+
}) {
|
|
618
|
+
const result = resources.map((resource) => ({
|
|
619
|
+
detachedUserIds: [],
|
|
620
|
+
resourceId: resource.resourceId,
|
|
621
|
+
resourceType: resource.resourceType
|
|
622
|
+
}));
|
|
623
|
+
const indexByType = /* @__PURE__ */ new Map();
|
|
624
|
+
resources.forEach((resource, index) => {
|
|
625
|
+
const entries = indexByType.get(resource.resourceType) ?? [];
|
|
626
|
+
entries.push({ index, resourceId: resource.resourceId });
|
|
627
|
+
indexByType.set(resource.resourceType, entries);
|
|
628
|
+
});
|
|
629
|
+
for (const [resourceType, entries] of indexByType) {
|
|
630
|
+
const { associatedField, associatedPath } = getAssociateAccessMeta(resourceType);
|
|
631
|
+
const resourceIds = entries.map((entry) => entry.resourceId);
|
|
632
|
+
const wanted = new Set(resourceIds.map((resourceId) => String(resourceId)));
|
|
633
|
+
const holders = await UserModel.find({
|
|
634
|
+
[associatedPath]: { $in: resourceIds }
|
|
635
|
+
}).select("_id associatedResources").lean().exec();
|
|
636
|
+
const userIdsByResource = /* @__PURE__ */ new Map();
|
|
637
|
+
for (const user of holders) {
|
|
638
|
+
for (const heldResourceId of user.associatedResources?.[associatedField] ?? []) {
|
|
639
|
+
const key = String(heldResourceId);
|
|
640
|
+
if (!wanted.has(key)) continue;
|
|
641
|
+
const userIds = userIdsByResource.get(key) ?? [];
|
|
642
|
+
userIds.push(user._id);
|
|
643
|
+
userIdsByResource.set(key, userIds);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
for (const entry of entries) {
|
|
647
|
+
const userIds = userIdsByResource.get(String(entry.resourceId));
|
|
648
|
+
if (!userIds || userIds.length === 0) continue;
|
|
649
|
+
await detachUsersFromResource({
|
|
650
|
+
resourceId: entry.resourceId,
|
|
651
|
+
resourceType,
|
|
652
|
+
userIds
|
|
653
|
+
});
|
|
654
|
+
result[entry.index].detachedUserIds = userIds;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
return result;
|
|
658
|
+
}
|
|
659
|
+
var DEFAULT_RECONCILE_BATCH_SIZE = 500;
|
|
660
|
+
async function reconcileUserBatch({
|
|
661
|
+
associatedField,
|
|
662
|
+
model,
|
|
663
|
+
resourceType,
|
|
664
|
+
users
|
|
665
|
+
}) {
|
|
666
|
+
const heldByResource = /* @__PURE__ */ new Map();
|
|
667
|
+
for (const user of users) {
|
|
668
|
+
for (const resourceId of user.associatedResources?.[associatedField] ?? []) {
|
|
669
|
+
const key = String(resourceId);
|
|
670
|
+
const entry = heldByResource.get(key) ?? { resourceId, userIds: [] };
|
|
671
|
+
entry.userIds.push(user._id);
|
|
672
|
+
heldByResource.set(key, entry);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (heldByResource.size === 0) return 0;
|
|
676
|
+
const resources = await model.find({
|
|
677
|
+
_id: {
|
|
678
|
+
$in: [...heldByResource.values()].map((entry) => entry.resourceId)
|
|
679
|
+
}
|
|
680
|
+
}).select("associates deletedAt").lean().exec();
|
|
681
|
+
const acceptedByResource = /* @__PURE__ */ new Map();
|
|
682
|
+
for (const resource of resources) {
|
|
683
|
+
acceptedByResource.set(
|
|
684
|
+
String(resource._id),
|
|
685
|
+
resource.deletedAt ? /* @__PURE__ */ new Set() : new Set(
|
|
686
|
+
acceptedAssociateUserIds(resource.associates).map(
|
|
687
|
+
(userId) => String(userId)
|
|
688
|
+
)
|
|
689
|
+
)
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
let detachedGrants = 0;
|
|
693
|
+
for (const [key, { resourceId, userIds }] of heldByResource) {
|
|
694
|
+
const accepted = acceptedByResource.get(key);
|
|
695
|
+
const staleUserIds = userIds.filter(
|
|
696
|
+
(userId) => !accepted?.has(String(userId))
|
|
697
|
+
);
|
|
698
|
+
if (staleUserIds.length === 0) continue;
|
|
699
|
+
const removedUserIds = await detachUnlessStillAccepted({
|
|
700
|
+
resourceId,
|
|
701
|
+
resourceType,
|
|
702
|
+
userIds: staleUserIds
|
|
703
|
+
});
|
|
704
|
+
detachedGrants += removedUserIds.length;
|
|
705
|
+
}
|
|
706
|
+
return detachedGrants;
|
|
707
|
+
}
|
|
708
|
+
async function reconcileResourceType({
|
|
709
|
+
batchSize,
|
|
710
|
+
resourceType
|
|
711
|
+
}) {
|
|
712
|
+
const { associatedField, associatedPath } = getAssociateAccessMeta(resourceType);
|
|
713
|
+
const model = ASSOCIABLE_MODELS[resourceType];
|
|
714
|
+
const result = {
|
|
715
|
+
detachedGrants: 0,
|
|
716
|
+
scannedUsers: 0
|
|
717
|
+
};
|
|
718
|
+
let lastId = null;
|
|
719
|
+
for (; ; ) {
|
|
720
|
+
const users = await UserModel.find({
|
|
721
|
+
...lastId ? { _id: { $gt: lastId } } : {},
|
|
722
|
+
[associatedPath]: { $exists: true, $ne: [] }
|
|
723
|
+
}).sort({ _id: 1 }).limit(batchSize).select("_id associatedResources").lean().exec();
|
|
724
|
+
if (users.length === 0) break;
|
|
725
|
+
result.scannedUsers += users.length;
|
|
726
|
+
lastId = users[users.length - 1]._id;
|
|
727
|
+
result.detachedGrants += await reconcileUserBatch({
|
|
728
|
+
associatedField,
|
|
729
|
+
model,
|
|
730
|
+
resourceType,
|
|
731
|
+
users
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
return result;
|
|
735
|
+
}
|
|
736
|
+
async function reconcileAssociateGrants({
|
|
737
|
+
batchSize = DEFAULT_RECONCILE_BATCH_SIZE
|
|
738
|
+
} = {}) {
|
|
739
|
+
const totals = {
|
|
740
|
+
detachedGrants: 0,
|
|
741
|
+
scannedUsers: 0
|
|
742
|
+
};
|
|
743
|
+
for (const resourceType of [
|
|
744
|
+
EnumResourceType2.EVENT,
|
|
745
|
+
EnumResourceType2.VENDOR,
|
|
746
|
+
EnumResourceType2.PARTNER
|
|
747
|
+
]) {
|
|
748
|
+
const result = await reconcileResourceType({ batchSize, resourceType });
|
|
749
|
+
totals.detachedGrants += result.detachedGrants;
|
|
750
|
+
totals.scannedUsers += result.scannedUsers;
|
|
751
|
+
}
|
|
752
|
+
return totals;
|
|
753
|
+
}
|
|
754
|
+
|
|
455
755
|
// src/service/database.ts
|
|
456
|
-
import
|
|
756
|
+
import mongoose3 from "mongoose";
|
|
457
757
|
|
|
458
758
|
// src/service/indexes.ts
|
|
459
|
-
import
|
|
759
|
+
import mongoose2 from "mongoose";
|
|
460
760
|
function registeredModels() {
|
|
461
|
-
return
|
|
462
|
-
const model =
|
|
761
|
+
return mongoose2.modelNames().map((modelName) => {
|
|
762
|
+
const model = mongoose2.model(modelName);
|
|
463
763
|
return { createIndexes: () => model.createIndexes(), modelName };
|
|
464
764
|
});
|
|
465
765
|
}
|
|
@@ -501,7 +801,7 @@ var connectToDatabase = async ({
|
|
|
501
801
|
// Fallback to MongoDB Atlas connection string
|
|
502
802
|
`mongodb+srv://${dbUser}:${dbPassword}@${dbName}.mongodb.net/?retryWrites=true&w=majority&appName=${appName}`
|
|
503
803
|
);
|
|
504
|
-
await
|
|
804
|
+
await mongoose3.connect(mongoUri);
|
|
505
805
|
const connectionType = mongodbUri ? "Local MongoDB" : "MongoDB Atlas";
|
|
506
806
|
console.log(
|
|
507
807
|
`${connectionType} connected from server/src/service/database.ts`
|
|
@@ -628,15 +928,15 @@ async function updateAdStatuses() {
|
|
|
628
928
|
}
|
|
629
929
|
|
|
630
930
|
// src/service/objectIdToString.ts
|
|
631
|
-
import
|
|
931
|
+
import mongoose4 from "mongoose";
|
|
632
932
|
function isValidObjectId(value) {
|
|
633
|
-
return
|
|
933
|
+
return mongoose4.Types.ObjectId.isValid(value);
|
|
634
934
|
}
|
|
635
935
|
function convertObjectIdsToStrings(obj) {
|
|
636
936
|
if (obj === null || obj === void 0) {
|
|
637
937
|
return obj;
|
|
638
938
|
}
|
|
639
|
-
if (obj instanceof
|
|
939
|
+
if (obj instanceof mongoose4.Types.ObjectId) {
|
|
640
940
|
return obj.toString();
|
|
641
941
|
}
|
|
642
942
|
if (obj instanceof Date) {
|
|
@@ -1393,7 +1693,7 @@ async function handlePartnerLicenceCompliance(user, now) {
|
|
|
1393
1693
|
import {
|
|
1394
1694
|
EnumNotificationResourceType as EnumNotificationResourceType2,
|
|
1395
1695
|
EnumNotificationType as EnumNotificationType2,
|
|
1396
|
-
|
|
1696
|
+
EnumResourceType as EnumResourceType4,
|
|
1397
1697
|
EnumUserLicence as EnumUserLicence6,
|
|
1398
1698
|
vendorLicences
|
|
1399
1699
|
} from "@timardex/cluemart-shared";
|
|
@@ -1401,20 +1701,21 @@ import dayjs6 from "dayjs";
|
|
|
1401
1701
|
|
|
1402
1702
|
// src/service/license/models.ts
|
|
1403
1703
|
import {
|
|
1404
|
-
EnumResourceType as
|
|
1704
|
+
EnumResourceType as EnumResourceType3,
|
|
1705
|
+
EnumSubscriptionStatus as EnumSubscriptionStatus2,
|
|
1405
1706
|
EnumUserLicence as EnumUserLicence5,
|
|
1406
1707
|
isNonVisitorResourceType,
|
|
1407
1708
|
LicenceExpiryDate
|
|
1408
1709
|
} from "@timardex/cluemart-shared";
|
|
1409
1710
|
import dayjs5 from "dayjs";
|
|
1410
1711
|
import { GraphQLError } from "graphql";
|
|
1411
|
-
var LicenceCategory = /* @__PURE__ */ ((
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
return
|
|
1712
|
+
var LicenceCategory = /* @__PURE__ */ ((LicenceCategory3) => {
|
|
1713
|
+
LicenceCategory3["EVENT"] = "event";
|
|
1714
|
+
LicenceCategory3["PARTNER"] = "partner";
|
|
1715
|
+
LicenceCategory3["VENDOR"] = "vendor";
|
|
1716
|
+
LicenceCategory3["AFFILIATE"] = "affiliate";
|
|
1717
|
+
LicenceCategory3["SCHOOL"] = "school";
|
|
1718
|
+
return LicenceCategory3;
|
|
1418
1719
|
})(LicenceCategory || {});
|
|
1419
1720
|
var licenceCategoryMapping = {
|
|
1420
1721
|
[EnumUserLicence5.PRO_EVENT]: "event" /* EVENT */,
|
|
@@ -1447,6 +1748,16 @@ var standardLicenceSet = new Set(standardLicences);
|
|
|
1447
1748
|
function isStandardLicence(licence) {
|
|
1448
1749
|
return standardLicenceSet.has(licence);
|
|
1449
1750
|
}
|
|
1751
|
+
var proLicences = [
|
|
1752
|
+
EnumUserLicence5.PRO_EVENT,
|
|
1753
|
+
EnumUserLicence5.PRO_PLUS_EVENT,
|
|
1754
|
+
EnumUserLicence5.PRO_VENDOR,
|
|
1755
|
+
EnumUserLicence5.PRO_PLUS_VENDOR
|
|
1756
|
+
];
|
|
1757
|
+
var proLicenceSet = new Set(proLicences);
|
|
1758
|
+
function isProLicence(licence) {
|
|
1759
|
+
return proLicenceSet.has(licence);
|
|
1760
|
+
}
|
|
1450
1761
|
var categoryRegexMapping = {
|
|
1451
1762
|
["event" /* EVENT */]: /_event$/,
|
|
1452
1763
|
["partner" /* PARTNER */]: /_partner$/,
|
|
@@ -1597,11 +1908,11 @@ async function checkOwnerHasLicence({
|
|
|
1597
1908
|
const owner = await UserModel.findById(userId);
|
|
1598
1909
|
if (!owner) throw new GraphQLError("Owner not found");
|
|
1599
1910
|
const categoryByResource = {
|
|
1600
|
-
[
|
|
1601
|
-
[
|
|
1602
|
-
[
|
|
1603
|
-
[
|
|
1604
|
-
[
|
|
1911
|
+
[EnumResourceType3.EVENT]: "event" /* EVENT */,
|
|
1912
|
+
[EnumResourceType3.PARTNER]: "partner" /* PARTNER */,
|
|
1913
|
+
[EnumResourceType3.VENDOR]: "vendor" /* VENDOR */,
|
|
1914
|
+
[EnumResourceType3.AFFILIATE]: "affiliate" /* AFFILIATE */,
|
|
1915
|
+
[EnumResourceType3.SCHOOL]: "school" /* SCHOOL */
|
|
1605
1916
|
};
|
|
1606
1917
|
if (!isNonVisitorResourceType(resourceType)) {
|
|
1607
1918
|
throw new GraphQLError(`Invalid resource type: ${resourceType}.`);
|
|
@@ -1625,6 +1936,128 @@ async function checkOwnerHasLicence({
|
|
|
1625
1936
|
);
|
|
1626
1937
|
}
|
|
1627
1938
|
}
|
|
1939
|
+
var resourceLicenceCategory = {
|
|
1940
|
+
[EnumResourceType3.EVENT]: "event" /* EVENT */,
|
|
1941
|
+
[EnumResourceType3.PARTNER]: "partner" /* PARTNER */,
|
|
1942
|
+
[EnumResourceType3.VENDOR]: "vendor" /* VENDOR */
|
|
1943
|
+
};
|
|
1944
|
+
function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
|
|
1945
|
+
const stripe = user.stripe;
|
|
1946
|
+
if (!stripe || stripe.currentPlan !== licenceType) {
|
|
1947
|
+
return false;
|
|
1948
|
+
}
|
|
1949
|
+
const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs5(stripe.paymentGracePeriodEndsAt) : null;
|
|
1950
|
+
const hasActivePaymentGracePeriod = (stripe.status === EnumSubscriptionStatus2.PAST_DUE || stripe.status === EnumSubscriptionStatus2.INACTIVE) && Boolean(stripe.paymentGraceSubscriptionId) && (!stripe.subscriptionId || stripe.subscriptionId === stripe.paymentGraceSubscriptionId) && gracePeriodEndsAt !== null && gracePeriodEndsAt.isValid() && gracePeriodEndsAt.isAfter(now);
|
|
1951
|
+
if (hasActivePaymentGracePeriod) {
|
|
1952
|
+
return true;
|
|
1953
|
+
}
|
|
1954
|
+
if (!stripe.subscriptionId) {
|
|
1955
|
+
return false;
|
|
1956
|
+
}
|
|
1957
|
+
if (stripe.status !== EnumSubscriptionStatus2.ACTIVE) {
|
|
1958
|
+
return false;
|
|
1959
|
+
}
|
|
1960
|
+
if (!stripe.endDate) {
|
|
1961
|
+
return false;
|
|
1962
|
+
}
|
|
1963
|
+
const paidThrough = dayjs5(stripe.endDate);
|
|
1964
|
+
return paidThrough.isValid() && paidThrough.isAfter(now);
|
|
1965
|
+
}
|
|
1966
|
+
function effectiveLicences({
|
|
1967
|
+
includeStripeProtected = false,
|
|
1968
|
+
licences,
|
|
1969
|
+
now,
|
|
1970
|
+
owner
|
|
1971
|
+
}) {
|
|
1972
|
+
const nowDayjs = dayjs5(now);
|
|
1973
|
+
return (licences ?? []).filter((licence) => {
|
|
1974
|
+
if (!licence.licenceType || licence.expiryDate == null) {
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1977
|
+
if (new Date(licence.expiryDate).getTime() > now.getTime()) {
|
|
1978
|
+
return true;
|
|
1979
|
+
}
|
|
1980
|
+
return Boolean(
|
|
1981
|
+
includeStripeProtected && owner && isProtectedByCurrentStripeSubscription(
|
|
1982
|
+
owner,
|
|
1983
|
+
licence.licenceType,
|
|
1984
|
+
nowDayjs
|
|
1985
|
+
)
|
|
1986
|
+
);
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1989
|
+
async function getActiveLicenceForResource({
|
|
1990
|
+
includeStripeProtected = false,
|
|
1991
|
+
userId,
|
|
1992
|
+
resourceType,
|
|
1993
|
+
now = /* @__PURE__ */ new Date()
|
|
1994
|
+
}) {
|
|
1995
|
+
const category = resourceLicenceCategory[resourceType];
|
|
1996
|
+
if (!category) {
|
|
1997
|
+
return null;
|
|
1998
|
+
}
|
|
1999
|
+
const owner = await UserModel.findById(userId, {
|
|
2000
|
+
licences: 1,
|
|
2001
|
+
...includeStripeProtected ? { stripe: 1 } : {}
|
|
2002
|
+
}).lean();
|
|
2003
|
+
if (!owner?.licences?.length) {
|
|
2004
|
+
return null;
|
|
2005
|
+
}
|
|
2006
|
+
return getHighestLicenceForCategory(
|
|
2007
|
+
effectiveLicences({
|
|
2008
|
+
includeStripeProtected,
|
|
2009
|
+
licences: owner.licences,
|
|
2010
|
+
now,
|
|
2011
|
+
owner
|
|
2012
|
+
}),
|
|
2013
|
+
category
|
|
2014
|
+
);
|
|
2015
|
+
}
|
|
2016
|
+
async function getUserIdsWithActiveProLicence({
|
|
2017
|
+
includeStripeProtected = false,
|
|
2018
|
+
resourceType,
|
|
2019
|
+
userIds,
|
|
2020
|
+
now = /* @__PURE__ */ new Date()
|
|
2021
|
+
}) {
|
|
2022
|
+
const category = resourceLicenceCategory[resourceType];
|
|
2023
|
+
if (!category || userIds.length === 0) {
|
|
2024
|
+
return /* @__PURE__ */ new Set();
|
|
2025
|
+
}
|
|
2026
|
+
const owners = await UserModel.find(
|
|
2027
|
+
{ _id: { $in: userIds } },
|
|
2028
|
+
{ licences: 1, ...includeStripeProtected ? { stripe: 1 } : {} }
|
|
2029
|
+
).lean();
|
|
2030
|
+
const allowed = /* @__PURE__ */ new Set();
|
|
2031
|
+
for (const owner of owners ?? []) {
|
|
2032
|
+
const activeLicence = getHighestLicenceForCategory(
|
|
2033
|
+
effectiveLicences({
|
|
2034
|
+
includeStripeProtected,
|
|
2035
|
+
licences: owner.licences,
|
|
2036
|
+
now,
|
|
2037
|
+
owner
|
|
2038
|
+
}),
|
|
2039
|
+
category
|
|
2040
|
+
);
|
|
2041
|
+
if (activeLicence && isProLicence(activeLicence)) {
|
|
2042
|
+
allowed.add(String(owner._id));
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
return allowed;
|
|
2046
|
+
}
|
|
2047
|
+
async function hasActiveProLicenceForResource({
|
|
2048
|
+
includeStripeProtected = false,
|
|
2049
|
+
userId,
|
|
2050
|
+
resourceType,
|
|
2051
|
+
now = /* @__PURE__ */ new Date()
|
|
2052
|
+
}) {
|
|
2053
|
+
const activeLicence = await getActiveLicenceForResource({
|
|
2054
|
+
includeStripeProtected,
|
|
2055
|
+
now,
|
|
2056
|
+
resourceType,
|
|
2057
|
+
userId
|
|
2058
|
+
});
|
|
2059
|
+
return activeLicence !== null && isProLicence(activeLicence);
|
|
2060
|
+
}
|
|
1628
2061
|
|
|
1629
2062
|
// src/service/license/compliance/vendor.ts
|
|
1630
2063
|
async function findUserVendor(userId, logLabel) {
|
|
@@ -1745,28 +2178,6 @@ async function restoreVendorProFeatures(userId, licenceType) {
|
|
|
1745
2178
|
throw error;
|
|
1746
2179
|
}
|
|
1747
2180
|
}
|
|
1748
|
-
function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
|
|
1749
|
-
const stripe = user.stripe;
|
|
1750
|
-
if (!stripe || stripe.currentPlan !== licenceType) {
|
|
1751
|
-
return false;
|
|
1752
|
-
}
|
|
1753
|
-
const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs6(stripe.paymentGracePeriodEndsAt) : null;
|
|
1754
|
-
const hasActivePaymentGracePeriod = (stripe.status === EnumSubscriptionStatus2.PAST_DUE || stripe.status === EnumSubscriptionStatus2.INACTIVE) && Boolean(stripe.paymentGraceSubscriptionId) && (!stripe.subscriptionId || stripe.subscriptionId === stripe.paymentGraceSubscriptionId) && gracePeriodEndsAt !== null && gracePeriodEndsAt.isValid() && gracePeriodEndsAt.isAfter(now);
|
|
1755
|
-
if (hasActivePaymentGracePeriod) {
|
|
1756
|
-
return true;
|
|
1757
|
-
}
|
|
1758
|
-
if (!stripe.subscriptionId) {
|
|
1759
|
-
return false;
|
|
1760
|
-
}
|
|
1761
|
-
if (stripe.status !== EnumSubscriptionStatus2.ACTIVE) {
|
|
1762
|
-
return false;
|
|
1763
|
-
}
|
|
1764
|
-
if (!stripe.endDate) {
|
|
1765
|
-
return false;
|
|
1766
|
-
}
|
|
1767
|
-
const paidThrough = dayjs6(stripe.endDate);
|
|
1768
|
-
return paidThrough.isValid() && paidThrough.isAfter(now);
|
|
1769
|
-
}
|
|
1770
2181
|
async function handleVendorLicenceCompliance(user, now) {
|
|
1771
2182
|
if (!user.licences || !user.vendor) {
|
|
1772
2183
|
return;
|
|
@@ -1777,10 +2188,10 @@ async function handleVendorLicenceCompliance(user, now) {
|
|
|
1777
2188
|
if (vendorLicencesOfUser.length === 0) {
|
|
1778
2189
|
return;
|
|
1779
2190
|
}
|
|
1780
|
-
const
|
|
2191
|
+
const proLicences2 = vendorLicencesOfUser.filter(
|
|
1781
2192
|
(licence) => licence.licenceType !== EnumUserLicence6.STANDARD_VENDOR
|
|
1782
2193
|
);
|
|
1783
|
-
const expiredProLicences =
|
|
2194
|
+
const expiredProLicences = proLicences2.filter(
|
|
1784
2195
|
(licence) => !dayjs6(licence.expiryDate).isAfter(now)
|
|
1785
2196
|
);
|
|
1786
2197
|
const stripeProtectedLicenceTypes = new Set(
|
|
@@ -1791,7 +2202,7 @@ async function handleVendorLicenceCompliance(user, now) {
|
|
|
1791
2202
|
const expiredProLicence = expiredProLicences.some(
|
|
1792
2203
|
(licence) => !stripeProtectedLicenceTypes.has(licence.licenceType)
|
|
1793
2204
|
);
|
|
1794
|
-
const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(
|
|
2205
|
+
const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(proLicences2, vendorLicences, now);
|
|
1795
2206
|
const hasExpiredVendorLicence = vendorLicencesOfUser.some(
|
|
1796
2207
|
(licence) => !dayjs6(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
|
|
1797
2208
|
);
|
|
@@ -1840,26 +2251,11 @@ async function vendorOwnerHasProLicence(vendorId) {
|
|
|
1840
2251
|
if (!vendor) {
|
|
1841
2252
|
return false;
|
|
1842
2253
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
return false;
|
|
1849
|
-
}
|
|
1850
|
-
const now = dayjs6();
|
|
1851
|
-
const effectiveVendorLicences = owner.licences?.filter(
|
|
1852
|
-
(licence) => licence?.licenceType && (dayjs6(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
|
|
1853
|
-
owner,
|
|
1854
|
-
licence.licenceType,
|
|
1855
|
-
now
|
|
1856
|
-
))
|
|
1857
|
-
);
|
|
1858
|
-
const highestVendorLicence = getHighestLicenceForCategory(
|
|
1859
|
-
effectiveVendorLicences,
|
|
1860
|
-
"vendor" /* VENDOR */
|
|
1861
|
-
);
|
|
1862
|
-
return highestVendorLicence !== null && licencePriority[highestVendorLicence] >= licencePriority[EnumUserLicence6.PRO_VENDOR];
|
|
2254
|
+
return hasActiveProLicenceForResource({
|
|
2255
|
+
includeStripeProtected: true,
|
|
2256
|
+
resourceType: EnumResourceType4.VENDOR,
|
|
2257
|
+
userId: vendor.owner.userId
|
|
2258
|
+
});
|
|
1863
2259
|
}
|
|
1864
2260
|
|
|
1865
2261
|
// src/service/license/compliance/utils.ts
|
|
@@ -2278,6 +2674,7 @@ export {
|
|
|
2278
2674
|
REMINDER_WINDOW_MAX_HOURS,
|
|
2279
2675
|
REMINDER_WINDOW_MIN_HOURS,
|
|
2280
2676
|
SUBSCRIPTION_REWARD_TIMEZONE,
|
|
2677
|
+
acceptedAssociateUserIds,
|
|
2281
2678
|
activeAffiliateCodeExists,
|
|
2282
2679
|
awardAffiliateVendorSubscriptionRewards,
|
|
2283
2680
|
buildLicenceExpiryPipeline,
|
|
@@ -2291,6 +2688,9 @@ export {
|
|
|
2291
2688
|
convertObjectIdsToStrings,
|
|
2292
2689
|
createLicenceObject,
|
|
2293
2690
|
dedupeArchivedSlotsByStartDateTime,
|
|
2691
|
+
detachAssociateGrantsForResources,
|
|
2692
|
+
detachStaleAssociateGrants,
|
|
2693
|
+
detachUsersFromResource,
|
|
2294
2694
|
didRemoveAnyEventDates,
|
|
2295
2695
|
didRemoveAnyStartedDates,
|
|
2296
2696
|
downgradeVendorProFeatures,
|
|
@@ -2300,6 +2700,8 @@ export {
|
|
|
2300
2700
|
eventLicences,
|
|
2301
2701
|
findAffiliateByPromoCode,
|
|
2302
2702
|
findEventOrImportedMarketById,
|
|
2703
|
+
getActiveLicenceForResource,
|
|
2704
|
+
getAssociateAccessMeta,
|
|
2303
2705
|
getEventResourceInfo,
|
|
2304
2706
|
getHighestLicenceForCategory,
|
|
2305
2707
|
getLicenceCategory,
|
|
@@ -2309,11 +2711,17 @@ export {
|
|
|
2309
2711
|
getPayingStripeSubscriptionPlan,
|
|
2310
2712
|
getPayingVendorStripeSubscriptionPlan,
|
|
2311
2713
|
getSubscriptionRewardPeriodBounds,
|
|
2714
|
+
getUserIdsWithActiveProLicence,
|
|
2312
2715
|
getVendorResourceInfo,
|
|
2716
|
+
grantAssociateGrants,
|
|
2313
2717
|
handleEventLicenceCompliance,
|
|
2314
2718
|
handlePartnerLicenceCompliance,
|
|
2315
2719
|
handleVendorLicenceCompliance,
|
|
2720
|
+
hasActiveProLicenceForResource,
|
|
2316
2721
|
hasValidLicence,
|
|
2722
|
+
isAssociableResourceType,
|
|
2723
|
+
isProLicence,
|
|
2724
|
+
isProtectedByCurrentStripeSubscription,
|
|
2317
2725
|
isStandardLicence,
|
|
2318
2726
|
isValidObjectId,
|
|
2319
2727
|
licenceCategoryMapping,
|
|
@@ -2328,6 +2736,7 @@ export {
|
|
|
2328
2736
|
processUserCompliance,
|
|
2329
2737
|
processUserLicenseReminders,
|
|
2330
2738
|
publishNotificationEvents,
|
|
2739
|
+
reconcileAssociateGrants,
|
|
2331
2740
|
remindUsersAboutLicenseExpiration,
|
|
2332
2741
|
replaceUserLicenceWithObject,
|
|
2333
2742
|
restoreVendorProFeatures,
|