From ba69ae75397626ef0bd27cc08b06db101a26fc24 Mon Sep 17 00:00:00 2001 From: Eman0209 Date: Fri, 24 May 2024 11:45:20 +0200 Subject: [PATCH 1/5] actualitzar funcions edit y create --- back_api/routes/users.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/back_api/routes/users.js b/back_api/routes/users.js index 042ed03..d53c0f4 100644 --- a/back_api/routes/users.js +++ b/back_api/routes/users.js @@ -2,9 +2,13 @@ const admin = require('firebase-admin') const express = require('express') const router = express.Router() +const { v4: uuidv4 } = require("uuid"); +const multer=require('multer') +const upload=multer({storage: multer.memoryStorage()}) + router.use(express.json()); -const { db } = require('../firebaseConfig'); +const { db, bucket} = require('../firebaseConfig'); const { checkUserAndFetchData, checkAdmin, checkPerson, decryptToken } = require('./middleware'); const crypto = require('crypto'); @@ -19,6 +23,15 @@ function encrypt(text) { return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') }; } +//funcio per afegir imatges al bucket +async function createImage(file){ + const uuid = uuidv4(); + const name = uuid + '_' + file.originalname; + const fileName = 'grups/' + name + '.jpg'; + await bucket.file(fileName).createWriteStream().end(file.buffer); + return fileName; +} + router.get('/exists', async (req, res) => { try { var uid = req.query.uid; @@ -350,7 +363,6 @@ router.get('/data/:data', checkUserAndFetchData, async (req, res) => { }) - router.get('/username', async (req, res) => { try { var uid = req.query.uid; @@ -369,20 +381,27 @@ router.get('/username', async (req, res) => { } }); -router.post('/edit', checkUserAndFetchData, async(req, res) => { //MODIFICAR PARA USO DE TOKENS +router.post('/edit', checkUserAndFetchData, upload.single('file'), async(req, res) => { //MODIFICAR PARA USO DE TOKENS try { - const { uid, username, favcategories } = req.body; + const { uid, username, favcategories, imatge } = req.body; userDoc = await req.userDocument; const categories = JSON.parse(favcategories); const usersCollection = db.collection('users'); + filename = imatge; + + if (req.file !== undefined) { + filename = await createImage(req.file); + } + if (userDoc.exists && userDoc.id == uid) { await usersCollection.doc(uid).update({ 'username': username, 'favcategories': categories, + 'image': filename }); res.status(200).send('OK'); } @@ -663,7 +682,7 @@ router.put('/escanearQR', checkUserAndFetchData, async (req, res) => { } }); -router.post('/create', async(req, res) => { +router.post('/create', upload.single('file'), async(req, res) => { try { const { uid, username, email, favcategories } = req.body; @@ -678,6 +697,13 @@ router.post('/create', async(req, res) => { const blockedUsers = []; const AssitedActivities = []; + + filename = ''; + + if (req.file !== undefined) { + filename = await createImage(req.file); + } + await usersCollection.doc(uid).set({ 'email': email, 'username': username, @@ -687,7 +713,8 @@ router.post('/create', async(req, res) => { 'valoradas': valoradas, 'blockedUsers': blockedUsers, 'AssitedActivities': AssitedActivities, - 'private': false + 'private': false, + 'image': filename }); await crearInsignies(uid); res.status(200).send('OK'); From 499af50f16213de124228c0aa78c090a7e454ccf Mon Sep 17 00:00:00 2001 From: Eman0209 Date: Fri, 24 May 2024 11:55:16 +0200 Subject: [PATCH 2/5] modificar carpeta del bucket --- back_api/routes/users.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back_api/routes/users.js b/back_api/routes/users.js index d53c0f4..ab507d0 100644 --- a/back_api/routes/users.js +++ b/back_api/routes/users.js @@ -27,7 +27,7 @@ function encrypt(text) { async function createImage(file){ const uuid = uuidv4(); const name = uuid + '_' + file.originalname; - const fileName = 'grups/' + name + '.jpg'; + const fileName = 'users/' + name + '.jpg'; await bucket.file(fileName).createWriteStream().end(file.buffer); return fileName; } From f349c2082d179ae48293bb1bf6b0e6c4d491f0a9 Mon Sep 17 00:00:00 2001 From: Eman0209 Date: Fri, 24 May 2024 13:50:32 +0200 Subject: [PATCH 3/5] proba postman --- back_api/routes/users.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/back_api/routes/users.js b/back_api/routes/users.js index ab507d0..46fc9b0 100644 --- a/back_api/routes/users.js +++ b/back_api/routes/users.js @@ -387,7 +387,8 @@ router.post('/edit', checkUserAndFetchData, upload.single('file'), async(req, re const { uid, username, favcategories, imatge } = req.body; userDoc = await req.userDocument; - const categories = JSON.parse(favcategories); + const categories = Array.isArray(favcategories) ? favcategories : JSON.parse(favcategories) + //const categories = JSON.parse(favcategories); const usersCollection = db.collection('users'); From 1c987c7b65599bdb2fa6649677d8135a08b075f0 Mon Sep 17 00:00:00 2001 From: Eman0209 Date: Sat, 25 May 2024 18:20:27 +0200 Subject: [PATCH 4/5] modifcar imatge en la creacio del user --- back_api/routes/users.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/back_api/routes/users.js b/back_api/routes/users.js index 46fc9b0..dd2b582 100644 --- a/back_api/routes/users.js +++ b/back_api/routes/users.js @@ -683,7 +683,7 @@ router.put('/escanearQR', checkUserAndFetchData, async (req, res) => { } }); -router.post('/create', upload.single('file'), async(req, res) => { +router.post('/create', /*upload.single('file'),*/ async(req, res) => { try { const { uid, username, email, favcategories } = req.body; @@ -699,11 +699,14 @@ router.post('/create', upload.single('file'), async(req, res) => { const AssitedActivities = []; + /* + filename = ''; if (req.file !== undefined) { filename = await createImage(req.file); } + */ await usersCollection.doc(uid).set({ 'email': email, @@ -715,7 +718,8 @@ router.post('/create', upload.single('file'), async(req, res) => { 'blockedUsers': blockedUsers, 'AssitedActivities': AssitedActivities, 'private': false, - 'image': filename + //'image': filename + 'image': '' }); await crearInsignies(uid); res.status(200).send('OK'); From a9bbb2c0d179e6e4b60ea42af59e99c4b1838135 Mon Sep 17 00:00:00 2001 From: Eman0209 Date: Tue, 28 May 2024 18:23:05 +0200 Subject: [PATCH 5/5] modificar tests /users/edit --- back_api/tests/users.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/back_api/tests/users.test.js b/back_api/tests/users.test.js index d878914..099fb5f 100644 --- a/back_api/tests/users.test.js +++ b/back_api/tests/users.test.js @@ -365,7 +365,8 @@ describe('POST /users/edit', () => { .send({ uid: 'testUid1', username: 'newUsername', - favcategories: JSON.stringify(['festa', 'cinema']) + favcategories: JSON.stringify(['festa', 'cinema']), + imatge: '' }); expect(res2.statusCode).toEqual(200);