From 4620fd41f6c3008e9ee1aa3a0da53c9555bb2e5c Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 13 Oct 2020 22:19:47 -0400 Subject: [PATCH] API fixes --- .../comm.libraryofcode.org/routes/report.ts | 130 ++++++++++++++++++ src/commands/addmerchant.ts | 10 +- src/models/Merchant.ts | 8 +- 3 files changed, 142 insertions(+), 6 deletions(-) diff --git a/src/api/comm.libraryofcode.org/routes/report.ts b/src/api/comm.libraryofcode.org/routes/report.ts index 1a0c5ef..284d725 100644 --- a/src/api/comm.libraryofcode.org/routes/report.ts +++ b/src/api/comm.libraryofcode.org/routes/report.ts @@ -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 = 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,11 +225,49 @@ export default class Report extends Route { const chan = 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) { diff --git a/src/commands/addmerchant.ts b/src/commands/addmerchant.ts index d41451f..f07a253 100644 --- a/src/commands/addmerchant.ts +++ b/src/commands/addmerchant.ts @@ -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 `; + this.usage = `${this.client.config.prefix}addmerchant `; 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); } diff --git a/src/models/Merchant.ts b/src/models/Merchant.ts index 6c26d9b..c78274a 100644 --- a/src/models/Merchant.ts +++ b/src/models/Merchant.ts @@ -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, });