Initial commit
This commit is contained in:
28
functions/.eslintrc.js
Normal file
28
functions/.eslintrc.js
Normal file
@@ -0,0 +1,28 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
es6: true,
|
||||
node: true,
|
||||
},
|
||||
parserOptions: {
|
||||
"ecmaVersion": 2018,
|
||||
},
|
||||
extends: [
|
||||
"eslint:recommended",
|
||||
"google",
|
||||
],
|
||||
rules: {
|
||||
"no-restricted-globals": ["error", "name", "length"],
|
||||
"prefer-arrow-callback": "error",
|
||||
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
files: ["**/*.spec.*"],
|
||||
env: {
|
||||
mocha: true,
|
||||
},
|
||||
rules: {},
|
||||
},
|
||||
],
|
||||
globals: {},
|
||||
};
|
||||
2
functions/.gitignore
vendored
Normal file
2
functions/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
*.local
|
||||
67
functions/index.js
Normal file
67
functions/index.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const {onRequest} = require("firebase-functions/v2/https");
|
||||
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
|
||||
const admin = require("firebase-admin");
|
||||
|
||||
admin.initializeApp();
|
||||
const db = admin.firestore();
|
||||
|
||||
// HTTP test
|
||||
exports.helloWorld = onRequest((req, res) => {
|
||||
res.send("Hello from Firebase!");
|
||||
});
|
||||
|
||||
// Trigger on new ride created
|
||||
exports.onRideCreated = onDocumentCreated("rides/{rideId}", async (event) => {
|
||||
const ride = event.data.data();
|
||||
const rideId = event.params.rideId;
|
||||
|
||||
console.log("🚕 New ride created:", rideId);
|
||||
|
||||
if (ride.driverId) {
|
||||
console.log("Ride already has driver — skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
const driversSnap = await db
|
||||
.collection("users")
|
||||
.where("role", "==", "driver")
|
||||
.where("active", "==", true)
|
||||
.get();
|
||||
|
||||
if (driversSnap.empty) {
|
||||
console.log("❌ No drivers are active right now.");
|
||||
return;
|
||||
}
|
||||
|
||||
const messaging = admin.messaging();
|
||||
let count = 0;
|
||||
|
||||
for (const doc of driversSnap.docs) {
|
||||
const driver = doc.data();
|
||||
|
||||
if (!driver.fcmToken) continue;
|
||||
|
||||
const message = {
|
||||
token: driver.fcmToken,
|
||||
notification: {
|
||||
title: "New Ride Request",
|
||||
body:
|
||||
`${ride.sourceLocationName} ➝ ` +
|
||||
`${ride.destinationLocationName}`.slice(0, 80), // max-len safe
|
||||
},
|
||||
data: {
|
||||
rideId,
|
||||
type: "NEW_RIDE",
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
await messaging.send(message);
|
||||
count++;
|
||||
} catch (err) {
|
||||
console.log("FCM Error:", err);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`📨 Sent ride request to ${count} drivers.`);
|
||||
});
|
||||
7682
functions/package-lock.json
generated
Normal file
7682
functions/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
functions/package.json
Normal file
26
functions/package.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "functions",
|
||||
"description": "Cloud Functions for Firebase",
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"serve": "firebase emulators:start --only functions",
|
||||
"shell": "firebase functions:shell",
|
||||
"start": "npm run shell",
|
||||
"deploy": "firebase deploy --only functions",
|
||||
"logs": "firebase functions:log"
|
||||
},
|
||||
"engines": {
|
||||
"node": "20"
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"firebase-admin": "^11.10.0",
|
||||
"firebase-functions": "^4.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^8.15.0",
|
||||
"eslint-config-google": "^0.14.0",
|
||||
"firebase-functions-test": "^3.4.1"
|
||||
},
|
||||
"private": true
|
||||
}
|
||||
Reference in New Issue
Block a user