-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
50 lines (47 loc) · 1.74 KB
/
index.js
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
const { Connection } = require("mongoose");
const { getConnectionInstance } = require("./helpers/common");
const { OrmType } = require("./helpers/constants");
const Plan = require("./models/entities/plan");
const Subscription = require("./models/entities/subscription");
function setupBilling(config) {
if(!config) {
throw Error("Missing config for setup");
}
if (!config.extension_id) {
throw Error("`extension_id` is required");
}
if (!config.db_connection) {
throw Error("`db_connection` is required");
}
if (!config.collection_name) {
throw Error("`collection_name` is required");
}
if (!config.collection_name.plan) {
throw Error("`collection_name.plan` is required");
}
if (!config.collection_name.subscription) {
throw Error("`collection_name.subscription` is required");
}
if (!Object.values(OrmType).includes(config.orm_type)) {
throw Error(`\`orm_type\` value is invalid. Allowed values are: ${Object.values(OrmType).join(", ")}`);
}
if(!(config.db_connection instanceof getConnectionInstance(config.orm_type))) {
throw Error("`db_connection` object type is invalid for orm");
}
const models = require("./models")(config.db_connection, config.collection_name, config.orm_type);
const { getActivePlans, subscribePlan, getActiveSubscription, updateSubscriptionStatus } = require("./controllers/subscription.helper")(config, models);
return {
planModel: models.planModel,
subscriptionModel: models.subscriptionModel,
getActivePlans,
subscribePlan,
getActiveSubscription,
updateSubscriptionStatus
}
}
module.exports = {
setupBilling,
OrmType,
Plan,
Subscription
}