Skip to content

Commit

Permalink
fix(providers): Sparkpost provider bugs (#3736)
Browse files Browse the repository at this point in the history
* fix: sparkpost provider region issue

* fix(provider): reuse region field for sparkpost

* feat: render sparkpost region as dropdown

* feat(providers): transform legacy region values for sparkpost

---------

Co-authored-by: Dima Grossman <[email protected]>
  • Loading branch information
michaldziuba03 and scopsy authored Nov 6, 2023
1 parent 117389c commit 40393b7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { CheckIntegrationEMail } from './check-integration-email.usecase';
import { CheckIntegrationCommand } from './check-integration.command';
import { ChannelTypeEnum, ICredentialsDto } from '@novu/shared';
import { ChannelTypeEnum } from '@novu/shared';

@Injectable()
export class CheckIntegration {
Expand Down
19 changes: 18 additions & 1 deletion apps/web/src/pages/integrations/components/IntegrationInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMantineTheme } from '@mantine/core';
import styled from '@emotion/styled';
import { CredentialsKeyEnum, IConfigCredentials, secureCredentials } from '@novu/shared';

import { Input, PasswordInput, Switch, Textarea, Text, Tooltip } from '@novu/design-system';
import { Input, PasswordInput, Switch, Textarea, Text, Tooltip, Select } from '@novu/design-system';
import { IntegrationSecretTextarea } from './IntegrationSecretTextarea';

const SwitchWrapper = styled.div`
Expand Down Expand Up @@ -67,6 +67,23 @@ export function IntegrationInput({
);
}

if (credential.type === 'dropdown') {
return (
<Select
{...field}
label={credential.displayName}
required={credential.required}
description={credential.description}
data-test-id={credential.key}
error={errors[credential.key]?.message}
data={credential.dropdown?.map((item) => ({
label: item.name,
value: item.value,
}))}
/>
);
}

if (credential.type === 'switch') {
let switchComponent = (
<Switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,15 @@ export const sparkpostConfig: IConfigCredentials[] = [
},
{
key: CredentialsKeyEnum.Region,
displayName: 'EU',
description: 'Use `eu` if your account is registered to SparkPost EU',
type: 'boolean',
displayName: 'Region',
description: 'Use EU if your account is registered to SparkPost EU',
type: 'dropdown',
required: false,
value: null,
dropdown: [
{ name: 'Default', value: null },
{ name: 'EU', value: 'eu' },
],
},
...mailConfigBase,
];
Expand Down
4 changes: 4 additions & 0 deletions libs/shared/src/consts/providers/provider.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface IConfigCredentials {
text: string;
when?: boolean;
};
dropdown?: Array<{
name: string;
value: string | null;
}>;
}

export interface ILogoFileName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ export class SparkPostHandler extends BaseHandler {
const config = {
from: from as string,
apiKey: credentials.apiKey as string,
eu: false,
region: credentials.region as string,
senderName: credentials.senderName as string,
};

if (credentials.region?.toLowerCase() === 'eu') {
config.eu = true;
}

this.provider = new SparkPostEmailProvider(config);
}
}
2 changes: 1 addition & 1 deletion providers/sparkpost/src/lib/sparkpost.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SparkPostEmailProvider } from './sparkpost.provider';
const mockConfig = {
apiKey:
'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',
eu: false,
region: undefined,
from: '[email protected]',
senderName: 'test',
};
Expand Down
25 changes: 22 additions & 3 deletions providers/sparkpost/src/lib/sparkpost.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ export class SparkPostEmailProvider implements IEmailProvider {
constructor(
private config: {
apiKey: string;
eu: boolean;
region: string;
from: string;
senderName: string;
}
) {
const endpoint = this.getEndpoint(config.region);
this.client = new SparkPost(config.apiKey, {
endpoint: config.eu ? 'https://api.eu.sparkpost.com:443' : undefined,
endpoint,
});
}

Expand All @@ -41,7 +42,7 @@ export class SparkPostEmailProvider implements IEmailProvider {

const files: Array<{ name: string; type: string; data: string }> = [];

attachments.forEach((attachment) => {
attachments?.forEach((attachment) => {
files.push({
name: attachment.name || randomUUID(),
type: attachment.mime,
Expand All @@ -62,6 +63,7 @@ export class SparkPostEmailProvider implements IEmailProvider {

return {
id: sent.results.id,
date: new Date().toISOString(),
};
}

Expand Down Expand Up @@ -90,4 +92,21 @@ export class SparkPostEmailProvider implements IEmailProvider {
};
}
}

private transformLegacyRegion(region: string | boolean) {
if (region === 'true' || region === true) return 'eu';

return region;
}

private getEndpoint(_region: string) {
const region = this.transformLegacyRegion(_region);

switch (region) {
case 'eu':
return 'https://api.eu.sparkpost.com:443';
default:
return;
}
}
}

1 comment on commit 40393b7

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.