mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 22:27:26 +01:00
* wip: define items as mixed objects * add default owned gear * mark modified * more mark modified * more mark modified * more mark modified * more mark modified * fix common tests * fix common tests * update mongoose * add itemsUtils * use new util function in hall controller * add tests for items utils * update website/server to mark all items as modified * start updating common code * update login incentives * update unlock * remove changes to package-lock.json * remove changes to package.json
61 lines
1.9 KiB
JavaScript
61 lines
1.9 KiB
JavaScript
/* eslint-disable camelcase */
|
|
|
|
import mongoose from 'mongoose';
|
|
import _ from 'lodash';
|
|
import shared from '../../common';
|
|
import couponCode from 'coupon-code';
|
|
import baseModel from '../libs/baseModel';
|
|
import {
|
|
BadRequest,
|
|
NotAuthorized,
|
|
} from '../libs/errors';
|
|
|
|
export let schema = new mongoose.Schema({
|
|
_id: {$type: String, default: couponCode.generate, required: true},
|
|
event: {$type: String, enum: ['wondercon', 'google_6mo']},
|
|
user: {$type: String, ref: 'User'},
|
|
}, {
|
|
strict: true,
|
|
minimize: false, // So empty objects are returned
|
|
typeKey: '$type', // So that we can use fields named `type`
|
|
});
|
|
|
|
schema.plugin(baseModel, {
|
|
timestamps: true,
|
|
_id: false,
|
|
});
|
|
|
|
schema.statics.generate = async function generateCoupons (event, count = 1) {
|
|
let coupons = _.times(count, () => {
|
|
return {event};
|
|
});
|
|
|
|
return await this.create(coupons);
|
|
};
|
|
|
|
schema.statics.apply = async function applyCoupon (user, req, code) {
|
|
let coupon = await this.findById(couponCode.validate(code)).exec();
|
|
if (!coupon) throw new BadRequest(shared.i18n.t('invalidCoupon', req.language));
|
|
if (coupon.user) throw new NotAuthorized(shared.i18n.t('couponUsed', req.language));
|
|
|
|
if (coupon.event === 'wondercon') {
|
|
user.items.gear.owned.eyewear_special_wondercon_red = true;
|
|
user.items.gear.owned.eyewear_special_wondercon_black = true;
|
|
user.items.gear.owned.back_special_wondercon_black = true;
|
|
user.items.gear.owned.back_special_wondercon_red = true;
|
|
user.items.gear.owned.body_special_wondercon_red = true;
|
|
user.items.gear.owned.body_special_wondercon_black = true;
|
|
user.items.gear.owned.body_special_wondercon_gold = true;
|
|
user.markModified('items.gear.owned');
|
|
|
|
user.extra = {signupEvent: 'wondercon'};
|
|
}
|
|
|
|
await user.save();
|
|
coupon.user = user._id;
|
|
await coupon.save();
|
|
};
|
|
|
|
module.exports.schema = schema;
|
|
export let model = mongoose.model('Coupon', schema);
|