-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3248 from ONEARMY/master
🤖 Release PR
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { logger } from 'src/logger' | ||
|
||
import type { IDiscussion } from 'src/models' | ||
import type { DatabaseV2 } from '../databaseV2' | ||
import type { DBEndpoint } from '../databaseV2/endpoints' | ||
|
||
type DiscussionEndpoints = Extract< | ||
DBEndpoint, | ||
'howtos' | 'research' | 'questions' | ||
> | ||
|
||
export const updateDiscussionMetadata = ( | ||
db: DatabaseV2, | ||
discussion: IDiscussion, | ||
) => { | ||
const collectionName = getCollectionName(discussion.sourceType) | ||
|
||
if (!collectionName) { | ||
logger.trace( | ||
`Unable to find collection. Discussion metadata was not updated. sourceType: ${discussion.sourceType}`, | ||
) | ||
return | ||
} | ||
|
||
const commentCount = discussion.comments.length | ||
const latestCommentDate = new Date( | ||
Math.max( | ||
...discussion.comments.map((comment) => | ||
new Date(comment._created).valueOf(), | ||
), | ||
), | ||
) | ||
|
||
return db | ||
.collection(collectionName) | ||
.doc(discussion.sourceId) | ||
.update({ commentCount, latestCommentDate }) | ||
} | ||
|
||
const getCollectionName = (sourceType: string): DiscussionEndpoints | null => { | ||
switch (sourceType) { | ||
case 'question': | ||
return 'questions' | ||
case 'howto': | ||
return 'howtos' | ||
case 'research': | ||
return 'research' | ||
default: | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters