API additions

- implemented event creation
- added id to requests
- added getGroup, getRequest, getEvent by id to api
pull/2/head
Trivernis 5 years ago
parent 82d0e1bc59
commit 78ac194d37

@ -49,6 +49,22 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No chatId given."); 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() { acceptCookies() {
req.session.cookiesAccepted = true; req.session.cookiesAccepted = true;
return true; return true;
@ -307,7 +323,6 @@ export function resolver(req: any, res: any): any {
res.status(status.FORBIDDEN); res.status(status.FORBIDDEN);
return new GraphQLError("You are not allowed to remove a creator as an admin."); return new GraphQLError("You are not allowed to remove a creator as an admin.");
} }
try { try {
return await dataaccess return await dataaccess
.changeGroupMembership(groupId, userId, dataaccess.MembershipChangeAction.DEOP); .changeGroupMembership(groupId, userId, dataaccess.MembershipChangeAction.DEOP);
@ -315,7 +330,15 @@ export function resolver(req: any, res: any): any {
res.status(status.BAD_REQUEST); res.status(status.BAD_REQUEST);
return err.graphqlError; 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<models.Event>("rEvents", {name, dueDate});
} else { } else {
res.status(status.UNAUTHORIZED); res.status(status.UNAUTHORIZED);
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();

@ -11,6 +11,12 @@ type Query {
"returns the chat object for a chat id" "returns the chat object for a chat id"
getChat(chatId: ID!): ChatRoom 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" "find a post by the posted date or content"
findPost(first: Int, offset: Int, text: String!, postedDate: String): [Post] findPost(first: Int, offset: Int, text: String!, postedDate: String): [Post]
@ -228,6 +234,10 @@ type Post {
"represents a request of any type" "represents a request of any type"
type Request { type Request {
"Id of the request."
id: ID!
"Id of the user who sended the request" "Id of the user who sended the request"
sender: User! sender: User!

@ -5,6 +5,7 @@ import {ChatNotFoundError} from "./errors/ChatNotFoundError";
import {EmailAlreadyRegisteredError} from "./errors/EmailAlreadyRegisteredError"; import {EmailAlreadyRegisteredError} from "./errors/EmailAlreadyRegisteredError";
import {GroupNotFoundGqlError, NotLoggedInGqlError} from "./errors/graphqlErrors"; import {GroupNotFoundGqlError, NotLoggedInGqlError} from "./errors/graphqlErrors";
import {GroupNotFoundError} from "./errors/GroupNotFoundError"; import {GroupNotFoundError} from "./errors/GroupNotFoundError";
import {InvalidLoginError} from "./errors/InvalidLoginError";
import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError"; import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError";
import {UserNotFoundError} from "./errors/UserNotFoundError"; import {UserNotFoundError} from "./errors/UserNotFoundError";
import globals from "./globals"; import globals from "./globals";
@ -75,9 +76,13 @@ namespace dataaccess {
const hash = crypto.createHash("sha512"); const hash = crypto.createHash("sha512");
hash.update(password); hash.update(password);
password = hash.digest("hex"); password = hash.digest("hex");
const user = await models.User.findOne({where: {email, password}}); const user = await models.User.findOne({where: {email}});
if (user) { if (user) {
if (user.password === password) {
return user; return user;
} else {
throw new InvalidLoginError(email);
}
} else { } else {
throw new UserNotFoundError(email); throw new UserNotFoundError(email);
} }

@ -0,0 +1,7 @@
import {BaseError} from "./BaseError";
export class InvalidLoginError extends BaseError {
constructor(email: (string)) {
super(`Invalid login data for ${email}.`);
}
}
Loading…
Cancel
Save