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

fix: add support for SendGrid categories and streamline SendGrid detection #2245

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

Gustolandia
Copy link
Contributor

@Gustolandia Gustolandia commented Dec 21, 2024

This PR addresses the following improvements and fixes for the Firestore-Send-Email extension:

Enhancement: Increase in Payload Fields
Developers can now include categories.
Introduced a dynamic method for detecting SendGrid usage based on the SMTP_CONNECTION_URI.
Bug Fix: Categories Not Being Sent
The categories field was not properly passed to the SendGrid API in the previous implementation.
Modified the email preparation and transport logic to ensure that the categories field is passed as per SendGrid's requirements.
Refactored Code and Tests
Removed the unused nodemailer-sendgrid dependency to simplify the codebase and avoid confusion.
Updated helpers.ts and corresponding tests in helpers.test.ts to support the new dynamic SendGrid detection.
Added logs to indicate whether the email is being sent via SendGrid or the standard transport.
Changes:

Code Changes:
Updated src/index.ts to dynamically detect SendGrid and utilize the appropriate sending method.
Modified src/helpers.ts to add an isSendGrid function and streamline SMTP transport setup.
Updated src/types.ts to reflect the additional flexibility in payload fields.
Tests:
Refactored helpers.test.ts to test the new isSendGrid function.
Added additional test cases to validate the proper handling of various SMTP connection URIs.
Dependencies:
Removed nodemailer-sendgrid from package.json and package-lock.json.
How to Test:

Deploy the updated extension in a local Firebase project with the emulators enabled.
Use the following payload in the Firestore mail collection:

{
  "to": ["example@example.com"],
  "categories": ["Example_Category"],
  "message": {
    "subject": "Final Test for Categories",
    "text": "This is a test email to see if categories work.",
    "html": "<strong>This is a test email to see if categories work.</strong>"
  }
}

Verify:
The email is successfully sent.
The categories field is correctly registered in SendGrid.
Logs indicate whether the SendGrid or standard transport was used.

Linked Issues:

Resolves #2053
Reviewer Checklist:
Code adheres to the existing style and conventions.
New features are covered by unit tests.
Breaking changes are documented and communicated effectively.

@Gustolandia Gustolandia added type: bug Something isn't working invertase Assigned to external Invertase team extension: firestore-send-email Related to firestore-send-email extension labels Dec 21, 2024
@Gustolandia Gustolandia self-assigned this Dec 21, 2024
@Gustolandia Gustolandia requested a review from a team as a code owner December 21, 2024 01:55
Copy link
Contributor

@cabljac cabljac left a comment

Choose a reason for hiding this comment

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

Generally looks good! a few comments about logs/deleting code comments to look at

@Gustolandia Gustolandia force-pushed the @invertase/Extensions/firestore-send-email/2053 branch from 10dccd8 to 4a91d62 Compare December 23, 2024 23:12
@Gustolandia Gustolandia force-pushed the @invertase/Extensions/firestore-send-email/2053 branch from 4a91d62 to fa49236 Compare December 23, 2024 23:15
});

test("return invalid smtpConnectionUri credentials with invalid seprator", () => {
Copy link
Member

Choose a reason for hiding this comment

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

We should keep this test

@@ -153,30 +134,7 @@ describe("set server credentials helper function", () => {
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return valid smtpConnectionUri credentials with valid special chars in connectionUri password", () => {
Copy link
Member

Choose a reason for hiding this comment

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

why this test is removed?

}

async function deliver(
ref: admin.firestore.DocumentReference<QueuePayload>
): Promise<void> {
// Fetch the Firestore document
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Fetch the Firestore document

Comment on lines +349 to +366
msg: {
to: payload.to,
cc: payload.cc,
bcc: payload.bcc,
from: payload.from || config.defaultFrom,
replyTo: payload.replyTo || config.defaultReplyTo,
subject: payload.message?.subject,
text:
typeof payload.message?.text === "string"
? payload.message?.text
: undefined,
html:
typeof payload.message?.html === "string"
? payload.message?.html
: undefined,
categories: payload.categories,
headers: payload.headers,
},
Copy link
Member

Choose a reason for hiding this comment

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

assign the payload to a variable to avoid duplication

Comment on lines -356 to +423
// Wrapping in transaction to allow for automatic retries (#48)
// Update the Firestore document transactionally to allow retries
Copy link
Member

Choose a reason for hiding this comment

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

keep old comment as it's referencing an issue

@@ -82,4 +82,14 @@ export interface QueuePayload {
replyTo?: string;
headers?: any;
attachments: Attachment[];
categories?: string[];
Copy link
Member

Choose a reason for hiding this comment

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

is it needed here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension: firestore-send-email Related to firestore-send-email extension invertase Assigned to external Invertase team type: bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [firestore-send-email] Could not send category to sendGrid
4 participants