Using entityIds and alertIds fields in alert dialog API call for consistency. Improved new alert data validation.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user