Refactor backend to import/export syntax and update deps

This commit is contained in:
Ben Grant 2022-08-20 21:35:38 +10:00
parent 1463773480
commit 7d0f3898de
20 changed files with 3168 additions and 685 deletions

View file

@ -1,46 +1,48 @@
const dayjs = require('dayjs');
import dayjs from 'dayjs'
module.exports = async (req, res) => {
const taskRemoveOrphans = async (req, res) => {
if (req.header('X-Appengine-Cron') === undefined) {
return res.status(400).send('This task can only be run from a cron job');
return res.status(400).send('This task can only be run from a cron job')
}
const threeMonthsAgo = dayjs().subtract(3, 'month').unix();
const threeMonthsAgo = dayjs().subtract(3, 'month').unix()
console.log(`Running orphan removal task at ${dayjs().format('h:mma D MMM YYYY')}`);
console.log(`Running orphan removal task at ${dayjs().format('h:mma D MMM YYYY')}`)
try {
try {
// Fetch people that are older than 3 months
const peopleQuery = req.datastore.createQuery(req.types.person).filter('created', '<', threeMonthsAgo);
let oldPeople = (await req.datastore.runQuery(peopleQuery))[0];
const peopleQuery = req.datastore.createQuery(req.types.person).filter('created', '<', threeMonthsAgo)
const oldPeople = (await req.datastore.runQuery(peopleQuery))[0]
if (oldPeople && oldPeople.length > 0) {
console.log(`Found ${oldPeople.length} people older than 3 months, checking for events`);
if (oldPeople && oldPeople.length > 0) {
console.log(`Found ${oldPeople.length} people older than 3 months, checking for events`)
// Fetch events linked to the people discovered
let peopleWithoutEvents = 0;
await Promise.all(oldPeople.map(async (person) => {
let event = (await req.datastore.get(req.datastore.key([req.types.event, person.eventId])))[0];
let peopleWithoutEvents = 0
await Promise.all(oldPeople.map(async person => {
const event = (await req.datastore.get(req.datastore.key([req.types.event, person.eventId])))[0]
if (!event) {
peopleWithoutEvents++;
await req.datastore.delete(person[req.datastore.KEY]);
peopleWithoutEvents++
await req.datastore.delete(person[req.datastore.KEY])
}
}));
}))
if (peopleWithoutEvents > 0) {
console.log(`Orphan removal successful: ${oldEventIds.length} events and ${peopleDiscovered} people removed`);
res.sendStatus(200);
console.log(`Orphan removal successful: ${peopleWithoutEvents} people removed`)
res.sendStatus(200)
} else {
console.log(`Found 0 people without events, ending orphan removal`);
res.sendStatus(404);
console.log('Found 0 people without events, ending orphan removal')
res.sendStatus(404)
}
} else {
console.log(`Found 0 people older than 3 months, ending orphan removal`);
res.sendStatus(404);
}
} catch (e) {
console.error(e);
res.sendStatus(404);
}
};
} else {
console.log('Found 0 people older than 3 months, ending orphan removal')
res.sendStatus(404)
}
} catch (e) {
console.error(e)
res.sendStatus(404)
}
}
export default taskRemoveOrphans