diff --git a/src/utilities/userTagsPaginationUtils/getUserTagGraphQLConnectionFilter.ts b/src/utilities/userTagsPaginationUtils/getUserTagGraphQLConnectionFilter.ts index bee0198bb69..e7f6d88a2a6 100644 --- a/src/utilities/userTagsPaginationUtils/getUserTagGraphQLConnectionFilter.ts +++ b/src/utilities/userTagsPaginationUtils/getUserTagGraphQLConnectionFilter.ts @@ -7,24 +7,25 @@ import type { /** * This is typescript type of the object returned from function `getUserTagGraphQLConnectionFilter`. */ -type UserTagGraphQLConnectionFilter = - | { - _id?: { - $lt: string; - }; - name: { - $regex: RegExp; - }; - } - | { - _id?: { - $gt: string; - }; - name: { - $regex: RegExp; - }; - }; +type BaseUserTagGraphQLConnectionFilter = { + name: { + $regex: RegExp; + }; +}; +type UserTagGraphQLConnectionFilter = BaseUserTagGraphQLConnectionFilter & + ( + | { + _id?: { + $lt: string; + }; + } + | { + _id?: { + $gt: string; + }; + } + ); /** * This function is used to get an object containing filtering logic. */ @@ -48,28 +49,19 @@ export function getUserTagGraphQLConnectionFilter({ }; if (cursor !== null) { - if (sortById === "ASCENDING") { - if (direction === "BACKWARD") { - filter._id = { - $lt: cursor, - }; - } else { - filter._id = { - $gt: cursor, - }; - } - } else { - if (direction === "BACKWARD") { - filter._id = { - $gt: cursor, - }; - } else { - filter._id = { - $lt: cursor, - }; - } - } + filter._id = getCursorFilter(cursor, sortById, direction); } return filter; } + +function getCursorFilter( + cursor: string, + sortById: "ASCENDING" | "DESCENDING", + direction: GraphQLConnectionTraversalDirection, +): { $lt: string } | { $gt: string } { + if (sortById === "ASCENDING") { + return direction === "BACKWARD" ? { $lt: cursor } : { $gt: cursor }; + } + return direction === "BACKWARD" ? { $gt: cursor } : { $lt: cursor }; +} diff --git a/src/utilities/userTagsPaginationUtils/getUserTagMemberGraphQLConnectionFilter.ts b/src/utilities/userTagsPaginationUtils/getUserTagMemberGraphQLConnectionFilter.ts index 078a077beaf..3d00232d4e0 100644 --- a/src/utilities/userTagsPaginationUtils/getUserTagMemberGraphQLConnectionFilter.ts +++ b/src/utilities/userTagsPaginationUtils/getUserTagMemberGraphQLConnectionFilter.ts @@ -8,29 +8,29 @@ import type { /** * This is typescript type of the object returned from function `getUserTagMemberGraphQLConnectionFilter`. */ +type BaseUserTagMemberGraphQLConnectionFilter = { + firstName: { + $regex: RegExp; + }; + lastName: { + $regex: RegExp; + }; +}; + type UserTagMemberGraphQLConnectionFilter = - | { - _id?: { - $lt: Types.ObjectId; - }; - firstName: { - $regex: RegExp; - }; - lastName: { - $regex: RegExp; - }; - } - | { - _id?: { - $gt: Types.ObjectId; - }; - firstName: { - $regex: RegExp; - }; - lastName: { - $regex: RegExp; - }; - }; + BaseUserTagMemberGraphQLConnectionFilter & + ( + | { + _id?: { + $lt: Types.ObjectId; + }; + } + | { + _id?: { + $gt: Types.ObjectId; + }; + } + ); /** * This function is used to get an object containing filtering logic. @@ -62,28 +62,20 @@ export function getUserTagMemberGraphQLConnectionFilter({ }; if (cursor !== null) { - if (sortById === "ASCENDING") { - if (direction === "BACKWARD") { - filter._id = { - $lt: new Types.ObjectId(cursor), - }; - } else { - filter._id = { - $gt: new Types.ObjectId(cursor), - }; - } - } else { - if (direction === "BACKWARD") { - filter._id = { - $gt: new Types.ObjectId(cursor), - }; - } else { - filter._id = { - $lt: new Types.ObjectId(cursor), - }; - } - } + filter._id = getCursorFilter(cursor, sortById, direction); } return filter; } + +function getCursorFilter( + cursor: string, + sortById: "ASCENDING" | "DESCENDING", + direction: GraphQLConnectionTraversalDirection, +): { $lt: Types.ObjectId } | { $gt: Types.ObjectId } { + const cursorId = new Types.ObjectId(cursor); + if (sortById === "ASCENDING") { + return direction === "BACKWARD" ? { $lt: cursorId } : { $gt: cursorId }; + } + return direction === "BACKWARD" ? { $gt: cursorId } : { $lt: cursorId }; +}