Skip to content

Commit

Permalink
fix: Sendgrid can't create list if name is too long (#669)
Browse files Browse the repository at this point in the history
* fix: Sendgrid can't create list if name is too long

* fix: Lint error
  • Loading branch information
sashko9807 authored Oct 9, 2024
1 parent 4c1bfba commit c5eb0a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions apps/api/src/notifications/helpers/truncateNameToBytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function truncateNameToBytes(name: string, maxBytes: number) {
const encoder = new TextEncoder()
const decoder = new TextDecoder()

const encoded = encoder.encode(name)
if (encoded.length <= maxBytes) {
return name
}

let truncated = encoded.slice(0, maxBytes)

for (let i = maxBytes; i > 0; i--) {
try {
return decoder.decode(truncated)
} catch (e) {
truncated = truncated.slice(0, -1)
}
}

return ''
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import {
} from './notifications.sendgrid.types'
import { ClientRequest } from '@sendgrid/client/src/request'
import { DateTime } from 'luxon'

import { truncateNameToBytes } from '../helpers/truncateNameToBytes'

import { MassMailDto } from '../dto/massmail.dto'
import { ContactsMap } from '../notifications.service'
import { MailDataRequired } from '@sendgrid/mail'
import { PersonalizationData } from '@sendgrid/helpers/classes/personalization'


@Injectable()
export class SendGridNotificationsProvider
implements NotificationsProviderInterface<SendGridParams>
Expand Down Expand Up @@ -53,6 +57,7 @@ export class SendGridNotificationsProvider
return response as SGClientResponse<ContactListRes>
}
async createNewContactList(data: SendGridParams['CreateListParams']) {
data.name = truncateNameToBytes(data.name, 99)
const request = {
url: `/v3/marketing/lists`,
method: 'POST',
Expand Down

0 comments on commit c5eb0a7

Please sign in to comment.