Skip to content

Commit 89b7e1a

Browse files
chore(utils): include fast and throttled utils
1 parent 064786d commit 89b7e1a

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

utils/firebase/utils.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ const addResourceTimestamps = async (col) => {
660660
);
661661
};
662662

663-
const triggerUpdate = async (col = 'users', filters = {}) => {
663+
const triggerUpdate = async (col = 'users', filters = {}, throttle = false) => {
664664
console.log(`Fetching ${col}...`);
665665
const pathname = `https://develop.tutorbook.app/api/${col}`;
666666
const endpoint = url.format({
@@ -674,24 +674,35 @@ const triggerUpdate = async (col = 'users', filters = {}) => {
674674
console.log(`Updating ${data.hits} ${col}...`);
675675
let count = 0;
676676
bar.start(data[col].length, count);
677-
for (const res of data[col]) {
678-
if (!res.created) res.created = new Date().toJSON();
679-
if (!res.updated) res.updated = new Date().toJSON();
680-
const [err] = await to(
681-
axios.put(`${pathname}/${res.id}`, res, { headers })
682-
);
683-
bar.update((count += 1));
684-
if (err) {
685-
console.error(`${err.name} updating ${res.name} (${res.id}):`, res);
686-
debugger;
677+
if (throttle) {
678+
for (const res of data[col]) {
679+
const [err] = await to(
680+
axios.put(`${pathname}/${res.id}`, res, { headers })
681+
);
682+
bar.update((count += 1));
683+
if (err) {
684+
console.error(`${err.name} updating ${res.name} (${res.id}):`, res);
685+
debugger;
686+
}
687687
}
688+
} else {
689+
await Promise.all(
690+
data[col].map(async (res) => {
691+
const [err] = await to(
692+
axios.put(`${pathname}/${res.id}`, res, { headers })
693+
);
694+
bar.update((count += 1));
695+
if (err) {
696+
console.error(`${err.name} updating ${res.name} (${res.id}):`, res);
697+
debugger;
698+
}
699+
})
700+
);
688701
}
689-
console.log(`Updated ${data.hits} ${col}.`);
702+
console.log(`\nUpdated ${data.hits} ${col}.`);
690703
};
691704

692-
triggerUpdate('users', {
693-
orgs: encodeURIComponent(JSON.stringify(['quarantunes'])),
694-
});
705+
triggerUpdate('matches', { org: 'quarantunes' });
695706

696707
// Deletes all users that come from the old app:
697708
// - Anyone w/out a bio.

0 commit comments

Comments
 (0)