Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/974 extra infos profile #1040

Merged
merged 13 commits into from
Sep 30, 2020
Merged

Feat/974 extra infos profile #1040

merged 13 commits into from
Sep 30, 2020

Conversation

BenGamma
Copy link
Contributor

Closes #974
Based on work in #1031

@cypress
Copy link

cypress bot commented Sep 14, 2020



Test summary

60 0 0 0


Run details

Project onearmy-community-platform
Status Passed
Commit 209bce5 ℹ️
Started Sep 30, 2020 11:20 AM
Ended Sep 30, 2020 11:33 AM
Duration 12:41 💡
OS Linux Ubuntu Linux - 16.04
Browser Multiple

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@alromh87
Copy link
Collaborator

This is the script Im preparing form migration, I have been testing it from Cloud Console since I have no permissions to execute https endpoint

It also contains code for migration for 972 Update all flags in HowTos

const functions = require('firebase-functions')
const request = require('request')

const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

// Change for prod:
// import { db } from '../Firebase/firestoreDB'


// Compute one user stats
async function computeUserStats(owner){
  console.log("Calculando para ", owner);

  stats = {};
  try {
    stats.howToCount = await db.collection('v3_howtos').where("_createdBy", "==", owner).where("moderation", "==", "accepted")
      .get()
      .then(querySnapshot => {
        let count = 0;
        querySnapshot.forEach(doc => {
          count++;
        });
//        console.log('Accepted howtos for', owner ,', count: ', count);
        return count
    });

    stats.eventCount = await db.collection('v3_events').where("_createdBy", "==", owner).where("moderation", "==", "accepted")
      .get()
      .then(querySnapshot => {
        let count = 0;
        querySnapshot.forEach(doc => {
          count++;
        });
//        console.log('Accepted events for', owner ,',count: ', count);
        return count
    });

    return stats
  } catch(e) { 
    console.log('Error compute:',e)
  }
  return null
}

// Compute all user stats
function computeAllUsersStats() {

  console.log('Computing all user stats ...')

  db.collection('v3_users')
//.where("_id", "==", "benjj")
  .get()
  .then(querySnapshot =>{
    return querySnapshot.forEach(user => {
      computeUserStats(user.id).then( stats => {
        console.log('Response @All users');
        console.log(stats);
        if(stats){
          user.ref.update({
            _stats: admin.firestore.FieldValue.delete(),
            stats: stats
          });
        }
        return null
      }).catch( e => {});
    })
  }).catch( e =>{
    console.log('Error query usuarios:', e)
    return null
  })
}

// Set all user flags
function updateAllFlags() {

  console.log('Updating all flags ...')

  db.collection('v3_users')
  .get()
  .then(querySnapshot =>{
    return querySnapshot.forEach(user => {
      const country = (user.data().location && user.data().location.countryCode) ? user.data().location.countryCode : (user.data().country ? user.data().country.toLowerCase() : '')
      if(country !== ''){

        console.log('Procesing', user.data()._id, 'flags');

        db.collection('v3_howtos').where("_createdBy", "==", user.data()._id)
        .get()
        .then(querySnapshot => {
          querySnapshot.forEach(doc => {
            console.log('updating doc', doc.data()._id, 'to', country);
            doc.ref.update({'_creatorCountry': country})
            .then( () => {
              console.log("Document successfully updated!");
              return null
            }).catch( error => {
              console.error("Error updating HowToCountry: ", error);
              return null
            });
          });
          return null
        }).catch(e => console.log('Error getting user howTos:', e));
      }else{
        console.log('No info for ', user.data()._id, 'flags');
      }
    })
  }).catch( e =>{
    console.log('Error query usuarios:', e)
    return null
  })
}

// Cant trigger from url, fix permissions
exports.batchUpdate = functions.https.onRequest(async (req, res) => {
  await computeAllUsersStats();
//  await updateAllFlags();
  res.status(200).send('Finished');
});

@alromh87 alromh87 requested review from alromh87, chrismclarke and davehakkens and removed request for alromh87 September 14, 2020 13:52
Now they will calculate stats if user has no previous information
@alromh87
Copy link
Collaborator

alromh87 commented Sep 14, 2020

This is the script Im preparing form migration, ...

Updated the count functions to handle no previously calculated data:

  • now for first accepted event/how-to after installation trigger will compute user stats R38

@alromh87
Copy link
Collaborator

This is the script Im preparing form migration, ...

Updated the count functions to handle no previously calculated data:

  • now for first accepted event/how-to after installation trigger will compute user stats R38

This makes the PR ready to merge no need to execute the migration script

@alromh87 alromh87 self-requested a review September 15, 2020 12:30
@BenGamma
Copy link
Contributor Author

Thanks for updating the script !
I think it's better if we separate the part to update the flag on how-to (which is part of #972) from this PR, but since it's not part of the user-stats.ts file, to me it's good.
Other than that, it's good for me.
@chrismclarke can you double check the user-stats.ts file before merge ?

@alromh87
Copy link
Collaborator

alromh87 commented Sep 16, 2020

Thanks for updating the script !
I think it's better if we separate the part to update the flag on how-to (which is part of #972) from this PR, but since it's not part of the user-stats.ts file, to me it's good.
Other than that, it's good for me.
@chrismclarke can you double check the user-stats.ts file before merge ?

Glad to help

The files in PR are targeted only to this PR, the script posted here is only for reference and not included in PR.

@BenGamma
Copy link
Contributor Author

The files in PR are targeted only to this PR, the script posted here is only for reference and not included in PR.

Yes I got that :)

Copy link
Member

@chrismclarke chrismclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see a larger feature like this coming in, nice work on front and back end. I've given a few inline comments, but would be happy to still merge and review after we've tested a little on the dev site.

I'm guessing we might also want a one-off function that could be called from the API to populate the db with all the existing howtos/events.

functions/src/Integrations/user-stats.ts Show resolved Hide resolved
functions/src/Integrations/user-stats.ts Show resolved Hide resolved
functions/src/Integrations/user-stats.ts Show resolved Hide resolved
functions/src/Integrations/user-stats.ts Show resolved Hide resolved
functions/src/Integrations/user-stats.ts Outdated Show resolved Hide resolved
functions/src/Integrations/user-stats.ts Show resolved Hide resolved
src/models/user.models.tsx Show resolved Hide resolved
functions/src/Integrations/user-stats.ts Outdated Show resolved Hide resolved
@alromh87
Copy link
Collaborator

Could this be merged?

@BenGamma BenGamma merged commit ba4f202 into master Sep 30, 2020
@BenGamma BenGamma deleted the feat/974-extra-infos-profile branch September 30, 2020 11:34
This was referenced Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extra information on Profile
3 participants