Skip to content

Commit

Permalink
fix(Backend): Resolved syntax bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
talhabalaj committed Mar 24, 2022
1 parent e22218a commit aeefa6e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 52 deletions.
1 change: 0 additions & 1 deletion apps/api/src/global-types/classroom-role.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export enum ClassroomRole {
OWNER = 'OWNER',
STUDENT = 'STUDENT',
INSTRUCTOR = 'INSTRUCTOR',
TA = 'TA',
}

registerEnumType(ClassroomRole, {
Expand Down
62 changes: 26 additions & 36 deletions apps/api/src/modules/classroom/classroom.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ClassroomInviteCodeGenError extends Error {

@Injectable()
export default class ClassroomService {
constructor(private prismaService: PrismaService) { }
constructor(private prismaService: PrismaService) {}

async getUserClassrooms(user: User) {
return this.prismaService.classroom.findMany({
Expand All @@ -32,7 +32,7 @@ export default class ClassroomService {

getMemberInAction(classroomId: string, userId: string) {
return this.prismaService.classroomMember.findUnique({
where: { classroomId, userId },
where: { classroomId_userId: { classroomId, userId } },
})
}

Expand Down Expand Up @@ -125,63 +125,51 @@ export default class ClassroomService {
classroomId: string,
userId: string,
memberId: string,
role: ClassroomRole) {

role: ClassroomRole
) {
//get user from the classroom
const memberInAction = this.getMemberInAction(classroomId, userId)
const memberInAction = await this.getMemberInAction(classroomId, userId)

//check if user exists in the classroom
if (!memberInAction) {
throw new UserInputError(
'You are not a member of this classroom'
)
throw new UserInputError('You are not a member of this classroom')
}

//check if user is owner of the classroom
if (memberInAction.classroomRole !== ClassroomRole.OWNER) {
throw new UserInputError(
'You are not the owner of this classroom'
)
throw new UserInputError('You are not the owner of this classroom')
}

//get member from the classroom
const ifMemberExist = await this.prismaService.classroomMember.findUnique({
where: { classroomId, userId: memberId },
where: { classroomId_userId: { classroomId, userId: memberId } },
})

//check if member exists in the classroom
if (!ifMemberExist) {
throw new UserInputError(
'This user is not a member of this classroom'
)
throw new UserInputError('This user is not a member of this classroom')
}

//update member role
return this.prismaService.classroomMember.update({
where: { classroomId, userId },
data: { classroomRole: role },
where: { classroomId_userId: { classroomId, userId } },
data: { classroomRole: { set: role } },
})
}

async removeMember(
classroomId: string,
userId: string,
memberId: string) {

async removeMember(classroomId: string, userId: string, memberId: string) {
//get member who tries to remove someone from the classroom
const memberInAction = this.getMemberInAction(classroomId, userId)
const memberInAction = await this.getMemberInAction(classroomId, userId)

//check if member exists in the classroom
if (!memberInAction) {
throw new UserInputError(
'You are not a member of this classroom'
)
throw new UserInputError('You are not a member of this classroom')
}

//check if any member tries to remove himself
if (memberInAction.userId === memberId) {
return this.prismaService.classroomMember.delete({
where: { classroomId, userId: memberId },
where: { classroomId_userId: { classroomId, userId: memberId } },
})
}

Expand All @@ -195,37 +183,39 @@ export default class ClassroomService {

//get member who is being removed from the classroom
const memberToRemove = await this.prismaService.classroomMember.findUnique({
where: { classroomId, userId: memberId },
where: { classroomId_userId: { classroomId, userId: memberId } },
})

//check if memberToRemove exists in the classroom
if (!memberToRemove) {
throw new UserInputError(
'This user is not a member of this classroom'
)
throw new UserInputError('This user is not a member of this classroom')
}

//check if instructor tries to remove owner
//instructor is not allowed to remove owner
if (memberToRemove.classroomRole === ClassroomRole.OWNER
&& memberInAction.classroomRole === ClassroomRole.INSTRUCTOR) {
if (
memberToRemove.classroomRole === ClassroomRole.OWNER &&
memberInAction.classroomRole === ClassroomRole.INSTRUCTOR
) {
throw new UserInputError(
'You are not allowed to remove the owner of the classroom'
)
}

//check if instructor tries to remove another instructor
//instructor is not allowed to remove another instructor
if (memberToRemove.classroomRole === ClassroomRole.INSTRUCTOR
&& memberInAction.classroomRole === ClassroomRole.INSTRUCTOR) {
if (
memberToRemove.classroomRole === ClassroomRole.INSTRUCTOR &&
memberInAction.classroomRole === ClassroomRole.INSTRUCTOR
) {
throw new UserInputError(
'You are not allowed to remove any instructor of the classroom'
)
}

//remove member from the classroom
return this.prismaService.classroomMember.delete({
where: { classroomId, userId: memberId },
where: { classroomId_userId: { classroomId, userId: memberId } },
})
}

Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/modules/competition/competition.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ModelsModule } from '@aelp-app/models'
import { Module } from '@nestjs/common'
import CompetitionResolver from './competition.resolver'
import CompetitionService from './competition.service'

@Module({
imports: [ModelsModule],
providers: [
CompetitionResolver,
CompetitionService
],
})
Expand Down
21 changes: 13 additions & 8 deletions apps/api/src/modules/competition/competition.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import { User } from '../user/types/user.model'

@Injectable()
export default class CompetitionService {
constructor(private prismaService: PrismaService) { }
constructor(private prismaService: PrismaService) {}

async createCompetition(data: CreateCompetitionInput, user: User) {

return this.prismaService.competition.create({
data: {
...data,
isPrivate: data.isPrivate,
assessment: { connect: { id: data.assessmentId } },
creatorUser: { connect: { id: user.id } },
},
})
}

async updateCompetition(id: string, data: CreateCompetitionInput, user: User) {
async updateCompetition(
id: string,
data: CreateCompetitionInput,
user: User
) {
const competition = await this.prismaService.competition.findUnique({
where: { id },
})
Expand All @@ -29,7 +33,7 @@ export default class CompetitionService {

return this.prismaService.competition.update({
where: { id },
data: { data },
data,
})
}

Expand Down Expand Up @@ -75,8 +79,10 @@ export default class CompetitionService {

return this.prismaService.competitionParticipant.delete({
where: {
competitionId: competition.id,
userId: user.id,
competitionId_userId: {
userId: user.id,
competitionId: competition.id,
},
},
})
}
Expand All @@ -94,5 +100,4 @@ export default class CompetitionService {
},
})
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { InputType, Field } from '@nestjs/graphql'
import { Assessment } from '../../assessment/types/assessment.model'

@InputType()
export class CreateCompetitionInput {
@Field()
isPrivate!: string
@Field(() => Boolean)
isPrivate!: boolean

@Field({ nullable: true })
assessment?: Assessment
@Field(() => String, { nullable: true })
assessmentId?: string
}

0 comments on commit aeefa6e

Please sign in to comment.