Make every userId in status fields optional

- Make userId in joined on Group and Event optional
- Make userId and deletable in userVote on Post optional
pull/5/head
trivernis 5 years ago
parent 0fbc7f68a6
commit e0dd9b270f

@ -304,10 +304,10 @@ type Post {
createdAt: String!
"the type of vote the user performed on the post"
userVote(userId: ID!): VoteType
userVote(userId: ID): VoteType
"if the post can be deleted by the specified user"
deletable(userId: ID!): Boolean!
deletable(userId: ID): Boolean!
"the activity that belongs to the post"
activity: Activity
@ -387,7 +387,7 @@ type Group {
events(first: Int=10, offset: Int=0): [Event!]!
"If the user with the specified id has joined the group"
joined(userId: Int!): Boolean!
joined(userId: Int): Boolean!
}
type Event {
@ -407,7 +407,7 @@ type Event {
participants(first: Int=10, offset: Int=0): [User!]!
"Returns if the user with the specified id has joined the event"
joined(userId: Int!): Boolean
joined(userId: Int): Boolean
}
"respresents an access token entry with the value as the acutal token and expires as the date the token expires."

@ -64,8 +64,10 @@ export class Event extends Model<Event> {
/**
* Returns if the specified user has joined the event
* @param userId
* @param request
*/
public async joined({userId}: {userId: number}): Promise<boolean> {
public async joined({userId}: {userId: number}, request: any): Promise<boolean> {
userId = userId ?? request.session.userId;
const participants = await this.$get("rParticipants", {where: {id: userId}}) as User[];
return participants.length !== 0;
}

@ -125,8 +125,10 @@ export class Group extends Model<Group> {
/**
* Returns if a user has joined the group
* @param userId
* @param request
*/
public async joined({userId}: {userId: number}): Promise<boolean> {
public async joined({userId}: {userId: number}, request: any): Promise<boolean> {
userId = userId ?? request.session.userId;
const members = await this.$get("rMembers", {where: {id: userId}}) as User[];
return members.length !== 0;
}

@ -1,5 +1,6 @@
import * as sqz from "sequelize";
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
import globals from "../globals";
import markdown from "../markdown";
import {Activity} from "./Activity";
import {PostVote, VoteType} from "./PostVote";
@ -132,8 +133,10 @@ export class Post extends Model<Post> {
/**
* Returns the type of vote that was performed on the post by the user specified by the user id.
* @param userId
* @param request
*/
public async userVote({userId}: {userId: number}): Promise<VoteType> {
public async userVote({userId}: {userId: number}, request: any): Promise<VoteType> {
userId = userId ?? request.session.userId;
const votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
return votes[0]?.PostVote?.voteType;
}
@ -141,9 +144,10 @@ export class Post extends Model<Post> {
/**
* Returns if the post can be deleted by the user with the given id.
* @param userId
* @param request
*/
public async deletable({userId}: {userId: number}): Promise<boolean> {
public async deletable({userId}: {userId: number}, request: any): Promise<boolean> {
userId = userId ?? request.session.userId;
const isAuthor = Number(userId) === Number(this.authorId);
if (!isAuthor) {
return (await User.findOne({where: {id: userId}})).isAdmin;

Loading…
Cancel
Save