Fixed alert tool to transfer combinations.
This commit is contained in:
parent
9bdd3ff2f3
commit
9c63b31dfa
|
@ -5,34 +5,57 @@ const fs = require('fs');
|
||||||
const knex = require('../knex');
|
const knex = require('../knex');
|
||||||
const args = require('../argv');
|
const args = require('../argv');
|
||||||
|
|
||||||
async function getActorId(alert) {
|
async function importActors(alert) {
|
||||||
if (alert.actorSlug) {
|
await alert.actors.reduce(async (chain, actor) => {
|
||||||
const actor = await knex('actors')
|
await chain;
|
||||||
|
|
||||||
|
const actorEntry = await knex('actors')
|
||||||
.select('actors.id')
|
.select('actors.id')
|
||||||
.leftJoin('entities', 'entities.id', 'actors.entity_id')
|
.leftJoin('entities', 'entities.id', 'actors.entity_id')
|
||||||
.where('actors.slug', alert.actorSlug)
|
.where('actors.slug', actor.slug)
|
||||||
.where((builder) => {
|
.where((builder) => {
|
||||||
if (alert.actorEntitySlug) {
|
if (actor.entitySlug) {
|
||||||
builder
|
builder
|
||||||
.where('entities.slug', alert.actorEntitySlug)
|
.where('entities.slug', actor.entitySlug)
|
||||||
.where('entities.type', alert.actorEntityType);
|
.where('entities.type', actor.entityType);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.first();
|
.first();
|
||||||
|
|
||||||
if (actor) {
|
if (!actorEntry) {
|
||||||
return actor.id;
|
throw new Error(`No actor ${actor.slug} for ${alert.username}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`No actor ${alert.actorSlug} for ${alert.username}`);
|
await knex('alerts_actors').insert({
|
||||||
}
|
alert_id: alert.id,
|
||||||
|
actor_id: actorEntry.id,
|
||||||
return null;
|
});
|
||||||
|
}, Promise.resolve());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getEntityId(alert) {
|
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) {
|
if (alert.entitySlug) {
|
||||||
const entity = await knex('entities')
|
const entityEntry = await knex('entities')
|
||||||
.select('entities.id')
|
.select('entities.id')
|
||||||
.where({
|
.where({
|
||||||
slug: alert.entitySlug,
|
slug: alert.entitySlug,
|
||||||
|
@ -40,88 +63,36 @@ async function getEntityId(alert) {
|
||||||
})
|
})
|
||||||
.first();
|
.first();
|
||||||
|
|
||||||
if (entity) {
|
if (!entityEntry) {
|
||||||
return entity.id;
|
throw new Error(`No entity ${alert.entitySlug} for ${alert.username}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`No entity ${alert.entitySlug} for ${alert.username}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getTagId(alert) {
|
|
||||||
if (alert.tagSlug) {
|
|
||||||
const tag = await knex('tags')
|
|
||||||
.select('tags.id')
|
|
||||||
.where('slug', alert.tagSlug)
|
|
||||||
.first();
|
|
||||||
|
|
||||||
if (tag) {
|
|
||||||
return tag.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`No tag ${alert.stashSlug} for ${alert.username}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getStashId(alert, userId) {
|
|
||||||
if (alert.stashSlug) {
|
|
||||||
const stash = await knex('stashes')
|
|
||||||
.select('stashes.id')
|
|
||||||
.where('stashes.slug', alert.stashSlug)
|
|
||||||
.where('stashes.user_id', userId)
|
|
||||||
.first();
|
|
||||||
|
|
||||||
if (stash) {
|
|
||||||
return stash.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`No stash ${alert.stashSlug} for ${alert.username}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createAlert(alert, { userId, actorId, entityId, tagId, stashId }) {
|
|
||||||
const [alertId] = await knex('alerts')
|
|
||||||
.insert({
|
|
||||||
user_id: userId,
|
|
||||||
notify: alert.notify,
|
|
||||||
email: alert.email,
|
|
||||||
created_at: alert.createdAt,
|
|
||||||
})
|
|
||||||
.returning('id');
|
|
||||||
|
|
||||||
if (actorId) {
|
|
||||||
await knex('alerts_actors').insert({
|
|
||||||
alert_id: alertId,
|
|
||||||
actor_id: actorId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entityId) {
|
|
||||||
await knex('alerts_entities').insert({
|
await knex('alerts_entities').insert({
|
||||||
alert_id: alertId,
|
alert_id: alert.id,
|
||||||
entity_id: entityId,
|
entity_id: entityEntry.id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (tagId) {
|
async function importStashes(alert, user) {
|
||||||
await knex('alerts_tags').insert({
|
await alert.stashes.reduce(async (chain, stash) => {
|
||||||
alert_id: alertId,
|
await chain;
|
||||||
tag_id: tagId,
|
|
||||||
});
|
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}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (stashId) {
|
|
||||||
await knex('alerts_stashes').insert({
|
await knex('alerts_stashes').insert({
|
||||||
alert_id: alertId,
|
alert_id: alert.id,
|
||||||
stash_id: stashId,
|
stash_id: stashEntry.id,
|
||||||
});
|
});
|
||||||
}
|
}, Promise.resolve());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
|
@ -145,18 +116,24 @@ async function load() {
|
||||||
throw new Error(`No user ${alert.username}`);
|
throw new Error(`No user ${alert.username}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const actorId = await getActorId(alert);
|
const [alertId] = await knex('alerts')
|
||||||
const entityId = await getEntityId(alert);
|
.insert({
|
||||||
const tagId = await getTagId(alert);
|
user_id: user.id,
|
||||||
const stashId = await getStashId(alert, user.id);
|
notify: alert.notify,
|
||||||
|
email: alert.email,
|
||||||
|
created_at: alert.createdAt,
|
||||||
|
})
|
||||||
|
.returning('id');
|
||||||
|
|
||||||
await createAlert(alert, {
|
try {
|
||||||
userId: user.id,
|
await importActors({ ...alert, id: alertId }, user);
|
||||||
actorId,
|
await importTags({ ...alert, id: alertId }, user);
|
||||||
entityId,
|
await importEntity({ ...alert, id: alertId }, user);
|
||||||
stashId,
|
await importStashes({ ...alert, id: alertId }, user);
|
||||||
tagId,
|
} catch (error) {
|
||||||
});
|
await knex('alerts').where('id', alertId).delete();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`Loaded alert ${index + 1}/${alerts.length} from ${alert.username}`);
|
console.log(`Loaded alert ${index + 1}/${alerts.length} from ${alert.username}`);
|
||||||
}, Promise.resolve());
|
}, Promise.resolve());
|
||||||
|
|
|
@ -12,45 +12,51 @@ async function save() {
|
||||||
.select([
|
.select([
|
||||||
'alerts.*',
|
'alerts.*',
|
||||||
'users.username',
|
'users.username',
|
||||||
'actors.slug as actor_slug',
|
|
||||||
'actors_entities.slug as actor_entity_slug',
|
|
||||||
'actors_entities.type as actor_entity_type',
|
|
||||||
'entities.slug as entity_slug',
|
'entities.slug as entity_slug',
|
||||||
'entities.type as entity_type',
|
'entities.type as entity_type',
|
||||||
'stashes.slug as stash_slug',
|
|
||||||
'tags.slug as tag_slug',
|
|
||||||
])
|
])
|
||||||
.leftJoin('users', 'users.id', 'alerts.user_id')
|
.leftJoin('users', 'users.id', 'alerts.user_id')
|
||||||
.leftJoin('alerts_actors', 'alerts_actors.alert_id', 'alerts.id')
|
|
||||||
.leftJoin('alerts_entities', 'alerts_entities.alert_id', 'alerts.id')
|
.leftJoin('alerts_entities', 'alerts_entities.alert_id', 'alerts.id')
|
||||||
.leftJoin('alerts_stashes', 'alerts_stashes.alert_id', 'alerts.id')
|
.leftJoin('entities', 'entities.id', 'alerts_entities.entity_id');
|
||||||
.leftJoin('alerts_tags', 'alerts_tags.alert_id', 'alerts.id')
|
|
||||||
.leftJoin('actors', 'actors.id', 'alerts_actors.actor_id')
|
|
||||||
.leftJoin('entities', 'entities.id', 'alerts_entities.entity_id')
|
|
||||||
.leftJoin('tags', 'tags.id', 'alerts_tags.tag_id')
|
|
||||||
.leftJoin('stashes', 'stashes.id', 'alerts_stashes.stash_id')
|
|
||||||
.leftJoin('entities as actors_entities', 'actors_entities.id', 'actors.entity_id');
|
|
||||||
|
|
||||||
let savedAlerts = 0;
|
let savedAlerts = 0;
|
||||||
|
|
||||||
await alerts.reduce(async (chain, alert) => {
|
await alerts.reduce(async (chain, alert) => {
|
||||||
await chain;
|
await chain;
|
||||||
|
|
||||||
const curatedAlert = JSON.stringify({
|
const actors = await knex('alerts_actors')
|
||||||
|
.select('actors.slug', 'entities.slug as entity_slug', 'entities.type as entity_type')
|
||||||
|
.leftJoin('actors', 'actors.id', 'alerts_actors.actor_id')
|
||||||
|
.leftJoin('entities', 'entities.id', 'actors.entity_id')
|
||||||
|
.where('alert_id', alert.id);
|
||||||
|
|
||||||
|
const tags = await knex('alerts_tags')
|
||||||
|
.select('tags.slug')
|
||||||
|
.leftJoin('tags', 'tags.id', 'alerts_tags.tag_id')
|
||||||
|
.where('alert_id', alert.id);
|
||||||
|
|
||||||
|
const stashes = await knex('alerts_stashes')
|
||||||
|
.select('stashes.slug')
|
||||||
|
.leftJoin('stashes', 'stashes.id', 'alerts_stashes.stash_id')
|
||||||
|
.where('alert_id', alert.id);
|
||||||
|
|
||||||
|
const curatedAlert = {
|
||||||
username: alert.username,
|
username: alert.username,
|
||||||
notify: alert.notify,
|
notify: alert.notify,
|
||||||
email: alert.email,
|
email: alert.email,
|
||||||
createdAt: alert.created_at,
|
createdAt: alert.created_at,
|
||||||
actorSlug: alert.actor_slug,
|
actors: actors.map((actor) => ({
|
||||||
actorEntitySlug: alert.actor_entity_slug,
|
slug: actor.slug,
|
||||||
actorEntityType: alert.actor_entity_type,
|
entitySlug: actor.entity_slug,
|
||||||
|
entityType: actor.entity_type,
|
||||||
|
})),
|
||||||
|
tags: tags.map((tag) => tag.slug),
|
||||||
|
stashes: stashes.map((stash) => stash.slug),
|
||||||
entitySlug: alert.entity_slug,
|
entitySlug: alert.entity_slug,
|
||||||
entityType: alert.entity_type,
|
entityType: alert.entity_type,
|
||||||
stashSlug: alert.stash_slug,
|
};
|
||||||
tagSlug: alert.tag_slug,
|
|
||||||
});
|
|
||||||
|
|
||||||
await fs.promises.appendFile(filename, `${curatedAlert}\n`);
|
await fs.promises.appendFile(filename, `${JSON.stringify(curatedAlert)}\n`);
|
||||||
|
|
||||||
console.log(`Saved ${alert.username} alert`);
|
console.log(`Saved ${alert.username} alert`);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue