@@ -8,7 +8,8 @@ import Firestore from '@google-cloud/firestore';
88
99const dlog = debug ( 'that:api:helpPostCommentCount' ) ;
1010const firestore = new Firestore ( ) ;
11- const helpPostColName = 'helpPost' ;
11+ // const helpPostColName = 'helpPost';
12+ const helpPostColName = 'helpPosts' ;
1213const commentColName = 'helpPostComments' ;
1314let version ;
1415let packageName ;
@@ -61,34 +62,24 @@ export const helpPostCommentCount = async (event, context) => {
6162
6263 const eventDoc = event . value . name ;
6364 dlog ( '👽 Event Doc path/name (undefined on delete):: %s' , eventDoc ) ;
64- dlog ( '👽 Firestore query & update actions' ) ;
6565
66- let commentCount ;
67- try {
68- commentCount = await firestore
66+ // If a read-write transaction fails with contention,
67+ // the transaction is retried up to five times
68+ return firestore . runTransaction ( transaction => {
69+ dlog ( '👽 executing transaction' ) ;
70+ return firestore
6971 . collection ( `${ helpPostColName } /${ helpPostId } /${ commentColName } ` )
7072 . count ( )
71- . get ( ) ;
72- } catch ( err ) {
73- const sentryId = Sentry . captureException ( err ) ;
74- console . log ( 'error reference:' , sentryId ) ;
75- return undefined ;
76- }
77-
78- commentCount = commentCount . data ( ) . count ?? 0 ;
79- try {
80- // If a read-write transaction fails with contention,
81- // the transaction is retried up to five times
82- const result = await firestore . runTransaction ( transaction => {
83- const docRef = firestore . doc ( `${ helpPostColName } /${ helpPostId } ` ) ;
84- transaction . update ( docRef , { commentCount } ) ;
85- return Promise . resolve ( new Date ( ) ) ;
86- } ) ;
87- dlog ( '👽 helpDoc update result: %o' , result ) ;
88- } catch ( err ) {
89- const sentryId = Sentry . captureException ( err ) ;
90- console . log ( 'error reference: ' , sentryId ) ;
91- }
92-
93- return undefined ;
73+ . get ( )
74+ . then ( countData => {
75+ const commentCount = countData . data ( ) . count ?? 0 ;
76+ const docRef = firestore . doc ( `${ helpPostColName } /${ helpPostId } ` ) ;
77+ return transaction . update ( docRef , { commentCount } ) ;
78+ } )
79+ . then ( ( ) => console . log ( 'transaction result' , true ) )
80+ . catch ( err => {
81+ const sentryId = Sentry . captureException ( err ) ;
82+ console . log ( 'error reference:' , sentryId ) ;
83+ } ) ;
84+ } ) ;
9485} ;
0 commit comments