Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions apps/sim/app/api/billing/daily/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function POST(request: NextRequest) {
{ status: 500 }
)
} catch (error) {
logger.error('Fatal error in monthly billing cron job', { error })
logger.error('Fatal error in daily billing cron job', { error })

return NextResponse.json(
{
Expand All @@ -90,18 +90,59 @@ export async function GET(request: NextRequest) {
return authError
}

return NextResponse.json({
status: 'ready',
message:
'Daily billing check cron job is ready to process users and organizations with periods ending today',
currentDate: new Date().toISOString().split('T')[0],
const startTime = Date.now()
const result = await processDailyBillingCheck()
const duration = Date.now() - startTime

if (result.success) {
logger.info('Daily billing check (GET) completed successfully', {
processedUsers: result.processedUsers,
processedOrganizations: result.processedOrganizations,
totalChargedAmount: result.totalChargedAmount,
duration: `${duration}ms`,
})

return NextResponse.json({
success: true,
summary: {
processedUsers: result.processedUsers,
processedOrganizations: result.processedOrganizations,
totalChargedAmount: result.totalChargedAmount,
duration: `${duration}ms`,
},
})
}

logger.error('Daily billing check (GET) completed with errors', {
processedUsers: result.processedUsers,
processedOrganizations: result.processedOrganizations,
totalChargedAmount: result.totalChargedAmount,
errorCount: result.errors.length,
errors: result.errors,
duration: `${duration}ms`,
})

return NextResponse.json(
{
success: false,
summary: {
processedUsers: result.processedUsers,
processedOrganizations: result.processedOrganizations,
totalChargedAmount: result.totalChargedAmount,
errorCount: result.errors.length,
duration: `${duration}ms`,
},
errors: result.errors,
},
{ status: 500 }
)
} catch (error) {
logger.error('Error in billing health check', { error })
logger.error('Fatal error in daily billing (GET) cron job', { error })
return NextResponse.json(
{
status: 'error',
error: error instanceof Error ? error.message : 'Unknown error',
success: false,
error: 'Internal server error during daily billing check',
details: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
Expand Down
4 changes: 3 additions & 1 deletion apps/sim/lib/auth/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ export async function verifyInternalToken(token: string): Promise<boolean> {
export function verifyCronAuth(request: NextRequest, context?: string): NextResponse | null {
const authHeader = request.headers.get('authorization')
const expectedAuth = `Bearer ${env.CRON_SECRET}`
const isVercelCron = request.headers.get('x-vercel-cron') === '1'

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