diff --git a/src/providers/user.service.ts b/src/providers/user.service.ts index 142626c..9bcb1d0 100644 --- a/src/providers/user.service.ts +++ b/src/providers/user.service.ts @@ -56,9 +56,18 @@ export class UserService { return isOnline; } - async setToOffline(username: string) { + async onlineInstances(request: Request) { + const user: User = request['user']; + const jobs = await this.onlineUsersQueue.getWaiting(); + const onlineInstances = jobs.filter((job) => job.data === user.username); + + return onlineInstances.length; + } + + async setToOffline(request: Request) { + const user: User = request['user']; const jobs = await this.onlineUsersQueue.getWaiting(); - const onlineUser = jobs.find((job) => job.data === username); + const onlineUser = jobs.find((job) => job.data === user.username); return await onlineUser .remove() diff --git a/src/resolvers/user.resolver.ts b/src/resolvers/user.resolver.ts index ee2add1..acd696a 100644 --- a/src/resolvers/user.resolver.ts +++ b/src/resolvers/user.resolver.ts @@ -112,8 +112,15 @@ export class UserResolver { return this.userService.isCurrentlyOnline(username); } + @UseGuards(JwtAuthGuard) + @Query(() => Int) + onlineInstances(@Context() context: { req: Request }) { + return this.userService.onlineInstances(context.req); + } + + @UseGuards(JwtAuthGuard) @Mutation(() => Boolean) - setToOffline(@Args('username') username: string) { - return this.userService.setToOffline(username); + setToOffline(@Context() context: { req: Request }) { + return this.userService.setToOffline(context.req); } }