Compare commits
No commits in common. "bd601d896d27237ac7032f6ae749a79e5fd48c89" and "0402e307dfb61aef6816701bca029c3f5992ee15" have entirely different histories.
bd601d896d
...
0402e307df
@ -27,6 +27,9 @@ jobs:
|
|||||||
|
|
||||||
- name: Replace variables in .env
|
- name: Replace variables in .env
|
||||||
run: |
|
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_ACCOUNT_SID}#/${{ secrets.TWILIO_ACCOUNT_SID }}/" functions/.env
|
||||||
sed -i "s/#{TWILIO_AUTH_TOKEN}#/${{ secrets.TWILIO_AUTH_TOKEN }}/" 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
|
sed -i "s/#{TWILIO_PHONE_NUMBER}#/${{ secrets.TWILIO_PHONE_NUMBER }}/" functions/.env
|
||||||
@ -47,7 +50,7 @@ jobs:
|
|||||||
- name: "Replace #{SERVICES_RGN}# in all .ts files"
|
- name: "Replace #{SERVICES_RGN}# in all .ts files"
|
||||||
run: |
|
run: |
|
||||||
find . -type f -name "*.ts" -exec sed -i "s/#{SERVICES_RGN}#/${{ vars.SERVICES_RGN }}/g" {} +
|
find . -type f -name "*.ts" -exec sed -i "s/#{SERVICES_RGN}#/${{ vars.SERVICES_RGN }}/g" {} +
|
||||||
|
cat functions/src/email/sendEmail.ts
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
npm install -g typescript
|
npm install -g typescript
|
||||||
|
|||||||
@ -1,64 +0,0 @@
|
|||||||
name: Deploy FitLien services to Dev
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- qa
|
|
||||||
jobs:
|
|
||||||
deploy:
|
|
||||||
name: Deploy to QA
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Node
|
|
||||||
uses: actions/setup-node@v3
|
|
||||||
with:
|
|
||||||
node-version: 22
|
|
||||||
|
|
||||||
- name: Clean install
|
|
||||||
run: npm clean-install
|
|
||||||
|
|
||||||
- name: Copy .env.example to .env
|
|
||||||
run: cp functions/.env.example functions/.env
|
|
||||||
|
|
||||||
- name: Replace variables in .env
|
|
||||||
run: |
|
|
||||||
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
|
|
||||||
sed -i "s/#{SERVICES_RGN}#/${{ vars.SERVICES_RGN }}/" functions/.env
|
|
||||||
sed -i "s/#{GOOGLE_MAPS_API_KEY}#/${{ secrets.GOOGLE_MAPS_API_KEY }}/" functions/.env
|
|
||||||
sed -i "s/#{SES_FROM_EMAIL}#/${{ vars.SES_FROM_EMAIL }}/" functions/.env
|
|
||||||
sed -i "s/#{SES_REPLY_TO_EMAIL}#/${{ vars.SES_REPLY_TO_EMAIL }}/" functions/.env
|
|
||||||
sed -i "s/#{AWS_ACCESS_KEY_ID}#/${{ secrets.AWS_ACCESS_KEY_ID }}/" functions/.env
|
|
||||||
sed -i "s/#{AWS_SECRET_ACCESS_KEY}#/${{ secrets.AWS_SECRET_ACCESS_KEY }}/" functions/.env
|
|
||||||
sed -i "s/#{AWS_REGION}#/${{ secrets.AWS_REGION }}/" functions/.env
|
|
||||||
sed -i "s/#{PHONEPE_CLIENT_ID}#/${{ secrets.PHONEPE_CLIENT_ID }}/" functions/.env
|
|
||||||
sed -i "s/#{PHONEPE_CLIENT_SECRET}#/${{ secrets.PHONEPE_CLIENT_SECRET }}/" functions/.env
|
|
||||||
sed -i "s/#{PHONEPE_API_URL}#/${{ secrets.PHONEPE_API_URL }}/" functions/.env
|
|
||||||
sed -i "s/#{PHONEPE_WEBHOOK_USERNAME}#/${{ secrets.PHONEPE_WEBHOOK_USERNAME }}/" functions/.env
|
|
||||||
sed -i "s/#{PHONEPE_WEBHOOK_PASSWORD}#/${{ secrets.PHONEPE_WEBHOOK_PASSWORD }}/" functions/.env
|
|
||||||
|
|
||||||
cat functions/.env
|
|
||||||
- name: "Replace #{SERVICES_RGN}# in all .ts files"
|
|
||||||
run: |
|
|
||||||
find . -type f -name "*.ts" -exec sed -i "s/#{SERVICES_RGN}#/${{ vars.SERVICES_RGN }}/g" {} +
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: |
|
|
||||||
npm install -g typescript
|
|
||||||
cd functions
|
|
||||||
npm install
|
|
||||||
npx tsc
|
|
||||||
cd ..
|
|
||||||
ls -la
|
|
||||||
|
|
||||||
- name: Deploy
|
|
||||||
run: |
|
|
||||||
curl -sL firebase.tools | upgrade=true bash
|
|
||||||
firebase use --token ${{ secrets.FIREBASE_TOKEN }} qa
|
|
||||||
firebase deploy --token "${{ secrets.FIREBASE_TOKEN }}" --force --non-interactive
|
|
||||||
@ -1 +1,3 @@
|
|||||||
|
export { sendEmailMessage } from './sendEmail';
|
||||||
|
export { sendEmailWithAttachment } from './sendEmailWithAttachment';
|
||||||
export { sendEmailSES } from './sendEmailSES';
|
export { sendEmailSES } from './sendEmailSES';
|
||||||
|
|||||||
40
functions/src/email/sendEmail.ts
Normal file
40
functions/src/email/sendEmail.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
81
functions/src/email/sendEmailWithAttachment.ts
Normal file
81
functions/src/email/sendEmailWithAttachment.ts
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
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<void>((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) });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -10,7 +10,7 @@ setGlobalOptions({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export * from './shared/config';
|
export * from './shared/config';
|
||||||
export { sendEmailSES } from './email';
|
export { sendEmailMessage, sendEmailWithAttachment, sendEmailSES } from './email';
|
||||||
export { sendSMSMessage } from './sms';
|
export { sendSMSMessage } from './sms';
|
||||||
export { accessFile } from './storage';
|
export { accessFile } from './storage';
|
||||||
export { processNotificationOnCreate } from './notifications';
|
export { processNotificationOnCreate } from './notifications';
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user