Skip to content

Commit

Permalink
feat(workspaces): make query for single workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
alemagio committed Jan 14, 2025
1 parent bb73ecb commit 85ce151
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 71 deletions.
23 changes: 12 additions & 11 deletions packages/server/assets/workspacesCore/typedefs/workspaces.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ extend type Query {
Validates the slug, to make sure it contains only valid characters and its not taken.
"""
validateWorkspaceSlug(slug: String!): Boolean!

"""
Get all join requests for all the workspaces the user is an admin of
"""
adminWorkspacesJoinRequests(
filter: AdminWorkspaceJoinRequestFilter
cursor: String
limit: Int! = 25
): WorkspaceJoinRequestCollection!
@hasServerRole(role: SERVER_USER)
@hasScope(scope: "workspace:read")
}

input WorkspaceInviteLookupOptions {
Expand Down Expand Up @@ -322,6 +311,18 @@ type Workspace {
Info about the workspace creation state
"""
creationState: WorkspaceCreationState
"""
Get all join requests for all the workspaces the user is an admin of
"""
adminWorkspacesJoinRequests(
filter: AdminWorkspaceJoinRequestFilter
cursor: String
limit: Int! = 25
): WorkspaceJoinRequestCollection!
@hasServerRole(role: SERVER_USER)
@hasScope(scope: "workspace:read")
@hasWorkspaceRole(role: ADMIN)
@isOwner
}

