API fixes

pull/29/head
Matthew 2020-10-13 22:19:47 -04:00
parent 839d940205
commit 4620fd41f6
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
3 changed files with 142 additions and 6 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable no-continue */
import { TextChannel } from 'eris';
import { Route, Server, RichEmbed } from '../../../class';
@ -23,6 +24,15 @@ export default class Report extends Route {
if (!member) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (member.locked) return res.status(403).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.messages.PERMISSION_DENIED });
if (merchant.type !== 1) return res.status(403).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.messages.PERMISSION_DENIED });
const set = [];
const accounts = await this.server.client.db.Score.find().lean().exec();
for (const sc of accounts) {
if (sc.total < 200) { continue; }
if (sc.total > 800) { set.push(800); continue; }
set.push(sc.total);
}
let totalScore: number;
let activityScore: number;
@ -87,6 +97,7 @@ export default class Report extends Route {
code: this.constants.codes.SUCCESS,
message: {
userID: member.userID,
percentile: Math.round(this.server.client.util.percentile(set, member.total)),
totalScore,
activityScore,
roleScore,
@ -102,6 +113,87 @@ export default class Report extends Route {
}
});
this.router.post('/v2/soft', async (req, res) => {
try {
if (!req.headers.authorization) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
if (!req.body.userID) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.messages.CLIENT_ERROR });
const merchant = await this.server.client.db.Merchant.findOne({ key: req.headers.authorization }).lean().exec();
if (!merchant) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
const member = await this.server.client.db.Score.findOne({ userID: req.body.userID }).lean().exec();
if (!member) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
let totalScore: number;
if (member.total < 200) totalScore = 0;
else if (member.total > 800) totalScore = 800;
else totalScore = Math.round(member.total);
await this.server.client.db.Merchant.updateOne({ key: req.headers.authorization }, { $addToSet: { pulls: { type: 1, reason: 'N/A', date: new Date() } } });
await this.server.client.db.Score.updateOne({ userID: member.userID }, { $addToSet: { softInquiries: { name: merchant.name, date: new Date() } } });
const embed = new RichEmbed();
embed.setTitle('Inquiry Notification');
embed.setColor('#00FFFF');
const mem = this.server.client.util.resolveMember(member.userID, this.server.client.guilds.get(this.server.client.config.guildID));
embed.addField('Member', `${mem.user.username}#${mem.user.discriminator} | <@${member.userID}>`, true);
embed.addField('Type', 'SOFT', true);
embed.addField('Department/Service', merchant.name.toUpperCase(), true);
embed.setTimestamp();
embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL);
const chan = <TextChannel> this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849');
chan.createMessage({ embed }).catch(() => {});
if (!merchant.privileged) {
return res.status(200).json({
code: this.constants.codes.SUCCESS,
message: {
userID: member.userID,
totalScore,
},
});
}
let activityScore: number;
const moderationScore = Math.round(member.moderation);
let roleScore: number;
let cloudServicesScore: number;
const otherScore = Math.round(member.other);
let miscScore: number;
if (member.activity < 10) activityScore = 0;
else if (member.activity > Math.floor((Math.log1p(3000 + 300 + 200 + 100) * 12))) activityScore = Math.floor((Math.log1p(3000 + 300 + 200 + 100) * 12));
else activityScore = Math.round(member.activity);
if (member.roles <= 0) roleScore = 0;
else if (member.roles > 54) roleScore = 54;
else roleScore = Math.round(member.roles);
if (member.staff <= 0) miscScore = 0;
else miscScore = Math.round(member.staff);
if (member.cloudServices === 0) cloudServicesScore = 0;
else if (member.cloudServices > 10) cloudServicesScore = 10;
else cloudServicesScore = Math.round(member.cloudServices);
return res.status(200).json({
code: this.constants.codes.SUCCESS,
message: {
userID: member.userID,
totalScore,
activityScore,
moderationScore,
roleScore,
cloudServicesScore,
otherScore,
miscScore,
},
});
} catch (err) {
return this.handleError(err, res);
}
});
this.router.post('/soft', async (req, res) => {
try {
if (!req.headers.authorization) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
@ -133,6 +225,7 @@ export default class Report extends Route {
const chan = <TextChannel> this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849');
chan.createMessage({ embed }).catch(() => {});
if (!merchant.privileged) {
return res.status(200).json({
code: this.constants.codes.SUCCESS,
message: {
@ -140,6 +233,43 @@ export default class Report extends Route {
totalScore,
},
});
}
let activityScore: number;
const moderationScore = Math.round(member.moderation);
let roleScore: number;
let cloudServicesScore: number;
const otherScore = Math.round(member.other);
let miscScore: number;
if (member.activity < 10) activityScore = 0;
else if (member.activity > Math.floor((Math.log1p(3000 + 300 + 200 + 100) * 12))) activityScore = Math.floor((Math.log1p(3000 + 300 + 200 + 100) * 12));
else activityScore = Math.round(member.activity);
if (member.roles <= 0) roleScore = 0;
else if (member.roles > 54) roleScore = 54;
else roleScore = Math.round(member.roles);
if (member.staff <= 0) miscScore = 0;
else miscScore = Math.round(member.staff);
if (member.cloudServices === 0) cloudServicesScore = 0;
else if (member.cloudServices > 10) cloudServicesScore = 10;
else cloudServicesScore = Math.round(member.cloudServices);
return res.status(200).json({
code: this.constants.codes.SUCCESS,
message: {
userID: member.userID,
totalScore,
activityScore,
moderationScore,
roleScore,
cloudServicesScore,
otherScore,
miscScore,
},
});
} catch (err) {
return this.handleError(err, res);
}

View File

@ -7,7 +7,7 @@ export default class AddMerchant extends Command {
super(client);
this.name = 'addmerchant';
this.description = 'Creates a new merchant.';
this.usage = `${this.client.config.prefix}addmerchant <merchant name>`;
this.usage = `${this.client.config.prefix}addmerchant <privileged: 1 for yes | 0 for no> <type: 0 for only soft | 1 for soft and hard> <merchant name>`;
this.aliases = ['am'];
this.permissions = 6;
this.guildOnly = true;
@ -16,14 +16,16 @@ export default class AddMerchant extends Command {
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
if (!args[1]) return this.client.commands.get('help').run(message, [this.name]);
if ((Number(args[0]) !== 0) && (Number(args[0]) !== 1)) return this.error(message.channel, 'Invalid permissions.');
const key = randomBytes(20).toString('hex');
const merchant = await (new this.client.db.Merchant({
name: args.join(' '),
name: args.slice(2).join(' '),
privileged: Number(args[0]),
key,
pulls: [],
})).save();
return this.success(message.channel, `Created merchant (${merchant._id}). \`${args.join(' ')}\`\n\n\`${key}\``);
return this.success(message.channel, `Created merchant (${merchant._id}). \`${args.slice(2).join(' ')}\`\n\n\`${key}\``);
} catch (err) {
return this.client.util.handleError(err, message, this);
}

View File

@ -3,17 +3,21 @@ import { Document, Schema, model } from 'mongoose';
export interface MerchantInterface extends Document {
name: string,
key: string,
privileged: boolean,
/**
* type
* - 0: hard
* - 1: soft
* - 0: soft
* - 1: hard
*/
type: 0 | 1;
pulls: [{ type: 0 | 1, reason: string, date: Date }],
}
const Merchant: Schema = new Schema({
name: String,
key: String,
privileged: Boolean,
type: Number,
pulls: Array,
});