forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ancestorTags field and resolver on the UserTag schema
- Loading branch information
Showing
6 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { | ||
InterfaceOrganizationTagUser} from "../../models"; | ||
import { | ||
OrganizationTagUser, | ||
} from "../../models"; | ||
import type { UserTagResolvers } from "../../types/generatedGraphQLTypes"; | ||
|
||
/** | ||
* Resolver function for the `ancestorTags` field of an `OrganizationTagUser`. | ||
* | ||
* This function retrieves the ancestor tags of a specific organization user tag by recursively finding | ||
* each parent tag until the root tag (where parentTagId is null) is reached. It then reverses the order, | ||
* appends the current tag at the end, and returns the final array of tags. | ||
* | ||
* @param parent - The parent object representing the user tag. It contains information about the tag, including its ID and parentTagId. | ||
* @returns A promise that resolves to the ordered array of ancestor tag documents found in the database. | ||
*/ | ||
export const ancestorTags: UserTagResolvers["ancestorTags"] = async ( | ||
parent, | ||
) => { | ||
// Initialize an array to collect the ancestor tags | ||
const ancestorTags: InterfaceOrganizationTagUser[] = []; | ||
|
||
// Start with the current parentTagId | ||
let currentParentId = parent.parentTagId; | ||
|
||
// Traverse up the hierarchy to find all ancestorTags | ||
while (currentParentId) { | ||
const tag = await OrganizationTagUser.findById(currentParentId).lean(); | ||
|
||
if (!tag) break; | ||
|
||
// Add the found tag to the ancestorTags array | ||
ancestorTags.push(tag); | ||
|
||
// Move up to the next parent | ||
currentParentId = tag.parentTagId; | ||
} | ||
|
||
// Reverse the ancestorTags to have the root tag first, then append the current tag | ||
ancestorTags.reverse(); | ||
|
||
return ancestorTags; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import "dotenv/config"; | ||
import type mongoose from "mongoose"; | ||
import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
import type { | ||
InterfaceOrganizationTagUser} from "../../../src/models"; | ||
import { | ||
OrganizationTagUser, | ||
} from "../../../src/models"; | ||
import { ancestorTags as ancestorTagsResolver } from "../../../src/resolvers/UserTag/ancestorTags"; | ||
import { connect, disconnect } from "../../helpers/db"; | ||
import type { TestUserTagType } from "../../helpers/tags"; | ||
import { createTwoLevelTagsWithOrg } from "../../helpers/tags"; | ||
import type { TestOrganizationType } from "../../helpers/userAndOrg"; | ||
|
||
let MONGOOSE_INSTANCE: typeof mongoose; | ||
let testRootTag: TestUserTagType, | ||
testSubTagLevel1: TestUserTagType, | ||
testSubTagLevel2: TestUserTagType; | ||
let testOrganization: TestOrganizationType; | ||
|
||
beforeAll(async () => { | ||
MONGOOSE_INSTANCE = await connect(); | ||
[, testOrganization, [testRootTag, testSubTagLevel1]] = | ||
await createTwoLevelTagsWithOrg(); | ||
|
||
testSubTagLevel2 = await OrganizationTagUser.create({ | ||
name: "testSubTagLevel2", | ||
parentTagId: testSubTagLevel1?._id, | ||
organizationId: testOrganization?._id, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await disconnect(MONGOOSE_INSTANCE); | ||
}); | ||
|
||
describe("resolvers -> Tag -> ancestorTags", () => { | ||
it(`returns the correct ancestorTags array`, async () => { | ||
const parent = testSubTagLevel2 as InterfaceOrganizationTagUser; | ||
|
||
const payload = await ancestorTagsResolver?.(parent, {}, {}); | ||
|
||
expect(payload).toEqual([testRootTag, testSubTagLevel1]); | ||
}); | ||
}); |