Skip to content

Commit

Permalink
fix: Optional email service proper handling
Browse files Browse the repository at this point in the history
  • Loading branch information
that-one-arab committed Dec 26, 2024
1 parent 5c651bc commit 147c2ce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
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

0 comments on commit 147c2ce

Please sign in to comment.