forked from djyde/cusdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.service.ts
172 lines (145 loc) · 3.49 KB
/
subscription.service.ts
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 { UsageLabel, usageLimitation } from "../config.common"
import { prisma, resolvedConfig } from "../utils.server"
export class SubscriptionService {
async update(body) {
const {
order_id,
product_id,
variant_id,
customer_id,
status,
ends_at,
urls: {
update_payment_method
}
} = body.data.attributes
const lemonSubscriptionId = body.data.id
const {
user_id
} = body.meta.custom_data
await prisma.subscription.upsert({
where: {
userId: user_id
},
create: {
userId: user_id,
orderId: `${order_id}`,
productId: `${product_id}`,
variantId: `${variant_id}`,
customerId: `${customer_id}`,
endsAt: ends_at,
lemonSubscriptionId,
status,
updatePaymentMethodUrl: update_payment_method
},
update: {
userId: user_id,
orderId: `${order_id}`,
productId: `${product_id}`,
variantId: `${variant_id}`,
customerId: `${customer_id}`,
lemonSubscriptionId,
endsAt: ends_at,
status,
updatePaymentMethodUrl: update_payment_method
}
})
}
async isActivated(userId: string) {
const subscription = await prisma.subscription.findUnique({
where: {
userId
},
})
if (!subscription) {
return false
}
let isActived = subscription?.status === 'active' || subscription?.status === 'cancelled'
return isActived
}
async getStatus(userId: string) {
const subscription = await prisma.subscription.findUnique({
where: {
userId
},
})
return {
isActived: await this.isActivated(userId),
status: subscription?.status || '',
endAt: subscription?.endsAt?.toISOString() || '',
updatePaymentMethodUrl: subscription?.updatePaymentMethodUrl || ''
}
}
async createProjectValidate(userId: string) {
if (!resolvedConfig.checkout.enabled) {
return true
}
const [projectCount] = await prisma.$transaction([
prisma.project.count({
where: {
ownerId: userId,
deletedAt: {
equals: null
}
}
}),
])
if (projectCount < usageLimitation['create_site']) {
return true
}
if (await this.isActivated(userId)) {
return true
}
return false
}
async approveCommentValidate(userId: string) {
if (!resolvedConfig.checkout.enabled) {
return true
}
const [usage] = await prisma.$transaction([
prisma.usage.findUnique({
where: {
userId_label: {
userId,
label: UsageLabel.ApproveComment
}
}
}),
])
if (await this.isActivated(userId)) {
return true
}
if (!usage) {
return true
}
if (usage.count <= usageLimitation[UsageLabel.ApproveComment]) {
return true
}
return false
}
async quickApproveValidate(userId: string) {
if (!resolvedConfig.checkout.enabled) {
return true
}
const [usage] = await prisma.$transaction([
prisma.usage.findUnique({
where: {
userId_label: {
userId,
label: UsageLabel.QuickApprove
}
}
}),
])
// if (await this.isActivated(userId)) {
// return true
// }
if (!usage) {
return true
}
if (usage.count <= usageLimitation[UsageLabel.QuickApprove]) {
return true
}
return false
}
}