diff --git a/libs/dal/src/repositories/notification/notification.repository.ts b/libs/dal/src/repositories/notification/notification.repository.ts index f08cf45e31c..e20610e3135 100644 --- a/libs/dal/src/repositories/notification/notification.repository.ts +++ b/libs/dal/src/repositories/notification/notification.repository.ts @@ -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'; @@ -147,7 +147,7 @@ export class NotificationRepository extends BaseRepository< channels: { $addToSet: '$channels' }, }, }, - { $sort: { _id: -1 } }, + { $sort: { createdAt: -1 } }, ], { readPreference: 'secondaryPreferred', diff --git a/libs/dal/src/repositories/notification/notification.schema.ts b/libs/dal/src/repositories/notification/notification.schema.ts index 8b0c923913d..a9c50e2c4d9 100644 --- a/libs/dal/src/repositories/notification/notification.schema.ts +++ b/libs/dal/src/repositories/notification/notification.schema.ts @@ -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) ||