From 78ac194d37e03c494ddc8f1a66287902bec28207 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Wed, 16 Oct 2019 17:08:38 +0200 Subject: [PATCH] API additions - implemented event creation - added id to requests - added getGroup, getRequest, getEvent by id to api --- src/graphql/resolvers.ts | 27 +++++++++++++++++++++++++-- src/graphql/schema.graphql | 10 ++++++++++ src/lib/dataaccess.ts | 9 +++++++-- src/lib/errors/InvalidLoginError.ts | 7 +++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/lib/errors/InvalidLoginError.ts diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts index 1898212..fcae8ad 100644 --- a/src/graphql/resolvers.ts +++ b/src/graphql/resolvers.ts @@ -49,6 +49,22 @@ export function resolver(req: any, res: any): any { return new GraphQLError("No chatId given."); } }, + async getGroup({groupId}: {groupId: number}) { + if (groupId) { + return models.Group.findByPk(groupId); + } else { + res.status(status.BAD_REQUEST); + return new GraphQLError("No group id given."); + } + }, + async getRequest({requestId}: {requestId: number}) { + if (requestId) { + return models.Request.findByPk(requestId); + } else { + res.status(status.BAD_REQUEST); + return new GraphQLError("No requestId given."); + } + }, acceptCookies() { req.session.cookiesAccepted = true; return true; @@ -307,7 +323,6 @@ export function resolver(req: any, res: any): any { res.status(status.FORBIDDEN); return new GraphQLError("You are not allowed to remove a creator as an admin."); } - try { return await dataaccess .changeGroupMembership(groupId, userId, dataaccess.MembershipChangeAction.DEOP); @@ -315,7 +330,15 @@ export function resolver(req: any, res: any): any { res.status(status.BAD_REQUEST); return err.graphqlError; } - + } else { + res.status(status.UNAUTHORIZED); + return new NotLoggedInGqlError(); + } + }, + async createEvent({name, dueDate, groupId}: {name: string, dueDate: Date, groupId: number}) { + if (req.session.userId) { + const group = await models.Group.findByPk(groupId); + const event = await group.$create("rEvents", {name, dueDate}); } else { res.status(status.UNAUTHORIZED); return new NotLoggedInGqlError(); diff --git a/src/graphql/schema.graphql b/src/graphql/schema.graphql index 2ca3ac2..66f0ed5 100644 --- a/src/graphql/schema.graphql +++ b/src/graphql/schema.graphql @@ -11,6 +11,12 @@ type Query { "returns the chat object for a chat id" getChat(chatId: ID!): ChatRoom + "return shte group object for its id" + getGroup(groupId: ID!): Group + + "returns the request object for its id" + getRequest(requestId: ID!): Request + "find a post by the posted date or content" findPost(first: Int, offset: Int, text: String!, postedDate: String): [Post] @@ -228,6 +234,10 @@ type Post { "represents a request of any type" type Request { + + "Id of the request." + id: ID! + "Id of the user who sended the request" sender: User! diff --git a/src/lib/dataaccess.ts b/src/lib/dataaccess.ts index 839a75d..3c3862f 100644 --- a/src/lib/dataaccess.ts +++ b/src/lib/dataaccess.ts @@ -5,6 +5,7 @@ import {ChatNotFoundError} from "./errors/ChatNotFoundError"; import {EmailAlreadyRegisteredError} from "./errors/EmailAlreadyRegisteredError"; import {GroupNotFoundGqlError, NotLoggedInGqlError} from "./errors/graphqlErrors"; import {GroupNotFoundError} from "./errors/GroupNotFoundError"; +import {InvalidLoginError} from "./errors/InvalidLoginError"; import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError"; import {UserNotFoundError} from "./errors/UserNotFoundError"; import globals from "./globals"; @@ -75,9 +76,13 @@ namespace dataaccess { const hash = crypto.createHash("sha512"); hash.update(password); password = hash.digest("hex"); - const user = await models.User.findOne({where: {email, password}}); + const user = await models.User.findOne({where: {email}}); if (user) { - return user; + if (user.password === password) { + return user; + } else { + throw new InvalidLoginError(email); + } } else { throw new UserNotFoundError(email); } diff --git a/src/lib/errors/InvalidLoginError.ts b/src/lib/errors/InvalidLoginError.ts new file mode 100644 index 0000000..a58991b --- /dev/null +++ b/src/lib/errors/InvalidLoginError.ts @@ -0,0 +1,7 @@ +import {BaseError} from "./BaseError"; + +export class InvalidLoginError extends BaseError { + constructor(email: (string)) { + super(`Invalid login data for ${email}.`); + } +}