1- import { db , eq , and , uptimeSchedules } from "@databuddy/db" ;
1+ import { and , db , eq , uptimeSchedules } from "@databuddy/db" ;
22import { logger } from "@databuddy/shared/logger" ;
33import { ORPCError } from "@orpc/server" ;
44import { Client } from "@upstash/qstash" ;
55import { z } from "zod" ;
66import { recordORPCError } from "../lib/otel" ;
77import { protectedProcedure } from "../orpc" ;
8- import { authorizeWebsiteAccess , authorizeUptimeScheduleAccess } from "../utils/auth" ;
9- import { nanoid } from "nanoid" ;
8+ import {
9+ authorizeUptimeScheduleAccess ,
10+ authorizeWebsiteAccess ,
11+ } from "../utils/auth" ;
12+
1013if ( ! process . env . UPSTASH_QSTASH_TOKEN ) {
1114 logger . error ( "UPSTASH_QSTASH_TOKEN environment variable is required" ) ;
1215}
@@ -52,17 +55,12 @@ async function findScheduleByWebsiteId(websiteId: string) {
5255 return schedule ;
5356}
5457
55- async function createQStashSchedule (
56- scheduleId : string ,
57- granularity : z . infer < typeof granularityEnum >
58- ) {
58+ async function createQStashSchedule ( granularity : z . infer < typeof granularityEnum > ) {
5959 const result = await client . schedules . create ( {
60- scheduleId,
6160 destination : UPTIME_URL_GROUP ,
6261 cron : CRON_GRANULARITIES [ granularity ] ,
6362 headers : {
6463 "Content-Type" : "application/json" ,
65- "X-Schedule-Id" : scheduleId ,
6664 } ,
6765 } ) ;
6866
@@ -195,8 +193,7 @@ export const uptimeRouter = {
195193 input . websiteId ?? null
196194 ) ;
197195
198- const scheduleId = input . websiteId ? input . websiteId : nanoid ( 10 ) ;
199- await createQStashSchedule ( scheduleId , input . granularity ) ;
196+ const scheduleId = await createQStashSchedule ( input . granularity ) ;
200197
201198 await db . insert ( uptimeSchedules ) . values ( {
202199 id : scheduleId ,
@@ -209,19 +206,6 @@ export const uptimeRouter = {
209206 isPaused : false ,
210207 } ) ;
211208
212- try {
213- await client . publish ( {
214- urlGroup : UPTIME_URL_GROUP ,
215- headers : {
216- "Content-Type" : "application/json" ,
217- "X-Schedule-Id" : scheduleId ,
218- } ,
219- } ) ;
220- logger . info ( { scheduleId } , "Triggered uptime check after creation" ) ;
221- } catch ( error ) {
222- logger . error ( { scheduleId, error } , "Failed to trigger initial check" ) ;
223- }
224-
225209 logger . info (
226210 {
227211 scheduleId,
@@ -245,6 +229,8 @@ export const uptimeRouter = {
245229 . input (
246230 z . object ( {
247231 scheduleId : z . string ( ) . min ( 1 , "Schedule ID is required" ) ,
232+ url : z . string ( ) . url ( "Valid URL is required" ) ,
233+ name : z . string ( ) . optional ( ) ,
248234 granularity : granularityEnum ,
249235 } )
250236 )
@@ -266,47 +252,36 @@ export const uptimeRouter = {
266252 userId : schedule . userId ,
267253 } ) ;
268254
269- try {
270- await client . schedules . delete ( input . scheduleId ) ;
271- } catch ( error ) {
272- logger . error (
273- { scheduleId : input . scheduleId , error } ,
274- "Failed to delete old QStash schedule during update"
275- ) ;
276- recordORPCError ( {
277- code : "INTERNAL_SERVER_ERROR" ,
278- message : "Failed to delete old QStash schedule" ,
279- } ) ;
280- throw new ORPCError ( "INTERNAL_SERVER_ERROR" , {
281- message : "Failed to delete old QStash schedule. Please try again." ,
282- } ) ;
283- }
255+ await client . schedules . delete ( input . scheduleId ) ;
256+ const newScheduleId = await createQStashSchedule ( input . granularity ) ;
284257
285- await db
286- . update ( uptimeSchedules )
287- . set ( {
288- granularity : input . granularity ,
289- cron : CRON_GRANULARITIES [ input . granularity ] ,
290- updatedAt : new Date ( ) ,
291- } )
292- . where ( eq ( uptimeSchedules . id , input . scheduleId ) ) ;
258+ await db . delete ( uptimeSchedules ) . where ( eq ( uptimeSchedules . id , input . scheduleId ) ) ;
293259
294- const newScheduleId = await createQStashSchedule (
295- input . scheduleId ,
296- input . granularity
297- ) ;
260+ await db . insert ( uptimeSchedules ) . values ( {
261+ id : newScheduleId ,
262+ websiteId : schedule . websiteId ,
263+ userId : schedule . userId ,
264+ url : input . url ,
265+ name : input . name ?? null ,
266+ granularity : input . granularity ,
267+ cron : CRON_GRANULARITIES [ input . granularity ] ,
268+ isPaused : false ,
269+ } ) ;
298270
299271 logger . info (
300272 {
301- scheduleId : input . scheduleId ,
273+ oldScheduleId : input . scheduleId ,
274+ newScheduleId,
275+ url : input . url ,
302276 granularity : input . granularity ,
303- userId : context . user . id ,
304277 } ,
305278 "Uptime schedule updated"
306279 ) ;
307280
308281 return {
309- scheduleId : input . scheduleId ,
282+ scheduleId : newScheduleId ,
283+ url : input . url ,
284+ name : input . name ,
310285 granularity : input . granularity ,
311286 cron : CRON_GRANULARITIES [ input . granularity ] ,
312287 } ;
0 commit comments