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 2 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 @@ -149,16 +149,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
96 changes: 44 additions & 52 deletions teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import MalformedUUIDError from './uuid.error'
@Injectable()
export class MapsService {
private readonly logger = new Logger(MapsService.name)

constructor(
@InjectRepository(MmpNode)
private nodesRepository: Repository<MmpNode>,
Expand Down Expand Up @@ -87,20 +87,17 @@ export class MapsService {
return;
}

const existingNode = await this.nodesRepository.findOne({
where: { id: node.id, nodeMapId: mapId },
})
if (existingNode) return existingNode

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

try {
return this.nodesRepository.save(newNode)
} catch(error) {
this.logger.warn(`${error.constructor.name} addNode(): Failed to add node ${newNode.id}: ${error}`)
await this.nodesRepository.upsert({
...node,
nodeMapId: mapId,
}, ["id", "nodeMapId"]);

return this.nodesRepository.findOne({
where: { id: node.id, nodeMapId: mapId },
})
} catch (error) {
this.logger.error(`${error.constructor.name} - Failed to add node ${node.id}: ${error}`)
return Promise.reject(error)
}
}
Expand All @@ -122,24 +119,23 @@ export class MapsService {
return [];
}

const reducer = async (previousPromise: Promise<MmpNode[]>, node: MmpNode): Promise<MmpNode[]> => {
const accCreatedNodes = await previousPromise;
if (await this.validatesNodeParentForNode(mapId, node)) {
try {
const newNode = await this.addNode(mapId, node);
if (newNode) {
return accCreatedNodes.concat([newNode]);
}
} catch (error) {
this.logger.warn(`Failed to add node ${node.id} to map ${mapId}: ${error}`);
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);
if (newNode) {
return accCreatedNodes.concat([newNode]);
}

} catch (error) {
this.logger.warn(`Failed to add node ${node.id} to map ${mapId}: ${error}`);
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 @@ -165,25 +161,21 @@ export class MapsService {
mapId: string,
clientNode: IMmpClientNode
): Promise<MmpNode | undefined> {
const existingNode = await this.nodesRepository.findOne({
where: { nodeMapId: mapId, id: clientNode.id },
})
const mmpNode = mapClientNodeToMmpNode(clientNode, mapId);

if (!existingNode) {
this.logger.warn(`updateNode(): Existing node on server for given client node ${clientNode.id} has not been found.`);
return;
try {
await this.nodesRepository.upsert({
JannikStreek marked this conversation as resolved.
Show resolved Hide resolved
...mmpNode,
lastModified: new Date(),
}, ["id", "nodeMapId"])

return await this.nodesRepository.findOne({
where: { nodeMapId: mapId, id: clientNode.id },
})
} catch (error) {
this.logger.error(`${error.constructor.name} - Failed to update node ${clientNode.id}: ${error}`)
return Promise.reject(error)
}

try {
return this.nodesRepository.save({
...existingNode,
...mapClientNodeToMmpNode(clientNode, mapId),
lastModified: new Date(),
})
} catch(error) {
this.logger.warn(`${error.constructor.name} updateNode(): Failed to update node ${existingNode.id}: ${error}`)
return Promise.reject(error)
}
}

async removeNode(
Expand Down Expand Up @@ -212,7 +204,7 @@ export class MapsService {
)
try {
await this.nodesRepository.save(newRootNode)
} catch(error) {
} catch (error) {
this.logger.warn(`${error.constructor.name} createEmptyMap(): Failed to create root node ${newRootNode.id}: ${error}`)
return Promise.reject(error)
}
Expand Down Expand Up @@ -246,13 +238,13 @@ export class MapsService {

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) {
} catch (error) {
this.logger.warn(`${error.constructor.name} diffUpdatedCallback(): Failed to update node ${serverNode.id}: ${error}`)
return Promise.reject(error)
}
Expand All @@ -267,11 +259,11 @@ export class MapsService {
id: key,
nodeMapId: mapId,
})

if (!existingNode) {
return
}

return this.nodesRepository.remove(existingNode)
}));
}
Expand All @@ -283,7 +275,7 @@ export class MapsService {
};

const diffKeys: DiffKey[] = ["added", "updated", "deleted"];

diffKeys.forEach(key => {
const changes = diff[key];
if (changes && Object.keys(changes).length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
export const mockMmpService = {
new: jest.fn(),
addNodeImage: jest.fn(),
Expand Down
Loading