From 6fda95f735f558b115da7aaba6d3ecf310fbcc8c Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 25 Jan 2020 12:47:32 +0100 Subject: [PATCH 1/2] Change user level only updating when points change --- src/lib/models/User.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/models/User.ts b/src/lib/models/User.ts index 87b3f9c..dd4dfcf 100644 --- a/src/lib/models/User.ts +++ b/src/lib/models/User.ts @@ -1,5 +1,6 @@ import * as sqz from "sequelize"; import { + BeforeCreate, BeforeUpdate, BelongsTo, BelongsToMany, @@ -42,11 +43,14 @@ export class User extends Model { * It assigns the corresponding level to the user * @param instance */ + @BeforeCreate @BeforeUpdate public static async assignLevel(instance: User) { - const level = await Level.findOne({where: {points: {[sqz.Op.lte]: instance.rankpoints}}, order: [["points", "desc"]]}) as Level; - if (level) { - instance.$set("rLevel", level); + if (instance.changed("rankpoints") || instance.isNewRecord) { + const level = await Level.findOne({where: {points: {[sqz.Op.lte]: instance.rankpoints}}, order: [["points", "desc"]]}) as Level; + if (level) { + instance.$set("rLevel", level); + } } } From d10ea0e4a40269180add97acde30dfea23bcca56 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 27 Jan 2020 15:05:14 +0100 Subject: [PATCH 2/2] Add getReportReasons graphql resolver --- src/routes/graphql/QueryResolver.ts | 9 ++++++++- src/routes/graphql/schema.graphql | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/routes/graphql/QueryResolver.ts b/src/routes/graphql/QueryResolver.ts index 3a7c374..e5bd3ee 100644 --- a/src/routes/graphql/QueryResolver.ts +++ b/src/routes/graphql/QueryResolver.ts @@ -15,7 +15,7 @@ import { Group, Level, Post, - Report, + Report, ReportReason, Request, User } from "../../lib/models"; @@ -212,6 +212,13 @@ export class QueryResolver extends MutationResolver { return Report.findAll({limit: first, offset, order: [["id", "DESC"]]}); } + /** + * Returns all report reasons + */ + public async getReportReasons(): Promise { + return ReportReason.findAll(); + } + /** * Returns the levels that are configured * @param first diff --git a/src/routes/graphql/schema.graphql b/src/routes/graphql/schema.graphql index 4ddf7be..d082f6d 100644 --- a/src/routes/graphql/schema.graphql +++ b/src/routes/graphql/schema.graphql @@ -35,7 +35,7 @@ type Query { getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post!]! @complexity(value: 1, multipliers: ["first"]) "returns all activities" - getActivities: [Activity] + getActivities: [Activity!]! "Returns an access token for the user that can be used in requests. To user the token in requests, it has to be set in the HTTP header 'Authorization' with the format Bearer ." getToken(email: String!, passwordHash: String!): Token! @@ -49,6 +49,9 @@ type Query { "Returns all issued reports with pagination" getReports(first: Int = 20, offset: Int = 0): [Report!]! @complexity(value: 1, multipliers: ["first"]) + "Returns all report reasons" + getReportReasons: [ReportReason!]! + "Returns the levels configured in the backend" getLevels(first: Int =20, offset: Int = 0): [Level!]! @complexity(value: 1, multipliers: ["first"]) }