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
21 changes: 21 additions & 0 deletions src/app/api/core/utils/afterIfAvailable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { after } from 'next/server'

/**
* Runs the callback via Next.js `after()` if inside a request scope,
* otherwise executes it directly. This allows shared service code to
* work in both Vercel serverless (request context) and Trigger.dev /
* CLI (no request context) environments.
*
* `after()` is a pure registration call — the only errors it can throw
* are scope/context errors. Real callback errors are handled separately
* via `.catch()` in the fallback path.
*/
export function afterIfAvailable(callback: () => Promise<void>): void {
try {
after(callback)
} catch {
void callback().catch((err) => {
console.error('[afterIfAvailable] callback failed:', err)
})
}
}
7 changes: 6 additions & 1 deletion src/app/api/quickbooks/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import IntuitAPI, { IntuitAPITokensType } from '@/utils/intuitAPI'
import dayjs from 'dayjs'
import { and, eq, SQL } from 'drizzle-orm'
import httpStatus from 'http-status'
import { afterIfAvailable } from '@/app/api/core/utils/afterIfAvailable'
import { after } from 'next/server'

export class AuthService extends BaseService {
Expand Down Expand Up @@ -344,7 +345,11 @@ export class AuthService extends BaseService {
await tokenService.turnOffSync(intuitRealmId)

// send notification to IU
after(async () => {
afterIfAvailable(async () => {
console.info(
'AuthService#handleConnectionError | running after() .. | Sending notification to IU',
)

const notificationService = new NotificationService(this.user)
await notificationService.sendNotificationToIU(
intiatedBy,
Expand Down
Loading