@@ -146,16 +146,35 @@ class SchedulerService {
146146 this . executor . stopAll ( ) ;
147147 }
148148
149+ /** 列出 scheduler,附 nextRun。disabled 默认排除 */
150+ async list ( opts ?: { includeDisabled ?: boolean } ) : Promise < ( SchedulerRow & { nextRun : number | null } ) [ ] > {
151+ const where = opts ?. includeDisabled ? { } : { disabled : false } ;
152+ const rows = await database . findAll < SchedulerRow > ( database . scheduler , { where } ) ;
153+ return rows . map ( r => ( { ...( r as any ) , nextRun : this . nextDate ( r . id ) } ) ) ;
154+ }
155+
149156 async delete ( schedulerId : number ) : Promise < void > {
150157 this . executor . cancel ( schedulerId ) ;
151158 await database . update ( database . scheduler , { disabled : true } , { where : { id : schedulerId } } ) ;
152159 }
153160
161+ /** 删除 profile 名下所有 scheduler(cancel cron + 硬删行)—— profile/session 删除时调用 */
162+ async cascadeDeleteByProfile ( profileId : number ) : Promise < void > {
163+ const rows = await database . findAll < SchedulerRow > ( database . scheduler , { where : { profileId } } ) ;
164+ for ( const r of rows ) this . executor . cancel ( r . id ) ;
165+ await database . destroy ( database . scheduler , { where : { profileId } } ) ;
166+ }
167+
154168 async update ( schedulerId : number , patch : Partial < Pick < SchedulerRow , "message" | "channelSessionId" | "aiProcess" > > ) : Promise < SchedulerRow | null > {
155169 const fields : Partial < SchedulerRow > = { } ;
156- if ( patch . message != null ) fields . message = patch . message ;
157- if ( patch . channelSessionId != null ) fields . channelSessionId = patch . channelSessionId ;
158- if ( patch . aiProcess != null ) fields . aiProcess = patch . aiProcess ;
170+ if ( patch . message != null ) fields . message = patch . message ;
171+ if ( patch . aiProcess != null ) fields . aiProcess = patch . aiProcess ;
172+ if ( patch . channelSessionId != null ) {
173+ // 改投递目标时 profileId 必须同步到新 session 的 profile,否则 list/delete 工具按 profileId 过滤会失配
174+ const session = await getChannelSession ( patch . channelSessionId , true ) ;
175+ fields . channelSessionId = patch . channelSessionId ;
176+ fields . profileId = session ! . profileId ;
177+ }
159178 if ( Object . keys ( fields ) . length === 0 ) return database . findByPk < SchedulerRow > ( database . scheduler , schedulerId ) ;
160179 await database . update ( database . scheduler , fields , { where : { id : schedulerId } } ) ;
161180 return database . findByPk < SchedulerRow > ( database . scheduler , schedulerId ) ;
0 commit comments