Skip to content

Commit

Permalink
refactor(push-notifications): rework some admin endpoints for the new…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
kkopanidis authored Aug 7, 2024
1 parent 84c0a4a commit ec3c617
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
105 changes: 99 additions & 6 deletions modules/push-notifications/src/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import {
ConduitRouteActions,
ConduitRouteReturnDefinition,
GrpcError,
Indexable,
ParsedRouterRequest,
TYPE,
UnparsedRouterResponse,
} from '@conduitplatform/grpc-sdk';
import {
ConduitBoolean,
ConduitJson,
ConduitNumber,
ConduitObjectId,
ConduitString,
GrpcServer,
RoutingManager,
Expand Down Expand Up @@ -97,16 +100,68 @@ export class AdminHandlers {
return 'Ok';
}

async getNotificationTokenByUserId(
call: ParsedRouterRequest,
): Promise<UnparsedRouterResponse> {
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<UnparsedRouterResponse> {
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<UnparsedRouterResponse> {
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();

Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/push-notifications/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
active: {
format: 'Boolean',
default: false,
default: true,
},
providerName: {
format: 'String',
Expand Down

0 comments on commit ec3c617

Please sign in to comment.