From 3220b37587be83d02f2daa206255bbcddd44ce5a Mon Sep 17 00:00:00 2001 From: meetul Date: Sat, 2 Nov 2024 10:52:29 +0530 Subject: [PATCH] add coderabbitai suggested changes --- src/resolvers/User/tagsAssignedWith.ts | 2 +- src/resolvers/UserTag/usersToAssignTo.ts | 2 +- .../getUserTagGraphQLConnectionFilter.ts | 13 +++++--- .../getUserTagGraphQLConnectionSort.ts | 6 ++-- ...getUserTagMemberGraphQLConnectionFilter.ts | 18 ++++++---- .../parseUserTagMemberWhere.ts | 6 ++-- .../parseUserTagSortedBy.ts | 33 +++++++++---------- .../parseUserTagWhere.ts | 2 +- tests/resolvers/UserTag/ancestorTags.spec.ts | 10 +++++- 9 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/resolvers/User/tagsAssignedWith.ts b/src/resolvers/User/tagsAssignedWith.ts index 303fb76a0fa..92212b1633e 100644 --- a/src/resolvers/User/tagsAssignedWith.ts +++ b/src/resolvers/User/tagsAssignedWith.ts @@ -119,7 +119,7 @@ type ParsedCursor = string; * @param cursorValue - The cursor value to be parsed. * @param cursorName - The name of the cursor argument. * @param cursorPath - The path of the cursor argument in the GraphQL query. - * @param tagId - The ID of the user to which the tags are assigned. + * @param userId - The ID of the user for which assigned tags are being queried. * @returns An object containing the parsed cursor value or an array of errors if the cursor value is invalid. * * @see TagUser - The TagUser model used to interact with the tag users collection in the database. diff --git a/src/resolvers/UserTag/usersToAssignTo.ts b/src/resolvers/UserTag/usersToAssignTo.ts index 891c5368816..b7dbee0bbbd 100644 --- a/src/resolvers/UserTag/usersToAssignTo.ts +++ b/src/resolvers/UserTag/usersToAssignTo.ts @@ -112,7 +112,7 @@ export const usersToAssignTo: UserTagResolvers["usersToAssignTo"] = async ( { $match: { ...objectListFilter, - joinedOrganizations: parent.organizationId, + joinedOrganizations: { $in: [parent.organizationId] }, }, }, ...commonPipeline, diff --git a/src/utilities/graphQLConnection/getUserTagGraphQLConnectionFilter.ts b/src/utilities/graphQLConnection/getUserTagGraphQLConnectionFilter.ts index 587b6efb2a0..4fb1dad8a30 100644 --- a/src/utilities/graphQLConnection/getUserTagGraphQLConnectionFilter.ts +++ b/src/utilities/graphQLConnection/getUserTagGraphQLConnectionFilter.ts @@ -5,9 +5,9 @@ import type { } from "../userTagsPaginationUtils"; /** - * This is typescript type of the object returned from function `getGraphQLConnectionFilter`. + * This is typescript type of the object returned from function `getUserTagGraphQLConnectionFilter`. */ -type GraphQLConnectionFilter = +type UserTagGraphQLConnectionFilter = | { _id: { $lt: string; @@ -37,11 +37,14 @@ export function getUserTagGraphQLConnectionFilter({ ParseUserTagWhereResult & { cursor: string | null; direction: GraphQLConnectionTraversalDirection; - }): GraphQLConnectionFilter { - const filter = {} as GraphQLConnectionFilter; + }): UserTagGraphQLConnectionFilter { + const filter = {} as UserTagGraphQLConnectionFilter; filter.name = { - $regex: new RegExp(`^${nameStartsWith}`, "i"), + $regex: new RegExp( + `^${nameStartsWith.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, + "i", + ), }; if (cursor !== null) { diff --git a/src/utilities/graphQLConnection/getUserTagGraphQLConnectionSort.ts b/src/utilities/graphQLConnection/getUserTagGraphQLConnectionSort.ts index fab95312217..a5c63a5c2d7 100644 --- a/src/utilities/graphQLConnection/getUserTagGraphQLConnectionSort.ts +++ b/src/utilities/graphQLConnection/getUserTagGraphQLConnectionSort.ts @@ -2,9 +2,9 @@ import type { GraphQLConnectionTraversalDirection } from "."; import type { ParseSortedByResult } from "../userTagsPaginationUtils"; /** - *This is typescript type of the object returned from `getGraphQLConnectionSort` function. + *This is typescript type of the object returned from `getUserTagGraphQLConnectionSort` function. */ -type GraphQLConnectionSort = +type UserTagGraphQLConnectionSort = | { _id: 1; } @@ -20,7 +20,7 @@ export function getUserTagGraphQLConnectionSort({ sortById, }: ParseSortedByResult & { direction: GraphQLConnectionTraversalDirection; -}): GraphQLConnectionSort { +}): UserTagGraphQLConnectionSort { if (sortById === "ASCENDING") { if (direction === "BACKWARD") { return { diff --git a/src/utilities/graphQLConnection/getUserTagMemberGraphQLConnectionFilter.ts b/src/utilities/graphQLConnection/getUserTagMemberGraphQLConnectionFilter.ts index 961ad7e29ae..056d65ab979 100644 --- a/src/utilities/graphQLConnection/getUserTagMemberGraphQLConnectionFilter.ts +++ b/src/utilities/graphQLConnection/getUserTagMemberGraphQLConnectionFilter.ts @@ -6,9 +6,9 @@ import type { } from "../userTagsPaginationUtils"; /** - * This is typescript type of the object returned from function `getGraphQLConnectionFilter`. + * This is typescript type of the object returned from function `getUserTagMemberGraphQLConnectionFilter`. */ -type GraphQLConnectionFilter = +type UserTagMemberGraphQLConnectionFilter = | { _id: { $lt: Types.ObjectId; @@ -45,14 +45,20 @@ export function getUserTagMemberGraphQLConnectionFilter({ ParseUserTagMemberWhereResult & { cursor: string | null; direction: GraphQLConnectionTraversalDirection; - }): GraphQLConnectionFilter { - const filter = {} as GraphQLConnectionFilter; + }): UserTagMemberGraphQLConnectionFilter { + const filter = {} as UserTagMemberGraphQLConnectionFilter; filter.firstName = { - $regex: new RegExp(`^${firstNameStartsWith}`, "i"), + $regex: new RegExp( + `^${firstNameStartsWith.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, + "i", + ), }; filter.lastName = { - $regex: new RegExp(`^${lastNameStartsWith}`, "i"), + $regex: new RegExp( + `^${lastNameStartsWith.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, + "i", + ), }; if (cursor !== null) { diff --git a/src/utilities/userTagsPaginationUtils/parseUserTagMemberWhere.ts b/src/utilities/userTagsPaginationUtils/parseUserTagMemberWhere.ts index 163dc81fb4d..2061eacb0e6 100644 --- a/src/utilities/userTagsPaginationUtils/parseUserTagMemberWhere.ts +++ b/src/utilities/userTagsPaginationUtils/parseUserTagMemberWhere.ts @@ -31,7 +31,7 @@ export function parseUserTagMemberWhere( } else { if (!where.firstName && !where.lastName) { errors.push({ - message: `Atleast one of firstName or lastName should be provided`, + message: `At least one of firstName or lastName should be provided`, path: ["where"], }); @@ -45,7 +45,7 @@ export function parseUserTagMemberWhere( ) { errors.push({ message: "Invalid firstName provided. It must be a string.", - path: ["whereFirstName"], + path: ["where", "firstName", "starts_with"], }); return { @@ -58,7 +58,7 @@ export function parseUserTagMemberWhere( ) { errors.push({ message: "Invalid lastName provided. It must be a string.", - path: ["whereLastName"], + path: ["where", "lastName", "starts_with"], }); return { diff --git a/src/utilities/userTagsPaginationUtils/parseUserTagSortedBy.ts b/src/utilities/userTagsPaginationUtils/parseUserTagSortedBy.ts index f5b99e6500b..825fca40b6d 100644 --- a/src/utilities/userTagsPaginationUtils/parseUserTagSortedBy.ts +++ b/src/utilities/userTagsPaginationUtils/parseUserTagSortedBy.ts @@ -27,25 +27,24 @@ export function parseUserTagSortedBy( isSuccessful: true, parsedSortedBy: { sortById: "DESCENDING" }, }; - } else { - if (sortedBy.id !== "DESCENDING" && sortedBy.id !== "ASCENDING") { - errors.push({ - message: - "Invalid sortedById provided. It must be a of type SortedByOrder.", - path: ["sortedById"], - }); - - return { - isSuccessful: false, - errors, - }; - } + } + if (sortedBy.id !== "DESCENDING" && sortedBy.id !== "ASCENDING") { + errors.push({ + message: + "Invalid sortedById provided. It must be a of type SortedByOrder.", + path: ["sortedBy", "id"], + }); return { - isSuccessful: true, - parsedSortedBy: { - sortById: sortedBy.id, - }, + isSuccessful: false, + errors, }; } + + return { + isSuccessful: true, + parsedSortedBy: { + sortById: sortedBy.id, + }, + }; } diff --git a/src/utilities/userTagsPaginationUtils/parseUserTagWhere.ts b/src/utilities/userTagsPaginationUtils/parseUserTagWhere.ts index 7fe9091ca23..76b7db88b76 100644 --- a/src/utilities/userTagsPaginationUtils/parseUserTagWhere.ts +++ b/src/utilities/userTagsPaginationUtils/parseUserTagWhere.ts @@ -30,7 +30,7 @@ export function parseUserTagWhere( if (typeof where.name.starts_with !== "string") { errors.push({ message: "Invalid name provided. It must be a string.", - path: ["whereName"], + path: ["where", "name", "starts_with"], }); return { diff --git a/tests/resolvers/UserTag/ancestorTags.spec.ts b/tests/resolvers/UserTag/ancestorTags.spec.ts index 1272da4d207..dd06ea52796 100644 --- a/tests/resolvers/UserTag/ancestorTags.spec.ts +++ b/tests/resolvers/UserTag/ancestorTags.spec.ts @@ -32,7 +32,15 @@ afterAll(async () => { }); describe("resolvers -> Tag -> ancestorTags", () => { - it(`returns the correct ancestorTags array`, async () => { + it(`returns an empty ancestorTags array for the root tag`, async () => { + const parent = testRootTag as InterfaceOrganizationTagUser; + + const payload = await ancestorTagsResolver?.(parent, {}, {}); + + expect(payload).toEqual([]); + }); + + it(`returns the correct ancestorTags array for a nested tag`, async () => { const parent = testSubTagLevel2 as InterfaceOrganizationTagUser; const payload = await ancestorTagsResolver?.(parent, {}, {});