Skip to content

Commit fec3851

Browse files
leomp12claude
andcommitted
fix(asaas): Improve webhook lifecycle and cron scheduling
- Add cache-busting timestamp to webhook URLs - Implement old webhook cleanup before creating new ones - Fix cron sort logic for proper load distribution - Adjust cron schedule to business-friendly hours (18:18 and 05:18) - Handle backward compatibility with legacy webhook data structures 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f08a23b commit fec3851

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

packages/apps/asaas/src/asaas-create-transaction.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,15 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
197197
} = config.get();
198198
const locationId = httpsFunctionOptions.region;
199199
const appBaseUri = `https://${locationId}-${process.env.GCLOUD_PROJECT}.cloudfunctions.net`;
200-
const webhookUrl = `${appBaseUri}/asaas-webhook`;
200+
const webhookUrl = `${appBaseUri}/asaas-webhook?t=${Date.now()}`;
201201
const docRef = getFirestore().doc('asaasSetup/webhook');
202202
const docSnap = await docRef.get();
203-
if (docSnap.data()?.asaasKeyId !== asaasKeyId) {
203+
const webhookSetupData = docSnap.data();
204+
if (webhookSetupData?.asaasKeyId !== asaasKeyId) {
205+
const oldWebhookId = (webhookSetupData?.webhookData || webhookSetupData)?.id;
206+
if (oldWebhookId) {
207+
await asaasAxios.delete(`/v3/webhooks/${oldWebhookId}`).catch(logger.warn);
208+
}
204209
try {
205210
const {
206211
data: webhookData,

packages/apps/asaas/src/asaas-events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const asaas = {
143143
cronCheckPayments: functions
144144
.region(config.get().httpsFunctionOptions.region)
145145
.runWith({ timeoutSeconds: 540 })
146-
.pubsub.schedule(process.env.CRONTAB_ASAAS_CHECK_PAYMENTS || '28 15,2 * * *')
146+
.pubsub.schedule(process.env.CRONTAB_ASAAS_CHECK_PAYMENTS || '18 18,5 * * *')
147147
.timeZone('America/Sao_Paulo')
148148
.onRun(async () => {
149149
await setAsaasEnv();
@@ -157,7 +157,7 @@ export const asaas = {
157157
+ '&transactions.app.intermediator.code=asaas3'
158158
+ '&financial_status.current=pending'
159159
+ `&created_at>=${d.toISOString()}`
160-
+ `&sort=${(isOddHourExec ? '' : '-')}number`
160+
+ `&sort=${(isOddHourExec ? '-' : '')}number`
161161
+ '&limit=500' as `orders?${string}`;
162162
const { data: { result: orders } } = await api.get(endpoint);
163163
logger.info(`${orders.length} orders listed`, {

0 commit comments

Comments
 (0)