Using entityIds and alertIds fields in alert dialog API call for consistency. Improved new alert data validation.
This commit is contained in:
parent
dc876e7aa2
commit
5ca478772c
|
@ -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),
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue