Add Postgres Services

This commit is contained in:
Matt DiMeglio 2025-08-18 10:20:36 -04:00
parent e6c4389393
commit a9782fef08
5 changed files with 80 additions and 29 deletions

View file

@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';
import messaging from '@react-native-firebase/messaging';
import { router } from 'expo-router';
import { createUserWithEmailAndPassword, updateProfile } from 'firebase/auth';
import { getDatabase, ref, set } from 'firebase/database';
import { auth } from '@/contexts/firebase';
import { useAuth } from '@/contexts/AuthContext';
import { View, ScrollView, Text, TouchableOpacity, KeyboardAvoidingView, Platform } from 'react-native';
@ -52,16 +52,18 @@ export default function Register() {
await updateProfile(userCredential.user, {
displayName: `${values.firstName} ${values.lastName}`
});
const token = await messaging().getToken();
const db = getDatabase();
await set(ref(db, 'users/' + userCredential.user.uid), {
await postgresServices.createUser({
firstName: values.firstName,
lastName: values.lastName,
number: values.number,
provider: values.provider,
email: values.email,
provider: values.provider,
departments: [],
token,
uid: userCredential.user.uid
});
})
router.replace('./incidents');
} catch (err) {
setError(err.message);
@ -100,7 +102,8 @@ export default function Register() {
<View style={{ flex: 1 }}>
<StatusBar style="dark" />
<PageHeader
leftHeader={ <TouchableOpacity onPress={router.back} style={{ flexDirection: 'row', alignItems: 'center', paddingBottom: 5 }}>
leftHeader={
<TouchableOpacity onPress={router.back} style={{ flexDirection: 'row', alignItems: 'center', paddingBottom: 5 }}>
<Ionicons name="chevron-back-outline" size={22} color="red" style={{ paddingLeft: 20 }} />
<Text style={{ color: 'red', fontWeight: 600 }}>Back to Login</Text>
</TouchableOpacity>}

View file

@ -1,3 +1,4 @@
export { AuthProvider } from './AuthContext';
export { GlobalVariablesProvider, GlobalVariablesContext } from './GlobalVariablesContext';
export { postgresServices } from './postgres';
export { WebSocketProvider, WebSocketContext } from './WebSocketContext';

62
contexts/postgres.js Normal file
View file

@ -0,0 +1,62 @@
export const postgresServices = {
getCallsParams: async (args) => {
let response;
try {
const { numCalls, departments, status } = args;
response = await fetch(`${process.env.EXPO_PUBLIC_DATABASE_URL}/getCallsParams`, {
method: "POST",
headers: {
apiKey: process.env.EXPO_PUBLIC_DATABASE_API_KEY,
"Content-Type": 'application/json'
},
body: JSON.stringify({
numCalls,
departments,
status
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (e) {
console.error("Failed to fetch initial calls: ", e);
}
return response?.json() || [];
},
createUser: async (args) => {
try {
const {
firstName,
lastName,
number,
email,
provider,
departments,
token,
uid
} = args;
await fetch(`${process.env.EXPO_PUBLIC_DATABASE_URL}/createUser`, {
method: "POST",
headers: {
apiKey: process.env.EXPO_PUBLIC_DATABASE_API_KEY,
"Content-Type": 'application/json'
},
body: JSON.stringify({
firstName,
lastName,
number,
email,
provider,
departments,
globalRole: 'user',
primaryDept,
token,
firebaseUid: uid
}),
});
} catch (e) {
console.error("Failed to Add User to Database: ", e);
}
return 'User Added to Database';
}
}

View file

@ -5,6 +5,7 @@ import {
Cpr,
FourByFour,
} from "healthicons-react-native/dist/outline";
import { postgresServices } from "@/contexts";
import { useWebSocketContext } from "@/hooks";
const callIconMap = {
@ -83,27 +84,11 @@ const formatCallDateTime = (callValue) => {
};
const getIncidents = async (departments, incidentStatus) => {
let response;
try {
response = await fetch(`${process.env.EXPO_PUBLIC_DATABASE_URL}/getCallsParams`, {
method: "POST",
headers: {
apiKey: process.env.EXPO_PUBLIC_DATABASE_API_KEY,
"Content-Type": 'application/json'
},
body: JSON.stringify({
numCalls: 25,
departments,
status: incidentStatus
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (e) {
console.error("Failed to fetch initial calls:", e);
}
return response?.json() || [];
return postgresServices.getCallsParams({
numCalls: 25,
departments,
status: incidentStatus
});
};
export const useCallFeed = (callPage = false) => {

View file

@ -1,9 +1,9 @@
const express = require('express');
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(express.json());
app.post('/send-notification', async (req, res) => {
const { expoPushToken, title, body, data } = req.body;