Fixed alert tool to transfer combinations.

This commit is contained in:
DebaucheryLibrarian
2023-06-08 03:22:46 +02:00
parent 9bdd3ff2f3
commit 9c63b31dfa
2 changed files with 103 additions and 120 deletions

View File

@@ -5,34 +5,57 @@ const fs = require('fs');
const knex = require('../knex');
const args = require('../argv');
async function getActorId(alert) {
if (alert.actorSlug) {
const actor = await knex('actors')
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', alert.actorSlug)
.where('actors.slug', actor.slug)
.where((builder) => {
if (alert.actorEntitySlug) {
if (actor.entitySlug) {
builder
.where('entities.slug', alert.actorEntitySlug)
.where('entities.type', alert.actorEntityType);
.where('entities.slug', actor.entitySlug)
.where('entities.type', actor.entityType);
}
})
.first();
if (actor) {
return actor.id;
if (!actorEntry) {
throw new Error(`No actor ${actor.slug} for ${alert.username}`);
}
throw new Error(`No actor ${alert.actorSlug} for ${alert.username}`);
}
return null;
await knex('alerts_actors').insert({
alert_id: alert.id,
actor_id: actorEntry.id,
});
}, 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) {
const entity = await knex('entities')
const entityEntry = await knex('entities')
.select('entities.id')
.where({
slug: alert.entitySlug,
@@ -40,88 +63,36 @@ async function getEntityId(alert) {
})
.first();
if (entity) {
return entity.id;
if (!entityEntry) {
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({
alert_id: alertId,
entity_id: entityId,
alert_id: alert.id,
entity_id: entityEntry.id,
});
}
}
if (tagId) {
await knex('alerts_tags').insert({
alert_id: alertId,
tag_id: tagId,
});
}
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}`);
}
if (stashId) {
await knex('alerts_stashes').insert({
alert_id: alertId,
stash_id: stashId,
alert_id: alert.id,
stash_id: stashEntry.id,
});
}
}, Promise.resolve());
}
async function load() {
@@ -145,18 +116,24 @@ async function load() {
throw new Error(`No user ${alert.username}`);
}
const actorId = await getActorId(alert);
const entityId = await getEntityId(alert);
const tagId = await getTagId(alert);
const stashId = await getStashId(alert, user.id);
const [alertId] = await knex('alerts')
.insert({
user_id: user.id,
notify: alert.notify,
email: alert.email,
created_at: alert.createdAt,
})
.returning('id');
await createAlert(alert, {
userId: user.id,
actorId,
entityId,
stashId,
tagId,
});
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());