diff --git a/packages/backend/src/common/constants/env.constants.ts b/packages/backend/src/common/constants/env.constants.ts index 315c1492..033ae292 100644 --- a/packages/backend/src/common/constants/env.constants.ts +++ b/packages/backend/src/common/constants/env.constants.ts @@ -10,6 +10,7 @@ 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, @@ -17,9 +18,9 @@ export const ENV = { 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(",") : [], diff --git a/packages/backend/src/common/constants/error.constants.ts b/packages/backend/src/common/constants/error.constants.ts index a7a1c466..e04c7260 100644 --- a/packages/backend/src/common/constants/error.constants.ts +++ b/packages/backend/src/common/constants/error.constants.ts @@ -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, diff --git a/packages/backend/src/user/services/email.service.ts b/packages/backend/src/user/services/email.service.ts index fe395f04..8e0031e5 100644 --- a/packages/backend/src/user/services/email.service.ts +++ b/packages/backend/src/user/services/email.service.ts @@ -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; + } }; }