diff --git a/src/resolvers/Query/helperFunctions/getWhere.ts b/src/resolvers/Query/helperFunctions/getWhere.ts index 5304c99f2b..71238d744d 100644 --- a/src/resolvers/Query/helperFunctions/getWhere.ts +++ b/src/resolvers/Query/helperFunctions/getWhere.ts @@ -194,7 +194,7 @@ export const getWhere = ( if (where.is_active) { wherePayload = { ...wherePayload, - isCompleted: !where.is_active, + isCompleted: false, }; } @@ -202,7 +202,7 @@ export const getWhere = ( if (where.is_completed) { wherePayload = { ...wherePayload, - isCompleted: where.is_completed, + isCompleted: true, }; } diff --git a/tests/helpers/actionItem.ts b/tests/helpers/actionItem.ts index 1ebb112f69..bbdba68b99 100644 --- a/tests/helpers/actionItem.ts +++ b/tests/helpers/actionItem.ts @@ -69,11 +69,18 @@ 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({ @@ -81,6 +88,15 @@ export const createTestActionItems = async (): Promise< 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({ @@ -88,7 +104,7 @@ export const createTestActionItems = async (): Promise< description: `description${nanoid().toLowerCase()}`, allDay: true, startDate: new Date(), - recurring: true, + recurring: false, isPublic: true, isRegisterable: true, creatorId: testUser?._id, diff --git a/tests/resolvers/Query/actionItemsByOrganization.spec.ts b/tests/resolvers/Query/actionItemsByOrganization.spec.ts index b4883a793a..c8d86dca8a 100644 --- a/tests/resolvers/Query/actionItemsByOrganization.spec.ts +++ b/tests/resolvers/Query/actionItemsByOrganization.spec.ts @@ -1,19 +1,25 @@ import "dotenv/config"; import { ActionItem, ActionItemCategory } from "../../../src/models"; import { connect, disconnect } from "../../helpers/db"; -import type { QueryActionItemsByOrganizationArgs } from "../../../src/types/generatedGraphQLTypes"; +import type { + ActionItemWhereInput, + ActionItemsOrderByInput, + QueryActionItemsByOrganizationArgs, +} from "../../../src/types/generatedGraphQLTypes"; import { actionItemsByOrganization as actionItemsByOrganizationResolver } from "../../../src/resolvers/Query/actionItemsByOrganization"; import { beforeAll, afterAll, describe, it, expect } from "vitest"; import type mongoose from "mongoose"; import { createTestActionItems } from "../../helpers/actionItem"; import type { TestOrganizationType } from "../../helpers/userAndOrg"; +import type { TestEventType } from "../../helpers/events"; let MONGOOSE_INSTANCE: typeof mongoose; let testOrganization: TestOrganizationType; +let testEvent: TestEventType; beforeAll(async () => { MONGOOSE_INSTANCE = await connect(); - [, , testOrganization] = await createTestActionItems(); + [, testEvent, testOrganization] = await createTestActionItems(); }); afterAll(async () => { @@ -21,9 +27,101 @@ afterAll(async () => { }); describe("resolvers -> Query -> actionItemsByOrganization", () => { - it(`returns list of all action items associated with an organization`, async () => { + it(`returns list of all action items associated with an organization in ascending order`, async () => { + const orderBy: ActionItemsOrderByInput = "createdAt_ASC"; + + const args: QueryActionItemsByOrganizationArgs = { + organizationId: testOrganization?._id, + orderBy, + }; + + const actionItemsByOrganizationPayload = + await actionItemsByOrganizationResolver?.({}, args, {}); + + const actionItemCategories = await ActionItemCategory.find({ + organizationId: args.organizationId, + }); + const actionItemCategoriesIds = actionItemCategories.map( + (category) => category._id + ); + + const actionItemsByOrganizationInfo = await ActionItem.find({ + actionItemCategoryId: { $in: actionItemCategoriesIds }, + }).lean(); + + expect(actionItemsByOrganizationPayload).toEqual( + actionItemsByOrganizationInfo + ); + }); + + it(`returns list of all action items associated with an organization in descending order`, async () => { + const orderBy: ActionItemsOrderByInput = "createdAt_DESC"; + + const args: QueryActionItemsByOrganizationArgs = { + organizationId: testOrganization?._id, + orderBy, + }; + + const actionItemsByOrganizationPayload = + await actionItemsByOrganizationResolver?.({}, args, {}); + + actionItemsByOrganizationPayload?.reverse(); + + const actionItemCategories = await ActionItemCategory.find({ + organizationId: args.organizationId, + }); + const actionItemCategoriesIds = actionItemCategories.map( + (category) => category._id + ); + + const actionItemsByOrganizationInfo = await ActionItem.find({ + actionItemCategoryId: { $in: actionItemCategoriesIds }, + }).lean(); + + expect(actionItemsByOrganizationPayload).toEqual( + actionItemsByOrganizationInfo + ); + }); + + it(`returns list of all action items associated with an organization and belonging to an action item category`, async () => { + const actionItemCategories = await ActionItemCategory.find({ + organizationId: testOrganization?._id, + }); + + const actionItemCategoriesIds = actionItemCategories.map( + (category) => category._id + ); + + const actionItemCategoryId = actionItemCategoriesIds[0]; + + const where: ActionItemWhereInput = { + actionItemCategory_id: actionItemCategoryId, + }; + + const args: QueryActionItemsByOrganizationArgs = { + organizationId: testOrganization?._id, + where, + }; + + const actionItemsByOrganizationPayload = + await actionItemsByOrganizationResolver?.({}, args, {}); + + const actionItemsByOrganizationInfo = await ActionItem.find({ + actionItemCategoryId, + }).lean(); + + expect(actionItemsByOrganizationPayload).toEqual( + actionItemsByOrganizationInfo + ); + }); + it(`returns list of all action items associated with an organization that are active`, async () => { + const where: ActionItemWhereInput = { + is_active: true, + }; + const args: QueryActionItemsByOrganizationArgs = { organizationId: testOrganization?._id, + where, }; const actionItemsByOrganizationPayload = @@ -38,6 +136,102 @@ describe("resolvers -> Query -> actionItemsByOrganization", () => { const actionItemsByOrganizationInfo = await ActionItem.find({ actionItemCategoryId: { $in: actionItemCategoriesIds }, + isCompleted: false, + }).lean(); + + expect(actionItemsByOrganizationPayload).toEqual( + actionItemsByOrganizationInfo + ); + }); + + it(`returns list of all action items associated with an organization that are completed`, async () => { + const where: ActionItemWhereInput = { + is_completed: true, + }; + + const args: QueryActionItemsByOrganizationArgs = { + organizationId: testOrganization?._id, + where, + }; + + const actionItemsByOrganizationPayload = + await actionItemsByOrganizationResolver?.({}, args, {}); + + const actionItemCategories = await ActionItemCategory.find({ + organizationId: args.organizationId, + }); + const actionItemCategoriesIds = actionItemCategories.map( + (category) => category._id + ); + + const actionItemsByOrganizationInfo = await ActionItem.find({ + actionItemCategoryId: { $in: actionItemCategoriesIds }, + isCompleted: true, + }).lean(); + + expect(actionItemsByOrganizationPayload).toEqual( + actionItemsByOrganizationInfo + ); + }); + + it(`returns list of all action items associated with an organization and belonging to an event`, async () => { + const where: ActionItemWhereInput = { + event_id: testEvent?._id, + }; + + const args: QueryActionItemsByOrganizationArgs = { + organizationId: testOrganization?._id, + where, + }; + + const actionItemsByOrganizationPayload = + await actionItemsByOrganizationResolver?.({}, args, {}); + + const actionItemCategories = await ActionItemCategory.find({ + organizationId: args.organizationId, + }); + const actionItemCategoriesIds = actionItemCategories.map( + (category) => category._id + ); + + const actionItemsByOrganizationInfo = await ActionItem.find({ + actionItemCategoryId: { $in: actionItemCategoriesIds }, + eventId: testEvent?._id, + }).lean(); + + expect(actionItemsByOrganizationPayload).toEqual( + actionItemsByOrganizationInfo + ); + }); + + it(`returns list of all action items associated with an organization and belonging to an event and a specific category and are completed`, async () => { + const actionItemCategories = await ActionItemCategory.find({ + organizationId: testOrganization?._id, + }); + const actionItemCategoriesIds = actionItemCategories.map( + (category) => category._id + ); + + const actionItemCategoryId = actionItemCategoriesIds[0]; + + const where: ActionItemWhereInput = { + actionItemCategory_id: actionItemCategoryId, + event_id: testEvent?._id, + is_completed: true, + }; + + const args: QueryActionItemsByOrganizationArgs = { + organizationId: testOrganization?._id, + where, + }; + + const actionItemsByOrganizationPayload = + await actionItemsByOrganizationResolver?.({}, args, {}); + + const actionItemsByOrganizationInfo = await ActionItem.find({ + actionItemCategoryId, + eventId: testEvent?._id, + isCompleted: true, }).lean(); expect(actionItemsByOrganizationPayload).toEqual(