Skip to content

Commit

Permalink
Merge pull request #3174 from novuhq/NV-1943/notification-collection-…
Browse files Browse the repository at this point in the history
…indexes-refactor

Index Cleanup - Notification
  • Loading branch information
djabarovgeorge authored Apr 24, 2023
2 parents 9787beb + a41e266 commit 203a865
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FilterQuery, QueryWithHelpers, Types } from 'mongoose';
import { ChannelTypeEnum, StepTypeEnum } from '@novu/shared';
import { subYears, subMonths, subWeeks } from 'date-fns';
import { subMonths, subWeeks } from 'date-fns';

import { BaseRepository } from '../base-repository';
import { NotificationEntity, NotificationDBModel } from './notification.entity';
Expand Down Expand Up @@ -147,7 +147,7 @@ export class NotificationRepository extends BaseRepository<
channels: { $addToSet: '$channels' },
},
},
{ $sort: { _id: -1 } },
{ $sort: { createdAt: -1 } },
],
{
readPreference: 'secondaryPreferred',
Expand Down
60 changes: 60 additions & 0 deletions libs/dal/src/repositories/notification/notification.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,66 @@ notificationSchema.virtual('jobs', {
foreignField: '_notificationId',
});

/*
*
* Path: libs/dal/src/repositories/notification/notification.repository.ts
* Context: findBySubscriberId()
* Query: find({_environmentId: environmentId,
* _subscriberId: subscriberId,});
*
*/
notificationSchema.index({
_subscriberId: 1,
_environmentId: 1,
});

/*
* Path: libs/dal/src/repositories/notification/notification.repository.ts
* Context: getFeed()
* Query: find({
* transactionId: subscriberId,
* _environmentId: environmentId,
* _templateId = {$in: query.templates};
* _subscriberId = {$in: query._subscriberIds};
* channels = {$in: query.channels};
* .sort('-createdAt')});
*
* Path: libs/dal/src/repositories/notification/notification.repository.ts
* Context: getFeed()
* Query: MongooseModel.countDocuments({
* transactionId: subscriberId,
* _environmentId: environmentId,
* _templateId = {$in: query.templates};
* _subscriberId = {$in: query._subscriberIds};
* channels = {$in: query.channels}});
*
*/
notificationSchema.index({
transactionId: 1,
_environmentId: 1,
createdAt: -1,
});

/*
*
* Path: libs/dal/src/repositories/notification/notification.repository.ts
* Context: getActivityGraphStats()
* Query: aggregate(
* {createdAt: { $gte: date }_environmentId: new Types.ObjectId(environmentId),
* { $sort: { createdAt: -1 } }})
*
* Path: libs/dal/src/repositories/notification/notification.repository.ts
* Context: getStats()
* Query: aggregate({
* _environmentId: this.convertStringToObjectId(environmentId),
* createdAt: {$gte: monthBefore}
* weekly: { $sum: { $cond: [{ $gte: ['$createdAt', weekBefore] }, 1, 0] } },
*/
notificationSchema.index({
_environmentId: 1,
createdAt: -1,
});

// eslint-disable-next-line @typescript-eslint/naming-convention
export const Notification =
(mongoose.models.Notification as mongoose.Model<NotificationDBModel>) ||
Expand Down

0 comments on commit 203a865

Please sign in to comment.