Avoiding recursive references

This commit is contained in:
Benoy Bose 2025-04-17 19:55:26 +05:30
parent 370573de1a
commit 59bc1fde6b
15 changed files with 58 additions and 37 deletions

View File

@ -1,2 +1,2 @@
export * from './sendEmail';
export * from './sendEmailWithAttachment';
export { sendEmailMessage } from './sendEmail';
export { sendEmailWithAttachment } from './sendEmailWithAttachment';

View File

@ -1,7 +1,7 @@
export * from './shared/config';
export * from './email';
export * from './storage';
export * from './sms';
export * from './notifications';
export * from './payments';
export * from './places';
export { sendEmailMessage, sendEmailWithAttachment } from './email';
export { accessFile } from './storage';
export { sendSMSMessage } from './sms';
export { processNotificationOnCreate } from './notifications';
export { createCashfreeLink, createCashfreeOrder, verifyCashfreePayment } from './payments';
export { getPlaceDetails, getPlacesAutocomplete } from './places';

View File

@ -1 +1 @@
export * from './processNotification';
export { processNotificationOnCreate } from './processNotification';

View File

@ -1,6 +1,10 @@
import { onDocumentCreated } from "firebase-functions/v2/firestore";
import { logger } from "../shared/config";
import { admin } from "../shared/config";
import { getLogger } from "../shared/config";
import { getAdmin } from "../shared/config";
import * as admin from 'firebase-admin';
const app = getAdmin();
const logger = getLogger();
interface NotificationData {
notificationSent?: boolean;
@ -49,7 +53,7 @@ export const processNotificationOnCreate = onDocumentCreated({
const message = prepareNotificationMessage(notification, fcmToken);
try {
const fcmResponse = await admin.messaging().send({
const fcmResponse = await app.messaging().send({
...message,
token: fcmToken
});
@ -80,7 +84,7 @@ async function getUserAndFCMToken(notification: NotificationData): Promise<{ use
userId = notification.invitorId;
fcmToken = await getFCMTokenFromUserDoc(userId);
} else if (notification.phoneNumber) {
const userQuery = await admin
const userQuery = await app
.firestore()
.collection('users')
.where('phoneNumber', '==', notification.phoneNumber)
@ -98,7 +102,7 @@ async function getUserAndFCMToken(notification: NotificationData): Promise<{ use
}
async function getFCMTokenFromUserDoc(userId: string): Promise<string | null> {
const userDoc = await admin.firestore().collection('users').doc(userId).get();
const userDoc = await app.firestore().collection('users').doc(userId).get();
return userDoc.exists ? userDoc.data()?.fcmToken : null;
}
@ -204,15 +208,15 @@ function getInvitationBody(status: string, name?: string): string {
}
async function markNotificationAsSent(notificationId: string): Promise<void> {
await admin.firestore().collection('notifications').doc(notificationId).update({
await app.firestore().collection('notifications').doc(notificationId).update({
notificationSent: true,
sentAt: admin.firestore.FieldValue.serverTimestamp()
sentAt: app.firestore.FieldValue.serverTimestamp()
});
}
async function updateNotificationWithError(notificationId: string, error: string): Promise<void> {
await admin.firestore().collection('notifications').doc(notificationId).update({
await app.firestore().collection('notifications').doc(notificationId).update({
notificationError: error,
updatedAt: admin.firestore.FieldValue.serverTimestamp()
updatedAt: app.firestore.FieldValue.serverTimestamp()
});
}

View File

@ -1,10 +1,14 @@
import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import { corsHandler } from "../../shared/middleware";
import { admin, logger } from "../../shared/config";
import { getCorsHandler } from "../../shared/middleware";
import { getAdmin, getLogger } from "../../shared/config";
import axios from "axios";
const { v4: uuidv4 } = require('uuid');
const corsHandler = getCorsHandler();
const admin = getAdmin();
const logger = getLogger();
interface CashfreeLinkRequest {
amount: number;
customerName?: string;

View File

@ -1,3 +1,3 @@
export * from './createOrder';
export * from './createLink';
export * from './verifyPayment';
export { createCashfreeLink } from './createLink';
export { verifyCashfreePayment } from './verifyPayment';
export { createCashfreeOrder } from './createOrder';

View File

@ -1,9 +1,12 @@
import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import * as express from "express";
import { logger } from "../shared/config";
import { getLogger } from "../shared/config";
import { getCorsHandler } from "../shared/middleware";
import axios from "axios";
import { corsHandler } from "../shared/middleware";
const logger = getLogger();
const corsHandler = getCorsHandler();
export const getPlacesAutocomplete = onRequest({
region: '#{SERVICES_RGN}#'

View File

@ -1,9 +1,11 @@
import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import * as express from "express";
import * as logger from "firebase-functions/logger";
import axios from "axios";
import { corsHandler } from "../shared/middleware";
const corsHandler = require('../shared/middleware').corsHandler;
const logger = require('../shared/config').getLogger();
export const getPlaceDetails = onRequest({
region: '#{SERVICES_RGN}#'

View File

@ -1,2 +1,2 @@
export * from './autocomplete';
export * from './details';
export { getPlaceDetails } from './details';
export { getPlacesAutocomplete } from './autocomplete';

View File

@ -5,4 +5,5 @@ if (!admin.apps.length) {
admin.initializeApp();
}
export { admin, logger };
export const getAdmin = () => admin;
export const getLogger = () => logger;

View File

@ -1,3 +1,3 @@
import cors from 'cors';
export const corsHandler = cors({ origin: true });
export const getCorsHandler = () => cors({ origin: true });

View File

@ -1 +1 @@
export * from './sendSMS';
export { sendSMSMessage } from './sendSMS';

View File

@ -1,9 +1,12 @@
import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import { corsHandler } from "../shared/middleware";
import { logger } from "../shared/config";
import { getCorsHandler } from "../shared/middleware";
import { getLogger } from "../shared/config";
import twilio from 'twilio';
const corsHandler = getCorsHandler();
const logger = getLogger();
// Initialize Twilio client
const twilioClient = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);

View File

@ -1,8 +1,12 @@
import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import * as path from 'path';
import { corsHandler } from "../shared/middleware";
import { admin, logger } from "../shared/config";
import { getCorsHandler } from "../shared/middleware";
import { getLogger, getAdmin } from "../shared/config";
const corsHandler = getCorsHandler();
const admin = getAdmin();
const logger = getLogger();
export const accessFile = onRequest({
region: '#{SERVICES_RGN}#'

View File

@ -1 +1 @@
export * from './accessFile';
export { accessFile } from './accessFile';