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

Make updateNode more robust #603

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 2 additions & 7 deletions teammapper-backend/src/map/services/maps.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,11 @@ describe('MapsController', () => {
const clientNode = mapMmpNodeToClient(node)
clientNode.name = 'new'

// we save the time before the update to be able to compare the lastModified date and make sure it's newer than this:
const timeBeforeUpdate = new Date()
await mapsService.updateNode(map.id, clientNode)
const updatedNode = await nodesRepo.findOne({
where: { id: node.id },
})
const updatedNode = await mapsService.updateNode(map.id, clientNode)

expect(updatedNode?.lastModified).not.toEqual(oldDate)
expect(updatedNode?.lastModified!.getTime()).toBeGreaterThan(
timeBeforeUpdate.getTime()
oldDate.getTime()
)
})
})
Expand Down
148 changes: 68 additions & 80 deletions teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,20 @@ export class MapsService {
return
}

const existingNode = await this.nodesRepository.findOne({
where: { id: node.id, nodeMapId: mapId },
})
if (existingNode) return existingNode
try {
await this.nodesRepository.upsert({
...node,
nodeMapId: mapId,
}, ["id", "nodeMapId"]);

const newNode = this.nodesRepository.create({
...node,
nodeMapId: mapId,
})
const newNode = await this.nodesRepository.findOne({
where: { id: node.id, nodeMapId: mapId },
})

try {
return this.nodesRepository.save(newNode)
if (!newNode) return;
return newNode;
} catch (error) {
this.logger.warn(
`${error.constructor.name} addNode(): Failed to add node ${newNode.id}: ${error}`
)
this.logger.error(`${error.constructor.name} - Failed to add node ${node.id}: ${error}`)
return Promise.reject(error)
}
}
Expand All @@ -128,31 +126,23 @@ export class MapsService {
return []
}

const reducer = async (
previousPromise: Promise<MmpNode[]>,
node: MmpNode
): Promise<MmpNode[]> => {
const accCreatedNodes = await previousPromise
const reducer = async (previousPromise: Promise<MmpNode[]>, node: MmpNode): Promise<MmpNode[]> => {
const accCreatedNodes = await previousPromise;
if (await this.validatesNodeParentForNode(mapId, node)) {
JannikStreek marked this conversation as resolved.
Show resolved Hide resolved
try {
const newNode = await this.addNode(mapId, node)
const newNode = await this.addNode(mapId, node);
if (newNode) {
return accCreatedNodes.concat([newNode])
return accCreatedNodes.concat([newNode]);
}
} catch (error) {
this.logger.warn(
`Failed to add node ${node.id} to map ${mapId}: ${error}`
)
this.logger.warn(`Failed to add node ${node.id} to map ${mapId}: ${error}`);
return accCreatedNodes;
}

return accCreatedNodes
}

this.logger.warn(
`Parent with id ${node.nodeParentId} does not exist for node ${node.id} and map ${mapId}`
)
return accCreatedNodes
}
this.logger.warn(`Parent with id ${node.nodeParentId} does not exist for node ${node.id} and map ${mapId}`);
return accCreatedNodes;
};

return nodes.reduce(reducer, Promise.resolve(new Array<MmpNode>()))
}
Expand All @@ -177,27 +167,22 @@ export class MapsService {
mapId: string,
clientNode: IMmpClientNode
): Promise<MmpNode | undefined> {
const existingNode = await this.nodesRepository.findOne({
where: { nodeMapId: mapId, id: clientNode.id },
})

if (!existingNode) {
this.logger.warn(
`updateNode(): Existing node on server for given client node ${clientNode.id} has not been found.`
)
return
}
const mmpNode = mapClientNodeToMmpNode(clientNode, mapId);

try {
return this.nodesRepository.save({
...existingNode,
...mapClientNodeToMmpNode(clientNode, mapId),
await this.nodesRepository.upsert({
JannikStreek marked this conversation as resolved.
Show resolved Hide resolved
...mmpNode,
lastModified: new Date(),
}, ["id", "nodeMapId"])

const updatedNode = await this.nodesRepository.findOne({
where: { nodeMapId: mapId, id: clientNode.id },
})

if (!updatedNode) return;
return updatedNode;
} catch (error) {
this.logger.warn(
`${error.constructor.name} updateNode(): Failed to update node ${existingNode.id}: ${error}`
)
this.logger.error(`${error.constructor.name} - Failed to update node ${clientNode.id}: ${error}`)
return Promise.reject(error)
}
}
Expand Down Expand Up @@ -229,9 +214,7 @@ export class MapsService {
try {
await this.nodesRepository.save(newRootNode)
} catch (error) {
this.logger.warn(
`${error.constructor.name} createEmptyMap(): Failed to create root node ${newRootNode.id}: ${error}`
)
this.logger.warn(`${error.constructor.name} createEmptyMap(): Failed to create root node ${newRootNode.id}: ${error}`)
return Promise.reject(error)
}
}
Expand Down Expand Up @@ -265,32 +248,37 @@ export class MapsService {
) => {
await Promise.all(
Object.keys(diff).map(async (key) => {
const clientNode = diff[key]

if (clientNode) {
const serverNode = await this.nodesRepository.findOne({
where: { nodeMapId: mapId, id: key },
})

if (serverNode) {
const mergedNode = mergeClientNodeIntoMmpNode(
clientNode,
serverNode
)
Object.assign(serverNode, mergedNode)
try {
await this.nodesRepository.save(serverNode)
} catch (error) {
this.logger.warn(
`${error.constructor.name} diffUpdatedCallback(): Failed to update node ${serverNode.id}: ${error}`
)
return Promise.reject(error)
}
}
const clientNode = diff[key];

if (!clientNode) {
return;
}

const serverNode = await this.nodesRepository.findOne({
where: {
nodeMapId: mapId,
id: key
}
});

if (!serverNode) {
return;
}

const mergedNode = mergeClientNodeIntoMmpNode(clientNode, serverNode);
Object.assign(serverNode, mergedNode);

try {
await this.nodesRepository.save(serverNode);
} catch (error) {
this.logger.warn(
`${error.constructor.name} diffUpdatedCallback(): Failed to update node ${serverNode.id}: ${error}`
);
return Promise.reject(error);
}
})
)
}
);
};

const diffDeletedCallback: DiffCallback = async (
diff: IMmpClientSnapshotChanges
Expand All @@ -300,16 +288,16 @@ export class MapsService {
const existingNode = await this.nodesRepository.findOneBy({
id: key,
nodeMapId: mapId,
})

});
if (!existingNode) {
return
return;
}

return this.nodesRepository.remove(existingNode)
return this.nodesRepository.remove(existingNode);
})
)
}
);
};

const callbacks: Record<keyof IMmpClientMapDiff, DiffCallback> = {
added: diffAddedCallback,
Expand Down
Loading