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: Optional email service proper handling #202

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
7 changes: 4 additions & 3 deletions packages/backend/src/common/constants/env.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ export const IS_DEV = isDev(_nodeEnv);
const db = IS_DEV ? "dev_calendar" : "prod_calendar";

const _error = ">> TODO: set this value in .env <<";
const _optional = "";

export const ENV = {
BASEURL: process.env["BASEURL"] as string,
CHANNEL_EXPIRATION_MIN: process.env["CHANNEL_EXPIRATION_MIN"] || "10",
CLIENT_ID: process.env["CLIENT_ID"] || _error,
CLIENT_SECRET: process.env["CLIENT_SECRET"] || _error,
DB: db,
EMAILER_KEY: process.env["EMAILER_API_KEY"] || _error,
EMAILER_SECRET: process.env["EMAILER_API_SECRET"] || _error,
EMAILER_LIST_ID: process.env["EMAILER_LIST_ID"] || _error,
EMAILER_KEY: process.env["EMAILER_API_KEY"] || _optional,
EMAILER_SECRET: process.env["EMAILER_API_SECRET"] || _optional,
EMAILER_LIST_ID: process.env["EMAILER_LIST_ID"] || _optional,
MONGO_URI: process.env["MONGO_URI"] || _error,
NODE_ENV: _nodeEnv,
ORIGINS_ALLOWED: process.env["CORS"] ? process.env["CORS"].split(",") : [],
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/common/constants/error.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export const DbError = {
};

export const EmailerError = {
IncorrectApiKey: {
description:
"Incorrect API key. Please make sure environment variables beginning with EMAILER_ are set correctly",
status: Status.BAD_REQUEST,
isOperational: true,
},
AddToListFailed: {
description: "Failed to add email to list",
status: Status.UNSURE,
Expand Down
37 changes: 31 additions & 6 deletions packages/backend/src/user/services/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,41 @@ const logger = Logger("app:emailer.service");

class EmailService {
addToEmailList = async (email: string, firstName: string) => {
if (!ENV.EMAILER_LIST_ID && !ENV.EMAILER_SECRET) {
logger.warn(
"Email service is disabled. Required environment variables are missing."
);
return;
}

const url = `https://api.convertkit.com/v3/tags/${ENV.EMAILER_LIST_ID}/subscribe?api_secret=${ENV.EMAILER_SECRET}&email=${email}&first_name=${firstName}`;

const response = await axios.post(url);
try {
const response = await axios.post(url);

if (response.status !== 200) {
throw error(EmailerError.AddToListFailed, "Failed to add email to list");
logger.error(response.data);
}
if (response.status !== 200) {
throw error(
EmailerError.AddToListFailed,
"Failed to add email to list"
);
logger.error(response.data);
}

return response;
return response;
} catch (e) {
if (
axios.isAxiosError(e) &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
e?.response?.data?.message === "API Key not valid"
) {
throw error(
EmailerError.IncorrectApiKey,
"Incorrect API key. Please make sure environment variables beginning with EMAILER_ are set correctly or set to an empty string."
);
}

throw e;
}
};
}

Expand Down