add hard-pull api for merchants

pull/29/head
Matthew 2020-09-29 02:45:13 -04:00
parent 8e2c1c1b28
commit 16554a0f29
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
4 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import { Server, ServerManagement } from '../../class';
export default (management: ServerManagement) => {
const server = new Server(management, 3895, `${__dirname}/routes`);
return server;
};

View File

@ -0,0 +1 @@
export { default as report } from './report';

View File

@ -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);
}
});
}
}

View File

@ -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,
};