Skip to content

Commit

Permalink
feat: merge release branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ainouzgali committed Sep 5, 2022
2 parents b20ad71 + 165f3f1 commit cac4cfa
Show file tree
Hide file tree
Showing 21 changed files with 997 additions and 250 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions apps/api/src/app/events/e2e/trigger-event.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,105 @@ describe('Trigger event - /v1/events/trigger (POST)', function () {
const subscriberIds = messages.map((message) => message._subscriberId).filter(isUnique);
expect(subscriberIds.length).to.equal(4);
});

it('should not filter a message with correct payload', async function () {
template = await session.createTemplate({
steps: [
{
type: StepTypeEnum.EMAIL,
subject: 'Password reset',
content: [
{
type: 'text',
content: 'This are the text contents of the template for {{firstName}}',
},
{
type: 'button',
content: 'SIGN UP',
url: 'https://url-of-app.com/{{urlVariable}}',
},
],
filters: [
{
isNegated: false,

type: 'GROUP',

value: 'AND',

children: [
{
field: 'run',
value: 'true',
operator: 'EQUAL',
on: 'payload',
},
],
},
],
},
{
type: StepTypeEnum.EMAIL,
subject: 'Password reset',
content: [
{
type: 'text',
content: 'This are the text contents of the template for {{firstName}}',
},
{
type: 'button',
content: 'SIGN UP',
url: 'https://url-of-app.com/{{urlVariable}}',
},
],
filters: [
{
isNegated: false,

type: 'GROUP',

value: 'AND',

children: [
{
field: 'subscriberId',
value: subscriber.subscriberId,
operator: 'NOT_EQUAL',
on: 'subscriber',
},
],
},
],
},
],
});

await axiosInstance.post(
`${session.serverUrl}/v1/events/trigger`,
{
name: template.triggers[0].identifier,
to: subscriber.subscriberId,
payload: {
firstName: 'Testing of User Name',
urlVariable: '/test/url/path',
run: true,
},
},
{
headers: {
authorization: `ApiKey ${session.apiKey}`,
},
}
);

await session.awaitRunningJobs(template._id);

const messages = await messageRepository.count({
_templateId: template._id,
});

expect(messages).to.equal(1);
});
});

async function createTemplate(session, channelType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Injectable } from '@nestjs/common';
import { ChannelTypeEnum, StepTypeEnum } from '@novu/shared';
import { StepTypeEnum } from '@novu/shared';
import { SendMessageCommand } from './send-message.command';
import { SendMessageEmail } from './send-message-email.usecase';
import { SendMessageSms } from './send-message-sms.usecase';
import { SendMessageInApp } from './send-message-in-app.usecase';
import { SendMessageChat } from './send-message-chat.usecase';
import { SendMessagePush } from './send-message-push.usecase';
import { Digest } from './digest/digest.usecase';
import { matchMessageWithFilters } from '../trigger-event/message-filter.matcher';
import { SubscriberRepository } from '@novu/dal';

@Injectable()
export class SendMessage {
Expand All @@ -16,10 +18,17 @@ export class SendMessage {
private sendMessageInApp: SendMessageInApp,
private sendMessageChat: SendMessageChat,
private sendMessagePush: SendMessagePush,
private digest: Digest
private digest: Digest,
private subscriberRepository: SubscriberRepository
) {}

public async execute(command: SendMessageCommand) {
const shouldRun = await this.filter(command);

if (!shouldRun) {
return;
}

switch (command.step.template.type) {
case StepTypeEnum.SMS:
return await this.sendMessageSms.execute(command);
Expand All @@ -35,4 +44,28 @@ export class SendMessage {
return await this.digest.execute(command);
}
}

private async filter(command: SendMessageCommand) {
const data = await this.getFilterData(command);

return matchMessageWithFilters(command.step, data);
}

private async getFilterData(command: SendMessageCommand) {
const fetchSubscriber = command.step?.filters?.find((filter) => {
return filter?.children?.find((item) => item?.on === 'subscriber');
});

let subscriber = undefined;

if (fetchSubscriber) {
/// TODO: refactor command.subscriberId to command._subscriberId
subscriber = await this.subscriberRepository.findById(command.subscriberId);
}

return {
subscriber,
payload: command.payload,
};
}
}
Loading

0 comments on commit cac4cfa

Please sign in to comment.