Skip to content

Commit

Permalink
Merge pull request #9 from devcord/point-type
Browse files Browse the repository at this point in the history
Point type
  • Loading branch information
austinmccalley authored Jun 7, 2020
2 parents 0447e62 + 94c1af0 commit c1c8c4e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions backend/src/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ class UserController {

const body: InputUpdateBodyType = ctx.request.body;



const user: UserDocument | null = await UserModel.findOneAndUpdate({
id: body.id,
}, {
Expand All @@ -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();
Expand Down
10 changes: 7 additions & 3 deletions backend/src/model/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ type toNormalizationFunction = () => PointType;
type mapFunction = () => PointType[];

export type PointDocument = MONGOOSE.Document & {
amount: number;
userID: string;
amount: number;
type: string;
toNormalization: toNormalizationFunction;
date: string;
};

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 }
);
Expand All @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion backend/src/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 6 additions & 7 deletions bot/src/utils/points_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
}
Expand Down Expand Up @@ -92,11 +91,11 @@ export class PointsHandler {
}
}

private async updateUser(user: UserType): Promise<UserType> {
private async updateUser(user: UserType, type: string): Promise<UserType> {
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);
Expand Down Expand Up @@ -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));
})


Expand Down Expand Up @@ -166,11 +165,11 @@ export class PointsHandler {
}
}

private async givePoints(u: User, amount: number): Promise<void> {
private async givePoints(u: User, amount: number, type: string): Promise<void> {
const user = await this.getUser(u);
if (user) {
user.totalPoints = amount;
this.updateUser(user);
this.updateUser(user, type);
} else {
console.log("User is undefined");
}
Expand Down

0 comments on commit c1c8c4e

Please sign in to comment.