-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy path24-03-25_00-40-migrate-purchases-to-invoices copy.js
More file actions
172 lines (154 loc) · 5.24 KB
/
24-03-25_00-40-migrate-purchases-to-invoices copy.js
File metadata and controls
172 lines (154 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import mongoose from "mongoose";
import { nanoid } from "nanoid";
function generateUniqueId() {
return nanoid();
}
mongoose.connect(process.env.DB_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const DomainSchema = new mongoose.Schema({
name: { type: String, required: true, unique: true },
});
const Domain = mongoose.model("Domain", DomainSchema);
const PaymentPlanSchema = new mongoose.Schema(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
planId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
name: { type: String, required: true },
type: {
type: String,
required: true,
enum: ["free", "onetime"],
},
oneTimeAmount: { type: Number },
userId: { type: String, required: true },
archived: { type: Boolean, default: false },
internal: { type: Boolean, default: false },
},
{
timestamps: true,
},
);
const PaymentPlan = mongoose.model("PaymentPlan", PaymentPlanSchema);
export const UserSchema = new mongoose.Schema({
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
userId: { type: String, required: true },
});
const User = mongoose.model("User", UserSchema);
const MembershipSchema = new mongoose.Schema(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
membershipId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
userId: { type: String, required: true },
paymentPlanId: String,
entityId: { type: String, required: true },
entityType: {
type: String,
enum: ["course", "community"],
required: true,
},
sessionId: { type: String, required: true, default: generateUniqueId },
status: {
type: String,
enum: ["active", "pending"],
default: "pending",
},
},
{
timestamps: true,
},
);
const Membership = mongoose.model("Membership", MembershipSchema);
const PurchaseSchema = new mongoose.Schema({
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
orderId: { type: String, required: true, default: generateUniqueId },
courseId: { type: String, required: true },
purchasedOn: { type: Date, required: true, default: () => new Date() },
purchasedBy: { type: String, required: true },
paymentMethod: { type: String, required: true },
paymentId: { type: String },
amount: { type: Number, required: true },
currencyISOCode: { type: String, required: true },
discount: { type: Number },
status: { type: String, required: true, default: "initiated" },
remark: { type: String },
});
const Purchase = mongoose.model("Purchase", PurchaseSchema);
const InvoiceSchema = new mongoose.Schema(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
invoiceId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
membershipId: { type: String, required: true },
membershipSessionId: { type: String, required: true },
amount: { type: Number, required: true },
status: {
type: String,
enum: ["pending", "paid", "failed"],
default: "pending",
},
paymentProcessor: { type: String, required: true },
paymentProcessorEntityId: { type: String },
paymentProcessorTransactionId: { type: String },
currencyISOCode: { type: String, required: true },
},
{
timestamps: true,
},
);
const Invoice = mongoose.model("Invoice", InvoiceSchema);
const migratePurchasesToInvoices = async () => {
const purchases = await Purchase.find({});
for (const purchase of purchases) {
const membership = await Membership.findOne({
domain: purchase.domain,
userId: purchase.purchasedBy,
entityId: purchase.courseId,
entityType: "course",
});
if (membership) {
const existingInvoice = await Invoice.findOne({
domain: purchase.domain,
membershipId: membership.membershipId,
invoiceId: purchase.orderId,
});
if (existingInvoice) {
console.log(
"Invoice already exists",
existingInvoice.invoiceId,
);
continue;
}
const invoicePayload = {
domain: purchase.domain,
invoiceId: purchase.orderId,
membershipId: membership.membershipId,
membershipSessionId: membership.sessionId,
amount: purchase.amount,
paymentProcessor: purchase.paymentMethod,
paymentProcessorEntityId: purchase.paymentId,
currencyISOCode: purchase.currencyISOCode,
};
await Invoice.create(invoicePayload);
}
}
};
(async () => {
await migratePurchasesToInvoices();
mongoose.connection.close();
})();