From f42dab5cc2e4f9f1aa779d7afd83fabfa1e62fcb Mon Sep 17 00:00:00 2001 From: Austin M Date: Sat, 30 May 2020 14:34:11 -0700 Subject: [PATCH 1/3] Backend point type configuration --- backend/src/controller/user.ts | 3 +-- backend/src/model/point.ts | 10 +++++++--- backend/src/router/user.ts | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/src/controller/user.ts b/backend/src/controller/user.ts index b53181b..65dbcb8 100644 --- a/backend/src/controller/user.ts +++ b/backend/src/controller/user.ts @@ -95,8 +95,6 @@ class UserController { const body: InputUpdateBodyType = ctx.request.body; - - const user: UserDocument | null = await UserModel.findOneAndUpdate({ id: body.id, }, { @@ -109,6 +107,7 @@ class UserController { PointModel.create({ amount: body.totalPoints, userID: user.id, + type: ctx.params.type, }).catch(() => ctx.throw(400, Responses.CANT_CREATE_POINT)).then(res => { user.points.push(res._id.toString()); user.save(); diff --git a/backend/src/model/point.ts b/backend/src/model/point.ts index 851ee6f..992fad7 100644 --- a/backend/src/model/point.ts +++ b/backend/src/model/point.ts @@ -4,8 +4,9 @@ type toNormalizationFunction = () => PointType; type mapFunction = () => PointType[]; export type PointDocument = MONGOOSE.Document & { - amount: number; userID: string; + amount: number; + type: string; toNormalization: toNormalizationFunction; date: string; }; @@ -13,15 +14,17 @@ export type PointDocument = MONGOOSE.Document & { export type PointType = { id: string; userID: string; + type: string; amount: number; date: string; }; const pointSchema = new MONGOOSE.Schema( { - amount: { type: Number, required: true }, date: { type: Date, default: Date.now }, - userID: {type: String, required: true} + userID: { type: String, required: true }, + type: { type: String, required: true}, + amount: { type: Number, required: true }, }, { timestamps: true } ); @@ -32,6 +35,7 @@ const toNormalization: toNormalizationFunction = function () { const PointObject: PointType = { id: _userObject._id.toString(), userID: _userObject.userID, + type: _userObject.type, amount: _userObject.amount, date: _userObject.date, }; diff --git a/backend/src/router/user.ts b/backend/src/router/user.ts index 41c359f..4c54e1e 100644 --- a/backend/src/router/user.ts +++ b/backend/src/router/user.ts @@ -13,7 +13,7 @@ class UserRouter { public static points: Spec = { method: HELPER.methods.POST || HELPER.methods.PUT, - path: "/user/:id/points/", + path: "/user/:id/points/:type", validate: { continueOnError: true, type: HELPER.contentType.JSON, From cc342cf630aa1a8a34516883ade7f5cdde11f41f Mon Sep 17 00:00:00 2001 From: Austin M Date: Sat, 30 May 2020 14:34:29 -0700 Subject: [PATCH 2/3] Bot handles point type --- bot/src/utils/points_handler.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bot/src/utils/points_handler.ts b/bot/src/utils/points_handler.ts index e8eb3e2..0da34d7 100644 --- a/bot/src/utils/points_handler.ts +++ b/bot/src/utils/points_handler.ts @@ -3,7 +3,6 @@ import { config } from "../config/config"; import { UserType } from "../types" import * as jwt from "jsonwebtoken"; import axios from "axios"; -import { resolve } from "path"; const API_URL = config.apiUrl; @@ -30,7 +29,7 @@ export class PointsHandler { } else { // Give points based off of location if(!config.debug){ - this.givePoints(message.author, this.getMultiplier(message)).catch(err => console.log(err)); + this.givePoints(message.author, this.getMultiplier(message), 'message').catch(err => console.log(err)); } } } @@ -92,11 +91,11 @@ export class PointsHandler { } } - private async updateUser(user: UserType): Promise { + private async updateUser(user: UserType, type: string): Promise { const token = this.getToken(user.id); try { - const resp = await axios(config.apiUrl + '/user/' + user.id + '/points', { method: "POST", data: user, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } }); + const resp = await axios(config.apiUrl + '/user/' + user.id + '/points/' + type, { method: "POST", data: user, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } }); return resp.data.data; } catch (e) { if (e && e.response) console.error(e.response.data); @@ -133,7 +132,7 @@ export class PointsHandler { if (thankeesArray.length != 0){ thankees.forEach(thankee => { - this.givePoints(thankee.user, config.multipliers['thanks']).catch(err => console.log(err)); + this.givePoints(thankee.user, config.multipliers['thanks'], 'thanks').catch(err => console.log(err)); }) @@ -166,11 +165,11 @@ export class PointsHandler { } } - private async givePoints(u: User, amount: number): Promise { + private async givePoints(u: User, amount: number, type: string): Promise { const user = await this.getUser(u); if (user) { user.totalPoints = amount; - this.updateUser(user); + this.updateUser(user, type); } else { console.log("User is undefined"); } From 94c1af022733db6c7656b60d9fdefefd4c2fa929 Mon Sep 17 00:00:00 2001 From: Austin M Date: Sat, 30 May 2020 14:35:23 -0700 Subject: [PATCH 3/3] Bot script for test db --- bot/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/package.json b/bot/package.json index 7bdf9a7..632daf5 100644 --- a/bot/package.json +++ b/bot/package.json @@ -5,6 +5,7 @@ "license": "MIT", "scripts": { "dev": "ts-node-dev ./src/server.ts", + "dev-db": "NODE_ENV=db ts-node-dev ./src/server.ts", "build": "tsc", "watch": "tsc -w", "serve": "node ./dist/server.js"