Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*node_modules/*
69 changes: 69 additions & 0 deletions Mitoapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const express = require('express');
const admin = require('firebase-admin');
const https = require('https'); // Added HTTPS module
const fs = require('fs'); // Added for reading certificate files
const app = express();
const port = 3000;
const serviceAccount = require('./testapplication-3b8c2-firebase-adminsdk-fbsvc-c9d4c90188.json');

// Initialize Firebase Admin
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});

const deviceToken = "fP-XH1Z1RzuylXIMsEhKb8:APA91bFN_9jPcjDnjke8hDWc9baxH0pWRQTsijpKkAFMC0FZU9jAQUlrt_ebrWk9xBJJWWVnPezhMv8jGbe9bWwOu4deNvPhyiJQwSFSxUd8D11pSLpIxzg";

// Middleware
app.use(express.json());

// Routes (unchanged)
app.get('/', async (req, res) => {
try {
console.log("Received GET request");
res.json({ message: "Hello from the server!" });
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});

app.post("/signup", async (req, res) => {
try {
const data = req.body;
console.log("Received signup data:", data);
res.json({
status: "success",
message: "User data received",
receivedData: data
});
} catch (error) {
console.error("Signup error:", error);
res.status(400).json({ error: "Bad request" });
}
});

app.post("/store", async (req, res) => {
try {
const data = req.body;
console.log(data);
res.json({
status: "success",
message: `Data stored for ID`,
data: data
});
} catch (error) {
console.error("Store error:", error);
res.status(500).json({ error: "Failed to store data" });
}
});

// HTTPS Setup (only addition)
const sslOptions = {
key: fs.readFileSync('path/to/private.key'), // Replace with your key path
cert: fs.readFileSync('path/to/certificate.crt') // Replace with your cert path
};

// Create HTTPS server (replaced app.listen)
https.createServer(sslOptions, app).listen(port, () => {
console.log(`HTTPS Server listening on port ${port}`);
});
121 changes: 121 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const express = require('express');
const admin = require('firebase-admin');
const app = express();
const port = 3000;
const serviceAccount = require('./mito-2e978-firebase-adminsdk-fbsvc-a214db1a48.json');
const pool = require('./database/db_connect');
const cors = require('cors')
// Initialize Firebase Admin
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});

let deviceToken = "";

// Middleware
app.use(cors());
app.use(express.json());

// Routes
async function getDeviceToken(phone) {
const db_response = await pool.query(
`SELECT device_token from user_devices where phone_number = ?`,
[phone]
);
return db_response[0][0].device_token;
}
app.get('/hospital',async (req,res)=>{
try{
const data = req.body;
const token = await getDeviceToken(data.PhoneNumber);
const message = {
notification: {
title: "Fetch",
body: "P1234"
},
token: token
};
const response = await admin.messaging().send(message);
console.log('Successfully sent notification:', response);
res.json(token);
}catch(err){
console.log(err)
}

})
app.get('/', async (req, res) => {
try {
console.log("Received GET request");
// Uncomment to send notification
const message = {
notification: {
title: "Test Notification",
body: "This is a test notification"
},
token: deviceToken
};
const response = await admin.messaging().send(message);
console.log('Successfully sent notification:', response);
res.json({ message: "Hello from the server!" });
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});

app.post("/signup", async (req, res) => {
try {
// Note: req.json() is incorrect - express.json() middleware already parsed it
const data = req.body;
const { deviceToken: newToken } = req.body;
// console.log(data.PhoneNumber)
if (!newToken) {
return res.status(400).json({ error: "Device token is required" });
}
deviceToken = newToken;
// Here you would typically:
// 1. Validate the data
// 2. Store in database
// 3. Maybe send a welcome notification
const db_response = await pool.query(
`INSERT INTO user_devices (phone_number, contract_address, device_token)
VALUES (?, ?, ?)`,
[data.PhoneNumber, '0xafaefaefae', deviceToken]
);
console.log(db_response);
res.json({
status: "success",
message: "User data received",
receivedData: data
});
} catch (error) {
console.error("Signup error:", error);
res.status(400).json({ error: "Bad request" });
}
});

// Start server
app.post("/store", async (req, res) => {
try {
const data = req.body;

console.log(data);

// Here you would typically:
// 1. Validate the data
// 2. Store in database with the ID
// 3. Maybe send a confirmation

res.json({
status: "success",
message: `Data stored for ID`,
data: data
});
} catch (error) {
console.error("Store error:", error);
res.status(500).json({ error: "Failed to store data" });
}
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
50 changes: 50 additions & 0 deletions api1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const express = require('express')
const admin = require('firebase-admin');
const app = express()
const port = 3000;
const service_account = require('./testapplication-3b8c2-firebase-adminsdk-fbsvc-c9d4c90188.json')

admin.initializeApp({
credential:admin.credential.cert(service_account)
})
const deviceToken = "fP-XH1Z1RzuylXIMsEhKb8:APA91bFN_9jPcjDnjke8hDWc9baxH0pWRQTsijpKkAFMC0FZU9jAQUlrt_ebrWk9xBJJWWVnPezhMv8jGbe9bWwOu4deNvPhyiJQwSFSxUd8D11pSLpIxzg"
app.use(express.json())

app.get('/' ,async (req,res)=>{
try{const message = {
notification: {
title: "Action Required",
body: "Do you approve this request?"
},
token: deviceToken,
data: { // Custom data payload
action_type: "approval_request",
request_id: "12345" // Unique ID for tracking
},
android: {
priority: "high",
notification: {
click_action: "APPROVAL_ACTION" // For intent filtering
}
},
apns: { // For iOS (if needed)
payload: {
aps: {
category: "APPROVAL_CATEGORY"
}
}
}
};

const response = await admin.messaging().send(message);
res.json({ success: true, messageId: response });
}catch(error){
console.log(error)
}


})

app.listen(port,()=>{
console.log(`Listing on port ${port}`)
})
Loading