Removed .js from all imports to smooth ts migration
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
import { ValidatedMethod } from 'meteor/mdg:validated-method';
|
||||
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
|
||||
import { getUserTier } from '/imports/api/users/patreon/tiers.js';
|
||||
import { getUserTier } from '/imports/api/users/patreon/tiers';
|
||||
|
||||
let Invites= new Mongo.Collection('invites');
|
||||
let Invites = new Mongo.Collection('invites');
|
||||
|
||||
let InviteSchema = new SimpleSchema({
|
||||
inviter: {
|
||||
@@ -34,13 +34,13 @@ let InviteSchema = new SimpleSchema({
|
||||
},
|
||||
});
|
||||
|
||||
if (Meteor.isServer){
|
||||
Accounts.onLogin(function({user}){
|
||||
if (Meteor.isServer) {
|
||||
Accounts.onLogin(function ({ user }) {
|
||||
alignInvitesWithPatreonTier(user);
|
||||
});
|
||||
}
|
||||
|
||||
function alignInvitesWithPatreonTier(user){
|
||||
function alignInvitesWithPatreonTier(user) {
|
||||
const tier = getUserTier(user);
|
||||
let availableInvites = tier.invites;
|
||||
let currentlyFundedInvites = [];
|
||||
@@ -48,7 +48,7 @@ function alignInvitesWithPatreonTier(user){
|
||||
Invites.find({
|
||||
inviter: user._id
|
||||
}).forEach(invite => {
|
||||
if (invite.isFunded){
|
||||
if (invite.isFunded) {
|
||||
currentlyFundedInvites.push(invite);
|
||||
} else {
|
||||
currenltyUnfundedInvites.push(invite);
|
||||
@@ -63,23 +63,23 @@ function alignInvitesWithPatreonTier(user){
|
||||
currenltyUnfundedInvites.sort((a, b) => b.dateConfirmed - a.dateConfirmed);
|
||||
|
||||
// Defund or delete excess invites
|
||||
while (currentlyFundedInvites.length > availableInvites){
|
||||
while (currentlyFundedInvites.length > availableInvites) {
|
||||
let inviteToDefund = currentlyFundedInvites.pop();
|
||||
if (inviteToDefund.invitee){
|
||||
Invites.update(inviteToDefund._id, {$set: {isFunded: false}});
|
||||
if (inviteToDefund.invitee) {
|
||||
Invites.update(inviteToDefund._id, { $set: { isFunded: false } });
|
||||
} else {
|
||||
Invites.remove(inviteToDefund._id);
|
||||
}
|
||||
}
|
||||
// Fund unfunded invites or insert new ones
|
||||
while (currentlyFundedInvites.length < availableInvites){
|
||||
if (currenltyUnfundedInvites.length){
|
||||
while (currentlyFundedInvites.length < availableInvites) {
|
||||
if (currenltyUnfundedInvites.length) {
|
||||
let inviteToFund = currenltyUnfundedInvites.pop();
|
||||
currentlyFundedInvites.push(inviteToFund);
|
||||
Invites.update(inviteToFund._id, {$set: {isFunded: true}});
|
||||
Invites.update(inviteToFund._id, { $set: { isFunded: true } });
|
||||
} else {
|
||||
let inviteId = Invites.insert({inviter: user._id, isFunded: true});
|
||||
currentlyFundedInvites.push({_id: inviteId});
|
||||
let inviteId = Invites.insert({ inviter: user._id, isFunded: true });
|
||||
currentlyFundedInvites.push({ _id: inviteId });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,17 +97,17 @@ const getInviteToken = new ValidatedMethod({
|
||||
numRequests: 5,
|
||||
timeInterval: 5000,
|
||||
},
|
||||
run({inviteId}) {
|
||||
run({ inviteId }) {
|
||||
let invite = Invites.findOne(inviteId);
|
||||
if (this.userId !== invite.inviter) {
|
||||
throw new Meteor.Error('Invites.methods.getToken.denied',
|
||||
'You need to be the inviter of the invite to create a token');
|
||||
'You need to be the inviter of the invite to create a token');
|
||||
}
|
||||
if (invite.inviteToken){
|
||||
if (invite.inviteToken) {
|
||||
return invite.inviteToken;
|
||||
} else {
|
||||
let inviteToken = Random.id(5);
|
||||
Invites.update(inviteId, {$set: {inviteToken}})
|
||||
Invites.update(inviteId, { $set: { inviteToken } })
|
||||
return inviteToken;
|
||||
}
|
||||
},
|
||||
@@ -125,32 +125,32 @@ const acceptInviteToken = new ValidatedMethod({
|
||||
numRequests: 5,
|
||||
timeInterval: 5000,
|
||||
},
|
||||
run({inviteToken}) {
|
||||
run({ inviteToken }) {
|
||||
if (!this.userId) {
|
||||
throw new Meteor.Error('Invites.methods.acceptToken.denied',
|
||||
'You need to be the logged in to accept a token');
|
||||
'You need to be the logged in to accept a token');
|
||||
}
|
||||
if (Meteor.isClient) return;
|
||||
let invite = Invites.findOne({inviteToken});
|
||||
if (!invite){
|
||||
let invite = Invites.findOne({ inviteToken });
|
||||
if (!invite) {
|
||||
throw new Meteor.Error('Invites.methods.acceptToken.notFound',
|
||||
'No invite could be found for this link, maybe it has already been claimed');
|
||||
'No invite could be found for this link, maybe it has already been claimed');
|
||||
}
|
||||
// If the invitee is already filled, fix unexpected case by deleting the token
|
||||
if (invite.invitee){
|
||||
if (invite.invitee) {
|
||||
Invites.update(invite._id, {
|
||||
$unset: {inviteToken: 1}
|
||||
$unset: { inviteToken: 1 }
|
||||
});
|
||||
throw new Meteor.Error('Invites.methods.acceptToken.alreadyAccepted',
|
||||
'This invite has already been claimed');
|
||||
'This invite has already been claimed');
|
||||
}
|
||||
if (this.userId === invite.inviter){
|
||||
if (this.userId === invite.inviter) {
|
||||
throw new Meteor.Error('Invites.methods.acceptToken.ownToken',
|
||||
'You can\'t accept your own invite');
|
||||
'You can\'t accept your own invite');
|
||||
}
|
||||
Invites.update(invite._id, {
|
||||
$set: {invitee: this.userId},
|
||||
$unset: {inviteToken: 1},
|
||||
$set: { invitee: this.userId },
|
||||
$unset: { inviteToken: 1 },
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -168,28 +168,28 @@ const revokeInvite = new ValidatedMethod({
|
||||
numRequests: 5,
|
||||
timeInterval: 5000,
|
||||
},
|
||||
run({inviteId}) {
|
||||
run({ inviteId }) {
|
||||
if (!this.userId) {
|
||||
throw new Meteor.Error('Invites.methods.revokeInvite.denied',
|
||||
'You need to be the logged in to revoke a token');
|
||||
'You need to be the logged in to revoke a token');
|
||||
}
|
||||
if (Meteor.isClient) return;
|
||||
let invite = Invites.findOne(inviteId);
|
||||
if (!invite){
|
||||
if (!invite) {
|
||||
throw new Meteor.Error('Invites.methods.revokeInvite.notFound',
|
||||
'No invite could be found for this id');
|
||||
'No invite could be found for this id');
|
||||
}
|
||||
if (this.userId !== invite.inviter) {
|
||||
throw new Meteor.Error('Invites.methods.revokeInvite.denied',
|
||||
'You are not the owner of this invite');
|
||||
'You are not the owner of this invite');
|
||||
}
|
||||
|
||||
// If the invitee is empty, the token has already been revoked
|
||||
if (!invite.invitee){
|
||||
if (!invite.invitee) {
|
||||
return;
|
||||
}
|
||||
Invites.update(invite._id, {
|
||||
$unset: {invitee: 1, dateConfirmed: 1},
|
||||
$unset: { invitee: 1, dateConfirmed: 1 },
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user