Skip to content

Commit 88b2761

Browse files
authored
Merge pull request #228 from ThatConference/fix/comment-count-deploy
Tickle-bot, help-post-comment-count updates
2 parents 384da1c + 30ed8a7 commit 88b2761

8 files changed

Lines changed: 57 additions & 32 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
};

functions/tickle-bot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tickle-bot",
3-
"version": "2.5.2",
3+
"version": "2.5.3",
44
"description": "Provides authenticated requests against our api",
55
"main": "index.js",
66
"scripts": {

functions/tickle-bot/src/middleware/meetThatMatching.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default async function meetThatMatching(req, res, next) {
2929
const result = {
3030
createdAt: new Date(),
3131
idCountFromAllocations: 0,
32+
campmateIdsFromAllocations: 0,
3233
optInCount: 0,
3334
membersToMatch: 0,
3435
matchesMade: 0,
@@ -86,19 +87,34 @@ export default async function meetThatMatching(req, res, next) {
8687
}, []);
8788
dlog(`combined orderAllocation count: %d`, orderAllocations.length);
8889
const memberIdsFromAllocations = new Map();
90+
const campmateIdsFromAllocations = new Map();
8991
for (let i = 0; i < orderAllocations.length; i += 1) {
9092
const oa = orderAllocations[i];
9193
const allocatedTo = oa?.allocatedTo;
92-
if (allocatedTo?.id) {
94+
const uiReference = oa?.uiReference;
95+
if (
96+
allocatedTo?.id &&
97+
uiReference !== 'GEEKLING' &&
98+
uiReference !== 'CAMPMATE'
99+
) {
93100
if (!memberIdsFromAllocations.has(allocatedTo.id))
94101
memberIdsFromAllocations.set(allocatedTo.id, allocatedTo);
102+
} else if (allocatedTo?.id && uiReference === 'CAMPMATE') {
103+
// future use to send campmates engagement emails.
104+
if (!campmateIdsFromAllocations.has(allocatedTo.id))
105+
campmateIdsFromAllocations.set(allocatedTo.id, allocatedTo);
95106
}
96107
}
97108
dlog(
98109
'distinct member ids from allocations: %d',
99110
memberIdsFromAllocations.size,
100111
);
112+
dlog(
113+
'distinct campmate ids from allocations: %d',
114+
campmateIdsFromAllocations.size,
115+
);
101116
result.idCountFromAllocations = memberIdsFromAllocations.size;
117+
result.campmateIdsFromAllocations = campmateIdsFromAllocations.size;
102118

103119
let optedInMembers;
104120
try {

functions/tickle-bot/src/queries/oaForMemberMatching.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default {
1818
eventId
1919
orderAllocations(orderTypes: [REGULAR, PARTNER, SPEAKER]) {
2020
id
21+
uiReference
2122
allocatedTo {
2223
__typename
2324
id

0 commit comments

Comments
 (0)