Skip to content

Commit a265a1b

Browse files
authored
feat: get pending invites in team (#1400)
1 parent 5e2ae0c commit a265a1b

File tree

1 file changed

+62
-1
lines changed
  • modules/authentication/src/handlers

1 file changed

+62
-1
lines changed

modules/authentication/src/handlers/team.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ export class TeamsHandler implements IAuthenticationStrategy {
542542
sort,
543543
populate,
544544
});
545-
return { teams: teams, count };
545+
return { teams, count };
546546
}
547547

548548
async userInvite(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
@@ -616,6 +616,51 @@ export class TeamsHandler implements IAuthenticationStrategy {
616616
return invitation.token;
617617
}
618618

619+
async getTeamInvites(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
620+
const { user } = call.request.context;
621+
const { teamId } = call.request.urlParams;
622+
const config: Config = ConfigController.getInstance().config;
623+
if (!config.teams.invites.enabled) {
624+
throw new GrpcError(status.PERMISSION_DENIED, 'Team invites are disabled');
625+
}
626+
const team = await Team.getInstance().findOne({ _id: teamId });
627+
if (!team) {
628+
throw new GrpcError(
629+
status.INVALID_ARGUMENT,
630+
'Could not create invite, team does not exist',
631+
);
632+
}
633+
634+
const can = await this.grpcSdk.authorization!.can({
635+
subject: 'User:' + user._id,
636+
actions: ['invite'],
637+
resource: 'Team:' + teamId,
638+
});
639+
if (!can.allow) {
640+
throw new GrpcError(
641+
status.PERMISSION_DENIED,
642+
'You do not have permission to invite users to this team',
643+
);
644+
}
645+
646+
const invites = await Token.getInstance().findMany({
647+
tokenType: TokenType.TEAM_INVITE_TOKEN,
648+
// @ts-expect-error Unsafe nested property access
649+
'data.teamId': teamId,
650+
});
651+
652+
const count = await Token.getInstance().countDocuments({
653+
tokenType: TokenType.TEAM_INVITE_TOKEN,
654+
// @ts-expect-error Unsafe nested property access
655+
'data.teamId': teamId,
656+
});
657+
658+
return {
659+
invites,
660+
count,
661+
};
662+
}
663+
619664
async persistentInvite(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
620665
const { user } = call.request.context;
621666
const { teamId, role, userData } = call.request.params;
@@ -892,6 +937,22 @@ export class TeamsHandler implements IAuthenticationStrategy {
892937
}),
893938
this.getSubTeams.bind(this),
894939
);
940+
routingManager.route(
941+
{
942+
path: '/teams/:teamId/invites',
943+
description: `Retrieves pending invites for the team`,
944+
urlParams: {
945+
teamId: ConduitObjectId.Required,
946+
},
947+
action: ConduitRouteActions.GET,
948+
middlewares: authRouteMiddlewares,
949+
},
950+
new ConduitRouteReturnDefinition('GetTeamInvites', {
951+
invites: [Token.name],
952+
count: ConduitNumber.Required,
953+
}),
954+
this.getTeamInvites.bind(this),
955+
);
895956
routingManager.route(
896957
{
897958
path: '/teams/:teamId/members',

0 commit comments

Comments
 (0)