From ab74572e599711f2782ea6f4df59fb561458177c Mon Sep 17 00:00:00 2001 From: Daniel Adu-Gyan Date: Tue, 15 Aug 2023 09:59:33 +0200 Subject: [PATCH] :poop: Add separate nolla tag news filter param --- backend/services/core/graphql.schema.json | 12 +++++++++++ .../services/core/src/datasources/Events.ts | 7 ------- backend/services/core/src/datasources/News.ts | 21 +++++++++++++++++++ .../core/src/resolvers/newsResolvers.ts | 6 ++++-- .../services/core/src/schemas/news.graphql | 9 ++++++-- backend/services/core/src/shared/database.ts | 7 +++++++ backend/services/core/src/types/graphql.ts | 3 ++- frontend/api/news.graphql | 4 ++-- frontend/generated/graphql.tsx | 12 +++++++++-- 9 files changed, 65 insertions(+), 16 deletions(-) diff --git a/backend/services/core/graphql.schema.json b/backend/services/core/graphql.schema.json index 0a9b1bd42..e0ef96c37 100644 --- a/backend/services/core/graphql.schema.json +++ b/backend/services/core/graphql.schema.json @@ -10470,6 +10470,18 @@ "name": "news", "description": null, "args": [ + { + "name": "nollning", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false", + "isDeprecated": false, + "deprecationReason": null + }, { "name": "page", "description": null, diff --git a/backend/services/core/src/datasources/Events.ts b/backend/services/core/src/datasources/Events.ts index d76fa3052..cf1fec541 100644 --- a/backend/services/core/src/datasources/Events.ts +++ b/backend/services/core/src/datasources/Events.ts @@ -47,13 +47,6 @@ export default class EventAPI extends dbUtils.KnexDataSource { return ongoingEvents.some((e) => e.alarm_active); } - async getNollningTagId(): Promise { - const tag = await this.knex('tags') - .where({ name: 'Nollning' }) - .first(); - return tag?.id; - } - getEvents( ctx: context.UserContext, page?: number, diff --git a/backend/services/core/src/datasources/News.ts b/backend/services/core/src/datasources/News.ts index 948d58c30..a1e4083f1 100644 --- a/backend/services/core/src/datasources/News.ts +++ b/backend/services/core/src/datasources/News.ts @@ -271,6 +271,7 @@ export default class News extends dbUtils.KnexDataSource { page: number, perPage: number, tagIds?: string[], + nollning?: boolean, ): Promise { return this.withAccess('news:article:read', ctx, async () => { let query = this.knex('articles') @@ -281,6 +282,26 @@ export default class News extends dbUtils.KnexDataSource { .whereIn('article_tags.tag_id', tagIds); } + // Handle nollning filter separately from other tags + const nollningTagId = await this.getNollningTagId(); + if (nollningTagId) { + if (nollning) { + query = query.whereExists((qb) => { + qb.select('*') + .from('article_tags') + .whereRaw('article_tags.article_id = articles.id') + .where('article_tags.tag_id', nollningTagId); + }); + } else { + query = query.whereNotExists((qb) => { + qb.select('*') + .from('article_tags') + .whereRaw('article_tags.article_id = articles.id') + .where('article_tags.tag_id', nollningTagId); + }); + } + } + const result = await query .select('articles.*') .distinct('articles.id', 'published_datetime') diff --git a/backend/services/core/src/resolvers/newsResolvers.ts b/backend/services/core/src/resolvers/newsResolvers.ts index b03e122eb..f1a781bc0 100644 --- a/backend/services/core/src/resolvers/newsResolvers.ts +++ b/backend/services/core/src/resolvers/newsResolvers.ts @@ -9,8 +9,10 @@ interface DataSourceContext { const resolvers: Resolvers = { Query: { - news(_, { page, perPage, tagIds }, { user, roles, dataSources }) { - return dataSources.newsAPI.getArticles({ user, roles }, page, perPage, tagIds); + news(_, { + page, perPage, tagIds, nollning, + }, { user, roles, dataSources }) { + return dataSources.newsAPI.getArticles({ user, roles }, page, perPage, tagIds, nollning); }, article(_, { id, slug }, { user, roles, dataSources }) { return dataSources.newsAPI.getArticle({ user, roles }, dataSources, id, slug); diff --git a/backend/services/core/src/schemas/news.graphql b/backend/services/core/src/schemas/news.graphql index 8056eff63..f7bc7e54e 100644 --- a/backend/services/core/src/schemas/news.graphql +++ b/backend/services/core/src/schemas/news.graphql @@ -1,7 +1,12 @@ scalar Url extend type Query { - news(page: Int! = 0, perPage: Int! = 20, tagIds: [String!]): ArticlePagination + news( + page: Int! = 0 + perPage: Int! = 20 + tagIds: [String!] + nollning: Boolean = false + ): ArticlePagination article(id: UUID, slug: String): Article articleRequest(id: UUID!): ArticleRequest articleRequests(limit: Int): [ArticleRequest]! @@ -57,7 +62,7 @@ type Article @key(fields: "id") { status: ArticleRequestStatus! } -enum ArticleRequestStatus{ +enum ArticleRequestStatus { draft rejected approved diff --git a/backend/services/core/src/shared/database.ts b/backend/services/core/src/shared/database.ts index e382788ea..d4a57a916 100644 --- a/backend/services/core/src/shared/database.ts +++ b/backend/services/core/src/shared/database.ts @@ -232,6 +232,13 @@ export class KnexDataSource extends DataSource { return false; } + async getNollningTagId(): Promise { + const tag = await this.knex('tags') + .where({ name: 'Nollning' }) + .first(); + return tag?.id; + } + // used to cache the stab hidden setting private stabHidden = false; diff --git a/backend/services/core/src/types/graphql.ts b/backend/services/core/src/types/graphql.ts index bb1682a9d..2238ac2fc 100644 --- a/backend/services/core/src/types/graphql.ts +++ b/backend/services/core/src/types/graphql.ts @@ -1367,6 +1367,7 @@ export type QueryMembersArgs = { export type QueryNewsArgs = { + nollning?: InputMaybe; page?: Scalars['Int']['input']; perPage?: Scalars['Int']['input']; tagIds?: InputMaybe>; @@ -2806,7 +2807,7 @@ export type QueryResolvers, ParentType, ContextType>; mySubscriptionSettings?: Resolver, ParentType, ContextType>; myTagSubscriptions?: Resolver, ParentType, ContextType>; - news?: Resolver, ParentType, ContextType, RequireFields>; + news?: Resolver, ParentType, ContextType, RequireFields>; payment?: Resolver, ParentType, ContextType, RequireFields>; pings?: Resolver, ParentType, ContextType>; policies?: Resolver, ParentType, ContextType>; diff --git a/frontend/api/news.graphql b/frontend/api/news.graphql index 79f30e81d..2ec3e7f39 100644 --- a/frontend/api/news.graphql +++ b/frontend/api/news.graphql @@ -1,5 +1,5 @@ -query NewsPage($page_number: Int!, $per_page: Int!, $tagIds: [String!]) { - news(page: $page_number, perPage: $per_page, tagIds: $tagIds) { +query NewsPage($page_number: Int!, $per_page: Int!, $tagIds: [String!], $nollning: Boolean) { + news(page: $page_number, perPage: $per_page, tagIds: $tagIds, nollning: $nollning) { articles { id slug diff --git a/frontend/generated/graphql.tsx b/frontend/generated/graphql.tsx index 6962d49dc..0510d481c 100644 --- a/frontend/generated/graphql.tsx +++ b/frontend/generated/graphql.tsx @@ -1372,6 +1372,7 @@ export type QueryMembersArgs = { export type QueryNewsArgs = { + nollning?: InputMaybe; page?: Scalars['Int']; perPage?: Scalars['Int']; tagIds?: InputMaybe>; @@ -2462,6 +2463,7 @@ export type NewsPageQueryVariables = Exact<{ page_number: Scalars['Int']; per_page: Scalars['Int']; tagIds?: InputMaybe | Scalars['String']>; + nollning?: InputMaybe; }>; @@ -6524,8 +6526,13 @@ export type PingMemberMutationHookResult = ReturnType; export type PingMemberMutationOptions = Apollo.BaseMutationOptions; export const NewsPageDocument = gql` - query NewsPage($page_number: Int!, $per_page: Int!, $tagIds: [String!]) { - news(page: $page_number, perPage: $per_page, tagIds: $tagIds) { + query NewsPage($page_number: Int!, $per_page: Int!, $tagIds: [String!], $nollning: Boolean) { + news( + page: $page_number + perPage: $per_page + tagIds: $tagIds + nollning: $nollning + ) { articles { id slug @@ -6615,6 +6622,7 @@ export const NewsPageDocument = gql` * page_number: // value for 'page_number' * per_page: // value for 'per_page' * tagIds: // value for 'tagIds' + * nollning: // value for 'nollning' * }, * }); */