Skip to content

Commit

Permalink
refactor(CreateOrUpdateArticleReplyFeedback): rewrite into TS
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Jun 23, 2024
1 parent a9cbc53 commit 61fcb35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import { assertUser, getContentDefaultStatus } from 'util/user';
import FeedbackVote from 'graphql/models/FeedbackVote';
import ArticleReply from 'graphql/models/ArticleReply';

import type { Article, ArticleReply as ArticleReplyType } from 'rumors-db/schema/articles';
import type { ArticleReplyFeedback } from 'rumors-db/schema/articlereplyfeedbacks';
import type { Reply } from 'rumors-db/schema/replies';

import client from 'util/client';

export function getArticleReplyFeedbackId({
articleId,
replyId,
userId,
appId,
}: {
articleId: string;
replyId: string;
userId: string;
appId: string;
}) {
return `${articleId}__${replyId}__${userId}__${appId}`;
}
Expand All @@ -19,16 +28,13 @@ export function getArticleReplyFeedbackId({
* Updates the positive and negative feedback count of the article reply with
* specified `articleId` and `replyId`.
*
* @param {string} articleId
* @param {string} replyId
* @param {object[]} feedbacks
* @returns {object} The updated article reply
* @returns The updated article reply
*/
export async function updateArticleReplyByFeedbacks(
articleId,
replyId,
feedbacks
) {
articleId: string,
replyId: string,
feedbacks: ArticleReplyFeedback[]
): Promise<ArticleReplyType> {
const [positiveFeedbackCount, negativeFeedbackCount] = feedbacks
.filter(({ status }) => status === 'NORMAL')
.reduce(
Expand Down Expand Up @@ -75,7 +81,7 @@ export async function updateArticleReplyByFeedbacks(
},
},
},
_source: true,
_source: 'true',
});

/* istanbul ignore if */
Expand All @@ -84,7 +90,7 @@ export async function updateArticleReplyByFeedbacks(
}

return articleReplyUpdateResult.get._source.articleReplies.find(
(articleReply) => articleReply.replyId === replyId
(articleReply: ArticleReplyType) => articleReply.replyId === replyId
);
}

Expand Down Expand Up @@ -146,22 +152,33 @@ export default {
// Fill in reply & article reply author ID
//

const [{ userId: replyUserId }, article] =
await loaders.docLoader.loadMany([
{
index: 'replies',
id: replyId,
},
{
index: 'articles',
id: articleId,
},
]);
const [
{ userId: replyUserId },
article,
] = await loaders.docLoader.loadMany([
{
index: 'replies',
id: replyId,
},
{
index: 'articles',
id: articleId,
},
]) as [Reply, Article];

const { userId: articleReplyUserId } = article.articleReplies.find(
(ar) => ar.replyId === replyId
const ar = article.articleReplies.find(
ar => ar.replyId === replyId
);

// Make typescript happy
if (!ar) {
throw new Error(
`Cannot find article-reply with article ID = ${articleId} and reply ID = ${replyId}`
);
}

const { userId: articleReplyUserId } = ar;

await client.update({
index: 'articlereplyfeedbacks',
type: 'doc',
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/blockUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ async function processArticleReplyFeedbacks(userId: string) {
i,
{ articleId, replyId },
] of articleReplyIdsWithNormalFeedbacks.entries()) {
const feedbacks = [];
for await (const { _source: feedback } of getAllDocs(
const feedbacks: ArticleReplyFeedback[] = [];
for await (const { _source: feedback } of getAllDocs<ArticleReplyFeedback>(
'articlereplyfeedbacks',
{
bool: {
Expand Down

0 comments on commit 61fcb35

Please sign in to comment.