2022-09-20 16:11:53 +02:00
|
|
|
const express = require('express');
|
|
|
|
const cors = require('cors');
|
|
|
|
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
|
|
|
|
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
|
2022-11-08 14:41:39 +01:00
|
|
|
const { getMessaging } = require('firebase-admin/messaging')
|
2022-11-08 15:11:45 +01:00
|
|
|
const { getAuth } = require('firebase-admin/auth')
|
2022-09-20 16:11:53 +02:00
|
|
|
|
|
|
|
const serviceAccount = require('./firebase.json');
|
|
|
|
|
|
|
|
initializeApp({
|
|
|
|
credential: cert(serviceAccount)
|
|
|
|
});
|
|
|
|
|
|
|
|
const db = getFirestore();
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(cors({
|
|
|
|
origin: '*',
|
|
|
|
}));
|
|
|
|
|
|
|
|
app.listen(7289, () => console.log('API is online!'));
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.status(200).send({
|
|
|
|
status: 'success',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/checkrelatiecode', async (req, res) => {
|
|
|
|
const { relatiecode, email } = req.body;
|
|
|
|
|
|
|
|
if (!relatiecode) return res.status(400).send({ code: 'no-relatiecode'})
|
|
|
|
if (!email) return res.status(400).send({ code: 'no-email'})
|
|
|
|
|
|
|
|
try {
|
|
|
|
const docRef = db.collection('ledenlijst').doc(relatiecode);
|
|
|
|
const doc = await docRef.get();
|
|
|
|
|
|
|
|
if (!doc.exists) return res.status(400).send({ code: 'incorrect'})
|
|
|
|
|
|
|
|
const data = doc.data()
|
|
|
|
|
|
|
|
if (data.email[0] === email || data.email[1] === email) {
|
|
|
|
return res.status(200).send({ code: 'correct'})
|
|
|
|
} else {
|
|
|
|
return res.status(400).send({ code: 'incorrect'})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
2022-09-22 16:39:11 +02:00
|
|
|
return res.status(500).send({ code: 'error', error: e })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/getrelatiecodes', async (req, res) => {
|
|
|
|
const { email } = req.body;
|
|
|
|
|
|
|
|
if (!email) return res.status(400).send({ code: 'no-email'})
|
|
|
|
|
|
|
|
try {
|
|
|
|
const ledenlijstRef = db.collection('ledenlijst')
|
|
|
|
|
|
|
|
const snapshot = await ledenlijstRef.where("email", "array-contains", email).get()
|
|
|
|
|
|
|
|
if (snapshot.empty) {
|
|
|
|
res.status(400).send({ code: 'no-relatiecodes'})
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let relatiecodes = [];
|
2022-09-26 16:23:45 +02:00
|
|
|
let persons = [];
|
2022-09-22 16:39:11 +02:00
|
|
|
|
|
|
|
snapshot.forEach(doc => {
|
|
|
|
relatiecodes.push(doc.id)
|
2022-09-26 16:23:45 +02:00
|
|
|
const data = doc.data()
|
|
|
|
persons.push({ fullName: data.fullName, relatiecode: doc.id })
|
2022-09-22 16:39:11 +02:00
|
|
|
});
|
|
|
|
|
2022-09-26 16:23:45 +02:00
|
|
|
res.status(200).send({ code: 'success', relatiecodes: relatiecodes, persons: persons })
|
2022-09-22 16:39:11 +02:00
|
|
|
|
|
|
|
|
2022-11-08 14:41:39 +01:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(500).send({ code: 'error', error: e })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/subscribetotopic', async (req, res) => {
|
|
|
|
const { topic, registrationToken } = req.body;
|
|
|
|
|
|
|
|
if (!topic) return res.status(400).send({ code: 'no-topic'})
|
|
|
|
if (!registrationToken) return res.status(400).send({ code: 'no-registrationToken'})
|
|
|
|
|
|
|
|
try {
|
|
|
|
getMessaging().subscribeToTopic([registrationToken], topic)
|
|
|
|
.then((response) => {
|
|
|
|
console.log('Successfully subscribed to topic:', response);
|
|
|
|
|
|
|
|
res.status(200).send({ code: 'success' })
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log('Error subscribing to topic:', error);
|
|
|
|
|
2022-11-08 15:11:45 +01:00
|
|
|
return res.status(500).send({ code: 'error', error: error })
|
2022-11-08 14:41:39 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-11-08 15:11:45 +01:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(500).send({ code: 'error', error: e })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/sendmessage', async (req, res) => {
|
|
|
|
const { title, body, token } = req.body;
|
|
|
|
|
|
|
|
if (!title) return res.status(400).send({ code: 'no-topic'})
|
|
|
|
if (!body) return res.status(400).send({ code: 'no-registrationToken'})
|
|
|
|
if (!token) return res.status(400).send({ code: 'no-token'})
|
|
|
|
|
|
|
|
try {
|
|
|
|
getAuth()
|
|
|
|
.verifyIdToken(token)
|
|
|
|
.then(async (decodedToken) => {
|
|
|
|
const uid = decodedToken.uid;
|
|
|
|
|
|
|
|
const docRef = db.collection('users').doc(uid);
|
|
|
|
const doc = await docRef.get();
|
|
|
|
|
|
|
|
if (!doc.exists) return res.status(400).send({ code: 'not-found'})
|
|
|
|
|
|
|
|
const data = doc.data()
|
|
|
|
|
|
|
|
if (!data.sendNews) return res.status(400).send({ code: 'no-permissions'})
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
notification: {
|
|
|
|
title: title,
|
|
|
|
body: body
|
|
|
|
},
|
|
|
|
topic: 'all',
|
2022-11-14 14:57:59 +01:00
|
|
|
apns: {
|
|
|
|
headers: {
|
|
|
|
'apns-push-type': 'alert'
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 15:11:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
getMessaging().send(message)
|
|
|
|
.then((response) => {
|
|
|
|
// Response is a message ID string.
|
|
|
|
console.log('Successfully sent message:', response);
|
|
|
|
|
|
|
|
res.status(200).send({ code: 'success', response: response })
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log('Error sending message:', error);
|
|
|
|
|
|
|
|
return res.status(500).send({ code: 'error', error: error })
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error)
|
|
|
|
|
|
|
|
return res.status(500).send({ code: 'error', error: error })
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-22 16:39:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(500).send({ code: 'error', error: e })
|
2022-09-20 16:11:53 +02:00
|
|
|
}
|
|
|
|
})
|