@@ -4,7 +4,7 @@ const { Project } = require("@urbackend/common");
44const { getConnection } = require ( "@urbackend/common" ) ;
55const { getCompiledModel } = require ( "@urbackend/common" ) ;
66const { QueryEngine } = require ( "@urbackend/common" ) ;
7- const { validateData, validateUpdateData, aggregateSchema, dispatchWebhooks } = require ( "@urbackend/common" ) ;
7+ const { validateData, validateUpdateData, aggregateSchema, webhookQueue } = require ( "@urbackend/common" ) ;
88const { performance } = require ( 'perf_hooks' ) ;
99const { z } = require ( "zod" ) ;
1010const {
@@ -83,13 +83,12 @@ module.exports.insertData = async (req, res) => {
8383 ) ;
8484 }
8585
86- dispatchWebhooks ( {
86+ await webhookQueue . add ( 'trigger-webhook' , {
8787 projectId : project . _id ,
88+ event : 'document.inserted' ,
8889 collection : collectionName ,
89- action : 'insert' ,
90- document : result . toObject ? result . toObject ( ) : result ,
91- documentId : result . _id ,
92- } ) ;
90+ payload : result . toObject ? result . toObject ( ) : result
91+ } , { removeOnComplete : true } ) ;
9392
9493 if ( isDebug ) console . log ( `[DEBUG] insert data took ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms` ) ;
9594 res . status ( 201 ) . json ( result ) ;
@@ -536,13 +535,12 @@ module.exports.updateSingleData = async (req, res) => {
536535
537536 if ( ! result ) return res . status ( 404 ) . json ( { error : "Document not found." } ) ;
538537
539- dispatchWebhooks ( {
538+ await webhookQueue . add ( 'trigger-webhook' , {
540539 projectId : project . _id ,
540+ event : 'document.updated' ,
541541 collection : collectionName ,
542- action : 'update' ,
543- document : result ,
544- documentId : result . _id ,
545- } ) ;
542+ payload : result
543+ } , { removeOnComplete : true } ) ;
546544
547545 res . json ( { message : "Updated" , data : result } ) ;
548546 } catch ( err ) {
@@ -610,13 +608,12 @@ module.exports.deleteSingleDoc = async (req, res) => {
610608 console . error ( "Failed to enqueue trash cleanup job" , { projectId : String ( project . _id ) , collectionName, err } ) ;
611609 }
612610
613- dispatchWebhooks ( {
611+ await webhookQueue . add ( 'trigger-webhook' , {
614612 projectId : project . _id ,
613+ event : 'document.deleted' ,
615614 collection : collectionName ,
616- action : 'delete' ,
617- document : result ,
618- documentId : id ,
619- } ) ;
615+ payload : result
616+ } , { removeOnComplete : true } ) ;
620617
621618 res . json ( { success : true , data : { id } , message : "Document moved to trash" } ) ;
622619 } catch ( err ) {
@@ -679,14 +676,12 @@ module.exports.recoverSingleDoc = async (req, res, next) => {
679676 return next ( new AppError ( 404 , "Document not found or recovery window expired (30 days)." ) ) ;
680677 }
681678
682- dispatchWebhooks ( {
679+ await webhookQueue . add ( 'trigger-webhook' , {
683680 projectId : project . _id ,
681+ event : 'document.recovered' ,
684682 collection : collectionName ,
685- action : 'recover' ,
686- document : result ,
687- documentId : id ,
688- options : { }
689- } ) ;
683+ payload : result
684+ } , { removeOnComplete : true } ) ;
690685
691686 try {
692687 await syncCollectionCleanup ( project . _id , collectionName ) ;
0 commit comments