traxxx/src/tools/alerts-load.js

145 lines
3.2 KiB
JavaScript

'use strict';
const fs = require('fs');
const knex = require('../knex');
const args = require('../argv');
async function importActors(alert) {
await alert.actors.reduce(async (chain, actor) => {
await chain;
const actorEntry = await knex('actors')
.select('actors.id')
.leftJoin('entities', 'entities.id', 'actors.entity_id')
.where('actors.slug', actor.slug)
.where((builder) => {
if (actor.entitySlug) {
builder
.where('entities.slug', actor.entitySlug)
.where('entities.type', actor.entityType);
}
})
.first();
if (!actorEntry) {
throw new Error(`No actor ${actor.slug} for ${alert.username}`);
}
await knex('alerts_actors').insert({
alert_id: alert.id,
actor_id: actorEntry.id,
});
}, Promise.resolve());
}
async function importTags(alert) {
await alert.tags.reduce(async (chain, tag) => {
await chain;
const tagEntry = await knex('tags')
.select('tags.id')
.where('slug', tag)
.first();
if (!tag) {
throw new Error(`No tag ${tag} for ${alert.username}`);
}
await knex('alerts_tags').insert({
alert_id: alert.id,
tag_id: tagEntry.id,
});
}, Promise.resolve());
}
async function importEntity(alert) {
if (alert.entitySlug) {
const entityEntry = await knex('entities')
.select('entities.id')
.where({
slug: alert.entitySlug,
type: alert.entityType,
})
.first();
if (!entityEntry) {
throw new Error(`No entity ${alert.entitySlug} for ${alert.username}`);
}
await knex('alerts_entities').insert({
alert_id: alert.id,
entity_id: entityEntry.id,
});
}
}
async function importStashes(alert, user) {
await alert.stashes.reduce(async (chain, stash) => {
await chain;
const stashEntry = await knex('stashes')
.select('stashes.id')
.where('stashes.slug', stash)
.where('stashes.user_id', user.id)
.first();
if (!stashEntry) {
throw new Error(`No stash ${stash} for ${alert.username}`);
}
await knex('alerts_stashes').insert({
alert_id: alert.id,
stash_id: stashEntry.id,
});
}, Promise.resolve());
}
async function load() {
const filename = process.argv[2];
const file = await fs.promises.readFile(filename, 'utf8');
const alerts = file.split('\n')
.filter(Boolean)
.map((data) => JSON.parse(data))
.filter((alert) => !args.username || alert.username === args.username);
await alerts.reduce(async (chain, alert, index) => {
await chain;
const user = await knex('users')
.select('id')
.where('username', alert.username)
.first();
if (!user) {
throw new Error(`No user ${alert.username}`);
}
const [{ id: alertId }] = await knex('alerts')
.insert({
user_id: user.id,
notify: alert.notify,
email: alert.email,
created_at: alert.createdAt,
})
.returning('id');
try {
await importActors({ ...alert, id: alertId }, user);
await importTags({ ...alert, id: alertId }, user);
await importEntity({ ...alert, id: alertId }, user);
await importStashes({ ...alert, id: alertId }, user);
} catch (error) {
await knex('alerts').where('id', alertId).delete();
throw error;
}
console.log(`Loaded alert ${index + 1}/${alerts.length} from ${alert.username}`);
}, Promise.resolve());
process.exit();
}
load();