Skip to content

Commit

Permalink
refactorings, e.g. nodes creation in backend (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek authored Feb 1, 2024
1 parent 97aaa73 commit 9f13e84
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
4 changes: 3 additions & 1 deletion teammapper-backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import configService from './config.service'
import { createProxyMiddleware } from 'http-proxy-middleware'

async function bootstrap() {
const app = await NestFactory.create(AppModule)
const app = await NestFactory.create(AppModule, {
logger: ['error', 'warn'],
})

app.use(
'/arasaac/api',
Expand Down
34 changes: 29 additions & 5 deletions teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'
import { Injectable, Logger } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Brackets } from 'typeorm'
import { MmpMap } from '../entities/mmpMap.entity'
Expand All @@ -18,6 +18,8 @@ import configService from '../../config.service'

@Injectable()
export class MapsService {
private readonly logger = new Logger(MapsService.name)

constructor(
@InjectRepository(MmpNode)
private nodesRepository: Repository<MmpNode>,
Expand Down Expand Up @@ -73,6 +75,17 @@ 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
}
return accCreatedNodes.concat([await this.addNode(mapId, clientNode)])
}

Expand Down Expand Up @@ -135,18 +148,29 @@ export class MapsService {
async updateMap(clientMap: IMmpClientMap): Promise<MmpMap> {
// 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<any>, node: IMmpClientNode) => {
async (promise: Promise<MmpNode>, node: IMmpClientNode) => {
await promise
await this.nodesRepository.save(
// 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()
Promise.resolve(new MmpNode())
)

// reload map
Expand Down
3 changes: 1 addition & 2 deletions teammapper-frontend/mmp/src/map/handlers/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import Node, {
NodeProperties,
UserNodeProperties
} from '../models/node'
import MmpMap, { DomElements } from '../map'
import MmpMap from '../map'
import * as d3 from 'd3'
import DOMPurify from 'dompurify'
import { v4 as uuidv4 } from 'uuid'
import {Event} from './events'
import Log from '../../utils/log'
import Utils from '../../utils/utils'
import Export from './export'

/**
* Manage the nodes of the map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
ExportNodeProperties,
MapCreateEvent,
MapProperties,
MapSnapshot,
NodeUpdateEvent,
} from '@mmp/map/types';
import {
Expand Down Expand Up @@ -255,7 +254,7 @@ export class MapSyncService implements OnDestroy {
});
}

public updateMap(_oldMapData?: MapSnapshot) {
public updateMap() {
const cachedMapEntry: CachedMapEntry = this.getAttachedMap();
this.socket.emit('updateMap', {
mapId: cachedMapEntry.cachedMap.uuid,
Expand Down Expand Up @@ -306,8 +305,8 @@ export class MapSyncService implements OnDestroy {
});
}

private async checkModificationSecret() {
await this.socket.emit(
private checkModificationSecret() {
this.socket.emit(
'checkModificationSecret',
{
mapId: this.getAttachedMap().cachedMap.uuid,
Expand Down Expand Up @@ -508,11 +507,11 @@ export class MapSyncService implements OnDestroy {

private createMapListeners() {
// create is NOT called by the mmp lib for initial map load / and call, but for _imported_ maps
this.mmpService.on('create').subscribe((result: MapCreateEvent) => {
this.mmpService.on('create').subscribe((_result: MapCreateEvent) => {
this.attachedNodeSubject.next(this.mmpService.selectNode());

this.updateAttachedMap();
this.updateMap(result.previousMapData);
this.updateMap();
});

this.mmpService
Expand Down

0 comments on commit 9f13e84

Please sign in to comment.