Skip to content

Commit 30ed8a7

Browse files
committed
fix: deployment artifact folder
refactor: transaction result add readme and update functions readme
1 parent 0304687 commit 30ed8a7

5 files changed

Lines changed: 38 additions & 30 deletions

File tree

.github/workflows/help-post-comment-count_PushMaster.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
uses: actions/download-artifact@v3
5959
with:
6060
name: tc-api
61+
path: tc-api
6162
- name: Setup google auth for github actions
6263
uses: google-github-actions/auth@v1
6364
with:
@@ -90,7 +91,7 @@ jobs:
9091
- name: Run GCP Deploy Function - help-post-comment-count
9192
run: |
9293
cd tc-api
93-
gcloud functions deploy help-post-comment-count --runtime nodejs18 --trigger-event providers/cloud.firestore/eventTypes/document.create --trigger-resource "projects/all-that/databases/(default)/documents/helpPosts/{helpPostId}/helpPostComments/{commentId}" --entry-point helpPostCommentCount
94+
gcloud functions deploy help-post-comment-count --runtime nodejs18 --trigger-event providers/cloud.firestore/eventTypes/document.create --trigger-resource "projects/${{ secrets.GCP_PROJECT_ID }}/databases/(default)/documents/helpPosts/{helpPostId}/helpPostComments/{commentId}" --entry-point helpPostCommentCount
9495
env:
9596
CLOUDSDK_CORE_PROJECT: ${{ secrets.GCP_PROJECT_ID }}
9697
- name: Slack Notification

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ A collection of functions used for THAT
4646

4747
[![that-api-og-image Pull Request](https://github.com/ThatConference/that-api-functions/actions/workflows/that-api-og-image_PullRequest.yml/badge.svg)](https://github.com/ThatConference/that-api-functions/actions/workflows/that-api-og-image_PullRequest.yml)
4848
[![that-api-og-image Push Master](https://github.com/ThatConference/that-api-functions/actions/workflows/that-api-og-image_PushMaster.yml/badge.svg)](https://github.com/ThatConference/that-api-functions/actions/workflows/that-api-og-image_PushMaster.yml)
49+
50+
## [help-post-comment-count](functions/help-post-comment-count)
51+
52+
[![help-post-comment-count Pull Request](https://github.com/ThatConference/that-api-functions/actions/workflows/help-post-comment-count_PullRequest.yml/badge.svg)](https://github.com/ThatConference/that-api-functions/actions/workflows/help-post-comment-count_PullRequest.yml)
53+
[![help-post-comment-count Pull Request](https://github.com/ThatConference/that-api-functions/actions/workflows/help-post-comment-count_PullRequest.yml/badge.svg)](https://github.com/ThatConference/that-api-functions/actions/workflows/help-post-comment-count_PullRequest.yml)

functions/help-post-comment-count/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "help-post-comment-count",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Sets comment count on helpPost when a comment is added or removed.",
55
"main": "index.js",
66
"scripts": {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# THAT Functions
2+
3+
## help-post-comment-count
4+
5+
Firestore gcp function to update helpPost with its current comment count.
6+
7+
The Firestore trigger activates on `write` (create, update, delete) ignoring document `update` and updating count on `create` and `delete`.
8+
9+
### stuff
10+
11+
nodejs 18

functions/help-post-comment-count/src/index.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Firestore from '@google-cloud/firestore';
88

99
const dlog = debug('that:api:helpPostCommentCount');
1010
const firestore = new Firestore();
11-
const helpPostColName = 'helpPost';
11+
// const helpPostColName = 'helpPost';
12+
const helpPostColName = 'helpPosts';
1213
const commentColName = 'helpPostComments';
1314
let version;
1415
let 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

Comments
 (0)