2016-11-01 20:51:30 +00:00
|
|
|
import mongoose from 'mongoose';
|
2016-12-21 19:45:45 +00:00
|
|
|
import validator from 'validator';
|
2019-10-08 14:57:10 +00:00
|
|
|
import baseModel from '../libs/baseModel';
|
2022-09-21 19:42:39 +00:00
|
|
|
import { TransactionModel as Transaction } from './transaction';
|
2016-11-01 20:51:30 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
export const schema = new mongoose.Schema({
|
2016-11-01 20:51:30 +00:00
|
|
|
planId: String,
|
2016-12-21 19:45:45 +00:00
|
|
|
subscriptionId: String,
|
2020-01-29 05:49:55 +00:00
|
|
|
owner: { $type: String, ref: 'User', validate: [v => validator.isUUID(v), 'Invalid uuid for subscription owner.'] },
|
2019-10-08 14:57:10 +00:00
|
|
|
quantity: { $type: Number, default: 1 },
|
2017-02-02 00:39:37 +00:00
|
|
|
paymentMethod: String, // enum: ['Paypal', 'Stripe', 'Gift', 'Amazon Payments', 'Google', '']}
|
2016-11-01 20:51:30 +00:00
|
|
|
customerId: String, // Billing Agreement Id in case of Amazon Payments
|
|
|
|
|
dateCreated: Date,
|
|
|
|
|
dateTerminated: Date,
|
|
|
|
|
dateUpdated: Date,
|
2022-11-08 11:38:24 +00:00
|
|
|
dateCurrentTypeCreated: Date,
|
2019-10-08 14:57:10 +00:00
|
|
|
extraMonths: { $type: Number, default: 0 },
|
|
|
|
|
gemsBought: { $type: Number, default: 0 },
|
|
|
|
|
mysteryItems: { $type: Array, default: () => [] },
|
2019-06-10 20:37:30 +00:00
|
|
|
lastReminderDate: Date, // indicates the last time a subscription reminder was sent
|
2016-11-01 20:51:30 +00:00
|
|
|
lastBillingDate: Date, // Used only for Amazon Payments to keep track of billing date
|
2019-10-09 18:42:16 +00:00
|
|
|
// Example for Google: {'receipt': 'serialized receipt json', 'signature': 'signature string'}
|
|
|
|
|
additionalData: mongoose.Schema.Types.Mixed,
|
|
|
|
|
// indicates when the queue server should process this subscription again.
|
|
|
|
|
nextPaymentProcessing: Date,
|
2017-02-02 00:39:37 +00:00
|
|
|
nextBillingDate: Date, // Next time google will bill this user.
|
2024-11-14 18:31:57 +00:00
|
|
|
hourglassPromoReceived: Date,
|
|
|
|
|
cumulativeCount: { $type: Number, default: 0 },
|
2016-11-01 20:51:30 +00:00
|
|
|
consecutive: {
|
2019-10-08 14:57:10 +00:00
|
|
|
count: { $type: Number, default: 0 },
|
2019-10-09 18:42:16 +00:00
|
|
|
// when gifted subs, offset++ for each month. offset-- each new-month (cron).
|
|
|
|
|
// count doesn't ++ until offset==0
|
|
|
|
|
offset: { $type: Number, default: 0 },
|
2019-10-08 14:57:10 +00:00
|
|
|
gemCapExtra: { $type: Number, default: 0 },
|
|
|
|
|
trinkets: { $type: Number, default: 0 },
|
2024-11-14 18:31:57 +00:00
|
|
|
lastHourglassReceived: Date,
|
2016-11-01 20:51:30 +00:00
|
|
|
},
|
|
|
|
|
}, {
|
|
|
|
|
strict: true,
|
|
|
|
|
minimize: false, // So empty objects are returned
|
|
|
|
|
_id: false,
|
2018-10-28 14:23:41 +00:00
|
|
|
typeKey: '$type', // So that we can use fields named `type`
|
2016-11-01 20:51:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
schema.plugin(baseModel, {
|
2017-02-02 00:39:37 +00:00
|
|
|
private: ['additionalData'],
|
2016-11-01 20:51:30 +00:00
|
|
|
noSet: ['_id'],
|
|
|
|
|
timestamps: false,
|
2016-11-16 20:44:47 +00:00
|
|
|
_id: false,
|
2016-11-01 20:51:30 +00:00
|
|
|
});
|
|
|
|
|
|
2024-11-14 18:31:57 +00:00
|
|
|
schema.methods.rewardPerks = async function rewardPerks
|
2022-11-30 21:27:53 +00:00
|
|
|
(userID, adding) {
|
2024-11-14 18:31:57 +00:00
|
|
|
let perks = adding;
|
2022-12-09 11:16:33 +00:00
|
|
|
if (typeof adding === 'string' || adding instanceof String) {
|
2024-11-14 18:31:57 +00:00
|
|
|
perks = parseInt(adding, 10);
|
2022-12-02 16:12:17 +00:00
|
|
|
}
|
2022-11-30 14:06:50 +00:00
|
|
|
|
|
|
|
|
if (perks > 0) {
|
2024-11-14 18:31:57 +00:00
|
|
|
this.consecutive.gemCapExtra += 2 * perks; // 2 extra Gems every month
|
|
|
|
|
// cap it at 50 (hard 24 limit + extra 26)
|
|
|
|
|
if (this.consecutive.gemCapExtra > 26) this.consecutive.gemCapExtra = 26;
|
|
|
|
|
// one Hourglass every month
|
2022-11-30 14:06:50 +00:00
|
|
|
await this.updateHourglasses(userID, perks, 'subscription_perks'); // eslint-disable-line no-await-in-loop
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-16 21:18:47 +00:00
|
|
|
schema.methods.updateHourglasses = async function updateHourglasses (
|
|
|
|
|
userId,
|
2022-01-31 21:36:15 +00:00
|
|
|
amount,
|
|
|
|
|
transactionType,
|
|
|
|
|
reference,
|
2024-01-16 21:18:47 +00:00
|
|
|
referenceText,
|
|
|
|
|
) {
|
2022-01-31 21:36:15 +00:00
|
|
|
this.consecutive.trinkets += amount;
|
2024-11-14 18:31:57 +00:00
|
|
|
this.consecutive.lastHourglassReceived = new Date();
|
2022-01-31 21:36:15 +00:00
|
|
|
await Transaction.create({
|
|
|
|
|
currency: 'hourglasses',
|
|
|
|
|
userId,
|
|
|
|
|
transactionType,
|
|
|
|
|
amount,
|
|
|
|
|
reference,
|
|
|
|
|
referenceText,
|
2022-07-13 19:18:59 +00:00
|
|
|
|
|
|
|
|
currentAmount: this.consecutive.trinkets,
|
2022-01-31 21:36:15 +00:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
export const model = mongoose.model('SubscriptionPlan', schema);
|