From 16554a0f298fc886e2a0544c4132d09578023053 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 29 Sep 2020 02:45:13 -0400 Subject: [PATCH] add hard-pull api for merchants --- src/api/comm.libraryofcode.org/main.ts | 6 ++ .../comm.libraryofcode.org/routes/index.ts | 1 + .../comm.libraryofcode.org/routes/report.ts | 92 +++++++++++++++++++ src/api/index.ts | 2 + 4 files changed, 101 insertions(+) create mode 100644 src/api/comm.libraryofcode.org/main.ts create mode 100644 src/api/comm.libraryofcode.org/routes/index.ts create mode 100644 src/api/comm.libraryofcode.org/routes/report.ts diff --git a/src/api/comm.libraryofcode.org/main.ts b/src/api/comm.libraryofcode.org/main.ts new file mode 100644 index 0000000..50dfa27 --- /dev/null +++ b/src/api/comm.libraryofcode.org/main.ts @@ -0,0 +1,6 @@ +import { Server, ServerManagement } from '../../class'; + +export default (management: ServerManagement) => { + const server = new Server(management, 3895, `${__dirname}/routes`); + return server; +}; diff --git a/src/api/comm.libraryofcode.org/routes/index.ts b/src/api/comm.libraryofcode.org/routes/index.ts new file mode 100644 index 0000000..06de8be --- /dev/null +++ b/src/api/comm.libraryofcode.org/routes/index.ts @@ -0,0 +1 @@ +export { default as report } from './report'; diff --git a/src/api/comm.libraryofcode.org/routes/report.ts b/src/api/comm.libraryofcode.org/routes/report.ts new file mode 100644 index 0000000..d6b7619 --- /dev/null +++ b/src/api/comm.libraryofcode.org/routes/report.ts @@ -0,0 +1,92 @@ +import { Route, Server } from '../../../class'; + +export default class Report extends Route { + constructor(server: Server) { + super(server); + this.conf = { + path: '/report', + }; + } + + public bind() { + this.router.post('/hard', async (req, res) => { + try { + if (!req.headers.authorization) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED }); + if (!req.body.pin || !req.body.userID || !req.body.reason) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.codes.CLIENT_ERROR }); + if (req.body.reason?.length < 1) return res.status(400).json({ code: this.constants.codes.CLIENT_ERROR, message: this.constants.codes.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.codes.UNAUTHORIZED }); + + const member = await this.server.client.db.Score.findOne({ userID: req.body.userID, 'pin.2': req.body.pin }).lean().exec(); + if (!member) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED }); + + if (member.locked) return res.status(403).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.codes.PERMISSION_DENIED }); + + let totalScore: number; + 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.total < 200) totalScore = 0; + else if (member.total > 800) totalScore = 800; + else totalScore = Math.round(member.total); + + 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); + + const inquiries: [{ name: string, date: Date }?] = []; + if (member.inquiries?.length > 0) { + for (const inq of member.inquiries) { + const testDate = (new Date(new Date(inq.date).setHours(2190))); + if (testDate > new Date()) inquiries.push({ name: inq.name, date: inq.date }); + } + } + + if (member.notify) { + const chan = await this.server.client.getDMChannel(member.userID); + try { + chan.createMessage(`__**Community Score - Hard Pull Notification**__\n*You have signed up to be notified whenever your hard score has been pulled. See \`?score\` for more information.*\n\n**Department/Service:** ${merchant.name.toUpperCase()}`); + } catch (err) { + this.server.client.util.signale.error(`Unable to DM user: ${member.userID} | ${err}`); + } + } + + await this.server.client.db.Merchant.updateOne({ key: req.headers.authorization }, { $addToSet: { pulls: { type: 0, reason: req.body.reason, date: new Date() } } }); + await this.server.client.db.Score.updateOne({ userID: member.userID }, { $addToSet: { inquiries: { name: merchant.name, reason: req.body.reason, date: new Date() } } }); + + return res.status(200).json({ + code: this.constants.codes.SUCCESS, + message: { + userID: member.userID, + totalScore, + activityScore, + roleScore, + moderationScore, + cloudServicesScore, + miscScore, + otherScore, + inquiries, + }, + }); + } catch (err) { + return this.handleError(err, res); + } + }); + } +} diff --git a/src/api/index.ts b/src/api/index.ts index 2748a82..9af7ae9 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,7 +1,9 @@ import locsh from './loc.sh/main'; import crins from './cr.ins/main'; +import commlibraryofcodeorg from './comm.libraryofcode.org/main'; export default { 'loc.sh': locsh, 'cr.ins': crins, + 'comm.libraryofcode.org': commlibraryofcodeorg, };