Adding missing type inferences
This commit is contained in:
parent
3f3b42e2ec
commit
ee27b1b3cc
@ -1,6 +1,9 @@
|
|||||||
import { onRequest } from "firebase-functions/v2/https";
|
import { onRequest } from "firebase-functions/v2/https";
|
||||||
import { getCorsHandler } from "../shared/middleware";
|
import { getCorsHandler } from "../shared/middleware";
|
||||||
import { getAdmin, getLogger } from "../shared/config";
|
import { getAdmin, getLogger } from "../shared/config";
|
||||||
|
import { Request } from "firebase-functions/v2/https";
|
||||||
|
import { Response } from "express";
|
||||||
|
|
||||||
|
|
||||||
const corsHandler = getCorsHandler();
|
const corsHandler = getCorsHandler();
|
||||||
const admin = getAdmin();
|
const admin = getAdmin();
|
||||||
@ -8,7 +11,7 @@ const logger = getLogger();
|
|||||||
|
|
||||||
export const registerClient = onRequest({
|
export const registerClient = onRequest({
|
||||||
region: '#{SERVICES_RGN}#'
|
region: '#{SERVICES_RGN}#'
|
||||||
}, async (req, res) => {
|
}, async (req: Request, res: Response) => {
|
||||||
return corsHandler(req, res, async () => {
|
return corsHandler(req, res, async () => {
|
||||||
try {
|
try {
|
||||||
if (req.method !== 'POST') {
|
if (req.method !== 'POST') {
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { getLogger } from "../shared/config";
|
|||||||
import { getCorsHandler } from "../shared/middleware";
|
import { getCorsHandler } from "../shared/middleware";
|
||||||
import { onRequest } from "firebase-functions/v2/https";
|
import { onRequest } from "firebase-functions/v2/https";
|
||||||
import { Request } from "firebase-functions/v2/https";
|
import { Request } from "firebase-functions/v2/https";
|
||||||
|
import { Response } from "express";
|
||||||
import { SESClient } from "@aws-sdk/client-ses";
|
import { SESClient } from "@aws-sdk/client-ses";
|
||||||
import { SendEmailCommand, SendRawEmailCommand } from "@aws-sdk/client-ses";
|
import { SendEmailCommand, SendRawEmailCommand } from "@aws-sdk/client-ses";
|
||||||
import { HttpsError } from "firebase-functions/v2/https";
|
import { HttpsError } from "firebase-functions/v2/https";
|
||||||
@ -19,8 +20,8 @@ interface EmailRequest {
|
|||||||
from: string;
|
from: string;
|
||||||
replyTo?: string;
|
replyTo?: string;
|
||||||
attachments?: Attachment[];
|
attachments?: Attachment[];
|
||||||
fileUrl?: string;
|
fileUrl?: string;
|
||||||
fileName?: string;
|
fileName?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Attachment {
|
interface Attachment {
|
||||||
@ -136,13 +137,13 @@ async function downloadFileFromUrl(url: string): Promise<Buffer> {
|
|||||||
|
|
||||||
export const sendEmailSES = onRequest({
|
export const sendEmailSES = onRequest({
|
||||||
region: 'asia-south1'
|
region: 'asia-south1'
|
||||||
}, (request: Request, response) => {
|
}, (request: Request, response: Response) => {
|
||||||
return corsHandler(request, response, async () => {
|
return corsHandler(request, response, async () => {
|
||||||
try {
|
try {
|
||||||
const toAddress = request.body.toAddress;
|
const toAddress = request.body.toAddress;
|
||||||
const subject = request.body.subject;
|
const subject = request.body.subject;
|
||||||
const message = request.body.message;
|
const message = request.body.message;
|
||||||
|
|
||||||
// Initialize data with basic fields
|
// Initialize data with basic fields
|
||||||
const data: EmailRequest = {
|
const data: EmailRequest = {
|
||||||
to: toAddress,
|
to: toAddress,
|
||||||
@ -153,42 +154,42 @@ export const sendEmailSES = onRequest({
|
|||||||
replyTo: process.env.SES_REPLY_TO_EMAIL || 'support@fitlien.com',
|
replyTo: process.env.SES_REPLY_TO_EMAIL || 'support@fitlien.com',
|
||||||
attachments: request.body.attachments as Attachment[] || []
|
attachments: request.body.attachments as Attachment[] || []
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle file URL if provided
|
// Handle file URL if provided
|
||||||
if (request.body.fileUrl && request.body.fileName) {
|
if (request.body.fileUrl && request.body.fileName) {
|
||||||
logger.info(`Downloading attachment from URL: ${request.body.fileUrl}`);
|
logger.info(`Downloading attachment from URL: ${request.body.fileUrl}`);
|
||||||
try {
|
try {
|
||||||
const fileContent = await downloadFileFromUrl(request.body.fileUrl);
|
const fileContent = await downloadFileFromUrl(request.body.fileUrl);
|
||||||
|
|
||||||
// If attachments array doesn't exist, create it
|
// If attachments array doesn't exist, create it
|
||||||
if (!data.attachments) {
|
if (!data.attachments) {
|
||||||
data.attachments = [];
|
data.attachments = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the downloaded file as an attachment
|
// Add the downloaded file as an attachment
|
||||||
data.attachments.push({
|
data.attachments.push({
|
||||||
filename: request.body.fileName,
|
filename: request.body.fileName,
|
||||||
content: fileContent,
|
content: fileContent,
|
||||||
contentType: mime.lookup(request.body.fileName) || 'application/octet-stream'
|
contentType: mime.lookup(request.body.fileName) || 'application/octet-stream'
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Successfully downloaded attachment: ${request.body.fileName}`);
|
logger.info(`Successfully downloaded attachment: ${request.body.fileName}`);
|
||||||
} catch (downloadError) {
|
} catch (downloadError) {
|
||||||
logger.error(`Failed to download attachment: ${downloadError}`);
|
logger.error(`Failed to download attachment: ${downloadError}`);
|
||||||
throw new Error(`Failed to process attachment: ${downloadError}`);
|
throw new Error(`Failed to process attachment: ${downloadError}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.to || !data.subject || !data.html || !data.from) {
|
if (!data.to || !data.subject || !data.html || !data.from) {
|
||||||
throw new HttpsError(
|
throw new HttpsError(
|
||||||
'invalid-argument',
|
'invalid-argument',
|
||||||
'Missing required email fields'
|
'Missing required email fields'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`Sending Email '${data.subject}' to '${data.to}' from '${data.from}'`);
|
logger.info(`Sending Email '${data.subject}' to '${data.to}' from '${data.from}'`);
|
||||||
const recipients = Array.isArray(data.to) ? data.to : [data.to];
|
const recipients = Array.isArray(data.to) ? data.to : [data.to];
|
||||||
|
|
||||||
if (data.attachments && data.attachments.length > 0) {
|
if (data.attachments && data.attachments.length > 0) {
|
||||||
const messageResult = await sendEmailWithAttachments(data, recipients);
|
const messageResult = await sendEmailWithAttachments(data, recipients);
|
||||||
response.status(200).json(messageResult);
|
response.status(200).json(messageResult);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user