Skip to content

Commit

Permalink
Merge 9da6cb7 into 17a2f56
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Apr 16, 2020
2 parents 17a2f56 + 9da6cb7 commit de78710
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 96 deletions.
42 changes: 42 additions & 0 deletions src/graphql/queries/ListArticles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
GraphQLString,
GraphQLInputObjectType,
GraphQLList,
GraphQLBoolean,
} from 'graphql';
import client from 'util/client';

Expand Down Expand Up @@ -101,6 +102,14 @@ export default {
When specified, it overrides the settings of appId and userId.
`,
},
hasArticleReplyWithMorePositiveFeedback: {
type: GraphQLBoolean,
description: `
When true, return only articles with any article replies that has more positive feedback than negative.
When false, return articles with none of its article replies that has more positive feedback, including those with no replies yet.
In both scenario, deleted article replies are not taken into account.
`,
},
}),
},
orderBy: {
Expand Down Expand Up @@ -145,6 +154,7 @@ export default {
// Collecting queries that will be used in bool queries later
const shouldQueries = []; // Affects scores
const filterQueries = []; // Not affects scores
const mustNotQueries = [];

if (filter.fromUserOfArticleId) {
let specifiedArticle;
Expand Down Expand Up @@ -317,11 +327,43 @@ export default {
});
}

if (typeof filter.hasArticleReplyWithMorePositiveFeedback === 'boolean') {
(filter.hasArticleReplyWithMorePositiveFeedback
? shouldQueries
: mustNotQueries
).push({
nested: {
path: 'articleReplies',
query: {
bool: {
must: [
{
term: {
'articleReplies.status': 'NORMAL',
},
},
{
script: {
script: {
source:
"doc['articleReplies.positiveFeedbackCount'].value > doc['articleReplies.negativeFeedbackCount'].value",
lang: 'painless',
},
},
},
],
},
},
},
});
}

body.query = {
bool: {
should:
shouldQueries.length === 0 ? [{ match_all: {} }] : shouldQueries,
filter: filterQueries,
must_not: mustNotQueries,
minimum_should_match: 1, // At least 1 "should" query should present
},
};
Expand Down
38 changes: 30 additions & 8 deletions src/graphql/queries/__fixtures__/ListArticles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ export default {
status: 'NORMAL',
createdAt: '2020-02-08T15:11:04.472Z',
updatedAt: '2020-02-08T15:11:04.472Z',
positiveFeedbackCount: 1,
negativeFeedbackCount: 0,
},
{
status: 'NORMAL',
createdAt: '2020-02-05T14:41:19.044Z',
updatedAt: '2020-02-05T14:41:19.044Z',
positiveFeedbackCount: 0,
negativeFeedbackCount: 1,
},
],
articleCategories: [
Expand All @@ -48,6 +52,24 @@ export default {
status: 'NORMAL',
createdAt: '2020-02-09T15:11:04.472Z',
updatedAt: '2020-02-09T15:11:04.472Z',
positiveFeedbackCount: 0,
negativeFeedbackCount: 0,
},
{
// Deleted article replies are not taken into account for createdAt and feedbacks
status: 'DELETED',
createdAt: '2020-02-15T15:11:04.472Z',
updatedAt: '2020-02-16T15:11:04.472Z',
positiveFeedbackCount: 3,
negativeFeedbackCount: 0,
},
{
// Deleted article replies are not taken into account for createdAt and feedbacks
status: 'DELETED',
createdAt: '2020-02-04T15:11:04.472Z',
updatedAt: '2020-02-04T15:11:04.472Z',
positiveFeedbackCount: 3,
negativeFeedbackCount: 0,
},
],
articleCategories: [
Expand All @@ -68,19 +90,13 @@ export default {
createdAt: '2020-02-06T00:00:00.000Z',
text:
'人生幾何,離闊如此!況以膠漆之心,置於胡越之身,進不得相合,退不能相忘,牽攣乖隔,各欲白首。',
articleReplies: [
{
status: 'NORMAL',
createdAt: '2020-02-05T15:11:04.472Z',
updatedAt: '2020-02-05T15:11:04.472Z',
},
],
articleReplies: [],
},
'/articles/doc/listArticleTest4': {
userId: 'user2',
appId: 'app1',
replyRequestCount: 0,
normalArticleReplyCount: 0,
normalArticleReplyCount: 3,
updatedAt: 4,
createdAt: '2020-02-07T00:00:00.000Z',
text: '我好餓 http://gohome.com',
Expand All @@ -98,16 +114,22 @@ export default {
status: 'NORMAL',
createdAt: '2020-02-11T15:11:04.472Z',
updatedAt: '2020-02-11T15:11:04.472Z',
positiveFeedbackCount: 10,
negativeFeedbackCount: 11,
},
{
status: 'NORMAL',
createdAt: '2020-02-10T15:11:04.472Z',
updatedAt: '2020-02-10T15:11:04.472Z',
positiveFeedbackCount: 5,
negativeFeedbackCount: 7,
},
{
status: 'NORMAL',
createdAt: '2020-02-09T15:11:04.472Z',
updatedAt: '2020-02-09T15:11:04.472Z',
positiveFeedbackCount: 3,
negativeFeedbackCount: 4,
},
],
},
Expand Down
92 changes: 71 additions & 21 deletions src/graphql/queries/__tests__/ListArticles.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('ListArticles', () => {
edges {
node {
id
updatedAt
}
}
totalCount
Expand All @@ -46,7 +47,7 @@ describe('ListArticles', () => {
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('by updatedAt DESC');

expect(
await gql`
Expand All @@ -55,6 +56,7 @@ describe('ListArticles', () => {
edges {
node {
id
replyRequestCount
}
}
totalCount
Expand All @@ -65,7 +67,7 @@ describe('ListArticles', () => {
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('by replyRequestCount DESC');

expect(
await gql`
Expand All @@ -74,17 +76,15 @@ describe('ListArticles', () => {
edges {
node {
id
articleReplies(status: NORMAL) {
createdAt
}
}
}
totalCount
pageInfo {
firstCursor
lastCursor
}
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('by lastRepliedAt DESC');
});

const testReplyCount = async expression => {
Expand All @@ -97,13 +97,9 @@ describe('ListArticles', () => {
edges {
node {
id
replyCount
}
}
totalCount
pageInfo {
firstCursor
lastCursor
}
}
}
`()
Expand Down Expand Up @@ -232,7 +228,7 @@ describe('ListArticles', () => {
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('userId = user1, appId = app1');

// Lists only articles by fromUserOfArticleId
expect(
Expand All @@ -248,7 +244,7 @@ describe('ListArticles', () => {
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('author of listArticleTest1');
});

it('filters by time range', async () => {
Expand All @@ -261,13 +257,14 @@ describe('ListArticles', () => {
edges {
node {
id
createdAt
}
}
totalCount
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('later than 2020-02-06');
expect(
await gql`
{
Expand All @@ -277,13 +274,14 @@ describe('ListArticles', () => {
edges {
node {
id
createdAt
}
}
totalCount
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('earlier or equal to 2020-02-06');
expect(
await gql`
{
Expand All @@ -298,13 +296,14 @@ describe('ListArticles', () => {
edges {
node {
id
createdAt
}
}
totalCount
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('between 2020-02-04 and 2020-02-06');
});

it('filters by replies time range', async () => {
Expand All @@ -317,13 +316,16 @@ describe('ListArticles', () => {
edges {
node {
id
articleReplies(status: NORMAL) {
createdAt
}
}
}
totalCount
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('later than 2020-02-06');
expect(
await gql`
{
Expand All @@ -333,13 +335,16 @@ describe('ListArticles', () => {
edges {
node {
id
articleReplies(status: NORMAL) {
createdAt
}
}
}
totalCount
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('earlier or equal to 2020-02-06');
expect(
await gql`
{
Expand All @@ -354,13 +359,16 @@ describe('ListArticles', () => {
edges {
node {
id
articleReplies(status: NORMAL) {
createdAt
}
}
}
totalCount
}
}
`()
).toMatchSnapshot();
).toMatchSnapshot('between 2020-02-04 and 2020-02-06');
});

it('filters by mixed query', async () => {
Expand Down Expand Up @@ -529,5 +537,47 @@ describe('ListArticles', () => {
).toMatchSnapshot();
});

it('filters via article reply feedback count', async () => {
expect(
await gql`
{
ListArticles(
filter: { hasArticleReplyWithMorePositiveFeedback: true }
) {
edges {
node {
id
articleReplies(status: NORMAL) {
positiveFeedbackCount
negativeFeedbackCount
}
}
}
}
}
`()
).toMatchSnapshot('hasArticleReplyWithMorePositiveFeedback = true');

expect(
await gql`
{
ListArticles(
filter: { hasArticleReplyWithMorePositiveFeedback: false }
) {
edges {
node {
id
articleReplies(status: NORMAL) {
positiveFeedbackCount
negativeFeedbackCount
}
}
}
}
}
`()
).toMatchSnapshot('hasArticleReplyWithMorePositiveFeedback = false');
});

afterAll(() => unloadFixtures(fixtures));
});
Loading

0 comments on commit de78710

Please sign in to comment.