type WorkspaceCreationState {
Expand Down
20 changes: 10 additions & 10 deletions packages/server/modules/core/graph/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2531,8 +2531,6 @@ export type Query = {
* @deprecated use admin.UserList instead
*/
adminUsers?: Maybe<AdminUsersListCollection>;
/** Get all join requests for all the workspaces the user is an admin of */
adminWorkspacesJoinRequests: WorkspaceJoinRequestCollection;
/** Gets a specific app from the server. */
app?: Maybe<ServerApp>;
/**
Expand Down Expand Up @@ -2660,13 +2658,6 @@ export type QueryAdminUsersArgs = {
};


export type QueryAdminWorkspacesJoinRequestsArgs = {
cursor?: InputMaybe<Scalars['String']['input']>;
filter?: InputMaybe<AdminWorkspaceJoinRequestFilter>;
limit?: Scalars['Int']['input'];
};


export type QueryAppArgs = {
id: Scalars['String']['input'];
};
Expand Down Expand Up @@ -4183,6 +4174,8 @@ export type WebhookUpdateInput = {

export type Workspace = {
__typename?: 'Workspace';
/** Get all join requests for all the workspaces the user is an admin of */
adminWorkspacesJoinRequests: WorkspaceJoinRequestCollection;
automateFunctions: AutomateFunctionCollection;
createdAt: Scalars['DateTime']['output'];
/** Info about the workspace creation state */
Expand Down Expand Up @@ -4226,6 +4219,13 @@ export type Workspace = {
};


export type WorkspaceAdminWorkspacesJoinRequestsArgs = {
cursor?: InputMaybe<Scalars['String']['input']>;
filter?: InputMaybe<AdminWorkspaceJoinRequestFilter>;
limit?: Scalars['Int']['input'];
};


export type WorkspaceAutomateFunctionsArgs = {
cursor?: InputMaybe<Scalars['String']['input']>;
filter?: InputMaybe<AutomateFunctionsFilter>;
Expand Down Expand Up @@ -6235,7 +6235,6 @@ export type QueryResolvers<ContextType = GraphQLContext, ParentType = ResolversP
admin?: Resolver<ResolversTypes['AdminQueries'], ParentType, ContextType>;
adminStreams?: Resolver<Maybe<ResolversTypes['StreamCollection']>, ParentType, ContextType, RequireFields<QueryAdminStreamsArgs, 'limit' | 'offset'>>;
adminUsers?: Resolver<Maybe<ResolversTypes['AdminUsersListCollection']>, ParentType, ContextType, RequireFields<QueryAdminUsersArgs, 'limit' | 'offset' | 'query'>>;
adminWorkspacesJoinRequests?: Resolver<ResolversTypes['WorkspaceJoinRequestCollection'], ParentType, ContextType, RequireFields<QueryAdminWorkspacesJoinRequestsArgs, 'limit'>>;
app?: Resolver<Maybe<ResolversTypes['ServerApp']>, ParentType, ContextType, RequireFields<QueryAppArgs, 'id'>>;
apps?: Resolver<Maybe<Array<Maybe<ResolversTypes['ServerAppListItem']>>>, ParentType, ContextType>;
authenticatedAsApp?: Resolver<Maybe<ResolversTypes['ServerAppListItem']>, ParentType, ContextType>;
Expand Down Expand Up @@ -6759,6 +6758,7 @@ export type WebhookEventCollectionResolvers<ContextType = GraphQLContext, Parent
};

export type WorkspaceResolvers<ContextType = GraphQLContext, ParentType = ResolversParentTypes['Workspace']> = {
adminWorkspacesJoinRequests?: Resolver<ResolversTypes['WorkspaceJoinRequestCollection'], ParentType, ContextType, RequireFields<WorkspaceAdminWorkspacesJoinRequestsArgs, 'limit'>>;
automateFunctions?: Resolver<ResolversTypes['AutomateFunctionCollection'], ParentType, ContextType, RequireFields<WorkspaceAutomateFunctionsArgs, 'limit'>>;
createdAt?: Resolver<ResolversTypes['DateTime'], ParentType, ContextType>;
creationState?: Resolver<Maybe<ResolversTypes['WorkspaceCreationState']>, ParentType, ContextType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2511,8 +2511,6 @@ export type Query = {
* @deprecated use admin.UserList instead
*/
adminUsers?: Maybe<AdminUsersListCollection>;
/** Get all join requests for all the workspaces the user is an admin of */
adminWorkspacesJoinRequests: WorkspaceJoinRequestCollection;
/** Gets a specific app from the server. */
app?: Maybe<ServerApp>;
/**
Expand Down Expand Up @@ -2640,13 +2638,6 @@ export type QueryAdminUsersArgs = {
};


export type QueryAdminWorkspacesJoinRequestsArgs = {
cursor?: InputMaybe<Scalars['String']['input']>;
filter?: InputMaybe<AdminWorkspaceJoinRequestFilter>;
limit?: Scalars['Int']['input'];
};


export type QueryAppArgs = {
id: Scalars['String']['input'];
};
Expand Down Expand Up @@ -4163,6 +4154,8 @@ export type WebhookUpdateInput = {

export type Workspace = {
__typename?: 'Workspace';
/** Get all join requests for all the workspaces the user is an admin of */
adminWorkspacesJoinRequests: WorkspaceJoinRequestCollection;
automateFunctions: AutomateFunctionCollection;
createdAt: Scalars['DateTime']['output'];
/** Info about the workspace creation state */
Expand Down Expand Up @@ -4206,6 +4199,13 @@ export type Workspace = {
};


export type WorkspaceAdminWorkspacesJoinRequestsArgs = {
cursor?: InputMaybe<Scalars['String']['input']>;
filter?: InputMaybe<AdminWorkspaceJoinRequestFilter>;
limit?: Scalars['Int']['input'];
};


export type WorkspaceAutomateFunctionsArgs = {
cursor?: InputMaybe<Scalars['String']['input']>;
filter?: InputMaybe<AutomateFunctionsFilter>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { WorkspaceJoinRequestStatus } from '@/modules/workspacesCore/domain/type
import { WorkspaceJoinRequestGraphQLReturn } from '@/modules/workspacesCore/helpers/graphTypes'

export default {
Query: {
adminWorkspacesJoinRequests: async (_parent, args, ctx) => {
Workspace: {
adminWorkspacesJoinRequests: async (parent, args, ctx) => {
const { filter, cursor, limit } = args

return await getPaginatedItemsFactory<
Expand All @@ -28,8 +28,9 @@ export default {
getTotalCount: countAdminWorkspaceJoinRequestsFactory({ db })
})({
filter: {
workspaceId: parent.id,
status: filter?.status ?? undefined,
userId: ctx.userId!
userId: ctx.userId! // This is the worskpace admin, not the request userId
},
cursor: cursor ?? undefined,
limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const updateWorkspaceJoinRequestStatusFactory =
}

type WorkspaceJoinRequestFilter = {
workspaceId: string
status?: WorkspaceJoinRequestStatus | null
userId: string
}
Expand All @@ -51,6 +52,7 @@ const adminWorkspaceJoinRequestsBaseQueryFactory =
)
.where(WorkspaceAcl.col.role, Roles.Workspace.Admin)
.where(WorkspaceAcl.col.userId, filter.userId)
.where(WorkspaceJoinRequests.col.workspaceId, filter.workspaceId)
if (filter.status) query.andWhere(WorkspaceJoinRequests.col.status, filter.status)
return query
}
Expand Down
35 changes: 20 additions & 15 deletions packages/server/modules/workspaces/tests/helpers/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,27 +332,32 @@ export const requestToJoinWorkspaceMutation = gql`
}
`

export const adminWorkspaceJoinRequestsQuery = gql`
query adminWorkspaceJoinRequests(
export const getWorkspaceWithJoinRequestsQuery = gql`
query GetWorkspaceWithJoinRequests(
$workspaceId: String!
$filter: AdminWorkspaceJoinRequestFilter
$cursor: String
$limit: Int
) {
adminWorkspacesJoinRequests(filter: $filter, cursor: $cursor, limit: $limit) {
items {
status
user {
id
name
}
workspace {
id
name
workspace(id: $workspaceId) {
...BasicWorkspace
adminWorkspacesJoinRequests(filter: $filter, cursor: $cursor, limit: $limit) {
items {
status
user {
id
name
}
workspace {
id
name
}
createdAt
}
createdAt
cursor
totalCount
}
cursor
totalCount
}
}
${basicWorkspaceFragment}
`
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
createTestUser
} from '@/test/authHelper'
import {
AdminWorkspaceJoinRequestsDocument,
GetWorkspaceWithJoinRequestsDocument,
RequestToJoinWorkspaceDocument
} from '@/test/graphql/generated/graphql'
import { createTestContext, testApolloServer } from '@/test/graphqlHelper'
Expand All @@ -34,7 +34,7 @@ before(async () => {
})

describe('WorkspaceJoinRequests GQL', () => {
describe('User.adminWorkspacesJoinRequests', () => {
describe('Workspace.adminWorkspacesJoinRequests', () => {
it('should return the workspace join requests for the admin', async () => {
const admin = await createTestUser({
name: 'admin user',
Expand Down Expand Up @@ -101,25 +101,46 @@ describe('WorkspaceJoinRequests GQL', () => {
expect(joinReq2).to.not.haveGraphQLErrors()

const sessionAdmin = await login(admin)
const res = await sessionAdmin.execute(AdminWorkspaceJoinRequestsDocument, {})
expect(res).to.not.haveGraphQLErrors()
const workspace1Res = await sessionAdmin.execute(
GetWorkspaceWithJoinRequestsDocument,
{
workspaceId: workspace1.id
}
)
expect(workspace1Res).to.not.haveGraphQLErrors()

const { items, totalCount } = res.data?.adminWorkspacesJoinRequests
const { items: items1, totalCount: totalCount1 } =
workspace1Res.data!.workspace!.adminWorkspacesJoinRequests!

expect(totalCount).to.equal(2)
expect(totalCount1).to.equal(1)

expect(items).to.have.length(2)
assert.deepEqual(items[1], {
expect(items1).to.have.length(1)
assert.deepEqual(items1[0], {
status: 'pending',
user: { id: user1.id, name: user1.name },
workspace: { id: workspace1.id, name: workspace1.name },
createdAt: items[1].createdAt
createdAt: items1[0].createdAt
})
assert.deepEqual(items[0], {

const workspace2Res = await sessionAdmin.execute(
GetWorkspaceWithJoinRequestsDocument,
{
workspaceId: workspace2.id
}
)
expect(workspace2Res).to.not.haveGraphQLErrors()

const { items: items2, totalCount: totalCount2 } =
workspace2Res.data!.workspace!.adminWorkspacesJoinRequests!

expect(totalCount2).to.equal(1)

expect(items2).to.have.length(1)
assert.deepEqual(items2[0], {
status: 'pending',
user: { id: user2.id, name: user2.name },
workspace: { id: workspace2.id, name: workspace2.name },
createdAt: items[0].createdAt
createdAt: items2[0].createdAt
})
})
})
Expand Down
Loading

0 comments on commit 85ce151

Please sign in to comment.