From a89168bb74dda90b4e7543ff88431a22acb6cd49 Mon Sep 17 00:00:00 2001 From: Benoy Bose Date: Mon, 26 May 2025 20:01:19 +0530 Subject: [PATCH] Removed unused function --- .gitea/workflows/deploy-dev.yaml | 3 - functions/src/email/index.ts | 2 - functions/src/email/sendEmail.ts | 40 --------- .../src/email/sendEmailWithAttachment.ts | 81 ------------------- 4 files changed, 126 deletions(-) delete mode 100644 functions/src/email/sendEmail.ts delete mode 100644 functions/src/email/sendEmailWithAttachment.ts diff --git a/.gitea/workflows/deploy-dev.yaml b/.gitea/workflows/deploy-dev.yaml index 7862777..413dd79 100644 --- a/.gitea/workflows/deploy-dev.yaml +++ b/.gitea/workflows/deploy-dev.yaml @@ -27,9 +27,6 @@ jobs: - name: Replace variables in .env run: | - sed -i "s/#{MAILGUN_API_KEY}#/${{ secrets.MAILGUN_API_KEY }}/" functions/.env - sed -i "s/#{MAILGUN_SERVER}#/${{ secrets.MAILGUN_SERVER }}/" functions/.env - sed -i "s/#{MAILGUN_FROM_ADDRESS}#/${{ secrets.MAILGUN_FROM_ADDRESS }}/" functions/.env sed -i "s/#{TWILIO_ACCOUNT_SID}#/${{ secrets.TWILIO_ACCOUNT_SID }}/" functions/.env sed -i "s/#{TWILIO_AUTH_TOKEN}#/${{ secrets.TWILIO_AUTH_TOKEN }}/" functions/.env sed -i "s/#{TWILIO_PHONE_NUMBER}#/${{ secrets.TWILIO_PHONE_NUMBER }}/" functions/.env diff --git a/functions/src/email/index.ts b/functions/src/email/index.ts index 8a5ad52..53ac110 100644 --- a/functions/src/email/index.ts +++ b/functions/src/email/index.ts @@ -1,3 +1 @@ -export { sendEmailMessage } from './sendEmail'; -export { sendEmailWithAttachment } from './sendEmailWithAttachment'; export { sendEmailSES } from './sendEmailSES'; diff --git a/functions/src/email/sendEmail.ts b/functions/src/email/sendEmail.ts deleted file mode 100644 index 4200b76..0000000 --- a/functions/src/email/sendEmail.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { onRequest } from "firebase-functions/v2/https"; -import { Request } from "firebase-functions/v2/https"; -import { getCorsHandler } from "../shared/middleware"; -import { getLogger } from "../shared/config"; -import formData from 'form-data'; -import Mailgun from 'mailgun.js'; -const { convert } = require('html-to-text'); - -const mailgun = new Mailgun(formData); -const logger = getLogger(); -const corsHandler = getCorsHandler(); -export const sendEmailMessage = onRequest({ - region: '#{SERVICES_RGN}#' -}, (request: Request, response) => { - return corsHandler(request, response, async () => { - const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY! }); - - const toAddress = request.body.toAddress; - const subject = request.body.subject; - const message = request.body.message; - const options = { - wordwrap: 130, - }; - - const textMessage = convert(message, options); - mailGunClient.messages.create(process.env.MAILGUN_SERVER!, { - from: process.env.MAILGUN_FROM_ADDRESS, - to: toAddress, - subject: subject, - text: textMessage, - html: message - }).then((res: any) => { - logger.info(res); - response.send(res); - }).catch((err: any) => { - logger.error(err); - response.send(err); - }); - }); -}); \ No newline at end of file diff --git a/functions/src/email/sendEmailWithAttachment.ts b/functions/src/email/sendEmailWithAttachment.ts deleted file mode 100644 index 7c1bc74..0000000 --- a/functions/src/email/sendEmailWithAttachment.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { onRequest } from "firebase-functions/v2/https"; -import { Request } from "firebase-functions/v2/https"; -import * as os from 'os'; -import * as path from 'path'; -import * as fs from 'fs'; -import * as https from 'https'; -import { getCorsHandler } from "../shared/middleware"; -import { getLogger } from "../shared/config"; -import formData from 'form-data'; -import Mailgun from 'mailgun.js'; -const { convert } = require('html-to-text'); - -const mailgun = new Mailgun(formData); -const logger = getLogger(); -const corsHandler = getCorsHandler(); -export const sendEmailWithAttachment = onRequest({ - region: '#{SERVICES_RGN}#' -}, async (request: Request, response) => { - return corsHandler(request, response, async () => { - try { - const { toAddress, subject, message, fileUrl, fileName } = request.body; - - if (!toAddress || !subject || !message || !fileUrl) { - response.status(400).json({ - error: 'Missing required fields (toAddress, subject, message, fileUrl)' - }); - return; - } - - const tempFilePath = path.join(os.tmpdir(), fileName || 'attachment.pdf'); - await new Promise((resolve, reject) => { - const file = fs.createWriteStream(tempFilePath); - https.get(fileUrl, (res) => { - res.pipe(file); - file.on('finish', () => { - file.close(); - resolve(); - }); - }).on('error', (err) => { - fs.unlink(tempFilePath, () => { }); - reject(err); - }); - }); - - try { - const client = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY! }); - - const options = { - wordwrap: 130, - }; - const textMessage = convert(message, options); - const fileBuffer = fs.readFileSync(tempFilePath); - const attachmentFilename = fileName || path.basename(fileUrl.split('?')[0]); - - const data = { - from: process.env.MAILGUN_FROM_ADDRESS, - to: toAddress, - subject: subject, - text: textMessage, - html: message, - attachment: { - data: fileBuffer, - filename: attachmentFilename, - contentType: 'application/pdf', - } - }; - - const result = await client.messages.create(process.env.MAILGUN_SERVER!, data); - fs.unlinkSync(tempFilePath); - logger.info('Email with attachment from URL sent successfully'); - response.json({ success: true, result }); - - } catch (e) { - console.error(`Error while sending E-mail. Error: ${e}`); - } - } catch (error) { - logger.error('Error sending email with attachment from URL:', error); - response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) }); - } - }); -}); \ No newline at end of file