@@ -7,7 +7,12 @@ import { groupBy } from 'lodash';
77import { z } from 'zod' ;
88
99import type { ObjectId } from '@/models' ;
10- import Alert , { AlertSource , IAlert } from '@/models/alert' ;
10+ import Alert , {
11+ AlertChannel ,
12+ AlertSource ,
13+ getAlertChannels ,
14+ IAlert ,
15+ } from '@/models/alert' ;
1116import Dashboard , { IDashboard } from '@/models/dashboard' ;
1217import { ISavedSearch , SavedSearch } from '@/models/savedSearch' ;
1318import { IUser } from '@/models/user' ;
@@ -18,6 +23,7 @@ import { alertSchema, objectIdSchema } from '@/utils/zod';
1823export type AlertInput = Omit <
1924 IAlert ,
2025 | 'id'
26+ | 'channel'
2127 | 'scheduleStartAt'
2228 | 'savedSearchId'
2329 | 'createdAt'
@@ -27,6 +33,9 @@ export type AlertInput = Omit<
2733 | 'state'
2834> & {
2935 id ?: string ;
36+ // Exactly one of channel/channels is provided (enforced by alertSchema);
37+ // `channels` flows in optionally via IAlert.
38+ channel ?: AlertChannel ;
3039 // Replace the Date-type fields from IAlert
3140 scheduleStartAt ?: string | null ;
3241 // Replace the ObjectId-type fields from IAlert
@@ -44,7 +53,12 @@ export const validateAlertInput = async (
4453 teamId : ObjectId ,
4554 alertInput : Pick <
4655 AlertInput ,
47- 'source' | 'dashboardId' | 'tileId' | 'savedSearchId' | 'channel'
56+ | 'source'
57+ | 'dashboardId'
58+ | 'tileId'
59+ | 'savedSearchId'
60+ | 'channel'
61+ | 'channels'
4862 > ,
4963) => {
5064 if ( alertInput . source === AlertSource . TILE ) {
@@ -94,21 +108,33 @@ export const validateAlertInput = async (
94108 }
95109 }
96110
97- if ( alertInput . channel . type === 'webhook' ) {
98- validateObjectId ( alertInput . channel . webhookId , 'Invalid webhook ID' ) ;
111+ const channels = getAlertChannels ( alertInput ) ;
112+ if ( channels . length === 0 ) {
113+ throw new Api400Error ( 'At least one notification channel is required' ) ;
114+ }
99115
100- if (
101- ( await Webhook . findOne ( {
102- _id : alertInput . channel . webhookId ,
103- team : teamId ,
104- } ) ) == null
105- ) {
106- throw new Api400Error ( 'Webhook not found' ) ;
107- }
116+ const webhookIds = channels
117+ . filter ( c => c . type === 'webhook' )
118+ . map ( c => c . webhookId ) ;
119+ for ( const webhookId of webhookIds ) {
120+ validateObjectId ( webhookId , 'Invalid webhook ID' ) ;
121+ }
122+ const uniqueIds = [ ...new Set ( webhookIds ) ] ;
123+ const found = await Webhook . countDocuments ( {
124+ _id : { $in : uniqueIds } ,
125+ team : teamId ,
126+ } ) ;
127+ if ( found !== uniqueIds . length ) {
128+ throw new Api400Error ( 'Webhook not found' ) ;
108129 }
109130} ;
110131
111- const makeAlert = ( alert : AlertInput , userId ?: ObjectId ) : Partial < IAlert > => {
132+ // Exported for unit testing the channel-mirroring invariant (see
133+ // controllers/__tests__/alerts.test.ts) -- otherwise only used internally.
134+ export const makeAlert = (
135+ alert : AlertInput ,
136+ userId ?: ObjectId ,
137+ ) : Partial < IAlert > => {
112138 // Preserve existing DB value when scheduleStartAt is omitted from updates
113139 // (undefined), while still allowing explicit clears via null.
114140 const hasScheduleStartAt = alert . scheduleStartAt !== undefined ;
@@ -124,9 +150,14 @@ const makeAlert = (alert: AlertInput, userId?: ObjectId): Partial<IAlert> => {
124150 : alert . scheduleOffsetMinutes ;
125151 const isSavedSearch = alert . source === AlertSource . SAVED_SEARCH ;
126152 const isTile = alert . source === AlertSource . TILE ;
153+ const channels = getAlertChannels ( alert ) ;
127154
128155 return {
129- channel : alert . channel ,
156+ // `channels` is canonical; `channel` mirrors channels[0] so readers that
157+ // predate multi-channel support (older task runners mid-rollout) still
158+ // notify the first target.
159+ channel : channels [ 0 ] ?? { type : null } ,
160+ channels,
130161 interval : alert . interval ,
131162 ...( normalizedScheduleOffsetMinutes != null && {
132163 scheduleOffsetMinutes : normalizedScheduleOffsetMinutes ,
0 commit comments