Skip to content

Commit a9dbbe0

Browse files
committed
migration for payment plans
1 parent ea78b78 commit a9dbbe0

4 files changed

Lines changed: 142 additions & 20 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import mongoose from "mongoose";
2+
import { nanoid } from "nanoid";
3+
4+
function generateUniqueId() {
5+
return nanoid();
6+
}
7+
8+
mongoose.connect(process.env.DB_CONNECTION_STRING, {
9+
useNewUrlParser: true,
10+
useUnifiedTopology: true,
11+
});
12+
13+
const PaymentPlanSchema = new mongoose.Schema(
14+
{
15+
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
16+
planId: {
17+
type: String,
18+
required: true,
19+
unique: true,
20+
default: generateUniqueId,
21+
},
22+
entityId: { type: String, required: true },
23+
internal: { type: Boolean, default: false },
24+
},
25+
{
26+
timestamps: true,
27+
},
28+
);
29+
const PaymentPlan = mongoose.model("PaymentPlan", PaymentPlanSchema);
30+
31+
export const MembershipSchema = new mongoose.Schema(
32+
{
33+
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
34+
membershipId: {
35+
type: String,
36+
required: true,
37+
unique: true,
38+
default: generateUniqueId,
39+
},
40+
paymentPlanId: { type: String, required: true },
41+
joiningReason: { type: String },
42+
},
43+
{
44+
timestamps: true,
45+
},
46+
);
47+
const Membership = mongoose.model("Membership", MembershipSchema);
48+
49+
const migrateInternalPaymentPlans = async () => {
50+
console.log("🏁 Migrating internal payment plans");
51+
const paymentPlans = await PaymentPlan.find({
52+
internal: true,
53+
});
54+
for (const paymentPlan of paymentPlans) {
55+
if (paymentPlan.entityId) continue;
56+
57+
paymentPlan.entityId = "internal";
58+
await paymentPlan.save();
59+
console.log(`Updated payment plan ${paymentPlan.planId}`);
60+
}
61+
console.log("✅ Migrating internal payment plans completed\n");
62+
};
63+
64+
const migrateMembershipOfCommunityCreators = async () => {
65+
console.log("🏁 Migrating membership of community creators");
66+
const paymentPlans = await PaymentPlan.find({
67+
internal: true,
68+
});
69+
for (const paymentPlan of paymentPlans) {
70+
const memberships = await Membership.find({
71+
domain: paymentPlan.domain,
72+
joiningReason: "Joined as creator",
73+
});
74+
for (const membership of memberships) {
75+
if (membership.paymentPlanId) continue;
76+
77+
membership.paymentPlanId = paymentPlan.planId;
78+
await membership.save();
79+
console.log(
80+
`Updated membership ${membership.membershipId} with internal payment plan ${paymentPlan.planId}`,
81+
);
82+
}
83+
}
84+
console.log("✅ Migrating membership of community creators completed\n");
85+
};
86+
87+
const migratePaymentPlansWithNoInternalProperty = async () => {
88+
console.log("🏁 Migrating payment plans with no internal property");
89+
const paymentPlans = await PaymentPlan.find({
90+
internal: { $exists: false },
91+
});
92+
for (const paymentPlan of paymentPlans) {
93+
paymentPlan.internal = false;
94+
await paymentPlan.save();
95+
console.log(`Updated payment plan ${paymentPlan.planId}`);
96+
}
97+
console.log(
98+
"✅ Migrating payment plans with no internal property completed\n",
99+
);
100+
};
101+
102+
(async () => {
103+
await migrateInternalPaymentPlans();
104+
await migrateMembershipOfCommunityCreators();
105+
await migratePaymentPlansWithNoInternalProperty();
106+
mongoose.connection.close();
107+
})();

