From 61fcb3550615c8b7242575018c44c7f6f79e5582 Mon Sep 17 00:00:00 2001 From: MrOrz Date: Mon, 24 Jun 2024 00:56:41 +0800 Subject: [PATCH] refactor(CreateOrUpdateArticleReplyFeedback): rewrite into TS --- ... => CreateOrUpdateArticleReplyFeedback.ts} | 63 ++++++++++++------- src/scripts/blockUser.ts | 4 +- 2 files changed, 42 insertions(+), 25 deletions(-) rename src/graphql/mutations/{CreateOrUpdateArticleReplyFeedback.js => CreateOrUpdateArticleReplyFeedback.ts} (78%) diff --git a/src/graphql/mutations/CreateOrUpdateArticleReplyFeedback.js b/src/graphql/mutations/CreateOrUpdateArticleReplyFeedback.ts similarity index 78% rename from src/graphql/mutations/CreateOrUpdateArticleReplyFeedback.js rename to src/graphql/mutations/CreateOrUpdateArticleReplyFeedback.ts index 2ac6d65d..68ac34aa 100644 --- a/src/graphql/mutations/CreateOrUpdateArticleReplyFeedback.js +++ b/src/graphql/mutations/CreateOrUpdateArticleReplyFeedback.ts @@ -4,6 +4,10 @@ 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({ @@ -11,6 +15,11 @@ export function getArticleReplyFeedbackId({ replyId, userId, appId, +}: { + articleId: string; + replyId: string; + userId: string; + appId: string; }) { return `${articleId}__${replyId}__${userId}__${appId}`; } @@ -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 { const [positiveFeedbackCount, negativeFeedbackCount] = feedbacks .filter(({ status }) => status === 'NORMAL') .reduce( @@ -75,7 +81,7 @@ export async function updateArticleReplyByFeedbacks( }, }, }, - _source: true, + _source: 'true', }); /* istanbul ignore if */ @@ -84,7 +90,7 @@ export async function updateArticleReplyByFeedbacks( } return articleReplyUpdateResult.get._source.articleReplies.find( - (articleReply) => articleReply.replyId === replyId + (articleReply: ArticleReplyType) => articleReply.replyId === replyId ); } @@ -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', diff --git a/src/scripts/blockUser.ts b/src/scripts/blockUser.ts index 4f077452..987b5888 100644 --- a/src/scripts/blockUser.ts +++ b/src/scripts/blockUser.ts @@ -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( 'articlereplyfeedbacks', { bool: {