Skip to content

Commit 277dfd1

Browse files
author
Rajat Saxena
committed
WIP: Untested but fully developed code; incomplete doc added
1 parent 3b7b873 commit 277dfd1

17 files changed

Lines changed: 824 additions & 814 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Grant Access to Additional Products for Members
3+
description: Guide to grant access to additional products for community members
4+
layout: ../../../layouts/MainLayout.astro
5+
---
6+
7+
## Steps to Grant Access to Additional Products
8+
9+
1. Navigate to the **Memberships** section in your community dashboard.
10+
2. Click on the member's name/email you want to manage.
11+
3. You will be taken to user's edit screen.
12+
13+
## FAQ
14+
15+
1. When a members gets access to additional products, are these enrollments be tracked?
16+
Yes, these enrollments are tracked and will show up in the product's enrollment metric. However, they will not count towards the product's sales metrics.
17+
18+
2. Will the included product's email sequences be triggered?
19+
Yes, the included product's email sequences will be triggered.
20+
21+
![User edit screen](/assets/communities/member-permissions.png)

apps/web/app/api/payment/helpers.ts

Lines changed: 18 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ import {
44
Constants,
55
Membership,
66
PaymentPlan,
7-
Progress,
8-
Event,
97
} from "@courselit/common-models";
10-
import { User } from "@courselit/common-models";
11-
import { triggerSequences } from "@/lib/trigger-sequences";
12-
import { recordActivity } from "@/lib/record-activity";
13-
import constants from "@config/constants";
14-
import CourseModel, { InternalCourse } from "@models/Course";
15-
import { getPlanPrice } from "@ui-lib/utils";
16-
import UserModel from "@models/User";
178
import CommunityModel from "@models/Community";
189
import mongoose from "mongoose";
10+
import { addIncludedProductsMemberships } from "@/graphql/paymentplans/logic";
11+
import { runPostMembershipTasks } from "@/graphql/users/logic";
1912

2013
export async function activateMembership(
2114
domain: Domain & { _id: mongoose.Types.ObjectId },
@@ -46,97 +39,30 @@ export async function activateMembership(
4639
membership.status = Constants.MembershipStatus.ACTIVE;
4740
membership.role = Constants.MembershipRole.POST;
4841
}
42+
if (
43+
membership.status === Constants.MembershipStatus.ACTIVE &&
44+
paymentPlan &&
45+
paymentPlan.includedProducts &&
46+
paymentPlan.includedProducts.length > 0
47+
) {
48+
await addIncludedProductsMemberships({
49+
domain: domain._id,
50+
userId: membership.userId,
51+
paymentPlan,
52+
sessionId: membership.sessionId,
53+
});
54+
}
4955
} else {
5056
membership.status = Constants.MembershipStatus.ACTIVE;
5157
}
5258

5359
await (membership as any).save();
5460

5561
if (paymentPlan) {
56-
await finalizePurchase({ domain, membership, paymentPlan });
57-
}
58-
}
59-
60-
export async function finalizePurchase({
61-
domain,
62-
membership,
63-
paymentPlan,
64-
}: {
65-
domain: Domain & { _id: mongoose.Types.ObjectId };
66-
membership: Membership;
67-
paymentPlan: PaymentPlan;
68-
}) {
69-
const user = await UserModel.findOne<User>({ userId: membership.userId });
70-
if (!user) {
71-
return;
72-
}
73-
74-
let event: Event | undefined = undefined;
75-
if (paymentPlan.type !== Constants.PaymentPlanType.FREE) {
76-
await recordActivity({
77-
domain: domain._id,
78-
userId: user.userId,
79-
type: constants.activityTypes[1],
80-
entityId: membership.entityId,
81-
metadata: {
82-
cost: getPlanPrice(paymentPlan).amount,
83-
purchaseId: membership.sessionId,
84-
},
85-
});
86-
}
87-
if (membership.entityType === Constants.MembershipEntityType.COMMUNITY) {
88-
await recordActivity({
89-
domain: domain._id,
90-
userId: user.userId,
91-
type: constants.activityTypes[15],
92-
entityId: membership.entityId,
93-
});
94-
95-
event = Constants.EventType.COMMUNITY_JOINED;
96-
}
97-
if (membership.entityType === Constants.MembershipEntityType.COURSE) {
98-
const product = await CourseModel.findOne<InternalCourse>({
99-
courseId: membership.entityId,
100-
});
101-
if (product) {
102-
await addProductToUser({
103-
user,
104-
product,
105-
// cost: getPlanPrice(paymentPlan).amount,
106-
});
107-
}
108-
await recordActivity({
62+
await runPostMembershipTasks({
10963
domain: domain._id,
110-
userId: user.userId,
111-
type: constants.activityTypes[0],
112-
entityId: membership.entityId,
113-
});
114-
115-
event = Constants.EventType.PRODUCT_PURCHASED;
116-
}
117-
118-
if (event) {
119-
await triggerSequences({ user, event, data: membership.entityId });
120-
}
121-
}
122-
123-
async function addProductToUser({
124-
user,
125-
product,
126-
}: {
127-
user: User;
128-
product: InternalCourse;
129-
}) {
130-
if (
131-
!user.purchases.some(
132-
(purchase: Progress) => purchase.courseId === product.courseId,
133-
)
134-
) {
135-
user.purchases.push({
136-
courseId: product.courseId,
137-
completedLessons: [],
138-
accessibleGroups: [],
64+
membership,
65+
paymentPlan,
13966
});
140-
await (user as any).save();
14167
}
14268
}

0 commit comments

Comments
 (0)