apps/web/app/(with-contexts)/dashboard/(sidebar)/community/[id]/manage/page.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default function Page({
116116
const [deleteConfirmation, setDeleteConfirmation] = useState("");
117117
const [isDeleting, setIsDeleting] = useState(false);
118118
const router = useRouter();
119-
const fetch = useGraphQLFetch()
119+
const fetch = useGraphQLFetch();
120120

121121
useEffect(() => {
122122
if (communityLoaded && community) {
@@ -368,7 +368,7 @@ export default function Page({
368368
}
369369
`;
370370
try {
371-
const fetchRequest = fetch
371+
const fetchRequest = fetch
372372
.setPayload({
373373
query,
374374
variables: {
@@ -446,22 +446,22 @@ export default function Page({
446446
// const onPlanSubmitted = async (plan: PaymentPlan) => {
447447
// const query = `
448448
// mutation CreatePlan(
449-
// $name: String!,
450-
// $type: PaymentPlanType!,
449+
// $name: String!,
450+
// $type: PaymentPlanType!,
451451
// $entityId: String!,
452452
// $entityType: MembershipEntityType!
453-
// $oneTimeAmount: Int,
453+
// $oneTimeAmount: Int,
454454
// $emiAmount: Int,
455455
// $emiTotalInstallments: Int,
456456
// $subscriptionMonthlyAmount: Int,
457457
// $subscriptionYearlyAmount: Int,
458458
// ) {
459459
// plan: createPlan(
460-
// name: $name,
461-
// type: $type,
460+
// name: $name,
461+
// type: $type,
462462
// entityId: $entityId,
463463
// entityType: $entityType,
464-
// oneTimeAmount: $oneTimeAmount,
464+
// oneTimeAmount: $oneTimeAmount,
465465
// emiAmount: $emiAmount,
466466
// emiTotalInstallments: $emiTotalInstallments,
467467
// subscriptionMonthlyAmount: $subscriptionMonthlyAmount,
@@ -798,7 +798,12 @@ export default function Page({
798798
<h3 className="text-lg font-semibold text-destructive">
799799
{DANGER_ZONE_HEADER}
800800
</h3>
801-
<AlertDialog onOpenChange={(open) => !open && (setDeleteConfirmation(""), setIsDeleting(false))}>
801+
<AlertDialog
802+
onOpenChange={(open) =>
803+
!open &&
804+
(setDeleteConfirmation(""), setIsDeleting(false))
805+
}
806+
>
802807
<AlertDialogTrigger asChild>
803808
<Button variant="destructive">Delete Community</Button>
804809
</AlertDialogTrigger>
@@ -813,23 +818,31 @@ export default function Page({
813818
</AlertDialogDescription>
814819
</AlertDialogHeader>
815820
<div className="py-4">
816-
<Label htmlFor="delete-confirmation" className="text-sm font-medium">
821+
<Label
822+
htmlFor="delete-confirmation"
823+
className="text-sm font-medium"
824+
>
817825
Type &quot;delete&quot; to confirm
818826
</Label>
819827
<Input
820828
id="delete-confirmation"
821829
type="text"
822830
placeholder="Type 'delete' to confirm"
823831
value={deleteConfirmation}
824-
onChange={(e) => setDeleteConfirmation(e.target.value)}
832+
onChange={(e) =>
833+
setDeleteConfirmation(e.target.value)
834+
}
825835
className="mt-2"
826836
/>
827837
</div>
828838
<AlertDialogFooter>
829839
<AlertDialogCancel>Cancel</AlertDialogCancel>
830-
<AlertDialogAction
840+
<AlertDialogAction
831841
onClick={handleDeleteConfirm}
832-
disabled={deleteConfirmation !== "delete" || isDeleting}
842+
disabled={
843+
deleteConfirmation !== "delete" ||
844+
isDeleting
845+
}
833846
className="bg-destructive text-destructive-foreground hover:bg-destructive/90 disabled:opacity-50 disabled:cursor-not-allowed"
834847
>
835848
{isDeleting ? (

apps/web/app/(with-contexts)/dashboard/(sidebar)/paymentplan/[type]/[id]/edit/[planid]/page.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export default function EditPaymentPlanPage() {
2727
const params = useParams();
2828
const router = useRouter();
2929
const type = params?.type as "community" | "product";
30-
const entityType = type === "community"
31-
? membershipEntityType.COMMUNITY
32-
: membershipEntityType.COURSE;
30+
const entityType =
31+
type === "community"
32+
? membershipEntityType.COMMUNITY
33+
: membershipEntityType.COURSE;
3334
const entityId = params?.id as string;
3435
const planId = params?.planid as string;
3536
const { product, community } = useEntityValidation(entityType, entityId);
@@ -101,7 +102,7 @@ export default function EditPaymentPlanPage() {
101102
? {
102103
planId: paymentPlan.planId,
103104
name: paymentPlan.name,
104-
description: paymentPlan.description,
105+
description: paymentPlan.description || "",
105106
type: paymentPlan.type,
106107
oneTimeAmount: paymentPlan.oneTimeAmount,
107108
emiAmount: paymentPlan.emiAmount,

apps/web/app/(with-contexts)/dashboard/(sidebar)/paymentplan/[type]/[id]/new/page.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const { MembershipEntityType: membershipEntityType } = Constants;
1717
export default function NewPaymentPlanPage() {
1818
const params = useParams();
1919
const type = params?.type as "community" | "product";
20-
const entityType = type === "community"
21-
? membershipEntityType.COMMUNITY
22-
: membershipEntityType.COURSE;
20+
const entityType =
21+
type === "community"
22+
? membershipEntityType.COMMUNITY
23+
: membershipEntityType.COURSE;
2324
const entityId = params?.id as string;
2425
const { product, community } = useEntityValidation(entityType, entityId);
2526

0 commit comments

Comments
 (0)