From d43ede093795608eb6e7bca009323c85fc38b194 Mon Sep 17 00:00:00 2001 From: meetul Date: Sat, 10 Feb 2024 19:23:28 +0530 Subject: [PATCH] add custom filter and sort for action items --- README.md | 9 +++-- schema.graphql | 14 +++++++- .../Query/actionItemsByOrganization.ts | 17 +++++++-- .../Query/helperFunctions/getWhere.ts | 36 ++++++++++++++++++- src/typeDefs/enums.ts | 5 +++ src/typeDefs/inputs.ts | 7 ++++ src/typeDefs/queries.ts | 6 +++- src/types/generatedGraphQLTypes.ts | 16 +++++++++ 8 files changed, 100 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 327fa7941a..200e9cffa8 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,10 @@ Core features include: -- [Talawa API](#talawa-api) - - [Talawa Components](#talawa-components) - - [Documentation](#documentation) - - [Installation](#installation) - - [Image Upload](#image-upload) +- [Talawa Components](#talawa-components) +- [Documentation](#documentation) +- [Installation](#installation) +- [Image Upload](#image-upload) diff --git a/schema.graphql b/schema.graphql index 19d7ad4b4d..f822b5856d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -29,6 +29,18 @@ type ActionItemCategory { updatedAt: Date! } +input ActionItemWhereInput { + actionItemCategory_id: ID + event_id: ID + is_active: Boolean + is_completed: Boolean +} + +enum ActionItemsOrderByInput { + createdAt_ASC + createdAt_DESC +} + type Address { city: String countryCode: String @@ -850,7 +862,7 @@ type Query { actionItemCategoriesByOrganization(organizationId: ID!): [ActionItemCategory] actionItemCategory(id: ID!): ActionItemCategory actionItemsByEvent(eventId: ID!): [ActionItem] - actionItemsByOrganization(organizationId: ID!): [ActionItem] + actionItemsByOrganization(orderBy: ActionItemsOrderByInput, organizationId: ID!, where: ActionItemWhereInput): [ActionItem] adminPlugin(orgId: ID!): [Plugin] checkAuth: User! customDataByOrganization(organizationId: ID!): [UserCustomData!]! diff --git a/src/resolvers/Query/actionItemsByOrganization.ts b/src/resolvers/Query/actionItemsByOrganization.ts index 4520f02af6..d6ae072c51 100644 --- a/src/resolvers/Query/actionItemsByOrganization.ts +++ b/src/resolvers/Query/actionItemsByOrganization.ts @@ -1,5 +1,12 @@ import type { QueryResolvers } from "../../types/generatedGraphQLTypes"; -import { ActionItem, ActionItemCategory } from "../../models"; +import type { + InterfaceActionItem} from "../../models"; +import { + ActionItem, + ActionItemCategory +} from "../../models"; +import { getWhere } from "./helperFunctions/getWhere"; +import { getSort } from "./helperFunctions/getSort"; /** * This query will fetch all action items for an organization from database. * @param _parent- @@ -8,6 +15,9 @@ import { ActionItem, ActionItemCategory } from "../../models"; */ export const actionItemsByOrganization: QueryResolvers["actionItemsByOrganization"] = async (_parent, args) => { + const where = getWhere(args.where); + const sort = getSort(args.orderBy); + // Get the ids of all ActionItemCategories associated with the organization const actionItemCategories = await ActionItemCategory.find({ organizationId: args.organizationId, @@ -18,7 +28,10 @@ export const actionItemsByOrganization: QueryResolvers["actionItemsByOrganizatio const actionItems = await ActionItem.find({ actionItemCategoryId: { $in: actionItemCategoriesIds }, - }).lean(); + ...where, + }) + .sort(sort) + .lean(); return actionItems; }; diff --git a/src/resolvers/Query/helperFunctions/getWhere.ts b/src/resolvers/Query/helperFunctions/getWhere.ts index a9dfbe4613..5304c99f2b 100644 --- a/src/resolvers/Query/helperFunctions/getWhere.ts +++ b/src/resolvers/Query/helperFunctions/getWhere.ts @@ -1,5 +1,6 @@ import type { FilterQuery } from "mongoose"; import type { + ActionItemWhereInput, DonationWhereInput, EventWhereInput, InputMaybe, @@ -29,7 +30,8 @@ export const getWhere = ( OrganizationWhereInput & PostWhereInput & UserWhereInput & - DonationWhereInput + DonationWhereInput & + ActionItemWhereInput > > | undefined @@ -180,6 +182,38 @@ export const getWhere = ( }; } + // Returns action items belonging to a specific category + if (where.actionItemCategory_id) { + wherePayload = { + ...wherePayload, + actionItemCategoryId: where.actionItemCategory_id, + }; + } + + // Return action items that are active + if (where.is_active) { + wherePayload = { + ...wherePayload, + isCompleted: !where.is_active, + }; + } + + // Return action items that are completed + if (where.is_completed) { + wherePayload = { + ...wherePayload, + isCompleted: where.is_completed, + }; + } + + // Return action items belonging to a specific event + if (where.event_id) { + wherePayload = { + ...wherePayload, + eventId: where.event_id, + }; + } + // Returns provided location objects if (where.location) { wherePayload = { diff --git a/src/typeDefs/enums.ts b/src/typeDefs/enums.ts index 9e9804c137..83acddde16 100644 --- a/src/typeDefs/enums.ts +++ b/src/typeDefs/enums.ts @@ -2,6 +2,11 @@ import { gql } from "graphql-tag"; // Place fields alphabetically to ensure easier lookup and navigation. export const enums = gql` + enum ActionItemsOrderByInput { + createdAt_ASC + createdAt_DESC + } + enum EventOrderByInput { id_ASC id_DESC diff --git a/src/typeDefs/inputs.ts b/src/typeDefs/inputs.ts index d0bdd94af4..c46594a9c7 100644 --- a/src/typeDefs/inputs.ts +++ b/src/typeDefs/inputs.ts @@ -47,6 +47,13 @@ export const inputs = gql` eventId: ID } + input ActionItemWhereInput { + actionItemCategory_id: ID + event_id: ID + is_active: Boolean + is_completed: Boolean + } + input CursorPaginationInput { cursor: String direction: PaginationDirection! diff --git a/src/typeDefs/queries.ts b/src/typeDefs/queries.ts index 975f839ad9..051043b23f 100644 --- a/src/typeDefs/queries.ts +++ b/src/typeDefs/queries.ts @@ -11,7 +11,11 @@ export const queries = gql` actionItemsByEvent(eventId: ID!): [ActionItem] - actionItemsByOrganization(organizationId: ID!): [ActionItem] + actionItemsByOrganization( + organizationId: ID! + where: ActionItemWhereInput + orderBy: ActionItemsOrderByInput + ): [ActionItem] actionItemCategory(id: ID!): ActionItemCategory diff --git a/src/types/generatedGraphQLTypes.ts b/src/types/generatedGraphQLTypes.ts index 161abec318..5c42971d73 100644 --- a/src/types/generatedGraphQLTypes.ts +++ b/src/types/generatedGraphQLTypes.ts @@ -83,6 +83,17 @@ export type ActionItemCategory = { updatedAt: Scalars['Date']['output']; }; +export type ActionItemWhereInput = { + actionItemCategory_id?: InputMaybe; + event_id?: InputMaybe; + is_active?: InputMaybe; + is_completed?: InputMaybe; +}; + +export type ActionItemsOrderByInput = + | 'createdAt_ASC' + | 'createdAt_DESC'; + export type Address = { __typename?: 'Address'; city?: Maybe; @@ -1483,7 +1494,9 @@ export type QueryActionItemsByEventArgs = { export type QueryActionItemsByOrganizationArgs = { + orderBy?: InputMaybe; organizationId: Scalars['ID']['input']; + where?: InputMaybe; }; @@ -2096,6 +2109,8 @@ export type ResolversInterfaceTypes> = { export type ResolversTypes = { ActionItem: ResolverTypeWrapper; ActionItemCategory: ResolverTypeWrapper; + ActionItemWhereInput: ActionItemWhereInput; + ActionItemsOrderByInput: ActionItemsOrderByInput; Address: ResolverTypeWrapper
; AddressInput: AddressInput; Advertisement: ResolverTypeWrapper & { creator?: Maybe }>; @@ -2237,6 +2252,7 @@ export type ResolversTypes = { export type ResolversParentTypes = { ActionItem: InterfaceActionItemModel; ActionItemCategory: InterfaceActionItemCategoryModel; + ActionItemWhereInput: ActionItemWhereInput; Address: Address; AddressInput: AddressInput; Advertisement: Omit & { creator?: Maybe };