Skip to content

Commit fdfa935

Browse files
v0.3.38: billing cron job fix
2 parents 4846f6c + 917552f commit fdfa935

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

apps/sim/app/api/billing/daily/route.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export async function POST(request: NextRequest) {
6767
{ status: 500 }
6868
)
6969
} catch (error) {
70-
logger.error('Fatal error in monthly billing cron job', { error })
70+
logger.error('Fatal error in daily billing cron job', { error })
7171

7272
return NextResponse.json(
7373
{
@@ -90,18 +90,59 @@ export async function GET(request: NextRequest) {
9090
return authError
9191
}
9292

93-
return NextResponse.json({
94-
status: 'ready',
95-
message:
96-
'Daily billing check cron job is ready to process users and organizations with periods ending today',
97-
currentDate: new Date().toISOString().split('T')[0],
93+
const startTime = Date.now()
94+
const result = await processDailyBillingCheck()
95+
const duration = Date.now() - startTime
96+
97+
if (result.success) {
98+
logger.info('Daily billing check (GET) completed successfully', {
99+
processedUsers: result.processedUsers,
100+
processedOrganizations: result.processedOrganizations,
101+
totalChargedAmount: result.totalChargedAmount,
102+
duration: `${duration}ms`,
103+
})
104+
105+
return NextResponse.json({
106+
success: true,
107+
summary: {
108+
processedUsers: result.processedUsers,
109+
processedOrganizations: result.processedOrganizations,
110+
totalChargedAmount: result.totalChargedAmount,
111+
duration: `${duration}ms`,
112+
},
113+
})
114+
}
115+
116+
logger.error('Daily billing check (GET) completed with errors', {
117+
processedUsers: result.processedUsers,
118+
processedOrganizations: result.processedOrganizations,
119+
totalChargedAmount: result.totalChargedAmount,
120+
errorCount: result.errors.length,
121+
errors: result.errors,
122+
duration: `${duration}ms`,
98123
})
124+
125+
return NextResponse.json(
126+
{
127+
success: false,
128+
summary: {
129+
processedUsers: result.processedUsers,
130+
processedOrganizations: result.processedOrganizations,
131+
totalChargedAmount: result.totalChargedAmount,
132+
errorCount: result.errors.length,
133+
duration: `${duration}ms`,
134+
},
135+
errors: result.errors,
136+
},
137+
{ status: 500 }
138+
)
99139
} catch (error) {
100-
logger.error('Error in billing health check', { error })
140+
logger.error('Fatal error in daily billing (GET) cron job', { error })
101141
return NextResponse.json(
102142
{
103-
status: 'error',
104-
error: error instanceof Error ? error.message : 'Unknown error',
143+
success: false,
144+
error: 'Internal server error during daily billing check',
145+
details: error instanceof Error ? error.message : 'Unknown error',
105146
},
106147
{ status: 500 }
107148
)

apps/sim/lib/auth/internal.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export async function verifyInternalToken(token: string): Promise<boolean> {
5757
export function verifyCronAuth(request: NextRequest, context?: string): NextResponse | null {
5858
const authHeader = request.headers.get('authorization')
5959
const expectedAuth = `Bearer ${env.CRON_SECRET}`
60+
const isVercelCron = request.headers.get('x-vercel-cron') === '1'
6061

61-
if (authHeader !== expectedAuth) {
62+
// Allow Vercel Cron requests (they include x-vercel-cron header instead of Authorization)
63+
if (!isVercelCron && authHeader !== expectedAuth) {
6264
const contextInfo = context ? ` for ${context}` : ''
6365
logger.warn(`Unauthorized CRON access attempt${contextInfo}`, {
6466
providedAuth: authHeader,

0 commit comments

Comments
 (0)