Files
habitica/website/server/models/subscriptionPlan.js
Phillip Thelen 6e43d4dc79 Add Transaction log for gem and hourglass changes (#13589)
* Log all gem transactions to database

* Also store hourglass transactions

* Fix tests

* Display transaction history in hall of heroes for admins

* add tests to new API call

* hide transaction settings tab for non admins

* fix(lint): remove console

* fix(lint): various automatic corrections

* fix(transactions): use enum expected pluralizations

* fix api unit tests

* fix lint

* fix failing test

* Fix minor inconsistencies

* Log all gem transactions to database

* Also store hourglass transactions

* Fix tests

* Display transaction history in hall of heroes for admins

* add tests to new API call

* hide transaction settings tab for non admins

* fix(lint): remove console

* fix(lint): various automatic corrections

* fix(transactions): use enum expected pluralizations

* fix api unit tests

* fix lint

* Fix minor inconsistencies

Co-authored-by: Sabe Jones <sabrecat@gmail.com>
2022-01-31 15:36:15 -06:00

65 lines
2.2 KiB
JavaScript

import mongoose from 'mongoose';
import validator from 'validator';
import baseModel from '../libs/baseModel';
import { model as Transaction } from './transaction';
export const schema = new mongoose.Schema({
planId: String,
subscriptionId: String,
owner: { $type: String, ref: 'User', validate: [v => validator.isUUID(v), 'Invalid uuid for subscription owner.'] },
quantity: { $type: Number, default: 1 },
paymentMethod: String, // enum: ['Paypal', 'Stripe', 'Gift', 'Amazon Payments', 'Google', '']}
customerId: String, // Billing Agreement Id in case of Amazon Payments
dateCreated: Date,
dateTerminated: Date,
dateUpdated: Date,
extraMonths: { $type: Number, default: 0 },
gemsBought: { $type: Number, default: 0 },
mysteryItems: { $type: Array, default: () => [] },
lastReminderDate: Date, // indicates the last time a subscription reminder was sent
lastBillingDate: Date, // Used only for Amazon Payments to keep track of billing date
// 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,
nextBillingDate: Date, // Next time google will bill this user.
consecutive: {
count: { $type: Number, default: 0 },
// when gifted subs, offset++ for each month. offset-- each new-month (cron).
// count doesn't ++ until offset==0
offset: { $type: Number, default: 0 },
gemCapExtra: { $type: Number, default: 0 },
trinkets: { $type: Number, default: 0 },
},
}, {
strict: true,
minimize: false, // So empty objects are returned
_id: false,
typeKey: '$type', // So that we can use fields named `type`
});
schema.plugin(baseModel, {
private: ['additionalData'],
noSet: ['_id'],
timestamps: false,
_id: false,
});
schema.methods.updateHourglasses = async function updateHourglasses (userId,
amount,
transactionType,
reference,
referenceText) {
this.consecutive.trinkets += amount;
await Transaction.create({
currency: 'hourglasses',
userId,
transactionType,
amount,
reference,
referenceText,
});
};
export const model = mongoose.model('SubscriptionPlan', schema);