Skip to content

Commit

Permalink
chore(webhooks): return data
Browse files Browse the repository at this point in the history
  • Loading branch information
anteqkois committed Jun 8, 2024
1 parent 7976d7a commit c69072a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 8 deletions.
3 changes: 2 additions & 1 deletion apps/api-gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ async function bootstrap() {

const configService = app.get(ConfigService)
const frontendUrl = configService.getOrThrow('FRONTEND_HOST')
if (!frontendUrl.length) console.error('FRONTEND_HOST is empty')

app.enableCors({
origin: [frontendUrl],
origin: frontendUrl,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
credentials: true,
})
Expand Down
1 change: 0 additions & 1 deletion apps/web/app/(landing)/components/LandingNavButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function LandingNavButtons() {
const [logoutButton, setLogoutButton] = useState(false)

useEffect(() => {
console.log('authStatus', authStatus);
if (authStatus === AuthStatus.AUTHENTICATED) setLogoutButton(true)
else setLogoutButton(false)
}, [authStatus])
Expand Down
44 changes: 44 additions & 0 deletions apps/web/app/(landing)/cors-error/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use client'

import { H4, Icons, P } from '@linkerry/ui-components/server'
import { useEffect } from 'react'
import { PageContainer } from '../../app/components/PageContainer'

const clearCookies = () => {
document.cookie.split(';').forEach((c) => {
document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/')
})
}

export default function IndexPage() {
useEffect(() => {
clearCookies()
}, [])

return (
<PageContainer variant={'centered'}>
<div className="max-w-xl">
<H4 className="flex justify-center items-center gap-2 text-warning">
<Icons.Warn size={'md'}/>
CORS error occures
</H4>
<P>
We encountered an issue with your request due to browser security policies (CORS), sometimes this can happen in browsers like Brave etc.. To
resolve this, please try the following steps:
</P>
<ol className='mt-4'>
<li>
<b>1. Logout:</b> If you are logged in, please log out and then log back in.
</li>
<li>
<b>2. Clear Cookies:</b> We have automatically cleared your cookies for this page. Please refresh the page.
</li>
<li>
<b>3. Turn Off Shields:</b> If you are using the Brave browser, try turning off Shields for this site.
</li>
</ol>
<P>If the issue persists, please contact our support team for further assistance.</P>
</div>
</PageContainer>
)
}
9 changes: 9 additions & 0 deletions apps/web/libs/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ apiClient.interceptors.response.use(
async (error) => {
const originalRequest = error.config

if (!error.response) {
// Network error or CORS error
if (error.message === 'Network Error' || error.message.includes('CORS')) {
console.error('CORS error detected:', error.message)
window.location.href = '/cors-error'
return Promise.reject(error)
}
}

if (error?.response?.status == 401 && error?.config && !error?.config?._isRetry) {
originalRequest._isRetry = true
try {
Expand Down
8 changes: 8 additions & 0 deletions libs/nest-core/src/modules/billing/payments/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class StripeService {
status: object.status,
},
} as SubscriptionUpdate)

return {
success: true,
}
}

@StripeWebhookHandler('customer.subscription.updated')
Expand Down Expand Up @@ -104,6 +108,10 @@ export class StripeService {
id: object.metadata.subscriptionId,
data: update,
} as SubscriptionUpdate)

return {
success: true,
}
}

private async _getCustomerOrCreate({ project }: { project: ProjectDocument<'owner'> }) {
Expand Down
35 changes: 29 additions & 6 deletions libs/nest-core/src/modules/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,17 @@ export class WebhooksController {
return
}
// this._asyncHandler(payload, flow.toObject()).catch(exceptionHandler.handle)
this._asyncHandler(payload, flow.toObject()).catch((error) => {
this.logger.error(`#handleWebhookParams _asyncHandler failure`, ErrorCode.INTERNAL_SERVER, { error: error?.message })
})
try {
await this._asyncHandler(payload, flow.toObject())
return response.send({
success: true,
})
} catch (error: any) {
this.logger.error(`#handleWebhookQuery _asyncHandler failure`, ErrorCode.INTERNAL_SERVER, { error: error?.message })
return response.send({
success: false,
})
}
}

// @UseGuards(JwtBearerTokenAuthGuard)
Expand All @@ -92,10 +100,19 @@ export class WebhooksController {
if (isHandshake) {
return
}

// this._asyncHandler(payload, flow.toObject()).catch(exceptionHandler.handle)
this._asyncHandler(payload, flow.toObject()).catch((error) => {
try {
await this._asyncHandler(payload, flow.toObject())
return response.send({
success: true,
})
} catch (error: any) {
this.logger.error(`#handleWebhookQuery _asyncHandler failure`, ErrorCode.INTERNAL_SERVER, { error: error?.message })
})
return response.send({
success: false,
})
}
}

// @UseGuards(JwtBearerTokenAuthGuard)
Expand All @@ -106,7 +123,9 @@ export class WebhooksController {

const isHandshake = await this._handshakeHandler(flow.toObject(), payload, true, response)
if (isHandshake) {
return
return {
success: true,
}
}

await this.webhooksService.simulationCallback({
Expand All @@ -118,6 +137,10 @@ export class WebhooksController {
queryParams: request.query as Record<string, string>,
},
})

return response.send({
success: true,
})
}

private async _handshakeHandler(flow: FlowPopulated, payload: EventPayload, simulate: boolean, response: FastifyReply): Promise<boolean> {
Expand Down

0 comments on commit c69072a

Please sign in to comment.