@@ -3,11 +3,11 @@ import type { ActionInstancesId, ActionRunsId, PubsId, StagesId } from "db/publi
33import { ActionRunStatus , Event } from "db/public" ;
44import { logger } from "logger" ;
55
6- import type { SchedulableRule } from "./rules " ;
7- import type { GetEventRuleOptions } from "~/lib/db/queries" ;
6+ import type { SchedulableAutomation } from "./automations " ;
7+ import type { GetEventAutomationOptions } from "~/lib/db/queries" ;
88import { db } from "~/kysely/database" ;
99import { addDuration } from "~/lib/dates" ;
10- import { getStageRules } from "~/lib/db/queries" ;
10+ import { getStageAutomations } from "~/lib/db/queries" ;
1111import { autoRevalidate } from "~/lib/server/cache/autoRevalidate" ;
1212import { getCommunitySlug } from "~/lib/server/cache/getCommunitySlug" ;
1313import { getJobsClient , getScheduledActionJobKey } from "~/lib/server/jobs" ;
@@ -17,7 +17,7 @@ type Shared = {
1717 stack : ActionRunsId [ ] ;
1818 /* Config for the action instance */
1919 config ?: Record < string , unknown > | null ;
20- } & GetEventRuleOptions ;
20+ } & GetEventAutomationOptions ;
2121
2222type ScheduleActionInstanceForPubOptions = Shared & {
2323 pubId : PubsId ;
@@ -42,85 +42,85 @@ export const scheduleActionInstances = async (options: ScheduleActionInstanceOpt
4242 throw new Error ( "PubId or body is required" ) ;
4343 }
4444
45- const [ rules , jobsClient ] = await Promise . all ( [
46- getStageRules ( options . stageId , options ) . execute ( ) ,
45+ const [ automations , jobsClient ] = await Promise . all ( [
46+ getStageAutomations ( options . stageId , options ) . execute ( ) ,
4747 getJobsClient ( ) ,
4848 ] ) ;
4949
50- if ( ! rules . length ) {
50+ if ( ! automations . length ) {
5151 logger . debug ( {
5252 msg : `No action instances found for stage ${ options . stageId } . Most likely this is because a Pub is moved into a stage without action instances.` ,
5353 pubId : options . pubId ,
5454 stageId : options . stageId ,
55- rules ,
55+ automations ,
5656 } ) ;
5757 return ;
5858 }
5959
60- const validRules = rules
60+ const validAutomations = automations
6161 . filter (
62- ( rule ) : rule is typeof rule & SchedulableRule =>
63- rule . event === Event . actionFailed ||
64- rule . event === Event . actionSucceeded ||
65- rule . event === Event . webhook ||
66- ( rule . event === Event . pubInStageForDuration &&
62+ ( automation ) : automation is typeof automation & SchedulableAutomation =>
63+ automation . event === Event . actionFailed ||
64+ automation . event === Event . actionSucceeded ||
65+ automation . event === Event . webhook ||
66+ ( automation . event === Event . pubInStageForDuration &&
6767 Boolean (
68- typeof rule . config === "object" &&
69- rule . config &&
70- "duration" in rule . config &&
71- rule . config . duration &&
72- "interval" in rule . config &&
73- rule . config . interval
68+ typeof automation . config === "object" &&
69+ automation . config &&
70+ "duration" in automation . config &&
71+ automation . config . duration &&
72+ "interval" in automation . config &&
73+ automation . config . interval
7474 ) )
7575 )
76- . map ( ( rule ) => ( {
77- ...rule ,
78- duration : rule . config ?. ruleConfig ?. duration || 0 ,
79- interval : rule . config ?. ruleConfig ?. interval || "minute" ,
76+ . map ( ( automation ) => ( {
77+ ...automation ,
78+ duration : automation . config ?. automationConfig ?. duration || 0 ,
79+ interval : automation . config ?. automationConfig ?. interval || "minute" ,
8080 } ) ) ;
8181
8282 const results = await Promise . all (
83- validRules . flatMap ( async ( rule ) => {
83+ validAutomations . flatMap ( async ( automation ) => {
8484 const runAt = addDuration ( {
85- duration : rule . duration ,
86- interval : rule . interval ,
85+ duration : automation . duration ,
86+ interval : automation . interval ,
8787 } ) . toISOString ( ) ;
8888
8989 const scheduledActionRun = await autoRevalidate (
9090 db
9191 . insertInto ( "action_runs" )
9292 . values ( {
93- actionInstanceId : rule . actionInstance . id ,
93+ actionInstanceId : automation . actionInstance . id ,
9494 pubId : options . pubId ,
9595 json : options . json ,
9696 status : ActionRunStatus . scheduled ,
97- config : options . config ?? rule . actionInstance . config ,
97+ config : options . config ?? automation . actionInstance . config ,
9898 result : { scheduled : `Action scheduled for ${ runAt } ` } ,
99- event : rule . event ,
99+ event : automation . event ,
100100 sourceActionRunId : options . stack . at ( - 1 ) ,
101101 } )
102102 . returning ( "id" )
103103 ) . executeTakeFirstOrThrow ( ) ;
104104
105105 const job = await jobsClient . scheduleAction ( {
106- actionInstanceId : rule . actionInstance . id ,
107- duration : rule . duration ,
108- interval : rule . interval ,
106+ actionInstanceId : automation . actionInstance . id ,
107+ duration : automation . duration ,
108+ interval : automation . interval ,
109109 stageId : options . stageId ,
110110 community : {
111111 slug : await getCommunitySlug ( ) ,
112112 } ,
113113 stack : options . stack ,
114114 scheduledActionRunId : scheduledActionRun . id ,
115- event : rule . event ,
115+ event : automation . event ,
116116 ...( options . pubId ? { pubId : options . pubId } : { json : options . json ! } ) ,
117- config : options . config ?? rule . actionInstance . config ?? null ,
117+ config : options . config ?? automation . actionInstance . config ?? null ,
118118 } ) ;
119119
120120 return {
121121 result : job ,
122- actionInstanceId : rule . actionInstance . id ,
123- actionInstanceName : rule . actionInstance . name ,
122+ actionInstanceId : automation . actionInstance . id ,
123+ actionInstanceName : automation . actionInstance . name ,
124124 runAt,
125125 } ;
126126 } )
0 commit comments