feature/add-client #22
96
functions/src/clientRegistration/clientRegistration.ts
Normal file
96
functions/src/clientRegistration/clientRegistration.ts
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
import { onRequest } from "firebase-functions/v2/https";
|
||||||
|
import { getAdmin } from "../shared/config";
|
||||||
|
import { getCorsHandler } from "../shared/middleware";
|
||||||
|
|
||||||
|
|
||||||
|
const corsHandler = getCorsHandler();
|
||||||
|
const admin = getAdmin();
|
||||||
|
export const registerClient = onRequest({
|
||||||
|
region: '#{SERVICES_RGN}#'
|
||||||
|
}, async (req, res) => {
|
||||||
|
return corsHandler(req, res, async () => {
|
||||||
|
try {
|
||||||
|
if (req.method !== 'POST') {
|
||||||
|
return res.status(405).json({ error: 'Method not allowed. Please use POST.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const gymUser = req.body;
|
||||||
|
if (!gymUser.phoneNumber) {
|
||||||
|
return res.status(400).json({ error: 'Phone number is required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const isdCode = gymUser.isdCode || '+1';
|
||||||
|
const formattedPhoneNumber = gymUser.phoneNumber.startsWith('+')
|
||||||
|
? gymUser.phoneNumber
|
||||||
|
: `${isdCode}${gymUser.phoneNumber}`;
|
||||||
|
|
||||||
|
let uid;
|
||||||
|
try {
|
||||||
|
const userRecord = await admin.auth().getUserByPhoneNumber(formattedPhoneNumber)
|
||||||
|
.catch(() => null);
|
||||||
|
|
||||||
|
if (userRecord) {
|
||||||
|
uid = userRecord.uid;
|
||||||
|
} else {
|
||||||
|
const newUser = await admin.auth().createUser({
|
||||||
|
phoneNumber: formattedPhoneNumber,
|
||||||
|
displayName: gymUser.name || '',
|
||||||
|
email: gymUser.email || null,
|
||||||
|
});
|
||||||
|
uid = newUser.uid;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating authentication user:', error);
|
||||||
|
return res.status(500).json({
|
||||||
|
error: 'Failed to create authentication user',
|
||||||
|
details: error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
gymUser.uid = uid;
|
||||||
|
|
||||||
|
if (gymUser.name) {
|
||||||
|
gymUser.normalizedName = gymUser.name.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gymUser.dateOfBirth && !(typeof gymUser.dateOfBirth === 'string')) {
|
||||||
|
gymUser.dateOfBirth = new Date(gymUser.dateOfBirth).toISOString();
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientData = {
|
||||||
|
...gymUser,
|
||||||
|
};
|
||||||
|
|
||||||
|
await admin.firestore().collection('client_profile').doc(uid).set(clientData);
|
||||||
|
|
||||||
|
return res.status(201).json({
|
||||||
|
success: true,
|
||||||
|
message: 'Client registered successfully',
|
||||||
|
clientId: uid
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating client profile:', error);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!gymUser.uid) {
|
||||||
|
await admin.auth().deleteUser(uid);
|
||||||
|
}
|
||||||
|
} catch (deleteError) {
|
||||||
|
console.error('Error deleting auth user after failed profile creation:', deleteError);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(500).json({
|
||||||
|
error: 'Failed to create client profile',
|
||||||
|
details: error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Unexpected error in client registration:', error);
|
||||||
|
return res.status(500).json({
|
||||||
|
error: 'Internal server error',
|
||||||
|
details: error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
1
functions/src/clientRegistration/index.ts
Normal file
1
functions/src/clientRegistration/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export { registerClient } from './clientRegistration';
|
||||||
@ -1,13 +1,14 @@
|
|||||||
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 { corsHandler } from "../shared/middleware";
|
import { getCorsHandler } from "../shared/middleware";
|
||||||
import { logger } from "../shared/config";
|
import { getLogger } from "../shared/config";
|
||||||
import formData from 'form-data';
|
import formData from 'form-data';
|
||||||
import Mailgun from 'mailgun.js';
|
import Mailgun from 'mailgun.js';
|
||||||
const { convert } = require('html-to-text');
|
const { convert } = require('html-to-text');
|
||||||
|
|
||||||
const mailgun = new Mailgun(formData);
|
const mailgun = new Mailgun(formData);
|
||||||
|
const logger = getLogger();
|
||||||
|
const corsHandler = getCorsHandler();
|
||||||
export const sendEmailMessage = onRequest({
|
export const sendEmailMessage = onRequest({
|
||||||
region: '#{SERVICES_RGN}#'
|
region: '#{SERVICES_RGN}#'
|
||||||
}, (request: Request, response) => {
|
}, (request: Request, response) => {
|
||||||
|
|||||||
@ -4,14 +4,15 @@ import * as os from 'os';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
import { corsHandler } from "../shared/middleware";
|
import { getCorsHandler } from "../shared/middleware";
|
||||||
import { logger } from "../shared/config";
|
import { getLogger } from "../shared/config";
|
||||||
import formData from 'form-data';
|
import formData from 'form-data';
|
||||||
import Mailgun from 'mailgun.js';
|
import Mailgun from 'mailgun.js';
|
||||||
const { convert } = require('html-to-text');
|
const { convert } = require('html-to-text');
|
||||||
|
|
||||||
const mailgun = new Mailgun(formData);
|
const mailgun = new Mailgun(formData);
|
||||||
|
const logger = getLogger();
|
||||||
|
const corsHandler = getCorsHandler();
|
||||||
export const sendEmailWithAttachment = onRequest({
|
export const sendEmailWithAttachment = onRequest({
|
||||||
region: '#{SERVICES_RGN}#'
|
region: '#{SERVICES_RGN}#'
|
||||||
}, async (request: Request, response) => {
|
}, async (request: Request, response) => {
|
||||||
|
|||||||
@ -5,3 +5,4 @@ export { sendSMSMessage } from './sms';
|
|||||||
export { processNotificationOnCreate } from './notifications';
|
export { processNotificationOnCreate } from './notifications';
|
||||||
export { createCashfreeLink, createCashfreeOrder, verifyCashfreePayment } from './payments';
|
export { createCashfreeLink, createCashfreeOrder, verifyCashfreePayment } from './payments';
|
||||||
export { getPlaceDetails, getPlacesAutocomplete } from './places';
|
export { getPlaceDetails, getPlacesAutocomplete } from './places';
|
||||||
|
export { registerClient } from './clientRegistration';
|
||||||
@ -1,9 +1,11 @@
|
|||||||
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 { corsHandler } from "../../shared/middleware";
|
import { getCorsHandler } from "../../shared/middleware";
|
||||||
import { admin, logger } from "../../shared/config";
|
import { getAdmin, getLogger } from "../../shared/config";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
const admin = getAdmin();
|
||||||
|
const logger = getLogger();
|
||||||
|
const corsHandler = getCorsHandler();
|
||||||
interface CashfreeOrderRequest {
|
interface CashfreeOrderRequest {
|
||||||
amount: number;
|
amount: number;
|
||||||
customerName?: string;
|
customerName?: string;
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
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 { corsHandler } from "../../shared/middleware";
|
import { getCorsHandler } from "../../shared/middleware";
|
||||||
import { admin, logger } from "../../shared/config";
|
import { getAdmin, getLogger } from "../../shared/config";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
const logger = getLogger();
|
||||||
|
const corsHandler = getCorsHandler();
|
||||||
|
const admin = getAdmin();
|
||||||
interface CashfreePaymentResponse {
|
interface CashfreePaymentResponse {
|
||||||
order_status: string;
|
order_status: string;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user