diff --git a/functions/src/index.ts b/functions/src/index.ts index 3a9f80c..5ae0cb5 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -1,59 +1,59 @@ import { onRequest } from "firebase-functions/v2/https"; import { Request } from "firebase-functions/v2/https"; -import { onDocumentCreated } from 'firebase-functions/v2/firestore'; import * as admin from 'firebase-admin'; import * as express from "express"; import * as logger from "firebase-functions/logger"; +const functions = require('firebase-functions'); const formData = require('form-data'); const Mailgun = require('mailgun.js'); const { convert } = require('html-to-text'); const twilio = require('twilio') export const sendEmail = onRequest((request: Request, response: express.Response) => { - const mailgun = new Mailgun(formData); - const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY }); + const mailgun = new Mailgun(formData); + 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 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); - }); + 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); + }); }); export const sendSMS = onRequest((request: Request, response: express.Response) => { - const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN); - const { to, body } = request.body; - client.messages - .create({ - body: body, - from: process.env.TWILIO_PHONE_NUMBER, - to: to - }) - .then((message: any) => { - logger.info('SMS sent successfully:', message.sid); - response.json({ success: true, messageId: message.sid }); - }) - .catch((error: any) => { - logger.error('Error sending SMS:', error); - response.status(500).json({ success: false, error: error.message }); - }); + const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN); + const { to, body } = request.body; + client.messages + .create({ + body: body, + from: process.env.TWILIO_PHONE_NUMBER, + to: to + }) + .then((message: any) => { + logger.info('SMS sent successfully:', message.sid); + response.json({ success: true, messageId: message.sid }); + }) + .catch((error: any) => { + logger.error('Error sending SMS:', error); + response.status(500).json({ success: false, error: error.message }); + }); }); @@ -64,9 +64,8 @@ interface Invitation { invitedByName: string; } -export const sendInvitationNotification = onDocumentCreated( - 'invitations/{invitationId}', - async (event) => { +export const sendInvitationNotification = functions.region('asia-south1').firestore.document('notifications/{notificationId}') + .onCreate(async (event: { data: { data: () => Invitation; }; params: { invitationId: any; }; }) => { const invitation = event.data?.data() as Invitation; const invitationId = event.params.invitationId; @@ -132,4 +131,4 @@ export const sendInvitationNotification = onDocumentCreated( return null; } } -); \ No newline at end of file + ); \ No newline at end of file