async function purge() {
    let purged = 0;
    try {
        // Get the current magister authcode before purging users.
        let mAuth = await MagisterAuth();
        // First check with a user we know to work to see we don't
        // purge users for google and magister being unreachable or so.
        let checkUser = await User.spot({email: 'kasper.77077@gmail.com'}).fetch();
        await checkUser.login(oAuth, mAuth); // check if checkuser can login.
        // Get all users from DB & run over them.
        let users = shuffleArray(await User.spot({isdisabled: false}).fetchAll());
        log.info(`Purging ${users.length} users...`);
        // Go through all users and purge if necessary.
        for (let user of users) {
            try {
                // Try logging in.
                await user.login(oAuth, mAuth);
                // Sleep a bit (0-10 sec) after logging in, to ease magister.
                await sleep(Math.floor(Math.random() * 5000));
                // If all tokens etc are valid and we log in,
                // then we actually get here. Check if there
                // are calendars left and if so, skip the purge.
                if (user.hasCalendars()) continue;
                user.log.warn(`Purging user for not having calendars left!`, {email: user.get('email')});
            } catch(err) {
                user.log.warn(`Purging user for `, err);
            }
            try {
                // If possible, warn user they are being placed on non-active.
                await user.calendar.events.insert({
                    calendarId: 'primary',
                    sendUpdates: 'all',
                    resource: {
                        summary: 'MAGBOT: Actie vereist! Lees mij!',
                        description: '<i>Volgens mij klopt er iets niet meer :(</i><br/>' + 
                                     '<br/>'+
                                     'Uw magister informatie is veranderd of u heeft ' +
                                     'bewust alle magbot agendas verwijderd.<br/>' +
                                     '<b>Bij deze wordt uw account op non-actief gezet '+
                                     'en zal niet automatisch meer werken!</b><br/>' +
                                     '<i>Heractiveer uw account op ' +
                                     '<a href="https://magbot.nl/signup/ ">magbot.nl</a></i><br/>' +
                                     '<br/>' +
                                     'Als u uw account niet meer wil hebben, onderneem dan ' + 
                                     'geen actie.<br/>' +
                                     '<br/>' +
                                     'Stuur bij vragen gerust een mailtje naar ' +
                                     '<a href="mailto:info@magbot.nl">info@magbot.nl</a><br/>',
                        // Set start and end two days from now at 0900-0930.
                        start: {
                            dateTime: new Date(new Date(Date.now() + 1728e5)
                            .setHours( 9,0,0,0))
                        },
                        end: {
                            dateTime: new Date(new Date(Date.now() + 1728e5)
                            .setHours(17,0,0,0))
                        },
                        location: 'internet',
                        colorId: 11, // Nice red color
                        reminders: {
                            useDefault: false,
                            overrides: [
                                { method: 'email', minutes: 1440 },
                                { method: 'popup', minutes: 1440 },
                                { method: 'popup', minutes: 5 }
                            ]
                        }
                    }
                });
                user.log.info(`Notified user of purge!`);
            } catch (err) {
                user.log.error(`Error warning to-be purged user! `, {err: err.toString()})
            }
            try {
                user.log.info(`Disabling!`)
                user.set('isdisabled', true);
                await user.update();
                log.info(`PURGED ${purged++} USERS...`);
            } catch (err) {
                log.error(`MAJOR PURGE ERROR `, err);
            }
        }
    } catch (err) { log.error('Purging error! ', err); }
    const next = scheduleTime() * 20;
    log.info(`Going for next purge in ${next} millis...`);
    setTimeout(purge, next);
}
/**
 * Purge function. Soft-deletes all users that have
 * no calendars left or can't log in anymore.
 * Note that soft-deleted users are still alowed
 * to update and reclaim their accounts.
 */
