Skip to content

Commit

Permalink
fix: optional condition for few fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jainpawan21 committed Jun 22, 2023
1 parent 3a454d6 commit 1325672
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 45 deletions.
8 changes: 4 additions & 4 deletions apps/api/src/app/messages/dtos/get-messages-requests.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { ChannelTypeEnum } from '@novu/shared';
import { IsNumber } from 'class-validator';
import { Transform } from 'class-transformer';
Expand All @@ -7,12 +7,12 @@ export class GetMessagesRequestDto {
@ApiPropertyOptional({
enum: ChannelTypeEnum,
})
channel: ChannelTypeEnum;
channel?: ChannelTypeEnum;

@ApiProperty({
@ApiPropertyOptional({
type: String,
})
subscriberId: string;
subscriberId?: string;

@ApiPropertyOptional({
type: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import { EnvironmentCommand } from '../../../shared/commands/project.command';

export class GetMessagesCommand extends EnvironmentCommand {
@IsOptional()
subscriberId: string;
subscriberId?: string;

@IsOptional()
channel: ChannelTypeEnum;
channel?: ChannelTypeEnum;

@IsNumber()
@IsOptional()
page: number;
page = 0;

@IsNumber()
@IsOptional()
limit: number;
limit = 10;

@IsOptional()
@IsArray()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { MessageEntity, MessageRepository, SubscriberRepository, SubscriberEntity } from '@novu/dal';
import { CachedEntity, buildSubscriberKey } from '@novu/application-generic';
import { BadRequestException, Injectable } from '@nestjs/common';
import { MessageEntity, MessageRepository, SubscriberEntity } from '@novu/dal';
import { ActorTypeEnum } from '@novu/shared';

import { GetMessagesCommand } from './get-messages.command';
import { GetSubscriber, GetSubscriberCommand } from '../../../subscribers/usecases/get-subscriber';

@Injectable()
export class GetMessages {
constructor(private messageRepository: MessageRepository, private subscriberRepository: SubscriberRepository) {}
constructor(private messageRepository: MessageRepository, private getSubscriberUseCase: GetSubscriber) {}

async execute(command: GetMessagesCommand) {
const LIMIT = command.limit;
Expand All @@ -23,12 +23,13 @@ export class GetMessages {
};

if (command.subscriberId) {
const subscriber = await this.fetchSubscriber({
_environmentId: command.environmentId,
subscriberId: command.subscriberId,
});

if (!subscriber) throw new NotFoundException(`Subscriber ${command.subscriberId} not found`);
const subscriber = await this.getSubscriberUseCase.execute(
GetSubscriberCommand.create({
subscriberId: command.subscriberId,
environmentId: command.environmentId,
organizationId: command.organizationId,
})
);

query._subscriberId = subscriber._id;
}
Expand Down Expand Up @@ -81,23 +82,6 @@ export class GetMessages {
return currentPaginationTotal < totalCount;
}

@CachedEntity({
builder: (command: { subscriberId: string; _environmentId: string }) =>
buildSubscriberKey({
_environmentId: command._environmentId,
subscriberId: command.subscriberId,
}),
})
private async fetchSubscriber({
subscriberId,
_environmentId,
}: {
subscriberId: string;
_environmentId: string;
}): Promise<SubscriberEntity | null> {
return await this.subscriberRepository.findBySubscriberId(_environmentId, subscriberId);
}

private processUserAvatar(actorSubscriber?: SubscriberEntity): string | null {
return actorSubscriber?.avatar || null;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/app/messages/usecases/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetSubscriber } from '../../subscribers/usecases/get-subscriber';
import { GetMessages } from './get-messages';
import { RemoveMessage } from './remove-message';

export const USE_CASES = [RemoveMessage, GetMessages];
export const USE_CASES = [RemoveMessage, GetMessages, GetSubscriber];
4 changes: 2 additions & 2 deletions docs/docs/platform/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ await novu.messages.list({
limit: 30,
channel: ChannelTypeEnum.IN_APP,
subscriberId: '123',
transactionId: ['transactionId1', 'transactionId2'],
transactionIds: ['transactionId1', 'transactionId2'],
});
```

Expand Down Expand Up @@ -89,7 +89,7 @@ await novu.messages.list({
"subscriberId": "subscriberId",
"id": "_subscriberId"
},
"actorSubscriber": null,
"actorSubscriber": 'actorSubscriberId',
"id": "messageId"
}
....
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/lib/messages/messages.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface IMessagesPayload {
limit?: number;
subscriberId?: string;
channel?: ChannelTypeEnum;
transactionId?: string[];
transactionIds?: string[];
}
4 changes: 2 additions & 2 deletions packages/node/src/lib/messages/messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Novu Node.js package - Messages class', () => {
__v: 0,
content: [Array],
subscriber: [Object],
actorSubscriber: null,
actorSubscriber: 'actorSubscriberId',
id: '649070afaa9e50289df42134',
},
],
Expand All @@ -89,7 +89,7 @@ describe('Novu Node.js package - Messages class', () => {
page: 1,
limit: 5,
channel: ChannelTypeEnum.EMAIL,
transactionId: ['transactionId1', 'transactionId2'],
transactionIds: ['transactionId1', 'transactionId2'],
});

expect(mockedAxios.get).toHaveBeenCalled();
Expand Down
6 changes: 4 additions & 2 deletions packages/node/src/lib/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const BASE_PATH = '/messages';

export class Messages extends WithHttp implements IMessages {
async list(data?: IMessagesPayload) {
const queryParams: Partial<IMessagesPayload> = {};
const queryParams: Partial<IMessagesPayload> & {
transactionId?: string[];
} = {};
data?.page && (queryParams.page = data?.page);
data?.limit && (queryParams.limit = data?.limit);
data?.subscriberId && (queryParams.subscriberId = data?.subscriberId);
data?.channel && (queryParams.channel = data?.channel);
data?.transactionId && (queryParams.transactionId = data?.transactionId);
data?.transactionIds && (queryParams.transactionId = data?.transactionIds);

return await this.http.get(BASE_PATH, {
params: queryParams,
Expand Down

0 comments on commit 1325672

Please sign in to comment.