From 5ca478772c555229113b5073a8d35fb1bda5e493 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Fri, 4 Apr 2025 06:03:02 +0200 Subject: [PATCH] Using entityIds and alertIds fields in alert dialog API call for consistency. Improved new alert data validation. --- components/alerts/create.vue | 4 ++-- src/alerts.js | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/components/alerts/create.vue b/components/alerts/create.vue index f302f5b..91d316b 100644 --- a/components/alerts/create.vue +++ b/components/alerts/create.vue @@ -446,9 +446,9 @@ async function createAlert() { allTags: tagAnd.value, allMatches: matchAnd.value, actors: actors.value.map((actor) => actor.id), - tags: tags.value.map((tag) => tag.id), + tagIds: tags.value.map((tag) => tag.id), matches: matches.value, - entities: entities.value.map((entity) => entity.id), + entityIds: entities.value.map((entity) => entity.id), notify: notify.value, email: email.value, stashes: stashes.value.map((stash) => stash.id), diff --git a/src/alerts.js b/src/alerts.js index 8f295ce..e149f04 100755 --- a/src/alerts.js +++ b/src/alerts.js @@ -136,7 +136,10 @@ export async function createAlert(alert, reqUser) { throw new HttpError('You are not authenthicated', 401); } - if ((!alert.actors || alert.actors.length === 0) && (!alert.tags || alert.tags.length === 0) && (!alert.entities || alert.entities.length === 0) && (!alert.matches || alert.matches.length === 0)) { + const tagIds = (await getIdsBySlug(alert.tags, 'tags') || []).concat(alert.tagIds || []); + const entityIds = (await getIdsBySlug(alert.entities || [], 'entities')).concat(alert.entityIds || []); + + if ((!alert.actors || alert.actors.length === 0) && tagIds.length === 0 && entityIds.length === 0 && (!alert.matches || alert.matches.length === 0)) { throw new HttpError('Alert must contain at least one actor, tag or entity', 400); } @@ -144,6 +147,14 @@ export async function createAlert(alert, reqUser) { throw new HttpError('Match must define a property and an expression', 400); } + if (tagIds.length < (alert.tags?.length || 0) + (alert.tagIds?.length || 0)) { + throw new HttpError('Failed to resolve all tags'); + } + + if (entityIds.length < (alert.entities?.length || 0) + (alert.entityIds?.length || 0)) { + throw new HttpError('Failed to resolve all entities'); + } + const [{ id: alertId }] = await knex('alerts') .insert({ user_id: reqUser.id, @@ -160,17 +171,6 @@ export async function createAlert(alert, reqUser) { }) .returning('id'); - const tagIds = (await getIdsBySlug(alert.tags, 'tags') || []).concat(alert.tagIds || []); - const entityIds = (await getIdsBySlug(alert.entities || [], 'entities')).concat(alert.entityIds || []); - - if (tagIds.length < (alert.tags?.length || 0) + (alert.tagIds?.length || 0)) { - throw new HttpError('Failed to resolve all tags'); - } - - if (entityIds.length < (alert.entities?.length || 0) + (alert.entityIds?.length || 0)) { - throw new HttpError('Failed to resolve all entities'); - } - await Promise.all([ alert.actors?.length > 0 && knex('alerts_actors').insert(alert.actors.map((actorId) => ({ alert_id: alertId,