Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alessandro/web 2358 filter discoverable workspaces by join request #3773

Merged
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
15 changes: 14 additions & 1 deletion packages/server/modules/workspaces/repositories/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Workspace,
WorkspaceAcl,
WorkspaceDomain,
WorkspaceJoinRequest,
WorkspaceWithOptionalRole
} from '@/modules/workspacesCore/domain/types'
import {
Expand Down Expand Up @@ -67,7 +68,9 @@ const tables = {
workspaceDomains: (db: Knex) => db<WorkspaceDomain>('workspace_domains'),
workspacesAcl: (db: Knex) => db<WorkspaceAcl>('workspace_acl'),
workspaceCreationState: (db: Knex) =>
db<WorkspaceCreationState>('workspace_creation_state')
db<WorkspaceCreationState>('workspace_creation_state'),
workspaceJoinRequests: (db: Knex) =>
db<WorkspaceJoinRequest>('workspace_join_requests')
}

export const getUserDiscoverableWorkspacesFactory =
Expand All @@ -93,6 +96,16 @@ export const getUserDiscoverableWorkspacesFactory =
'acl.workspaceId',
'workspaces.id'
)
.leftJoin(
tables
.workspaceJoinRequests(db)
.select('*')
.where({ userId })
.as('joinRequest'),
'joinRequest.workspaceId',
'workspaces.id'
)
.whereNull('joinRequest.workspaceId')
.whereIn('domain', domains)
.where('discoverabilityEnabled', true)
.where('verified', true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from '@/modules/core/repositories/streams'
import { omit } from 'lodash'
import { createAndStoreTestWorkspaceFactory } from '@/test/speckle-helpers/workspaces'
import { WorkspaceJoinRequests } from '@/modules/workspacesCore/helpers/db'

const getWorkspace = getWorkspaceFactory({ db })
const getWorkspaceBySlug = getWorkspaceBySlugFactory({ db })
Expand Down Expand Up @@ -771,6 +772,85 @@ describe('Workspace repositories', () => {

expect(workspaces.length).to.equal(1)
})

it('should not return discoverable workspaces with existing requests for the user', async () => {
const user = await createAndStoreTestUser()
await updateUserEmail({
query: {
email: user.email
},
update: {
verified: true
}
})
const otherUser = await createAndStoreTestUser()
await updateUserEmail({
query: {
email: otherUser.email
},
update: {
verified: true
}
})

const workspace = await createAndStoreTestWorkspace({
discoverabilityEnabled: true
})
await storeWorkspaceDomain({
workspaceDomain: {
id: cryptoRandomString({ length: 6 }),
domain: 'example.org',
workspaceId: workspace.id,
verified: true,
createdAt: new Date(),
updatedAt: new Date(),
createdByUserId: user.id
}
})
// existing request for other user
await db(WorkspaceJoinRequests.name).insert({
workspaceId: workspace.id,
userId: otherUser.id,
createdAt: new Date(),
status: 'pending'
})
const workspaceWithExistingRequest = await createAndStoreTestWorkspace({
discoverabilityEnabled: true
})
await storeWorkspaceDomain({
workspaceDomain: {
id: cryptoRandomString({ length: 6 }),
domain: 'example.org',
workspaceId: workspaceWithExistingRequest.id,
verified: true,
createdAt: new Date(),
updatedAt: new Date(),
createdByUserId: user.id
}
})
await db(WorkspaceJoinRequests.name).insert({
workspaceId: workspaceWithExistingRequest.id,
userId: user.id,
createdAt: new Date(),
status: 'pending'
})

const workspaces = await getUserDiscoverableWorkspaces({
domains: ['example.org'],
userId: user.id
})

expect(workspaces.length).to.equal(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make sure that we get the correct workspace id here

and lets do the test for the other user too

expect(workspaces[0].id).to.equal(workspace.id)

const otherUserWorkspaces = await getUserDiscoverableWorkspaces({
domains: ['example.org'],
userId: otherUser.id
})

expect(otherUserWorkspaces.length).to.equal(1)
expect(otherUserWorkspaces[0].id).to.equal(workspaceWithExistingRequest.id)
})
})

describe('getWorkspaceDomainsFactory creates a function, that', () => {
Expand Down