-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
93 lines (88 loc) · 2.29 KB
/
Copy pathplugin.js
File metadata and controls
93 lines (88 loc) · 2.29 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
/**
* We.js project plugin file, use to load routes and configs
*
* @param {String} projectPath current project path
* @param {Object} Plugin we.js Plugin class
* @return {Object} intance of we.js Plugin class
*/
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
/**
* Get user subscription
*
* @param {Number} userId
* @param {Number} newsletterId
* @return {Promisse} sequelize find promisse
*/
plugin.getSubscription = function getSubscription(userId, newsletterId) {
return plugin.we.db.models.newsletterSubscribers
.findOne({
where: {
newsletterId: newsletterId,
userId: userId
},
include: [{ all: true }]
});
}
/**
* check if user is Subscribed
*
* @param {Number} userId
* @param {Number} nesletterId
* @return {Promisse} sequelize find promisse
*/
plugin.isSubscribed = function isSubscribed(userId, nesletterId) {
return plugin.getSubscription(userId, nesletterId);
}
/**
* Get all user subscriptions
*
* @param {Number} userId
* @return {Promisse} Sequelize find promisse
*/
plugin.getAllSubscriptions = function getAllSubscriptions(userId) {
if (!userId) throw new Error('getAllSubscriptions.userId.required');
return plugin.we.db.models.newsletterSubscribers
.findAll({
where: {
userId: userId
},
limit: 100,
include: [{ all: true }]
});
}
/**
* subscribe one user in one newsletter
*
* @param {Number} userId
* @param {Number} newsletterId
* @return {Promisse} sequelize find promisse
*/
plugin.subscribe = function subscribe(userId, newsletterId) {
return plugin.we.db.models.newsletter
.findOne({
where: { id: newsletterId }
})
.then( (news)=> {
// return null;
if (!news) {
return null;
}
return news.hasSubscriber(userId)
.then( (have)=> {
if (have) {
return null;
} else {
return news.addSubscriber(userId)
.then( ()=> {
return null;
});
}
});
})
.then( ()=> {
return plugin.getSubscription(userId, newsletterId);
});
}
return plugin;
};