diff --git a/back_api/app.js b/back_api/app.js index 9ad8ae2..f9684ba 100644 --- a/back_api/app.js +++ b/back_api/app.js @@ -10,6 +10,7 @@ const xatsRoutes = require('./routes/xats') const grupsRoutes = require('./routes/grups') const organitzadorsRoutes = require('./routes/organitzadors') const insigniesRoutes = require('./routes/insignies') +const notificacioRoutes = require('./routes/notificacio') global.callCount = 0; @@ -30,5 +31,6 @@ app.use('/xats', xatsRoutes) app.use('/grups', grupsRoutes) app.use('/organitzadors', organitzadorsRoutes) app.use('/insignies', insigniesRoutes) +app.use('/notificacio', notificacioRoutes) module.exports = app \ No newline at end of file diff --git a/back_api/routes/grups.js b/back_api/routes/grups.js index 0bd0dea..eaf6e4c 100644 --- a/back_api/routes/grups.js +++ b/back_api/routes/grups.js @@ -269,6 +269,7 @@ router.post('/:grupId/mensajes', checkUserAndFetchData, async (req, res) => { }); res.status(201).send("Mensaje agregado exitosamente al grup"); + } catch (error) { res.status(500).send("Error interno del servidor"); } diff --git a/back_api/routes/notificacio.js b/back_api/routes/notificacio.js new file mode 100644 index 0000000..66a8cb9 --- /dev/null +++ b/back_api/routes/notificacio.js @@ -0,0 +1,31 @@ +const admin = require('firebase-admin') +const express = require('express') +const router = express.Router() + +router.use(express.json()); + +const checkUserAndFetchData = require('./middleware').checkUserAndFetchData; + +//penjar notificacions +router.post('/enviar', checkUserAndFetchData, async (req, res) => { + const { title, mensaje, token } = req.body; + + const message = { + notification: { + title: title, + body: mensaje, + }, + token: token, + }; + + admin.messaging().send(message) + .then((response) => { + res.status(201).send('Notificacio enviada'); + }) + .catch((error) => { + res.status(500).send('Error enviant la notificació'); + }); + +}); + +module.exports = router; \ No newline at end of file diff --git a/back_api/routes/users.js b/back_api/routes/users.js index ddc13e8..dbf8a1f 100644 --- a/back_api/routes/users.js +++ b/back_api/routes/users.js @@ -396,6 +396,30 @@ router.post('/edit', checkUserAndFetchData, async(req, res) => { //MODIFICAR PAR } }); +router.post('/addDevice', checkUserAndFetchData, async(req, res) => { + try { + const { uid, devices } = req.body; + + userDoc = await req.userDocument; + const devicesNou = JSON.parse(devices); + + const usersCollection = db.collection('users'); + + if (userDoc.exists && userDoc.id == uid) { + await usersCollection.doc(uid).update({ + 'devices': devicesNou + }); + res.status(200).send('OK'); + } + else { + res.status(401).send('Forbidden'); + } + + } catch (error){ + res.send(error); + } +}); + router.post('/changePrivacy', checkUserAndFetchData, async(req, res) => { try { @@ -665,7 +689,7 @@ router.put('/escanearQR', checkUserAndFetchData, async (req, res) => { router.post('/create', async(req, res) => { try { - const { uid, username, email, favcategories } = req.body; + const { uid, username, email, favcategories, devices } = req.body; const categories = favcategories; @@ -687,7 +711,8 @@ router.post('/create', async(req, res) => { 'valoradas': valoradas, 'blockedUsers': blockedUsers, 'AssitedActivities': AssitedActivities, - 'private': false + 'private': false, + 'devices': devices }); await crearInsignies(uid); res.status(200).send('OK'); diff --git a/back_api/tests/notificacio.test.js b/back_api/tests/notificacio.test.js new file mode 100644 index 0000000..8ecb2ee --- /dev/null +++ b/back_api/tests/notificacio.test.js @@ -0,0 +1,59 @@ +const request = require('supertest'); +const app = require('../app'); +const admin = require('firebase-admin'); + +const crypto = require('crypto'); +const algorithm = 'aes-256-cbc'; +const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex'); +const iv = Buffer.from(process.env.ENCRYPTION_IV, 'hex'); + +function encrypt(text) { + let cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv); + let encrypted = cipher.update(text); + encrypted = Buffer.concat([encrypted, cipher.final()]); + return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') }; +} + +jest.mock('firebase-admin', () => { + const messaging = { + send: jest.fn(), + }; + return { + messaging: () => messaging, + }; + }); + +describe('POST /notificacio/enviar', () => { + const users = [ + user1 = { + id: "user1", + username: "user1", + email: "", + blockedUsers: ["user2"] + }, + ]; + + beforeEach(async () => { + for (const user of users) { + await db.collection('users').doc(user.id).set(user); + } + }); + + it('should create a notification', async () => { + admin.messaging().send.mockResolvedValue('Successfully sent message'); + const body = { + title: 'titleTest', + mensaje: 'mensajeTest', + token: 'tokenTest' + }; + + const res = await request(app) + .post('/notificacio/enviar') + .set('Authorization', `Bearer ${encrypt('user1').encryptedData}`) + .send(body); + + expect(res.statusCode).toEqual(201); + expect(res.text).toBe('Notificacio enviada'); + }); + +}); \ No newline at end of file diff --git a/back_api/tests/users.test.js b/back_api/tests/users.test.js index d878914..523fbc5 100644 --- a/back_api/tests/users.test.js +++ b/back_api/tests/users.test.js @@ -23,17 +23,49 @@ describe('POST /users/create', () => { uid: 'testUid', username: 'testUser', email: 'testEmail', - favcategories: JSON.stringify(['festa', 'cinema']) + favcategories: JSON.stringify(['festa', 'cinema']), + devices: JSON.stringify(['deviceTest']) }); expect(res.statusCode).toEqual(200); - expect(res.text).toBe('OK'); const docs = await db.collection('users').doc('testUid').get(); expect(docs.empty).toBeFalsy(); }); }); +describe('POST /users/addDevice', () => { + it('should add a new device', async () => { + const resA = await request(app) + .post('/users/create') + .send({ + uid: 'testUid6', + username: 'testUser', + email: 'testEmail', + favcategories: JSON.stringify(['festa', 'cinema']), + devices: JSON.stringify(['deviceTest']) + }); + + expect(resA.statusCode).toEqual(200); + expect(resA.text).toBe('OK'); + + const res = await request(app) + .post('/users/addDevice') + .set('Authorization', `Bearer ${encrypt('testUid6').encryptedData}`) + .send({ + uid: 'testUid6', + devices: JSON.stringify("[\"deviceTest\",\"deviceTest1\"]") + }); + + expect(res.statusCode).toEqual(200); + expect(res.text).toBe('OK'); + + const docs = await db.collection('users').doc('testUid6').get(); + expect(docs.empty).toBeFalsy(); + expect(docs.data().devices).toBe(JSON.stringify(['deviceTest', 'deviceTest1'])); + }); +}); + describe('POST /users/activitats/signout', () => { it('should signout a user from an activity', async () => { const users = [ @@ -353,7 +385,8 @@ describe('POST /users/edit', () => { uid: 'testUid1', username: 'testUser', email: 'testEmail', - favcategories: JSON.stringify(['circ']) + favcategories: JSON.stringify(['festa', 'cinema']), + devices: JSON.stringify(['deviceTest']) }); expect(res.statusCode).toEqual(200); @@ -387,6 +420,7 @@ describe('POST /users/changePrivacy', () => { username: 'testUser', email: 'testEmail', favcategories: JSON.stringify(['circ', 'cinema']), + devices: JSON.stringify(['deviceTest']), privacy: false });