Purge function. Soft-deletes all users that have
no calendars left or can't log in anymore.
Note that soft-deleted users are still alowed
to update and reclaim their accounts.
Purge function . Soft - deletes all users that have no calendars left or can ' t log in anymore . Note that soft - deleted users are still alowed to update and reclaim their accounts .
async function purge() {
    let purged = 0;
    try {
        let mAuth = await MagisterAuth();
        let checkUser = await User.spot({email: 'kasper.77077@gmail.com'}).fetch();
        await checkUser.login(oAuth, mAuth); 
        let users = shuffleArray(await User.spot({isdisabled: false}).fetchAll());
        log.info(`Purging ${users.length} users...`);
        for (let user of users) {
            try {
                await user.login(oAuth, mAuth);
                await sleep(Math.floor(Math.random() * 5000));
                if (user.hasCalendars()) continue;
                user.log.warn(`Purging user for not having calendars left!`, {email: user.get('email')});
            } catch(err) {
                user.log.warn(`Purging user for `, err);
            }
            try {
                await user.calendar.events.insert({
                    calendarId: 'primary',
                    sendUpdates: 'all',
                    resource: {
                        summary: 'MAGBOT: Actie vereist! Lees mij!',
                        description: '<i>Volgens mij klopt er iets niet meer :(</i><br/>' + 
                                     '<br/>'+
                                     'Uw magister informatie is veranderd of u heeft ' +
                                     'bewust alle magbot agendas verwijderd.<br/>' +
                                     '<b>Bij deze wordt uw account op non-actief gezet '+
                                     'en zal niet automatisch meer werken!</b><br/>' +
                                     '<i>Heractiveer uw account op ' +
                                     '<a href="https://magbot.nl/signup/ ">magbot.nl</a></i><br/>' +
                                     '<br/>' +
                                     'Als u uw account niet meer wil hebben, onderneem dan ' + 
                                     'geen actie.<br/>' +
                                     '<br/>' +
                                     'Stuur bij vragen gerust een mailtje naar ' +
                                     '<a href="mailto:info@magbot.nl">info@magbot.nl</a><br/>',
                        start: {
                            dateTime: new Date(new Date(Date.now() + 1728e5)
                            .setHours( 9,0,0,0))
                        },
                        end: {
                            dateTime: new Date(new Date(Date.now() + 1728e5)
                            .setHours(17,0,0,0))
                        },
                        location: 'internet',
                        colorId: 11, 
                        reminders: {
                            useDefault: false,
                            overrides: [
                                { method: 'email', minutes: 1440 },
                                { method: 'popup', minutes: 1440 },
                                { method: 'popup', minutes: 5 }
                            ]
                        }
                    }
                });
                user.log.info(`Notified user of purge!`);
            } catch (err) {
                user.log.error(`Error warning to-be purged user! `, {err: err.toString()})
            }
            try {
                user.log.info(`Disabling!`)
                user.set('isdisabled', true);
                await user.update();
                log.info(`PURGED ${purged++} USERS...`);
            } catch (err) {
                log.error(`MAJOR PURGE ERROR `, err);
            }
        }
    } catch (err) { log.error('Purging error! ', err); }
    const next = scheduleTime() * 20;
    log.info(`Going for next purge in ${next} millis...`);
    setTimeout(purge, next);
}
async function purge ( ) { let purged = 0 ; try { let mAuth = await MagisterAuth ( ) ; let checkUser = await User . spot ( { email : 'kasper.77077@gmail.com' } ) . fetch ( ) ; await checkUser . login ( oAuth , mAuth ) ; let users = shuffleArray ( await User . spot ( { isdisabled : false } ) . fetchAll ( ) ) ; log . info ( ` ${ users . length } ` ) ; for ( let user of users ) { try { await user . login ( oAuth , mAuth ) ; await sleep ( Math . floor ( Math . random ( ) * 5000 ) ) ; if ( user . hasCalendars ( ) ) continue ; user . log . warn ( ` ` , { email : user . get ( 'email' ) } ) ; } catch ( err ) { user . log . warn ( ` ` , err ) ; } try { await user . calendar . events . insert ( { calendarId : 'primary' , sendUpdates : 'all' , resource : { summary : 'MAGBOT: Actie vereist! Lees mij!' , description : '<i>Volgens mij klopt er iets niet meer :(</i><br/>' + '<br/>' + 'Uw magister informatie is veranderd of u heeft ' + 'bewust alle magbot agendas verwijderd.<br/>' + '<b>Bij deze wordt uw account op non-actief gezet ' + 'en zal niet automatisch meer werken!</b><br/>' + '<i>Heractiveer uw account op ' + '<a href="https://magbot.nl/signup/ ">magbot.nl</a></i><br/>' + '<br/>' + 'Als u uw account niet meer wil hebben, onderneem dan ' + 'geen actie.<br/>' + '<br/>' + 'Stuur bij vragen gerust een mailtje naar ' + '<a href="mailto:info@magbot.nl">info@magbot.nl</a><br/>' , start : { dateTime : new Date ( new Date ( Date . now ( ) + 1728e5 ) . setHours ( 9 , 0 , 0 , 0 ) ) } , end : { dateTime : new Date ( new Date ( Date . now ( ) + 1728e5 ) . setHours ( 17 , 0 , 0 , 0 ) ) } , location : 'internet' , colorId : 11 , reminders : { useDefault : false , overrides : [ { method : 'email' , minutes : 1440 } , { method : 'popup' , minutes : 1440 } , { method : 'popup' , minutes : 5 } ] } } } ) ; user . log . info ( ` ` ) ; } catch ( err ) { user . log . error ( ` ` , { err : err . toString ( ) } ) } try { user . log . info ( ` ` ) user . set ( 'isdisabled' , true ) ; await user . update ( ) ; log . info ( ` ${ purged ++ } ` ) ; } catch ( err ) { log . error ( ` ` , err ) ; } } } catch ( err ) { log . error ( 'Purging error! ' , err ) ; } const next = scheduleTime ( ) * 20 ; log . info ( ` ${ next } ` ) ; setTimeout ( purge , next ) ; }
Purge function.
Purge function .
// Get the current magister authcode before purging users. // First check with a user we know to work to see we don't // purge users for google and magister being unreachable or so. // check if checkuser can login. // Get all users from DB & run over them. // Go through all users and purge if necessary. // Try logging in. // Sleep a bit (0-10 sec) after logging in, to ease magister. // If all tokens etc are valid and we log in, // then we actually get here. Check if there // are calendars left and if so, skip the purge. // If possible, warn user they are being placed on non-active. // Set start and end two days from now at 0900-0930. // Nice red color