swent-group10 / polyfit

0 stars 1 forks source link

Add userId to meals #315

Closed medwardson closed 3 months ago

medwardson commented 3 months ago

Update the Meal class to have a userId to prevent issues when a different user logs in and our database stores multi-user info. This will allow us to call getAllMeals() and other meal queries with a injected parameters of userId, to only return meals relevant to the logged in user.


I've ran a migration script on our Firebase user database to backfill all the meals to include the userId, and also for our posts. I'll run it again after merge. Here's the scripts I used:

$$\quad$$

const admin = require('firebase-admin');
const serviceAccount = require('./myPrivateKey');  // downloaded from firebase

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'https://<your-database-name>.firebaseio.com' // copy real from firebase
});

const db = admin.firestore();

// Update Posts
async function updatePostsMealUserIdField() {
    const postsCollection = db.collection('posts');

    try {
        const postsSnapshot = await postsCollection.get();

        if (postsSnapshot.empty) {
            console.log('No posts found.');
            return;
        }

        const batch = db.batch();

        postsSnapshot.forEach(postDoc => {
            const postRef = postsCollection.doc(postDoc.id);
            const meal = postDoc.data().meal || {};
            meal.userId = ''; 
            batch.update(postRef, { meal: meal });
        });

        await batch.commit();
        console.log('Updated userId field in meal for all posts.');
    } catch (error) {
        console.error('Error updating userId field in meal for posts:', error);
    }
}

// Update user's meals
async function backfillUserId() {
    const usersCollection = db.collection('users');

    try {
        const usersSnapshot = await usersCollection.get();

        for (const userDoc of usersSnapshot.docs) {
            const userId = userDoc.id;
            const mealsCollection = usersCollection.doc(userId).collection('meals');
            const mealsSnapshot = await mealsCollection.get();

            const batch = db.batch();

            mealsSnapshot.forEach(mealDoc => {
                const mealRef = mealsCollection.doc(mealDoc.id);
                batch.update(mealRef, { userId: userId });
            });

            await batch.commit();
            console.log(`Backfilled userId for meals of user ${userId}`);
        }

        console.log('Backfill complete for all users.');
    } catch (error) {
        console.error('Error backfilling userId:', error);
    }
}
RandomUsername1315 commented 3 months ago

@medwardson, it might be a good idea to wait before running the script to avoid any potential issues with the coaches' APK. I know that it shouldn't cause problems, but let's do that after the grading to avoid any potential crashes.

sonarcloud[bot] commented 3 months ago

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
87.3% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud