Feature/notification system #26

Open
mattdimegs wants to merge 34 commits from feature/notification-system into main
5 changed files with 80 additions and 29 deletions
Showing only changes of commit a9782fef08 - Show all commits

View file

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

View file

@ -1,3 +1,4 @@
export { AuthProvider } from './AuthContext'; export { AuthProvider } from './AuthContext';
export { GlobalVariablesProvider, GlobalVariablesContext } from './GlobalVariablesContext'; export { GlobalVariablesProvider, GlobalVariablesContext } from './GlobalVariablesContext';
export { postgresServices } from './postgres';
export { WebSocketProvider, WebSocketContext } from './WebSocketContext'; 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, Cpr,
FourByFour, FourByFour,
} from "healthicons-react-native/dist/outline"; } from "healthicons-react-native/dist/outline";
import { postgresServices } from "@/contexts";
import { useWebSocketContext } from "@/hooks"; import { useWebSocketContext } from "@/hooks";
const callIconMap = { const callIconMap = {
@ -83,27 +84,11 @@ const formatCallDateTime = (callValue) => {
}; };
const getIncidents = async (departments, incidentStatus) => { const getIncidents = async (departments, incidentStatus) => {
let response; return postgresServices.getCallsParams({
try { numCalls: 25,
response = await fetch(`${process.env.EXPO_PUBLIC_DATABASE_URL}/getCallsParams`, { departments,
method: "POST", status: incidentStatus
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() || [];
}; };
export const useCallFeed = (callPage = false) => { export const useCallFeed = (callPage = false) => {

View file

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