Skip to content

Commit

Permalink
add custom filter and sort for action items
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Feb 10, 2024
1 parent fa10711 commit d43ede0
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 10 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ Core features include:

<!-- toc -->

- [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)

<!-- tocstop -->

Expand Down
14 changes: 13 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!]!
Expand Down
17 changes: 15 additions & 2 deletions src/resolvers/Query/actionItemsByOrganization.ts
Original file line number Diff line number Diff line change
@@ -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-
Expand All @@ -8,6 +15,9 @@ import { ActionItem, ActionItemCategory } from "../../models";
*/
export const actionItemsByOrganization: QueryResolvers["actionItemsByOrganization"] =
async (_parent, args) => {
const where = getWhere<InterfaceActionItem>(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,
Expand All @@ -18,7 +28,10 @@ export const actionItemsByOrganization: QueryResolvers["actionItemsByOrganizatio

const actionItems = await ActionItem.find({
actionItemCategoryId: { $in: actionItemCategoriesIds },
}).lean();
...where,
})
.sort(sort)
.lean();

return actionItems;
};
36 changes: 35 additions & 1 deletion src/resolvers/Query/helperFunctions/getWhere.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FilterQuery } from "mongoose";
import type {
ActionItemWhereInput,
DonationWhereInput,
EventWhereInput,
InputMaybe,
Expand Down Expand Up @@ -29,7 +30,8 @@ export const getWhere = <T = unknown>(
OrganizationWhereInput &
PostWhereInput &
UserWhereInput &
DonationWhereInput
DonationWhereInput &
ActionItemWhereInput
>
>
| undefined
Expand Down Expand Up @@ -180,6 +182,38 @@ export const getWhere = <T = unknown>(
};
}

// 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 = {
Expand Down
5 changes: 5 additions & 0 deletions src/typeDefs/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/typeDefs/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
6 changes: 5 additions & 1 deletion src/typeDefs/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/types/generatedGraphQLTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export type ActionItemCategory = {
updatedAt: Scalars['Date']['output'];
};

export type ActionItemWhereInput = {
actionItemCategory_id?: InputMaybe<Scalars['ID']['input']>;
event_id?: InputMaybe<Scalars['ID']['input']>;
is_active?: InputMaybe<Scalars['Boolean']['input']>;
is_completed?: InputMaybe<Scalars['Boolean']['input']>;
};

export type ActionItemsOrderByInput =
| 'createdAt_ASC'
| 'createdAt_DESC';

export type Address = {
__typename?: 'Address';
city?: Maybe<Scalars['String']['output']>;
Expand Down Expand Up @@ -1483,7 +1494,9 @@ export type QueryActionItemsByEventArgs = {


export type QueryActionItemsByOrganizationArgs = {
orderBy?: InputMaybe<ActionItemsOrderByInput>;
organizationId: Scalars['ID']['input'];
where?: InputMaybe<ActionItemWhereInput>;
};


Expand Down Expand Up @@ -2096,6 +2109,8 @@ export type ResolversInterfaceTypes<RefType extends Record<string, unknown>> = {
export type ResolversTypes = {
ActionItem: ResolverTypeWrapper<InterfaceActionItemModel>;
ActionItemCategory: ResolverTypeWrapper<InterfaceActionItemCategoryModel>;
ActionItemWhereInput: ActionItemWhereInput;
ActionItemsOrderByInput: ActionItemsOrderByInput;
Address: ResolverTypeWrapper<Address>;
AddressInput: AddressInput;
Advertisement: ResolverTypeWrapper<Omit<Advertisement, 'creator'> & { creator?: Maybe<ResolversTypes['User']> }>;
Expand Down Expand Up @@ -2237,6 +2252,7 @@ export type ResolversTypes = {
export type ResolversParentTypes = {
ActionItem: InterfaceActionItemModel;
ActionItemCategory: InterfaceActionItemCategoryModel;
ActionItemWhereInput: ActionItemWhereInput;
Address: Address;
AddressInput: AddressInput;
Advertisement: Omit<Advertisement, 'creator'> & { creator?: Maybe<ResolversParentTypes['User']> };
Expand Down

0 comments on commit d43ede0

Please sign in to comment.