Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 authored Aug 24, 2024
2 parents 967e1bd + 27e091c commit ee58c65
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/resolvers/Query/getUserTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import { errors, requestContext } from "../../libraries";
import type { QueryResolvers } from "../../types/generatedGraphQLTypes";
import { TAG_NOT_FOUND } from "../../constants";

/**
* Retrieves a user tag by its ID.
*
* This function fetches a specific user tag from the database using its ID. If the user tag
* is not found, it throws an error indicating that the item does not exist.
*
* @param _parent - This parameter is not used in this resolver function.
* @param args - The arguments provided by the GraphQL query, including the ID of the user tag to retrieve.
*
* @returns The user tag with the specified ID.
*/

export const getUserTag: QueryResolvers["getUserTag"] = async (
_parent,
args,
Expand Down
33 changes: 23 additions & 10 deletions src/resolvers/Query/getUserTagAncestors.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import type { InterfaceOrganizationTagUser } from "../../models";
import { OrganizationTagUser } from "../../models";
import { errors, requestContext } from "../../libraries";
import type { QueryResolvers } from "../../types/generatedGraphQLTypes";
import { TAG_NOT_FOUND } from "../../constants";

/**
* Retrieves the ancestor tags of a given user tag.
*
* This function fetches the ancestor tags of a specific user tag from the database. If the user tag
* is not found, it throws an error indicating that the item does not exist.
*
* @param _parent - This parameter is not used in this resolver function.
* @param args - The arguments provided by the GraphQL query, including the ID of the given user tag.
*
* @returns The ancestor tags of the user tag.
*/

export const getUserTagAncestors: QueryResolvers["getUserTagAncestors"] =
async (_parent, args) => {
let currentTag = await OrganizationTagUser.findById(args.id).lean();

if (!currentTag) {
throw new errors.NotFoundError(
requestContext.translate(TAG_NOT_FOUND.MESSAGE),
TAG_NOT_FOUND.CODE,
TAG_NOT_FOUND.PARAM,
);
}

const tagAncestors = [currentTag];

while (currentTag?.parentTagId) {
const currentParent = await OrganizationTagUser.findById(
const currentParent = (await OrganizationTagUser.findById(
currentTag.parentTagId,
).lean();
).lean()) as InterfaceOrganizationTagUser | null;

if (currentParent) {
tagAncestors.push(currentParent);
currentTag = currentParent;
}
}

if (!currentTag) {
throw new errors.NotFoundError(
requestContext.translate(TAG_NOT_FOUND.MESSAGE),
TAG_NOT_FOUND.CODE,
TAG_NOT_FOUND.PARAM,
);
}

return tagAncestors.reverse();
};

0 comments on commit ee58c65

Please sign in to comment.