Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Args, Context, Int, Mutation, Query, Resolver } from '@nestjs/graphql'
import { CollaboratorStatus, CollaboratorRole } from '@prisma/client'
import { AuthenticatedRequest } from '@libs/auth'
import { IDValidationPipe } from '@libs/pipe'
import { CollaboratorService } from './collaborator.service'
import {
CollaboratorInput,
CollaboratorUpdateInput
} from './model/collaborator.input'

@Resolver()
export class CollaboratorResolver {
constructor(private readonly collaboratorService: CollaboratorService) {}

@Mutation()
async inviteCollaborator(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number,
@Args('input') input: CollaboratorInput
) {
return await this.collaboratorService.inviteCollaborator(
req.user.id,
polygonId,
input
)
}

@Query()
async getActiveCollaborator(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number
) {
return await this.collaboratorService.getCollaboratorsByStatus(
req.user.id,
polygonId,
CollaboratorStatus.Active
)
}

@Query()
async getPendingCollaborator(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number
) {
return await this.collaboratorService.getCollaboratorsByStatus(
req.user.id,
polygonId,
CollaboratorStatus.Pending
)
}

@Mutation()
async approveInvite(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number,
@Args('userId', { type: () => Int }, IDValidationPipe) userId: number
) {
return await this.collaboratorService.approveCollaborator(
req.user.id,
polygonId,
userId
)
}

@Mutation()
async rejectInvite(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number,
@Args('userId', { type: () => Int }, IDValidationPipe) userId: number
) {
return await this.collaboratorService.rejectCollaborator(
req.user.id,
polygonId,
userId
)
}

@Mutation()
async updateCollaboratorRole(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number,
@Args('input') input: CollaboratorUpdateInput
) {
return await this.collaboratorService.updateCollaboratorRole(
req.user.id,
polygonId,
input
)
}

@Mutation()
async removeCollaborator(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number,
@Args('userId', { type: () => Int }, IDValidationPipe) userId: number
) {
return await this.collaboratorService.removeCollaborator(
req.user.id,
polygonId,
userId
)
}

@Mutation()
async requestCollaboration(
@Context('req') req: AuthenticatedRequest,
@Args('polygonId', { type: () => Int }, IDValidationPipe) polygonId: number,
@Args('role') role: CollaboratorRole
) {
return await this.collaboratorService.requestCollaboration(
req.user.id,
polygonId,
role
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import { Test, type TestingModule } from '@nestjs/testing'
import { CollaboratorRole, CollaboratorStatus } from '@generated'
import { expect } from 'chai'
import { stub } from 'sinon'
import { ForbiddenAccessException } from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import { CollaboratorService } from './collaborator.service'

const exampleOwner = {
id: 1,
username: 'owner',
email: 'owner@test.com'
}

const exampleUser = {
id: 2,
username: 'invitee',
email: 'invitee@test.com'
}

const exampleEditorUser = {
id: 3,
username: 'editor',
email: 'editor@test.com'
}

const exampleViewerUser = {
id: 4,
username: 'viewer',
email: 'viewer@test.com'
}

const examplePendingUser = {
id: 5,
username: 'pending',
email: 'pending@test.com'
}

const exampleViewerCollaborator = {
id: 1,
problemId: 10,
userId: exampleViewerUser.id,
role: CollaboratorRole.Reviewer,
status: CollaboratorStatus.Active
}

const exampleEditorCollaborator = {
id: 2,
problemId: 10,
userId: exampleEditorUser.id,
role: CollaboratorRole.Editor,
status: CollaboratorStatus.Active
}

const examplePendingCollaborator = {
id: 3,
problemId: 10,
userId: examplePendingUser.id,
role: CollaboratorRole.Reviewer,
status: CollaboratorStatus.Pending
}

const exampleCollaboratorList = [
exampleViewerCollaborator,
exampleEditorCollaborator,
examplePendingCollaborator
]

const exampleProblem = {
id: 10,
createdById: exampleOwner.id,
polygonCollaborators: exampleCollaboratorList
}

const exampleCollaboratorListByStatus = [
{
role: exampleViewerCollaborator.role,
user: {
id: exampleViewerUser.id,
username: exampleViewerUser.username,
email: exampleViewerUser.email
}
},
{
role: exampleEditorCollaborator.role,
user: {
id: exampleEditorUser.id,
username: exampleEditorUser.username,
email: exampleEditorUser.email
}
}
]

const db = {
user: {
findUnique: stub()
},
polygonProblem: {
findUnique: stub()
},
polygonCollaborator: {
findFirst: stub(),
findMany: stub(),
create: stub(),
update: stub(),
delete: stub()
}
}

describe('CollaboratorService', () => {
let service: CollaboratorService

beforeEach(async () => {
db.user.findUnique.reset()
db.polygonProblem.findUnique.reset()
db.polygonCollaborator.findFirst.reset()
db.polygonCollaborator.findMany.reset()
db.polygonCollaborator.create.reset()
db.polygonCollaborator.update.reset()
db.polygonCollaborator.delete.reset()
const module: TestingModule = await Test.createTestingModule({
providers: [CollaboratorService, { provide: PrismaService, useValue: db }]
}).compile()

service = module.get<CollaboratorService>(CollaboratorService)
})

it('should be defined', () => {
expect(service).to.be.ok
})

describe('inviteCollaborator', () => {
it('owner can invite collaborator', async () => {
db.user.findUnique.resolves({ id: exampleUser.id })
db.polygonProblem.findUnique.resolves({
createdById: exampleProblem.createdById
})

db.polygonCollaborator.findFirst.resolves(null)
db.polygonCollaborator.create.resolves(exampleViewerCollaborator)

const result = await service.inviteCollaborator(
exampleOwner.id,
exampleProblem.id,
{
userEmail: exampleUser.email,
role: CollaboratorRole.Reviewer
}
)
expect(result).to.deep.equal(exampleViewerCollaborator)
})

it('Viewer cannot invite collaborator', async () => {
db.user.findUnique.resolves({ id: exampleUser.id })
db.polygonProblem.findUnique.resolves({
createdById: exampleProblem.createdById
})
db.polygonCollaborator.findFirst.resolves(exampleViewerCollaborator)

await expect(
service.inviteCollaborator(exampleViewerUser.id, exampleProblem.id, {
userEmail: exampleUser.email,
role: CollaboratorRole.Reviewer
})
).to.be.rejectedWith(ForbiddenAccessException)
})
})

describe('getCollaboratorsByStatus', () => {
it('return active collaborators', async () => {
db.polygonProblem.findUnique.resolves({
createdById: exampleProblem.createdById
})
db.polygonCollaborator.findMany.resolves(exampleCollaboratorListByStatus)

const result = await service.getCollaboratorsByStatus(
exampleOwner.id,
exampleProblem.id,
CollaboratorStatus.Active
)

expect(result).to.deep.equal([
{
id: exampleViewerUser.id,
username: exampleViewerUser.username,
email: exampleViewerUser.email,
role: exampleViewerCollaborator.role
},
{
id: exampleEditorUser.id,
username: exampleEditorUser.username,
email: exampleEditorUser.email,
role: exampleEditorCollaborator.role
}
])
})

it('returns pending collaborators', async () => {
db.polygonProblem.findUnique.resolves({
createdById: exampleProblem.createdById
})
db.polygonCollaborator.findMany.resolves([
{
role: examplePendingCollaborator.role,
user: {
id: examplePendingUser.id,
username: examplePendingUser.username,
email: examplePendingUser.email
}
}
])

const result = await service.getCollaboratorsByStatus(
exampleOwner.id,
exampleProblem.id,
CollaboratorStatus.Pending
)

expect(result).to.deep.equal([
{
id: examplePendingUser.id,
username: examplePendingUser.username,
email: examplePendingUser.email,
role: examplePendingCollaborator.role
}
])
})
})
describe('updateCollaboratorRole', () => {
it('owner updates collaborator role', async () => {
db.polygonProblem.findUnique.resolves({
createdById: exampleProblem.createdById
})
db.polygonCollaborator.findFirst.resolves({
id: exampleViewerCollaborator.id,
status: exampleViewerCollaborator.status
})
const updatedCollaborator = {
...exampleViewerCollaborator,
role: CollaboratorRole.Editor
}
db.polygonCollaborator.update.resolves(updatedCollaborator)

const result = await service.updateCollaboratorRole(
exampleOwner.id,
exampleProblem.id,
{ userId: exampleViewerUser.id, role: CollaboratorRole.Editor }
)

expect(result).to.deep.equal(updatedCollaborator)
})
it('Editor cannot update collaborator role', async () => {
db.polygonProblem.findUnique.resolves({
createdById: exampleProblem.createdById
})

await expect(
service.updateCollaboratorRole(
exampleEditorUser.id,
exampleProblem.id,
{ userId: exampleViewerUser.id, role: CollaboratorRole.Editor }
)
).to.be.rejectedWith(ForbiddenAccessException)
})
})
})
Loading
Loading