Files
watchparty/functions/modules2/scheduledCleanUp.js
T
2020-05-31 18:57:17 -04:00

37 lines
1.0 KiB
JavaScript

const admin = require('firebase-admin');
const FunctionModules = require('./index');
module.exports = async () => {
const startedTime = new Date();
const roomList = await admin
.firestore()
.collection('rooms')
.listDocuments()
.then((rL) => rL)
.catch((err) => {
console.error(
`Failed to run auto clean up (get room list) @ ${new Date().toLocaleTimeString()}`,
err
);
});
roomList.forEach((roomRef) => {
roomRef
.get()
.then(async (roomSnap) => {
if (
FunctionModules.timeDiff(roomSnap.createTime.toDate(), startedTime) >= 24 ||
FunctionModules.timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
)
await FunctionModules.cleanUpRoom(roomSnap.id);
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
return;
})
.catch((err) => {
console.error(
`Failed to run auto clean up (get room snap) @ ${new Date().toLocaleTimeString()}`,
err
);
});
});
};