Fix status fields error when not logged in

- Fix userVote, joined, deletable
pull/5/head
trivernis 5 years ago
parent e0dd9b270f
commit 0ff6791c19

@ -68,7 +68,11 @@ export class Event extends Model<Event> {
*/
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;
if (userId) {
const participants = await this.$get("rParticipants", {where: {id: userId}}) as User[];
return participants.length !== 0;
} else {
return false;
}
}
}

@ -129,7 +129,11 @@ export class Group extends Model<Group> {
*/
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;
if (userId) {
const members = await this.$get("rMembers", {where: {id: userId}}) as User[];
return members.length !== 0;
} else {
return false;
}
}
}

@ -137,8 +137,12 @@ export class Post extends Model<Post> {
*/
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;
if (userId) {
const votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
return votes[0]?.PostVote?.voteType;
} else {
return undefined;
}
}
/**
@ -149,7 +153,7 @@ export class Post extends Model<Post> {
public async deletable({userId}: {userId: number}, request: any): Promise<boolean> {
userId = userId ?? request.session.userId;
const isAuthor = Number(userId) === Number(this.authorId);
if (!isAuthor) {
if (userId && !isAuthor) {
return (await User.findOne({where: {id: userId}})).isAdmin;
}
return isAuthor;

Loading…
Cancel
Save