Skip to content
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

refactor async node creation #256

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 29 additions & 35 deletions teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@ export class MapsService {
clientNode: IMmpClientNode
): Promise<MmpNode[]> => {
const accCreatedNodes = await previousPromise
if (
clientNode.parent &&
!(await this.nodesRepository.exist({
where: { id: clientNode.parent, nodeMapId: mapId },
}))
) {
this.logger.warn(
`Parent with id ${clientNode.parent} does not exist for node ${clientNode.id} and map ${mapId}`
)
return accCreatedNodes

if (await this.validatesNodeParentForClientNode(mapId, clientNode)) {
return accCreatedNodes.concat([await this.addNode(mapId, clientNode)])
}
return accCreatedNodes.concat([await this.addNode(mapId, clientNode)])

this.logger.warn(
`Parent with id ${clientNode.parent} does not exist for node ${clientNode.id} and map ${mapId}`
)
this.logger.warn(clientNodes.map((node) => [node.id, node.parent]))
return accCreatedNodes
}

return clientNodes.reduce(reducer, Promise.resolve(new Array<MmpNode>()))
Expand All @@ -100,6 +98,14 @@ export class MapsService {
.getMany()
}

async existsNode(mapId: string, parentId: string): Promise<boolean> {
if (!mapId || !parentId) return false

return await this.nodesRepository.exist({
where: { id: parentId, nodeMapId: mapId },
})
}

async updateNode(
mapId: string,
clientNode: IMmpClientNode
Expand Down Expand Up @@ -149,30 +155,7 @@ export class MapsService {
// remove existing nodes, otherwise we will end up with multiple roots
await this.nodesRepository.delete({ nodeMapId: clientMap.uuid })
// Add new nodes from given map
// Reduce is used in conjunction with a promise to keep the order of creation.
// Otherwise there will be FK violations
await clientMap.data.reduce(
async (promise: Promise<MmpNode>, node: IMmpClientNode) => {
await promise
// check if parent actually exists - if not skip node, otherwise FK violations will be thrown
if (
node.parent &&
!(await this.nodesRepository.exist({
where: { id: node.parent, nodeMapId: clientMap.uuid },
}))
) {
this.logger.warn(
`Parent with id ${node.parent} does not exist for node ${node.id} and map ${clientMap.uuid}`
)
return
}
return await this.nodesRepository.save(
mapClientNodeToMmpNode(node, clientMap.uuid)
)
},
Promise.resolve(new MmpNode())
)

await this.addNodes(clientMap.uuid, clientMap.data)
// reload map
return this.findMap(clientMap.uuid)
}
Expand Down Expand Up @@ -266,4 +249,15 @@ export class MapsService {
deleteMap(uuid: string) {
this.mapsRepository.delete({ id: uuid })
}

async validatesNodeParentForClientNode(
mapId: string,
node: IMmpClientNode
): Promise<boolean> {
return (
node.isRoot ||
node.detached ||
(node.parent && (await this.existsNode(mapId, node.parent)))
)
}
}
1 change: 1 addition & 0 deletions teammapper-backend/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ describe('AppController (e2e)', () => {
font: {},
colors: {},
link: {},
detached: true
},
],
})
Expand Down
Loading