From ec3c6170521355999d0a70dd81c76093a7073ec6 Mon Sep 17 00:00:00 2001 From: Konstantinos Kopanidis Date: Wed, 7 Aug 2024 15:11:34 +0300 Subject: [PATCH] refactor(push-notifications): rework some admin endpoints for the new UI and correctness (#1100) * feat(push-notifications): set default state to enabled refactor(push-notifications): rework some admin endpoints for the new UI and correctness * fix(push-notifications): change search to object id for token search --- modules/push-notifications/src/admin/index.ts | 105 +++++++++++++++++- .../push-notifications/src/config/config.ts | 2 +- 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/modules/push-notifications/src/admin/index.ts b/modules/push-notifications/src/admin/index.ts index 120507c71..6f68e00cd 100644 --- a/modules/push-notifications/src/admin/index.ts +++ b/modules/push-notifications/src/admin/index.ts @@ -3,6 +3,7 @@ import { ConduitRouteActions, ConduitRouteReturnDefinition, GrpcError, + Indexable, ParsedRouterRequest, TYPE, UnparsedRouterResponse, @@ -10,6 +11,8 @@ import { import { ConduitBoolean, ConduitJson, + ConduitNumber, + ConduitObjectId, ConduitString, GrpcServer, RoutingManager, @@ -97,16 +100,68 @@ export class AdminHandlers { return 'Ok'; } + async getNotificationTokenByUserId( + call: ParsedRouterRequest, + ): Promise { + const tokenDocuments = await NotificationToken.getInstance().findMany( + { + userId: call.request.urlParams.userId, + }, + undefined, + undefined, + undefined, + undefined, + call.request.queryParams.populate, + ); + if (isNil(tokenDocuments)) { + throw new GrpcError(status.NOT_FOUND, 'Could not find a token for user'); + } + return { tokenDocuments }; + } + async getNotificationToken(call: ParsedRouterRequest): Promise { - const tokenDocuments = await NotificationToken.getInstance().findMany({ - userId: call.request.params.userId, - }); + const tokenDocuments = await NotificationToken.getInstance().findOne( + { + _id: call.request.urlParams.id, + }, + undefined, + call.request.queryParams.populate, + ); if (isNil(tokenDocuments)) { throw new GrpcError(status.NOT_FOUND, 'Could not find a token for user'); } return { tokenDocuments }; } + async getNotificationTokens( + call: ParsedRouterRequest, + ): Promise { + const { platform, search, sort, populate } = call.request.params; + const { skip } = call.request.params ?? 0; + const { limit } = call.request.params ?? 25; + + let query: Indexable = {}; + + if (!isNil(search)) { + query = { $or: [{ _id: search }, { userId: search }] }; + } + if (!isNil(platform)) { + query[platform] = platform; + } + + const tokens: NotificationToken[] = await NotificationToken.getInstance().findMany( + query, + undefined, + skip, + limit, + sort, + populate, + ); + const count: number = await NotificationToken.getInstance().countDocuments(query); + + return { tokens, count }; + } + private registerAdminRoutes() { this.routingManager.clear(); @@ -172,17 +227,55 @@ export class AdminHandlers { ); this.routingManager.route( { - path: '/token/:userId', + path: '/token', + action: ConduitRouteActions.GET, + description: `Get and search notification tokens.`, + queryParams: { + skip: ConduitNumber.Optional, + limit: ConduitNumber.Optional, + sort: ConduitString.Optional, + search: ConduitObjectId.Optional, + platform: ConduitString.Optional, + populate: ConduitString.Optional, + }, + }, + new ConduitRouteReturnDefinition('GetNotificationTokens', { + tokens: ['NotificationToken'], + count: ConduitNumber.Required, + }), + this.getNotificationTokens.bind(this), + ); + this.routingManager.route( + { + path: '/token/:id', + action: ConduitRouteActions.GET, + description: `Get a notification token by id.`, + urlParams: { + id: ConduitObjectId.Required, + }, + queryParams: { + populate: ConduitString.Optional, + }, + }, + new ConduitRouteReturnDefinition('GetNotificationToken', NotificationToken.name), + this.getNotificationToken.bind(this), + ); + this.routingManager.route( + { + path: '/token/user/:userId', action: ConduitRouteActions.GET, description: `Returns a user's notification token.`, urlParams: { userId: { type: TYPE.String, required: true }, }, + queryParams: { + populate: ConduitString.Optional, + }, }, - new ConduitRouteReturnDefinition('GetNotificationToken', { + new ConduitRouteReturnDefinition('GetUserNotificationTokens', { tokenDocuments: ['NotificationToken'], }), - this.getNotificationToken.bind(this), + this.getNotificationTokenByUserId.bind(this), ); this.routingManager.registerRoutes(); } diff --git a/modules/push-notifications/src/config/config.ts b/modules/push-notifications/src/config/config.ts index 567054bd3..b1a196aa3 100644 --- a/modules/push-notifications/src/config/config.ts +++ b/modules/push-notifications/src/config/config.ts @@ -1,7 +1,7 @@ export default { active: { format: 'Boolean', - default: false, + default: true, }, providerName: { format: 'String',