Using entityIds and alertIds fields in alert dialog API call for consistency. Improved new alert data validation.

This commit is contained in:
DebaucheryLibrarian 2025-04-04 06:03:02 +02:00
parent dc876e7aa2
commit 5ca478772c
2 changed files with 14 additions and 14 deletions

View File

@ -446,9 +446,9 @@ async function createAlert() {
allTags: tagAnd.value, allTags: tagAnd.value,
allMatches: matchAnd.value, allMatches: matchAnd.value,
actors: actors.value.map((actor) => actor.id), actors: actors.value.map((actor) => actor.id),
tags: tags.value.map((tag) => tag.id), tagIds: tags.value.map((tag) => tag.id),
matches: matches.value, matches: matches.value,
entities: entities.value.map((entity) => entity.id), entityIds: entities.value.map((entity) => entity.id),
notify: notify.value, notify: notify.value,
email: email.value, email: email.value,
stashes: stashes.value.map((stash) => stash.id), stashes: stashes.value.map((stash) => stash.id),

View File

@ -136,7 +136,10 @@ export async function createAlert(alert, reqUser) {
throw new HttpError('You are not authenthicated', 401); 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); 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); 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') const [{ id: alertId }] = await knex('alerts')
.insert({ .insert({
user_id: reqUser.id, user_id: reqUser.id,
@ -160,17 +171,6 @@ export async function createAlert(alert, reqUser) {
}) })
.returning('id'); .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([ await Promise.all([
alert.actors?.length > 0 && knex('alerts_actors').insert(alert.actors.map((actorId) => ({ alert.actors?.length > 0 && knex('alerts_actors').insert(alert.actors.map((actorId) => ({
alert_id: alertId, alert_id: alertId,