-
-
Notifications
You must be signed in to change notification settings - Fork 19
refactor: optimize history module architecture and fix pagination #2189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
caneppelevitor
merged 14 commits into
stage
from
adding-support-for-external-identifiers-to-history-module
Feb 5, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4a63929
refactor: optimize history module architecture and fix pagination
LuizFNJ 389f221
finishing some types and correcting sonar cloud comments
LuizFNJ 4e9373e
feat: add migration to convert string user IDs to ObjectId in history…
LuizFNJ b3dc400
correct name and import correctly
LuizFNJ 9b25587
adding specific typing for users and improving logic for saving inter…
LuizFNJ 3544e1d
add migration flag to revert only changed users
LuizFNJ 2777140
adding a lookup conditional that runs only when user is objectid
LuizFNJ ea57f73
adding type and conditional safety to getDescriptionForHide
LuizFNJ bc8df79
refactor: improve types and add validation for targetId and targetModel
LuizFNJ 7d558cc
test: add unit tests for history service and controller with mocks
LuizFNJ a3c2b36
fix: update chatbot user payload to use clientId and add frontend typ…
LuizFNJ 14955b7
feat(migration): convert history user strings to M2M objects
LuizFNJ 226ca00
fix imports
LuizFNJ 9b27640
unifying M2M types
LuizFNJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
migrations/20260117122748-change-history-user-strings-to-objectid.ts
This file contains hidden or 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,64 @@ | ||
| import { Db, ObjectId } from "mongodb"; | ||
|
|
||
| const HEX24 = /^[0-9a-fA-F]{24}$/; | ||
|
|
||
| export async function up(db: Db) { | ||
| try { | ||
| const historiesToChange = await db | ||
| .collection("histories") | ||
| .find({ user: { $type: "string", $regex: HEX24 } }) | ||
| .toArray(); | ||
|
|
||
| if (historiesToChange.length === 0) { | ||
| console.log("No histories with string users to change."); | ||
| } else { | ||
| const bulkOps = historiesToChange.map((history) => ({ | ||
| updateOne: { | ||
| filter: { _id: history._id }, | ||
| update: { | ||
| $set: { | ||
| user: new ObjectId(history.user as string), | ||
| migration_revert_flag: true, | ||
| }, | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| const result = await db.collection("histories").bulkWrite(bulkOps); | ||
| console.log(`Converted ${result.modifiedCount} history.user fields to ObjectId.`); | ||
| } | ||
| } catch (error) { | ||
| console.error("Migration UP failed:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export async function down(db: Db) { | ||
| try { | ||
| const historiesToRedefine = await db | ||
| .collection("histories") | ||
| .find({ migration_revert_flag: true }) | ||
| .toArray(); | ||
|
|
||
| if (historiesToRedefine.length === 0) return; | ||
|
|
||
| const bulkOps = historiesToRedefine.map((history) => { | ||
| const userIdString = history.user ? String(history.user) : null; | ||
|
|
||
| return { | ||
| updateOne: { | ||
| filter: { _id: history._id }, | ||
| update: { $set: { user: userIdString } }, | ||
| $unset: { migration_revert_flag: "" }, | ||
| }, | ||
| }; | ||
| }) | ||
|
|
||
| const result = await db.collection("histories").bulkWrite(bulkOps); | ||
| console.log(`Reverted ${result.modifiedCount} fields back to strings.`); | ||
|
|
||
| } catch (error) { | ||
| console.error("Migration DOWN failed:", error); | ||
| throw error; | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
migrations/20260124140000-migrate-history-user-strings-to-m2m-object.ts
This file contains hidden or 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,62 @@ | ||
| import { Db } from "mongodb"; | ||
|
|
||
| export async function up(db: Db) { | ||
| const historyCollection = db.collection("histories"); | ||
|
|
||
| const historiesFound = historyCollection.find({ | ||
| user: { $type: "string", $regex: "-" }, | ||
| }); | ||
|
|
||
| let count = 0; | ||
|
|
||
| while (await historiesFound.hasNext()) { | ||
| const history = await historiesFound.next(); | ||
| if (!history) continue; | ||
| const oldUserValue = history.user as string; | ||
|
|
||
| const newUser = { | ||
| isM2M: true, | ||
| clientId: oldUserValue, | ||
| subject: "chatbot-service", | ||
| scopes: ["read", "write"], | ||
| role: { main: "integration" }, | ||
| namespace: "main", | ||
| }; | ||
|
|
||
| await historyCollection.updateOne( | ||
| { _id: history._id }, | ||
| { $set: { user: newUser } } | ||
| ); | ||
|
|
||
| count++; | ||
| } | ||
|
|
||
| console.log(`Migration complete. Updated ${count} documents.`); | ||
| } | ||
|
|
||
| export async function down(db: Db) { | ||
| const historyCollection = db.collection("histories"); | ||
|
|
||
| const historiesFound = historyCollection.find({ | ||
| "user.isM2M": true, | ||
| "user.subject": "chatbot-service", | ||
| "user.clientId": { $exists: true } | ||
| }); | ||
|
|
||
| let count = 0; | ||
|
|
||
| while (await historiesFound.hasNext()) { | ||
| const history = await historiesFound.next(); | ||
| if (!history) continue; | ||
| const clientId = history.user.clientId; | ||
|
|
||
| await historyCollection.updateOne( | ||
| { _id: history._id }, | ||
| { $set: { user: clientId } } | ||
| ); | ||
|
|
||
| count++; | ||
| } | ||
|
|
||
| console.log(`Rollback complete. Reverted ${count} documents.`); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| import { Roles } from "../auth/ability/ability.factory"; | ||
| export class M2M { | ||
| isM2M: boolean; | ||
| role: { | ||
| main: string; | ||
| }; | ||
| scopes: string[]; | ||
| isM2M: boolean; | ||
| clientId: string; | ||
| subject: string; | ||
| scopes: string[]; | ||
| role: { | ||
| main: Roles.Integration; | ||
| }; | ||
| namespace: string; | ||
| } |
This file contains hidden or 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,56 @@ | ||
| import { Test } from "@nestjs/testing"; | ||
| import { HistoryController } from "./history.controller"; | ||
| import { HistoryService } from "./history.service"; | ||
| import { historyServiceMock, mockHistoryItem } from "../mocks/HistoryMock"; | ||
| import { TargetModel } from "./schema/history.schema"; | ||
|
|
||
| describe("HistoryController (Unit)", () => { | ||
| let controller: HistoryController; | ||
| let historyService: typeof historyServiceMock; | ||
|
|
||
| beforeEach(async () => { | ||
| const testingModule = await Test.createTestingModule({ | ||
| controllers: [HistoryController], | ||
| providers: [{ provide: HistoryService, useValue: historyServiceMock }], | ||
| }).compile(); | ||
|
|
||
| controller = testingModule.get(HistoryController); | ||
| historyService = testingModule.get(HistoryService); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("getHistory", () => { | ||
| it("should return history correctly (happy path)", async () => { | ||
| historyService.getHistoryForTarget.mockResolvedValue({ | ||
| history: [mockHistoryItem], | ||
| totalChanges: 1, | ||
| totalPages: 1, | ||
| page: 1, | ||
| pageSize: 10, | ||
| }); | ||
|
|
||
| const response = await controller.getHistory( | ||
| { targetId: "id", targetModel: TargetModel.Claim }, | ||
| {} | ||
| ); | ||
| expect(response.history.length).toBeGreaterThan(0); | ||
| expect(response.totalChanges).toBeGreaterThanOrEqual(1); | ||
| }); | ||
|
|
||
| it("should throw error if targetId is empty", async () => { | ||
| await expect( | ||
| controller.getHistory({ targetId: "", targetModel: TargetModel.Claim }, {}) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it("should throw error if service fails", async () => { | ||
| historyService.getHistoryForTarget.mockRejectedValue(new Error("fail")); | ||
| await expect( | ||
| controller.getHistory({ targetId: "id", targetModel: TargetModel.Claim }, {}) | ||
| ).rejects.toThrow("fail"); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.