forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling.ts
More file actions
299 lines (273 loc) · 7.86 KB
/
Copy pathbilling.ts
File metadata and controls
299 lines (273 loc) · 7.86 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { defineStore } from 'pinia'
import type {
EntitiesBillingUsage,
EntitiesUser,
EntitiesWebhook,
EntitiesDiscord,
EntitiesMessageSendSchedule,
EntitiesPhoneAPIKey,
RequestsWebhookStore,
RequestsWebhookUpdate,
RequestsDiscordStore,
RequestsDiscordUpdate,
RequestsMessageSendScheduleStore,
RequestsUserNotificationUpdate,
RequestsUserPaymentInvoice,
ResponsesUserSubscriptionPaymentsResponse,
} from '~~/shared/types/api'
export const useBillingStore = defineStore('billing', () => {
const billingUsage = ref<EntitiesBillingUsage | null>(null)
const billingUsageHistory = ref<EntitiesBillingUsage[]>([])
const { apiFetch } = useApi()
const notificationsStore = useNotificationsStore()
async function loadBillingUsage() {
const response = await apiFetch<{ data: EntitiesBillingUsage }>(
'/v1/billing/usage',
)
billingUsage.value = response.data
}
async function loadBillingUsageHistory() {
const response = await apiFetch<{ data: EntitiesBillingUsage[] }>(
'/v1/billing/usage-history',
)
billingUsageHistory.value = response.data
}
async function getSubscriptionUpdateLink(): Promise<string> {
const response = await apiFetch<{ data: string }>(
'/v1/users/subscription-update-url',
)
return response.data
}
async function cancelSubscription(): Promise<string> {
const response = await apiFetch<{ message: string }>(
'/v1/users/subscription',
{
method: 'DELETE',
},
)
return response.message
}
async function indexSubscriptionPayments(): Promise<ResponsesUserSubscriptionPaymentsResponse> {
const response = await apiFetch<ResponsesUserSubscriptionPaymentsResponse>(
'/v1/users/subscription/payments',
{ params: { limit: 100 } },
)
return response
}
async function generateSubscriptionPaymentInvoice(
subscriptionInvoiceId: string,
request: RequestsUserPaymentInvoice,
): Promise<void> {
const response = await apiFetch(
`/v1/users/subscription/invoices/${subscriptionInvoiceId}`,
{
method: 'POST',
body: request,
responseType: 'blob',
},
)
const pdfBlob = new Blob([response as Blob], { type: 'application/pdf' })
const url = window.URL.createObjectURL(pdfBlob)
const tempLink = document.createElement('a')
tempLink.href = url
tempLink.setAttribute('download', 'Invoice.pdf')
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
window.URL.revokeObjectURL(url)
}
// Webhooks
async function createWebhook(
payload: RequestsWebhookStore,
): Promise<EntitiesWebhook> {
const response = await apiFetch<{ data: EntitiesWebhook }>('/v1/webhooks', {
method: 'POST',
body: payload,
})
return response.data
}
async function getWebhooks(): Promise<EntitiesWebhook[]> {
const response = await apiFetch<{ data: EntitiesWebhook[] }>(
'/v1/webhooks',
{
params: { limit: 100 },
},
)
return response.data
}
async function updateWebhook(
payload: RequestsWebhookUpdate & { id: string },
): Promise<EntitiesWebhook> {
const response = await apiFetch<{ data: EntitiesWebhook }>(
`/v1/webhooks/${payload.id}`,
{
method: 'PUT',
body: payload,
},
)
return response.data
}
async function deleteWebhook(id: string): Promise<void> {
await apiFetch(`/v1/webhooks/${id}`, { method: 'DELETE' })
}
// Discord
async function createDiscord(
payload: RequestsDiscordStore,
): Promise<EntitiesDiscord> {
const response = await apiFetch<{ data: EntitiesDiscord }>(
'/v1/discord-integrations',
{
method: 'POST',
body: payload,
},
)
return response.data
}
async function getDiscordIntegrations(): Promise<EntitiesDiscord[]> {
const response = await apiFetch<{ data: EntitiesDiscord[] }>(
'/v1/discord-integrations',
{
params: { limit: 100 },
},
)
return response.data
}
async function updateDiscordIntegration(
payload: RequestsDiscordUpdate & { id: string },
): Promise<EntitiesDiscord> {
const response = await apiFetch<{ data: EntitiesDiscord }>(
`/v1/discord-integrations/${payload.id}`,
{
method: 'PUT',
body: payload,
},
)
return response.data
}
async function deleteDiscordIntegration(id: string): Promise<void> {
await apiFetch(`/v1/discord-integrations/${id}`, { method: 'DELETE' })
}
// Send Schedules
async function getSendSchedules(): Promise<EntitiesMessageSendSchedule[]> {
const response = await apiFetch<{ data: EntitiesMessageSendSchedule[] }>(
'/v1/send-schedules',
)
return response.data
}
async function createSendSchedule(
payload: RequestsMessageSendScheduleStore,
): Promise<EntitiesMessageSendSchedule> {
const response = await apiFetch<{ data: EntitiesMessageSendSchedule }>(
'/v1/send-schedules',
{
method: 'POST',
body: payload,
},
)
return response.data
}
async function updateSendSchedule(
payload: RequestsMessageSendScheduleStore & { id: string },
): Promise<EntitiesMessageSendSchedule> {
const response = await apiFetch<{ data: EntitiesMessageSendSchedule }>(
`/v1/send-schedules/${payload.id}`,
{
method: 'PUT',
body: payload,
},
)
return response.data
}
async function deleteSendSchedule(id: string): Promise<void> {
await apiFetch(`/v1/send-schedules/${id}`, { method: 'DELETE' })
}
// Phone API Keys
async function storePhoneApiKey(name: string): Promise<EntitiesPhoneAPIKey> {
const response = await apiFetch<{
data: EntitiesPhoneAPIKey
message: string
}>('/v1/phone-api-keys', {
method: 'POST',
body: { name },
})
notificationsStore.addNotification({
message: response.message,
type: 'success',
})
return response.data
}
async function indexPhoneApiKeys(): Promise<EntitiesPhoneAPIKey[]> {
const response = await apiFetch<{ data: EntitiesPhoneAPIKey[] }>(
'/v1/phone-api-keys',
{
params: { limit: 100 },
},
)
return response.data
}
async function deletePhoneApiKey(id: string): Promise<void> {
const response = await apiFetch<{ message: string }>(
`/v1/phone-api-keys/${id}`,
{ method: 'DELETE' },
)
notificationsStore.addNotification({
message: response.message,
type: 'success',
})
}
async function deletePhoneFromPhoneApiKey(
phoneApiKeyId: string,
phoneId: string,
): Promise<void> {
const response = await apiFetch<{ message: string }>(
`/v1/phone-api-keys/${phoneApiKeyId}/phones/${phoneId}`,
{ method: 'DELETE' },
)
notificationsStore.addNotification({
message: response.message,
type: 'success',
})
}
// Email notifications
async function saveEmailNotifications(
userId: string,
payload: RequestsUserNotificationUpdate,
): Promise<void> {
const authStore = useAuthStore()
const response = await apiFetch<{ data: EntitiesUser }>(
`/v1/users/${userId}/notifications`,
{
method: 'PUT',
body: payload,
},
)
authStore.user = response.data
}
return {
billingUsage,
billingUsageHistory,
loadBillingUsage,
loadBillingUsageHistory,
getSubscriptionUpdateLink,
cancelSubscription,
indexSubscriptionPayments,
generateSubscriptionPaymentInvoice,
createWebhook,
getWebhooks,
updateWebhook,
deleteWebhook,
createDiscord,
getDiscordIntegrations,
updateDiscordIntegration,
deleteDiscordIntegration,
getSendSchedules,
createSendSchedule,
updateSendSchedule,
deleteSendSchedule,
storePhoneApiKey,
indexPhoneApiKeys,
deletePhoneApiKey,
deletePhoneFromPhoneApiKey,
saveEmailNotifications,
}
})