Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Filter and Sort for Action Items #1855

Merged
merged 15 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -916,7 +928,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]
agendaCategory(id: ID!): AgendaCategory!
checkAuth: User!
Expand Down
11 changes: 10 additions & 1 deletion src/resolvers/Query/actionItemsByOrganization.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { QueryResolvers } from "../../types/generatedGraphQLTypes";
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 +11,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 +24,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: false,
};
}

// Return action items that are completed
if (where.is_completed) {
wherePayload = {
...wherePayload,
isCompleted: true,
};
}

// 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 @@ -45,6 +45,13 @@ export const inputs = gql`
eventId: ID
}

input ActionItemWhereInput {
actionItemCategory_id: ID
event_id: ID
is_active: Boolean
is_completed: Boolean
}

input CreateAgendaCategoryInput {
name: String!
description: String
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 @@ -85,6 +85,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 @@ -1609,7 +1620,9 @@ export type QueryActionItemsByEventArgs = {


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


Expand Down Expand Up @@ -2270,6 +2283,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 @@ -2422,6 +2437,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
18 changes: 17 additions & 1 deletion tests/helpers/actionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,42 @@ export const createTestActionItems = async (): Promise<
const randomUser = await createTestUser();
const [testUser, testOrganization, testCategory] = await createTestCategory();

const testCategory2 = await ActionItemCategory.create({
creatorId: testUser?._id,
organizationId: testOrganization?._id,
name: "ActionItemCategory 2",
});

const testActionItem1 = await ActionItem.create({
creatorId: testUser?._id,
assigneeId: randomUser?._id,
assignerId: testUser?._id,
actionItemCategoryId: testCategory?._id,
isCompleted: true,
});

const testActionItem2 = await ActionItem.create({
creatorId: testUser?._id,
assigneeId: randomUser?._id,
assignerId: testUser?._id,
actionItemCategoryId: testCategory?._id,
isCompleted: false,
});

await ActionItem.create({
creatorId: testUser?._id,
assigneeId: randomUser?._id,
assignerId: testUser?._id,
actionItemCategoryId: testCategory2?._id,
isCompleted: true,
});

const testEvent = await Event.create({
title: `title${nanoid().toLowerCase()}`,
description: `description${nanoid().toLowerCase()}`,
allDay: true,
startDate: new Date(),
recurring: true,
recurring: false,
isPublic: true,
isRegisterable: true,
creatorId: testUser?._id,
Expand Down
Loading
Loading