diff --git a/app/register.jsx b/app/register.jsx index 6a1d634..f4780d1 100644 --- a/app/register.jsx +++ b/app/register.jsx @@ -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() { + leftHeader={ + Back to Login } diff --git a/contexts/index.js b/contexts/index.js index 0be45a6..b064617 100644 --- a/contexts/index.js +++ b/contexts/index.js @@ -1,3 +1,4 @@ export { AuthProvider } from './AuthContext'; export { GlobalVariablesProvider, GlobalVariablesContext } from './GlobalVariablesContext'; +export { postgresServices } from './postgres'; export { WebSocketProvider, WebSocketContext } from './WebSocketContext'; \ No newline at end of file diff --git a/contexts/postgres.js b/contexts/postgres.js new file mode 100644 index 0000000..71d1d57 --- /dev/null +++ b/contexts/postgres.js @@ -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'; + } +} \ No newline at end of file diff --git a/hooks/useCallFeed/useCallFeed.jsx b/hooks/useCallFeed/useCallFeed.jsx index 31f413a..840a190 100644 --- a/hooks/useCallFeed/useCallFeed.jsx +++ b/hooks/useCallFeed/useCallFeed.jsx @@ -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) => { diff --git a/scripts/expo-push-server.js b/scripts/expo-push-server.js index 2a7c5bd..7677fb4 100644 --- a/scripts/expo-push-server.js +++ b/scripts/expo-push-server.js @@ -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;