Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add actor access to notification templates #3724

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const TriggerTitle = styled(Text)`
padding-bottom: 20px;
`;

export const ExecutionDetailTrigger = ({ identifier, step, subscriberVariables }) => {
export const ExecutionDetailTrigger = ({ identifier, step, subscriberVariables, actorVariables }) => {
const { payload, overrides } = step || {};

const curlSnippet = getCurlTriggerSnippet(identifier, subscriberVariables, payload, overrides);
const curlSnippet = getCurlTriggerSnippet(identifier, subscriberVariables, actorVariables, payload, overrides);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const useStyles = createStyles((theme) => ({
},
}));

export const ExecutionDetailsAccordion = ({ identifier, steps, subscriberVariables }) => {
export const ExecutionDetailsAccordion = ({ identifier, steps, subscriberVariables, actorVariables }) => {
const { classes } = useStyles();

if (!steps || steps.length <= 0) {
Expand All @@ -54,6 +54,7 @@ export const ExecutionDetailsAccordion = ({ identifier, steps, subscriberVariabl
identifier={identifier}
step={step}
subscriberVariables={subscriberVariables}
actorVariables={actorVariables}
/>
</Accordion.Panel>
</Accordion.Item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function ExecutionDetailsModal({
jobs,
_digestedNotificationId: digestedNotificationId,
to: subscriberVariables,
actor: actorVariables,
template,
} = response?.data || {};
const { triggers } = template || {};
Expand Down Expand Up @@ -82,7 +83,12 @@ export function ExecutionDetailsModal({
data-test-id="execution-details-modal-loading-overlay"
/>

<ExecutionDetailsAccordion identifier={identifier} steps={jobs} subscriberVariables={subscriberVariables} />
<ExecutionDetailsAccordion
identifier={identifier}
steps={jobs}
subscriberVariables={subscriberVariables}
actorVariables={actorVariables}
/>
<When truthy={digestedNotificationId}>
<Center mt={20}>
<Text mr={10} size="md" color={colors.B60}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const GridColContainer = styled(Container)`
padding: 20px;
`;

export const ExecutionDetailsStepContent = ({ identifier, step, subscriberVariables }) => {
export const ExecutionDetailsStepContent = ({ identifier, step, subscriberVariables, actorVariables }) => {
const [detailId, setDetailId] = useState<string>('');
const [executionDetailsRawSnippet, setExecutionDetailsRawSnippet] = useState<string>('');
const { executionDetails } = step || {};
Expand Down Expand Up @@ -61,7 +61,12 @@ export const ExecutionDetailsStepContent = ({ identifier, step, subscriberVariab
<Grid.Col span={6}>
<GridColContainer>
<When truthy={detailId.length === 0}>
<ExecutionDetailTrigger identifier={identifier} step={step} subscriberVariables={subscriberVariables} />
<ExecutionDetailTrigger
identifier={identifier}
step={step}
subscriberVariables={subscriberVariables}
actorVariables={actorVariables}
/>
</When>
<When truthy={detailId.length > 0 && executionDetailsRawSnippet}>
<ExecutionDetailRawSnippet raw={executionDetailsRawSnippet} onClose={onHideExecutionDetail} />
Expand Down
27 changes: 26 additions & 1 deletion apps/web/src/pages/templates/components/TestWorkflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ const makeToValue = (subscriberVariables: INotificationTriggerVariable[], curren
return JSON.stringify(subsVars, null, 2);
};

const makeActorValue = () => {
return JSON.stringify(
{
subscriberId: '<REPLACE_WITH_DATA>',
},
null,
2
);
};

const makePayloadValue = (variables: INotificationTriggerVariable[]) => {
return JSON.stringify(getPayloadValue(variables), null, 2);
};
Expand Down Expand Up @@ -61,11 +71,13 @@ export function TestWorkflow({ trigger }) {
const form = useForm({
initialValues: {
toValue: makeToValue(subscriberVariables, currentUser),
actorValue: makeActorValue(),
payloadValue: makePayloadValue(variables) === '{}' ? '{\n\n}' : makePayloadValue(variables),
overridesValue: overridesTrigger,
},
validate: {
toValue: jsonValidator,
actorValue: jsonValidator,
payloadValue: jsonValidator,
overridesValue: jsonValidator,
},
Expand All @@ -75,15 +87,17 @@ export function TestWorkflow({ trigger }) {
form.setValues({ toValue: makeToValue(subscriberVariables, currentUser) });
}, [subscriberVariables, currentUser]);

const onTrigger = async ({ toValue, payloadValue, overridesValue }) => {
const onTrigger = async ({ toValue, actorValue, payloadValue, overridesValue }) => {
const to = JSON.parse(toValue);
const actor = JSON.parse(actorValue);
const payload = JSON.parse(payloadValue);
const overrides = JSON.parse(overridesValue);

try {
const response = await triggerTestEvent({
name: trigger?.identifier,
to,
actor,
payload: {
...payload,
__source: 'test-workflow',
Expand Down Expand Up @@ -131,6 +145,17 @@ export function TestWorkflow({ trigger }) {
validationError="Invalid JSON"
mb={15}
/>
<JsonInput
data-test-id="test-trigger-actor-param"
formatOnBlur
autosize
styles={inputStyles}
label="Actor (optional)"
{...form.getInputProps('actorValue')}
minRows={3}
mb={15}
validationError="Invalid JSON"
/>
<JsonInput
data-test-id="test-trigger-overrides-param"
formatOnBlur
Expand Down
10 changes: 8 additions & 2 deletions apps/web/src/pages/templates/components/TriggerSnippetTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ export function TriggerSnippetTabs({ trigger }: { trigger: INotificationTrigger
const subscriberVariables = isPassingSubscriberId
? [...triggerSubscriberVariables]
: [{ name: 'subscriberId' }, ...triggerSubscriberVariables];
const actorVariables = [{ name: 'subscriberId' }];

const toValue = getSubscriberValue(subscriberVariables, (variable) => variable.value || '<REPLACE_WITH_DATA>');
const actorValue = getSubscriberValue(actorVariables, (variable) => variable.value || '<REPLACE_WITH_DATA>');
const payloadValue = getPayloadValue(trigger.variables);

const prismTabs = [
{
value: NODE_JS,
content: getNodeTriggerSnippet(trigger.identifier, toValue, payloadValue),
content: getNodeTriggerSnippet(trigger.identifier, toValue, actorValue, payloadValue),
},
{
value: CURL,
content: getCurlTriggerSnippet(trigger.identifier, toValue, payloadValue),
content: getCurlTriggerSnippet(trigger.identifier, toValue, actorValue, payloadValue),
},
];

Expand All @@ -36,6 +38,7 @@ export function TriggerSnippetTabs({ trigger }: { trigger: INotificationTrigger
export const getNodeTriggerSnippet = (
identifier: string,
to: Record<string, unknown>,
actor: Record<string, unknown>,
payload: Record<string, unknown>,
overrides?: Record<string, unknown>
) => {
Expand All @@ -46,6 +49,7 @@ const novu = new Novu('<API_KEY>');
novu.trigger('${identifier}', ${JSON.stringify(
{
to,
actor,
payload,
overrides,
},
Expand All @@ -67,6 +71,7 @@ novu.trigger('${identifier}', ${JSON.stringify(
export const getCurlTriggerSnippet = (
identifier: string,
to: Record<string, any>,
actor: Record<string, unknown>,
payload: Record<string, any>,
overrides?: Record<string, any>
) => {
Expand All @@ -77,6 +82,7 @@ export const getCurlTriggerSnippet = (
{
name: identifier,
to,
actor,
payload,
overrides,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ChatProviderIdEnum,
ExecutionDetailsSourceEnum,
ExecutionDetailsStatusEnum,
ActorTypeEnum,
} from '@novu/shared';
import {
InstrumentUsecase,
Expand Down Expand Up @@ -50,10 +51,18 @@ export class SendMessageChat extends SendMessageBase {

@InstrumentUsecase()
public async execute(command: SendMessageCommand) {
const subscriber = await this.getSubscriberBySubscriberId({
subscriberId: command.subscriberId,
_environmentId: command.environmentId,
});
const [subscriber, actorSubscriber] = await Promise.all([
this.getSubscriberBySubscriberId({
subscriberId: command.subscriberId,
_environmentId: command.environmentId,
}),
command.job._actorId
? this.getSubscriberById({
_id: command.job._actorId,
_environmentId: command.environmentId,
})
: Promise.resolve(null),
]);
if (!subscriber) throw new PlatformException('Subscriber not found');

Sentry.addBreadcrumb({
Expand All @@ -70,6 +79,7 @@ export class SendMessageChat extends SendMessageBase {
events: command.events,
total_count: command.events?.length,
},
actor: actorSubscriber,
...command.payload,
};

Expand Down Expand Up @@ -170,14 +180,14 @@ export class SendMessageChat extends SendMessageBase {
_environmentId: command.environmentId,
_organizationId: command.organizationId,
_subscriberId: command._subscriberId,
_actorId: command.job._actorId,
_templateId: command._templateId,
_messageTemplateId: chatChannel.template?._id,
channel: ChannelTypeEnum.CHAT,
transactionId: command.transactionId,
chatWebhookUrl: chatWebhookUrl,
content: this.storeContent() ? content : null,
providerId: subscriberChannel.providerId,
_jobId: command.jobId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be removed

});

if (!integration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
IAttachmentOptions,
IEmailOptions,
LogCodeEnum,
ActorTypeEnum,
} from '@novu/shared';
import * as Sentry from '@sentry/node';
import {
Expand Down Expand Up @@ -52,10 +53,18 @@ export class SendMessageEmail extends SendMessageBase {

@InstrumentUsecase()
public async execute(command: SendMessageCommand) {
const subscriber = await this.getSubscriberBySubscriberId({
subscriberId: command.subscriberId,
_environmentId: command.environmentId,
});
const [subscriber, actorSubscriber] = await Promise.all([
this.getSubscriberBySubscriberId({
subscriberId: command.subscriberId,
_environmentId: command.environmentId,
}),
command.job._actorId
? this.getSubscriberById({
_id: command.job._actorId,
_environmentId: command.environmentId,
})
: Promise.resolve(null),
]);
if (!subscriber) throw new PlatformException(`Subscriber ${command.subscriberId} not found`);

let integration: IntegrationEntity | undefined = undefined;
Expand Down Expand Up @@ -90,6 +99,7 @@ export class SendMessageEmail extends SendMessageBase {
if (!emailChannel.template) throw new PlatformException('Email channel template not found');

const email = command.payload.email || subscriber.email;
const { actor } = emailChannel.template;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed :)


Sentry.addBreadcrumb({
message: 'Sending Email',
Expand Down Expand Up @@ -143,6 +153,7 @@ export class SendMessageEmail extends SendMessageBase {
total_count: command.events?.length,
},
subscriber,
actor: actorSubscriber,
},
};

Expand All @@ -165,6 +176,11 @@ export class SendMessageEmail extends SendMessageBase {
overrides,
templateIdentifier: command.identifier,
_jobId: command.jobId,
...(actor &&
actor.type !== ActorTypeEnum.NONE && {
actor,
_actorId: command.job?._actorId,
}),
Comment on lines +179 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to be saved on message

});

let replyToAddress: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ export class SendMessageInApp extends SendMessageBase {

@InstrumentUsecase()
public async execute(command: SendMessageCommand) {
const subscriber = await this.getSubscriberBySubscriberId({
subscriberId: command.subscriberId,
_environmentId: command.environmentId,
});
const [subscriber, actorSubscriber] = await Promise.all([
this.getSubscriberBySubscriberId({
subscriberId: command.subscriberId,
_environmentId: command.environmentId,
}),
command.job._actorId
? this.getSubscriberById({
_id: command.job._actorId,
_environmentId: command.environmentId,
})
: Promise.resolve(null),
]);
if (!subscriber) throw new PlatformException('Subscriber not found');
if (!command.step.template) throw new PlatformException('Template not found');

Expand Down Expand Up @@ -103,6 +111,7 @@ export class SendMessageInApp extends SendMessageBase {
inAppChannel.template.content,
command.payload,
subscriber,
actorSubscriber,
command,
organization
);
Expand All @@ -112,6 +121,7 @@ export class SendMessageInApp extends SendMessageBase {
inAppChannel.template.cta?.data?.url,
command.payload,
subscriber,
actorSubscriber,
command,
organization
);
Expand All @@ -125,6 +135,7 @@ export class SendMessageInApp extends SendMessageBase {
action.content,
command.payload,
subscriber,
actorSubscriber,
command,
organization
);
Expand All @@ -146,6 +157,7 @@ export class SendMessageInApp extends SendMessageBase {
_notificationId: command.notificationId,
_environmentId: command.environmentId,
_subscriberId: command._subscriberId,
_actorId: command.job._actorId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed in searching for an old message

_templateId: command._templateId,
_messageTemplateId: inAppChannel.template._id,
channel: ChannelTypeEnum.IN_APP,
Expand Down Expand Up @@ -299,6 +311,7 @@ export class SendMessageInApp extends SendMessageBase {
content: string | IEmailBlock[],
payload: any,
subscriber: SubscriberEntity,
actor: SubscriberEntity | null,
command: SendMessageCommand,
organization: OrganizationEntity | null
): Promise<string> {
Expand All @@ -316,6 +329,7 @@ export class SendMessageInApp extends SendMessageBase {
logo: organization?.branding?.logo,
color: organization?.branding?.color || '#f47373',
},
actor,
...payload,
},
})
Expand Down
Loading