-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement visitor to change ids in links (#4625)
* implement a board visitor that recieves a mapping if ids, and replaces them in all linkelements in the board. * add a function to boardCopyService to use said visitor * after copying a course, the function is called for all Columnboards in the course, with the ids of all elements of the courseboard as well as the course itself
- Loading branch information
Showing
10 changed files
with
401 additions
and
9 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
130 changes: 130 additions & 0 deletions
130
...erver/src/modules/board/service/board-do-copy-service/swap-internal-links.visitor.spec.ts
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,130 @@ | ||
import { LinkElement } from '@shared/domain/domainobject'; | ||
import { EntityId } from '@shared/domain/types'; | ||
import { | ||
cardFactory, | ||
columnBoardFactory, | ||
columnFactory, | ||
externalToolElementFactory, | ||
fileElementFactory, | ||
linkElementFactory, | ||
richTextElementFactory, | ||
submissionContainerElementFactory, | ||
submissionItemFactory, | ||
} from '@shared/testing'; | ||
import { drawingElementFactory } from '@shared/testing/factory/domainobject/board/drawing-element.do.factory'; | ||
import { ObjectId } from 'bson'; | ||
import { SwapInternalLinksVisitor } from './swap-internal-links.visitor'; | ||
|
||
describe('swap internal links visitor', () => { | ||
it('should keep link unchanged', () => { | ||
const map = new Map<EntityId, EntityId>(); | ||
const linkElement = linkElementFactory.build({ url: 'testurl.dev' }); | ||
const visitor = new SwapInternalLinksVisitor(map); | ||
|
||
linkElement.accept(visitor); | ||
|
||
expect(linkElement.url).toEqual('testurl.dev'); | ||
}); | ||
|
||
const setupIdPair = () => { | ||
const originalId = new ObjectId().toString(); | ||
const newId = new ObjectId().toString(); | ||
const value = { | ||
originalId, | ||
newId, | ||
originalUrl: `testurl.dev/${originalId}`, | ||
expectedUrl: `testurl.dev/${newId}`, | ||
}; | ||
return value; | ||
}; | ||
|
||
const buildIdMap = (pairs: { originalId: string; newId: string }[]) => { | ||
const map = new Map<EntityId, EntityId>(); | ||
pairs.forEach((pair) => { | ||
map.set(pair.originalId, pair.newId); | ||
}); | ||
return map; | ||
}; | ||
|
||
const buildBoardContaining = (linkelelements: LinkElement[]) => { | ||
const cardContainingLinks = cardFactory.build({ children: linkelelements }); | ||
const submissionContainer = submissionContainerElementFactory.build({ | ||
children: [ | ||
submissionItemFactory.build({ | ||
children: [richTextElementFactory.build()], | ||
}), | ||
], | ||
}); | ||
const cardContainingOthers = cardFactory.build({ | ||
children: [ | ||
richTextElementFactory.build(), | ||
fileElementFactory.build(), | ||
submissionContainer, | ||
drawingElementFactory.build(), | ||
externalToolElementFactory.build(), | ||
], | ||
}); | ||
const column = columnFactory.build({ | ||
children: [cardContainingLinks, cardContainingOthers], | ||
}); | ||
const columnBoard = columnBoardFactory.build({ | ||
children: [column], | ||
}); | ||
return columnBoard; | ||
}; | ||
|
||
describe('when a single id is matched', () => { | ||
const setupWithIdPair = () => { | ||
const pair = setupIdPair(); | ||
const map = buildIdMap([pair]); | ||
const visitor = new SwapInternalLinksVisitor(map); | ||
|
||
return { pair, visitor }; | ||
}; | ||
|
||
it('should change ids in link', () => { | ||
const { pair, visitor } = setupWithIdPair(); | ||
const linkElement = linkElementFactory.build({ url: pair.originalUrl }); | ||
|
||
linkElement.accept(visitor); | ||
|
||
expect(linkElement.url).toEqual(pair.expectedUrl); | ||
}); | ||
|
||
it('should change ids in multiple matching links', () => { | ||
const { pair, visitor } = setupWithIdPair(); | ||
const firstLinkElement = linkElementFactory.build({ url: pair.originalUrl }); | ||
const secondLinkElement = linkElementFactory.build({ url: pair.originalUrl }); | ||
const root = buildBoardContaining([firstLinkElement, secondLinkElement]); | ||
|
||
root.accept(visitor); | ||
|
||
expect(firstLinkElement.url).toEqual(pair.expectedUrl); | ||
expect(secondLinkElement.url).toEqual(pair.expectedUrl); | ||
}); | ||
}); | ||
|
||
describe('when multiple different ids are matched', () => { | ||
const setupWithMultipleIds = () => { | ||
const pairs = [setupIdPair(), setupIdPair()]; | ||
|
||
const idMap = buildIdMap(pairs); | ||
const visitor = new SwapInternalLinksVisitor(idMap); | ||
|
||
return { visitor, pairs }; | ||
}; | ||
|
||
const buildLinkElementsWithUrls = (urls: string[]) => urls.map((url) => linkElementFactory.build({ url })); | ||
|
||
it('should change multiple ids in different links', () => { | ||
const { visitor, pairs } = setupWithMultipleIds(); | ||
const linkElements = buildLinkElementsWithUrls(pairs.map((pair) => pair.originalUrl)); | ||
const root = buildBoardContaining(linkElements); | ||
|
||
root.accept(visitor); | ||
|
||
expect(linkElements[0].url).toEqual(pairs[0].expectedUrl); | ||
expect(linkElements[1].url).toEqual(pairs[1].expectedUrl); | ||
}); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
apps/server/src/modules/board/service/board-do-copy-service/swap-internal-links.visitor.ts
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,64 @@ | ||
import { | ||
AnyBoardDo, | ||
BoardCompositeVisitor, | ||
Card, | ||
Column, | ||
ColumnBoard, | ||
LinkElement, | ||
SubmissionContainerElement, | ||
SubmissionItem, | ||
} from '@shared/domain/domainobject'; | ||
import { DrawingElement } from '@shared/domain/domainobject/board/drawing-element.do'; | ||
import { EntityId } from '@shared/domain/types'; | ||
|
||
export class SwapInternalLinksVisitor implements BoardCompositeVisitor { | ||
constructor(private readonly idMap: Map<EntityId, EntityId>) {} | ||
|
||
visitDrawingElement(drawingElement: DrawingElement): void { | ||
this.visitChildrenOf(drawingElement); | ||
} | ||
|
||
visitCard(card: Card): void { | ||
this.visitChildrenOf(card); | ||
} | ||
|
||
visitColumn(column: Column): void { | ||
this.visitChildrenOf(column); | ||
} | ||
|
||
visitColumnBoard(columnBoard: ColumnBoard): void { | ||
this.visitChildrenOf(columnBoard); | ||
} | ||
|
||
visitExternalToolElement(): void { | ||
this.doNothing(); | ||
} | ||
|
||
visitFileElement(): void { | ||
this.doNothing(); | ||
} | ||
|
||
visitLinkElement(linkElement: LinkElement): void { | ||
this.idMap.forEach((value, key) => { | ||
linkElement.url = linkElement.url.replace(key, value); | ||
}); | ||
} | ||
|
||
visitRichTextElement(): void { | ||
this.doNothing(); | ||
} | ||
|
||
visitSubmissionContainerElement(submissionContainerElement: SubmissionContainerElement): void { | ||
this.visitChildrenOf(submissionContainerElement); | ||
} | ||
|
||
visitSubmissionItem(submissionItem: SubmissionItem): void { | ||
this.visitChildrenOf(submissionItem); | ||
} | ||
|
||
private visitChildrenOf(boardDo: AnyBoardDo) { | ||
boardDo.children.forEach((child) => child.accept(this)); | ||
} | ||
|
||
private doNothing() {} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './file-name'; | ||
export * from './file-record'; | ||
export * from './path'; | ||
export * from './promise'; |
Oops, something went wrong.