@winible/winible-typed 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/migrations/20230412190423-migrate_purchased_contents.js +10 -0
- package/dist/migrations/20230412190423-migrate_purchased_contents.js.map +1 -0
- package/dist/migrations/20230412202031-migrate_other_fields.js +27 -0
- package/dist/migrations/20230412202031-migrate_other_fields.js.map +1 -0
- package/dist/migrations/20230412213952-deprecate_phone_users_and_phone_owners.js +27 -0
- package/dist/migrations/20230412213952-deprecate_phone_users_and_phone_owners.js.map +1 -0
- package/dist/scripts/deleteMalformedImages.js +2 -2
- package/dist/scripts/deleteMalformedImages.js.map +1 -1
- package/dist/typed-model/content-like.js +27 -15
- package/dist/typed-model/content-like.js.map +1 -1
- package/dist/typed-model/credit-payment.js +35 -371
- package/dist/typed-model/credit-payment.js.map +1 -1
- package/dist/typed-model/index.js +13 -15
- package/dist/typed-model/index.js.map +1 -1
- package/dist/typed-model/league-user.js +3 -3
- package/dist/typed-model/league-user.js.map +1 -1
- package/dist/typed-model/media.js +2 -6
- package/dist/typed-model/media.js.map +1 -1
- package/dist/typed-model/pb-sequelize.js +3 -1
- package/dist/typed-model/pb-sequelize.js.map +1 -1
- package/dist/typed-model/phone-owner.js +3 -3
- package/dist/typed-model/phone-owner.js.map +1 -1
- package/dist/typed-model/post.js +126 -0
- package/dist/typed-model/post.js.map +1 -0
- package/dist/typed-model/posts-on-users.js +92 -0
- package/dist/typed-model/posts-on-users.js.map +1 -0
- package/dist/typed-model/premium-content.js +9 -4
- package/dist/typed-model/premium-content.js.map +1 -1
- package/dist/typed-model/purchased-content.js +13 -13
- package/dist/typed-model/purchased-content.js.map +1 -1
- package/dist/typed-model/recurly-account.js.map +1 -1
- package/dist/typed-model/subscription-plan-on-post.js +55 -0
- package/dist/typed-model/subscription-plan-on-post.js.map +1 -0
- package/dist/typed-model/subscription-plan-on-users.js +111 -0
- package/dist/typed-model/subscription-plan-on-users.js.map +1 -0
- package/dist/typed-model/subscription-plan.js +22 -32
- package/dist/typed-model/subscription-plan.js.map +1 -1
- package/dist/typed-model/subscription.js +32 -40
- package/dist/typed-model/subscription.js.map +1 -1
- package/dist/typed-model/transaction.js +108 -0
- package/dist/typed-model/transaction.js.map +1 -0
- package/dist/typed-model/withdrawal.js +33 -0
- package/dist/typed-model/withdrawal.js.map +1 -0
- package/dist/webhooks/index.js.map +1 -1
- package/dist/webhooks/payment/refund.js +11 -11
- package/dist/webhooks/payment/refund.js.map +1 -1
- package/package.json +1 -1
- package/typed-model/content-like.ts +33 -17
- package/typed-model/index.ts +6 -7
- package/typed-model/league-user.ts +5 -3
- package/typed-model/media.ts +3 -9
- package/typed-model/pb-sequelize.ts +5 -1
- package/typed-model/{premium-content.ts → post.ts} +44 -42
- package/typed-model/{purchased-content.ts → posts-on-users.ts} +46 -25
- package/typed-model/recurly-account.ts +0 -1
- package/typed-model/subscription-plan-on-post.ts +77 -0
- package/typed-model/{subscription.ts → subscription-plan-on-users.ts} +43 -59
- package/typed-model/subscription-plan.ts +29 -46
- package/typed-model/transaction.ts +154 -0
- package/typed-model/user.ts +2 -2
- package/typed-model/{credit-payout.ts → withdrawal.ts} +9 -21
- package/typed-model/credit-payment.ts +0 -681
- package/typed-model/phone-owner.ts +0 -341
- package/typed-model/phone-user.ts +0 -146
@@ -1,681 +0,0 @@
|
|
1
|
-
import { Transaction } from "sequelize";
|
2
|
-
import {
|
3
|
-
Model,
|
4
|
-
InferAttributes,
|
5
|
-
InferCreationAttributes,
|
6
|
-
CreationOptional,
|
7
|
-
DataTypes,
|
8
|
-
fn,
|
9
|
-
NOW,
|
10
|
-
} from "sequelize";
|
11
|
-
|
12
|
-
import sequelize from "./pb-sequelize";
|
13
|
-
import PhoneOwner from "./phone-owner";
|
14
|
-
import PhoneUser from "./phone-user";
|
15
|
-
import PremiumContent from "./premium-content";
|
16
|
-
import Subscription from "./subscription";
|
17
|
-
import PurchasedContent from "./purchased-content";
|
18
|
-
import Notification, { NotificationType } from "./notification";
|
19
|
-
|
20
|
-
type InteractionType =
|
21
|
-
| "subscription"
|
22
|
-
| "tip"
|
23
|
-
| "text"
|
24
|
-
| "unlock_message"
|
25
|
-
| "content"
|
26
|
-
| "unlock"
|
27
|
-
| "audio"
|
28
|
-
| "referral"
|
29
|
-
| "video";
|
30
|
-
|
31
|
-
const INTERACTION_TYPES: { [key: string]: InteractionType } = {
|
32
|
-
CONTENT: "content",
|
33
|
-
AUDIO: "audio",
|
34
|
-
TEXT: "text",
|
35
|
-
VIDEO: "video",
|
36
|
-
TIP: "tip",
|
37
|
-
UNLOCK: "unlock",
|
38
|
-
UNLOCK_MESSAGE: "unlock_message",
|
39
|
-
REFERRAL: "referral",
|
40
|
-
SUBSCRIPTION: "subscription",
|
41
|
-
};
|
42
|
-
|
43
|
-
// order of InferAttributes & InferCreationAttributes is important.
|
44
|
-
class CreditPayments extends Model<
|
45
|
-
InferAttributes<CreditPayments>,
|
46
|
-
InferCreationAttributes<CreditPayments>
|
47
|
-
> {
|
48
|
-
// 'CreationOptional' is a special type that marks the field as optional
|
49
|
-
// when creating an instance of the model (such as using Model.create()).
|
50
|
-
declare id: CreationOptional<string>;
|
51
|
-
declare userId?: string;
|
52
|
-
declare ownerId: string;
|
53
|
-
declare creditAmountInCents: number;
|
54
|
-
declare interactionType: InteractionType;
|
55
|
-
declare payoutDate?: Date;
|
56
|
-
declare metadata?: string;
|
57
|
-
declare earningAmountInCents: number;
|
58
|
-
declare payoutId?: string;
|
59
|
-
declare referredCreditPaymentId?: string;
|
60
|
-
declare transId?: number;
|
61
|
-
declare invoiceId?: string;
|
62
|
-
declare createdAt: CreationOptional<Date>;
|
63
|
-
declare updatedAt: CreationOptional<Date>;
|
64
|
-
static getById: (id: string) => Promise<CreditPayments | null>;
|
65
|
-
static logContentPurchase: (
|
66
|
-
user: PhoneUser,
|
67
|
-
contentId: string,
|
68
|
-
owner: PhoneOwner,
|
69
|
-
alreadyPaid?: boolean,
|
70
|
-
transaction?: Transaction,
|
71
|
-
invoiceId?: string
|
72
|
-
) => Promise<CreditPayments>;
|
73
|
-
static logMessage: (
|
74
|
-
user: PhoneUser,
|
75
|
-
owner: PhoneOwner,
|
76
|
-
chatRateInCents: number
|
77
|
-
) => Promise<CreditPayments>;
|
78
|
-
static logTip: (
|
79
|
-
user: PhoneUser,
|
80
|
-
owner: PhoneOwner,
|
81
|
-
tipAmountInCents: number
|
82
|
-
) => Promise<CreditPayments>;
|
83
|
-
static logAudioCall: (
|
84
|
-
user: PhoneUser,
|
85
|
-
owner: PhoneOwner,
|
86
|
-
callAmountInCents: number
|
87
|
-
) => Promise<CreditPayments>;
|
88
|
-
static logVideoCall: (
|
89
|
-
user: PhoneUser,
|
90
|
-
owner: PhoneOwner,
|
91
|
-
callAmountInCents: number
|
92
|
-
) => Promise<CreditPayments>;
|
93
|
-
static logUnlockMessage: (
|
94
|
-
user: PhoneUser,
|
95
|
-
owner: PhoneOwner,
|
96
|
-
alreadyPaid?: boolean,
|
97
|
-
transaction?: Transaction,
|
98
|
-
invoiceId?: string
|
99
|
-
) => Promise<CreditPayments>;
|
100
|
-
static logSubscriptionPayment: (
|
101
|
-
userId: string,
|
102
|
-
owner: PhoneOwner,
|
103
|
-
subscriptionAmountInCents: number,
|
104
|
-
subscription: Subscription,
|
105
|
-
transaction: Transaction | undefined,
|
106
|
-
invoiceId?: string
|
107
|
-
) => Promise<CreditPayments>;
|
108
|
-
static logUnlockedContent: (
|
109
|
-
user: PhoneUser,
|
110
|
-
owner: PhoneOwner,
|
111
|
-
lockedContent: PremiumContent
|
112
|
-
) => Promise<CreditPayments>;
|
113
|
-
}
|
114
|
-
|
115
|
-
CreditPayments.init(
|
116
|
-
{
|
117
|
-
id: {
|
118
|
-
type: DataTypes.BIGINT,
|
119
|
-
primaryKey: true,
|
120
|
-
allowNull: false,
|
121
|
-
defaultValue: sequelize.fn("next_id"),
|
122
|
-
},
|
123
|
-
userId: {
|
124
|
-
type: DataTypes.BIGINT,
|
125
|
-
field: "user_id",
|
126
|
-
},
|
127
|
-
ownerId: {
|
128
|
-
type: DataTypes.BIGINT,
|
129
|
-
allowNull: false,
|
130
|
-
field: "owner_id",
|
131
|
-
},
|
132
|
-
creditAmountInCents: {
|
133
|
-
type: DataTypes.INTEGER,
|
134
|
-
field: "credit_amount_in_cents",
|
135
|
-
},
|
136
|
-
interactionType: {
|
137
|
-
type: DataTypes.STRING,
|
138
|
-
field: "interaction_type",
|
139
|
-
},
|
140
|
-
payoutDate: {
|
141
|
-
type: DataTypes.DATE,
|
142
|
-
field: "payout_date",
|
143
|
-
},
|
144
|
-
metadata: {
|
145
|
-
type: DataTypes.STRING,
|
146
|
-
field: "metadata",
|
147
|
-
},
|
148
|
-
earningAmountInCents: {
|
149
|
-
type: DataTypes.INTEGER,
|
150
|
-
field: "earning_amount_in_cents",
|
151
|
-
},
|
152
|
-
payoutId: {
|
153
|
-
type: DataTypes.BIGINT,
|
154
|
-
field: "payout_id",
|
155
|
-
},
|
156
|
-
referredCreditPaymentId: {
|
157
|
-
type: DataTypes.BIGINT,
|
158
|
-
field: "referred_credit_payment_id",
|
159
|
-
},
|
160
|
-
transId: {
|
161
|
-
type: DataTypes.BIGINT,
|
162
|
-
field: "trans_id",
|
163
|
-
},
|
164
|
-
invoiceId: {
|
165
|
-
type: DataTypes.STRING,
|
166
|
-
field: "invoice_id",
|
167
|
-
},
|
168
|
-
createdAt: DataTypes.DATE,
|
169
|
-
updatedAt: DataTypes.DATE,
|
170
|
-
},
|
171
|
-
{
|
172
|
-
// Other model options go here
|
173
|
-
tableName: "credit_payments",
|
174
|
-
sequelize,
|
175
|
-
}
|
176
|
-
);
|
177
|
-
|
178
|
-
const REFERRAL_PERCENTAGE = 0.03;
|
179
|
-
|
180
|
-
/*
|
181
|
-
====================================================================
|
182
|
-
Class functions
|
183
|
-
====================================================================
|
184
|
-
*/
|
185
|
-
|
186
|
-
CreditPayments.getById = async (id: string) => {
|
187
|
-
return await CreditPayments.findOne({
|
188
|
-
where: {
|
189
|
-
id,
|
190
|
-
},
|
191
|
-
});
|
192
|
-
};
|
193
|
-
|
194
|
-
CreditPayments.logContentPurchase = async (
|
195
|
-
user: PhoneUser,
|
196
|
-
contentId: string,
|
197
|
-
owner: PhoneOwner,
|
198
|
-
alreadyPaid?: boolean,
|
199
|
-
transaction?: Transaction,
|
200
|
-
invoiceId?: string
|
201
|
-
) => {
|
202
|
-
const content = await PremiumContent.getById(contentId);
|
203
|
-
|
204
|
-
if (!content) {
|
205
|
-
throw new Error("No such content for id: " + contentId);
|
206
|
-
}
|
207
|
-
|
208
|
-
if (user.creditsInCents! < content.amountInCents! && !alreadyPaid) {
|
209
|
-
throw new Error("Not enough credits");
|
210
|
-
}
|
211
|
-
|
212
|
-
if (content.amountInCents! < 0) {
|
213
|
-
throw new Error("Cannot be less than 0");
|
214
|
-
}
|
215
|
-
|
216
|
-
let purchasedContent = await PurchasedContent.create(
|
217
|
-
{
|
218
|
-
userId: user.id!,
|
219
|
-
contentId: content.id!,
|
220
|
-
},
|
221
|
-
{ transaction }
|
222
|
-
);
|
223
|
-
|
224
|
-
if (!alreadyPaid) {
|
225
|
-
user.creditsInCents = user.creditsInCents! - content.amountInCents!;
|
226
|
-
await user.save({ transaction });
|
227
|
-
}
|
228
|
-
|
229
|
-
let creditPayment = await CreditPayments.create(
|
230
|
-
{
|
231
|
-
userId: user.id,
|
232
|
-
ownerId: content.ownerId,
|
233
|
-
creditAmountInCents: content.amountInCents!,
|
234
|
-
earningAmountInCents: earningsAmount(
|
235
|
-
content.amountInCents!,
|
236
|
-
owner && owner.serviceFeePercentage
|
237
|
-
),
|
238
|
-
interactionType: INTERACTION_TYPES.CONTENT,
|
239
|
-
metadata: JSON.stringify({ contentId }),
|
240
|
-
invoiceId,
|
241
|
-
},
|
242
|
-
{ transaction }
|
243
|
-
);
|
244
|
-
|
245
|
-
try {
|
246
|
-
await Notification.create(
|
247
|
-
{
|
248
|
-
recipientId: user.id,
|
249
|
-
senderId: content.ownerId,
|
250
|
-
transactionId: creditPayment.id,
|
251
|
-
type: NotificationType.PURCHASED_POST,
|
252
|
-
purchasedPostId: purchasedContent.id,
|
253
|
-
},
|
254
|
-
{ transaction }
|
255
|
-
);
|
256
|
-
} catch (err) {
|
257
|
-
console.log(err);
|
258
|
-
}
|
259
|
-
|
260
|
-
if (owner && owner.referringOwnerId) {
|
261
|
-
await createReferralPayment(
|
262
|
-
owner,
|
263
|
-
creditPayment,
|
264
|
-
content.amountInCents!,
|
265
|
-
transaction
|
266
|
-
);
|
267
|
-
}
|
268
|
-
|
269
|
-
if (user.referringOwnerId) {
|
270
|
-
await createUserReferralPayment(
|
271
|
-
user,
|
272
|
-
creditPayment,
|
273
|
-
content.amountInCents!,
|
274
|
-
transaction
|
275
|
-
);
|
276
|
-
}
|
277
|
-
|
278
|
-
return creditPayment;
|
279
|
-
};
|
280
|
-
|
281
|
-
// CreditPayments.logTip = async (
|
282
|
-
// user: PhoneUser,
|
283
|
-
// owner: PhoneOwner,
|
284
|
-
// tipAmountInCents: number
|
285
|
-
// ) => {
|
286
|
-
// if (tipAmountInCents > user.creditsInCents!) {
|
287
|
-
// let url = await getPaymentUrl(user, owner);
|
288
|
-
// throw new Error(
|
289
|
-
// `Sorry, it looks like you've run out of credits. To send a tip, add more credits here: ${url}`
|
290
|
-
// );
|
291
|
-
// }
|
292
|
-
|
293
|
-
// if (tipAmountInCents < 0) {
|
294
|
-
// throw new Error("Cannot be less than 0");
|
295
|
-
// }
|
296
|
-
|
297
|
-
// user.creditsInCents = user.creditsInCents! - tipAmountInCents;
|
298
|
-
// await user.save();
|
299
|
-
|
300
|
-
// let creditPayment = await CreditPayments.create({
|
301
|
-
// userId: user.id,
|
302
|
-
// ownerId: owner.id,
|
303
|
-
// creditAmountInCents: tipAmountInCents,
|
304
|
-
// earningAmountInCents: earningsAmount(
|
305
|
-
// tipAmountInCents,
|
306
|
-
// owner.serviceFeePercentage
|
307
|
-
// ),
|
308
|
-
// interactionType: INTERACTION_TYPES.TIP,
|
309
|
-
// });
|
310
|
-
|
311
|
-
// if (owner.referringOwnerId) {
|
312
|
-
// await createReferralPayment(owner, creditPayment, tipAmountInCents);
|
313
|
-
// }
|
314
|
-
|
315
|
-
// if (user.referringOwnerId) {
|
316
|
-
// await createUserReferralPayment(user, creditPayment, tipAmountInCents);
|
317
|
-
// }
|
318
|
-
|
319
|
-
// try {
|
320
|
-
// await Notification.create({
|
321
|
-
// ownerId: owner.id,
|
322
|
-
// userId: user.id,
|
323
|
-
// creditPaymentId: creditPayment.id,
|
324
|
-
// type: "tip",
|
325
|
-
// });
|
326
|
-
// } catch (err) {
|
327
|
-
// console.log(err);
|
328
|
-
// }
|
329
|
-
|
330
|
-
// return creditPayment;
|
331
|
-
// };
|
332
|
-
|
333
|
-
CreditPayments.logMessage = async (
|
334
|
-
user: PhoneUser,
|
335
|
-
owner: PhoneOwner,
|
336
|
-
chatRateInCents: number
|
337
|
-
) => {
|
338
|
-
if (chatRateInCents > user.creditsInCents!) {
|
339
|
-
let url = await getPaymentUrl(user, owner);
|
340
|
-
throw new Error(
|
341
|
-
`Sorry, it looks like you've run out of credits. To keep texting, add more credits here: ${url}`
|
342
|
-
);
|
343
|
-
}
|
344
|
-
|
345
|
-
if (chatRateInCents < 0) {
|
346
|
-
throw new Error("Cannot be less than 0");
|
347
|
-
}
|
348
|
-
|
349
|
-
user.creditsInCents = user.creditsInCents! - chatRateInCents;
|
350
|
-
await user.save();
|
351
|
-
|
352
|
-
let creditPayment = await CreditPayments.create({
|
353
|
-
userId: user.id,
|
354
|
-
ownerId: owner.id,
|
355
|
-
creditAmountInCents: chatRateInCents,
|
356
|
-
earningAmountInCents: earningsAmount(chatRateInCents),
|
357
|
-
interactionType: INTERACTION_TYPES.TEXT,
|
358
|
-
});
|
359
|
-
|
360
|
-
if (owner.referringOwnerId) {
|
361
|
-
await createReferralPayment(owner, creditPayment, chatRateInCents);
|
362
|
-
}
|
363
|
-
|
364
|
-
if (user.referringOwnerId) {
|
365
|
-
await createUserReferralPayment(user, creditPayment, chatRateInCents);
|
366
|
-
}
|
367
|
-
|
368
|
-
return creditPayment;
|
369
|
-
};
|
370
|
-
|
371
|
-
CreditPayments.logAudioCall = async (
|
372
|
-
user: PhoneUser,
|
373
|
-
owner: PhoneOwner,
|
374
|
-
callAmountInCents: number
|
375
|
-
) => {
|
376
|
-
if (callAmountInCents < 0) {
|
377
|
-
throw new Error("Cannot be less than 0");
|
378
|
-
}
|
379
|
-
|
380
|
-
user.creditsInCents = user.creditsInCents! - callAmountInCents;
|
381
|
-
await user.save();
|
382
|
-
|
383
|
-
let creditPayment = await CreditPayments.create({
|
384
|
-
userId: user.id,
|
385
|
-
ownerId: owner.id,
|
386
|
-
creditAmountInCents: callAmountInCents,
|
387
|
-
earningAmountInCents: earningsAmount(
|
388
|
-
callAmountInCents,
|
389
|
-
owner.serviceFeePercentage
|
390
|
-
),
|
391
|
-
interactionType: INTERACTION_TYPES.AUDIO,
|
392
|
-
});
|
393
|
-
|
394
|
-
if (owner.referringOwnerId) {
|
395
|
-
await createReferralPayment(owner, creditPayment, callAmountInCents);
|
396
|
-
}
|
397
|
-
|
398
|
-
if (user.referringOwnerId) {
|
399
|
-
await createUserReferralPayment(user, creditPayment, callAmountInCents);
|
400
|
-
}
|
401
|
-
|
402
|
-
return creditPayment;
|
403
|
-
};
|
404
|
-
|
405
|
-
CreditPayments.logVideoCall = async (
|
406
|
-
user: PhoneUser,
|
407
|
-
owner: PhoneOwner,
|
408
|
-
callAmountInCents: number
|
409
|
-
) => {
|
410
|
-
if (callAmountInCents < 0) {
|
411
|
-
throw new Error("Cannot be less than 0");
|
412
|
-
}
|
413
|
-
|
414
|
-
user.creditsInCents = user.creditsInCents! - callAmountInCents;
|
415
|
-
await user.save();
|
416
|
-
|
417
|
-
let creditPayment = await CreditPayments.create({
|
418
|
-
userId: user.id,
|
419
|
-
ownerId: owner.id,
|
420
|
-
creditAmountInCents: callAmountInCents,
|
421
|
-
earningAmountInCents: earningsAmount(
|
422
|
-
callAmountInCents,
|
423
|
-
owner.serviceFeePercentage
|
424
|
-
),
|
425
|
-
interactionType: INTERACTION_TYPES.VIDEO,
|
426
|
-
});
|
427
|
-
|
428
|
-
if (owner.referringOwnerId) {
|
429
|
-
await createReferralPayment(owner, creditPayment, callAmountInCents);
|
430
|
-
}
|
431
|
-
|
432
|
-
if (user.referringOwnerId) {
|
433
|
-
await createUserReferralPayment(user, creditPayment, callAmountInCents);
|
434
|
-
}
|
435
|
-
|
436
|
-
return creditPayment;
|
437
|
-
};
|
438
|
-
|
439
|
-
// CreditPayments.logUnlockMessage = async (
|
440
|
-
// user: PhoneUser,
|
441
|
-
// owner: PhoneOwner,
|
442
|
-
// message: Message,
|
443
|
-
// alreadyPaid?: boolean,
|
444
|
-
// transaction?: Transaction,
|
445
|
-
// invoiceId?: string
|
446
|
-
// ) => {
|
447
|
-
// if (message.amountInCents! > user.creditsInCents! && !alreadyPaid) {
|
448
|
-
// throw new Error(`Sorry, it looks like you've run out of credits`);
|
449
|
-
// }
|
450
|
-
|
451
|
-
// if (!message.amountInCents) {
|
452
|
-
// throw new Error("Must contain message amount");
|
453
|
-
// }
|
454
|
-
|
455
|
-
// if (message.amountInCents! < 0) {
|
456
|
-
// throw new Error("Cannot be less than 0");
|
457
|
-
// }
|
458
|
-
|
459
|
-
// if (!alreadyPaid) {
|
460
|
-
// user.creditsInCents = user.creditsInCents! - message.amountInCents!;
|
461
|
-
// await user.save({ transaction });
|
462
|
-
// }
|
463
|
-
|
464
|
-
// let creditPayment = await CreditPayments.create(
|
465
|
-
// {
|
466
|
-
// userId: user.id,
|
467
|
-
// ownerId: owner.id,
|
468
|
-
// creditAmountInCents: message.amountInCents!,
|
469
|
-
// earningAmountInCents: earningsAmount(
|
470
|
-
// message.amountInCents!,
|
471
|
-
// owner.serviceFeePercentage
|
472
|
-
// ),
|
473
|
-
// invoiceId,
|
474
|
-
// interactionType: INTERACTION_TYPES.UNLOCK_MESSAGE,
|
475
|
-
// },
|
476
|
-
// { transaction }
|
477
|
-
// );
|
478
|
-
|
479
|
-
// try {
|
480
|
-
// await Notification.create(
|
481
|
-
// {
|
482
|
-
// ownerId: owner.id,
|
483
|
-
// userId: user.id,
|
484
|
-
// creditPaymentId: creditPayment.id,
|
485
|
-
// type: "unlock_message",
|
486
|
-
// messageId: message.id,
|
487
|
-
// },
|
488
|
-
// { transaction }
|
489
|
-
// );
|
490
|
-
// } catch (err) {
|
491
|
-
// console.log(err);
|
492
|
-
// }
|
493
|
-
|
494
|
-
// if (owner.referringOwnerId) {
|
495
|
-
// await createReferralPayment(
|
496
|
-
// owner,
|
497
|
-
// creditPayment,
|
498
|
-
// message.amountInCents!,
|
499
|
-
// transaction
|
500
|
-
// );
|
501
|
-
// }
|
502
|
-
|
503
|
-
// return creditPayment;
|
504
|
-
// };
|
505
|
-
|
506
|
-
CreditPayments.logUnlockedContent = async (
|
507
|
-
user: PhoneUser,
|
508
|
-
owner: PhoneOwner,
|
509
|
-
lockedContent: PremiumContent
|
510
|
-
) => {
|
511
|
-
if (lockedContent.amountInCents! < 0) {
|
512
|
-
throw new Error("Cannot be less than 0");
|
513
|
-
}
|
514
|
-
|
515
|
-
if (lockedContent.amountInCents! > user.creditsInCents!) {
|
516
|
-
let url = await getPaymentUrl(user, owner);
|
517
|
-
throw new Error(
|
518
|
-
`Sorry, it looks like you've run out of credits. To unlock this content, add more credits here: ${url}`
|
519
|
-
);
|
520
|
-
}
|
521
|
-
|
522
|
-
user.creditsInCents = user.creditsInCents! - lockedContent.amountInCents!;
|
523
|
-
await user.save();
|
524
|
-
|
525
|
-
let creditPayment = await CreditPayments.create({
|
526
|
-
userId: user.id,
|
527
|
-
ownerId: owner.id,
|
528
|
-
creditAmountInCents: lockedContent.amountInCents!,
|
529
|
-
earningAmountInCents: earningsAmount(
|
530
|
-
lockedContent.amountInCents!,
|
531
|
-
owner.serviceFeePercentage
|
532
|
-
),
|
533
|
-
interactionType: INTERACTION_TYPES.UNLOCK,
|
534
|
-
});
|
535
|
-
|
536
|
-
if (owner.referringOwnerId) {
|
537
|
-
await createReferralPayment(
|
538
|
-
owner,
|
539
|
-
creditPayment,
|
540
|
-
lockedContent.amountInCents!
|
541
|
-
);
|
542
|
-
}
|
543
|
-
|
544
|
-
if (user.referringOwnerId) {
|
545
|
-
await createUserReferralPayment(
|
546
|
-
user,
|
547
|
-
creditPayment,
|
548
|
-
lockedContent.amountInCents!
|
549
|
-
);
|
550
|
-
}
|
551
|
-
|
552
|
-
return creditPayment;
|
553
|
-
};
|
554
|
-
|
555
|
-
CreditPayments.logSubscriptionPayment = async (
|
556
|
-
userId: string,
|
557
|
-
owner: PhoneOwner,
|
558
|
-
subscriptionAmountInCents: number,
|
559
|
-
subscription: Subscription,
|
560
|
-
transaction: Transaction | undefined,
|
561
|
-
invoiceId?: string
|
562
|
-
) => {
|
563
|
-
let creditPayment = await CreditPayments.create(
|
564
|
-
{
|
565
|
-
userId: userId,
|
566
|
-
ownerId: owner.id,
|
567
|
-
creditAmountInCents: subscriptionAmountInCents,
|
568
|
-
earningAmountInCents: earningsAmount(
|
569
|
-
subscriptionAmountInCents,
|
570
|
-
owner.serviceFeePercentage
|
571
|
-
),
|
572
|
-
interactionType: INTERACTION_TYPES.SUBSCRIPTION,
|
573
|
-
metadata: JSON.stringify({ subscription_id: subscription.id }),
|
574
|
-
invoiceId,
|
575
|
-
},
|
576
|
-
{ transaction }
|
577
|
-
);
|
578
|
-
|
579
|
-
try {
|
580
|
-
if (owner.referringOwnerId) {
|
581
|
-
await createReferralPayment(
|
582
|
-
owner,
|
583
|
-
creditPayment,
|
584
|
-
subscriptionAmountInCents,
|
585
|
-
transaction
|
586
|
-
);
|
587
|
-
}
|
588
|
-
const user = await PhoneUser.getById(userId);
|
589
|
-
if (user && user.referringOwnerId) {
|
590
|
-
await createUserReferralPayment(
|
591
|
-
user,
|
592
|
-
creditPayment,
|
593
|
-
subscriptionAmountInCents,
|
594
|
-
transaction
|
595
|
-
);
|
596
|
-
}
|
597
|
-
} catch (err) {
|
598
|
-
console.log(err);
|
599
|
-
console.log("Issue creating referral payment");
|
600
|
-
}
|
601
|
-
|
602
|
-
return creditPayment;
|
603
|
-
};
|
604
|
-
|
605
|
-
const earningsAmount = (amount: number, serviceFeePercentage?: number) => {
|
606
|
-
return Math.floor(amount * (1 - (serviceFeePercentage || 0.2)));
|
607
|
-
};
|
608
|
-
|
609
|
-
const createReferralPayment = async (
|
610
|
-
owner: PhoneOwner,
|
611
|
-
creditPayment: CreditPayments,
|
612
|
-
creditAmount: number,
|
613
|
-
transaction?: Transaction
|
614
|
-
) => {
|
615
|
-
return await CreditPayments.create(
|
616
|
-
{
|
617
|
-
ownerId: owner.referringOwnerId!,
|
618
|
-
creditAmountInCents: creditAmount,
|
619
|
-
earningAmountInCents: Math.floor(
|
620
|
-
creditAmount * (owner.referralPercentage || REFERRAL_PERCENTAGE)
|
621
|
-
),
|
622
|
-
interactionType: INTERACTION_TYPES.REFERRAL,
|
623
|
-
referredCreditPaymentId: creditPayment.id,
|
624
|
-
},
|
625
|
-
{ transaction }
|
626
|
-
);
|
627
|
-
};
|
628
|
-
|
629
|
-
const createUserReferralPayment = async (
|
630
|
-
user: PhoneUser,
|
631
|
-
creditPayment: CreditPayments,
|
632
|
-
creditAmount: number,
|
633
|
-
transaction?: Transaction
|
634
|
-
) => {
|
635
|
-
return await CreditPayments.create(
|
636
|
-
{
|
637
|
-
ownerId: user.referringOwnerId!,
|
638
|
-
creditAmountInCents: creditAmount,
|
639
|
-
earningAmountInCents: Math.floor(creditAmount * 0.04),
|
640
|
-
interactionType: INTERACTION_TYPES.REFERRAL,
|
641
|
-
referredCreditPaymentId: creditPayment.id!,
|
642
|
-
},
|
643
|
-
{ transaction }
|
644
|
-
);
|
645
|
-
};
|
646
|
-
|
647
|
-
const getPaymentUrl = async (user: PhoneUser, owner: PhoneOwner) => {
|
648
|
-
let name = owner.name && owner.name.toLowerCase().replace(/\s+/g, "-");
|
649
|
-
return `https://${process.env.SITE_DOMAIN}/${name}`;
|
650
|
-
};
|
651
|
-
|
652
|
-
PhoneOwner.hasMany(CreditPayments, {
|
653
|
-
foreignKey: "owner_id",
|
654
|
-
onDelete: "NO ACTION",
|
655
|
-
constraints: false,
|
656
|
-
});
|
657
|
-
PhoneUser.hasMany(CreditPayments, {
|
658
|
-
foreignKey: "user_id",
|
659
|
-
onDelete: "NO ACTION",
|
660
|
-
constraints: false,
|
661
|
-
});
|
662
|
-
CreditPayments.hasOne(PhoneOwner, {
|
663
|
-
foreignKey: "id",
|
664
|
-
sourceKey: "owner_id",
|
665
|
-
onDelete: "NO ACTION",
|
666
|
-
constraints: false,
|
667
|
-
});
|
668
|
-
CreditPayments.hasOne(PhoneUser, {
|
669
|
-
foreignKey: "id",
|
670
|
-
sourceKey: "user_id",
|
671
|
-
onDelete: "NO ACTION",
|
672
|
-
constraints: false,
|
673
|
-
});
|
674
|
-
Notification.hasOne(CreditPayments, {
|
675
|
-
foreignKey: "id",
|
676
|
-
sourceKey: "transactionId",
|
677
|
-
constraints: false,
|
678
|
-
onDelete: "NO ACTION",
|
679
|
-
});
|
680
|
-
|
681
|
-
export default CreditPayments;
|