diff --git a/src/space/services/subspace/subspace-device.service.ts b/src/space/services/subspace/subspace-device.service.ts index 6ad6ef3..e993716 100644 --- a/src/space/services/subspace/subspace-device.service.ts +++ b/src/space/services/subspace/subspace-device.service.ts @@ -90,6 +90,17 @@ export class SubspaceDeviceService { ); } + if (!device.tag) { + const tag = this.tagRepository.create({ + tag: `Tag ${this.findNextTag()}`, + product: device.productDevice, + subspace: subspace, + device: device, + }); + await this.tagRepository.save(tag); + device.tag = tag; + } + device.subspace = subspace; const newDevice = await this.deviceRepository.save(device); @@ -139,6 +150,19 @@ export class SubspaceDeviceService { ); } + if (!device.tag) { + const tag = this.tagRepository.create({ + tag: `Tag ${this.findNextTag()}`, + product: device.productDevice, + subspace: null, + space: device.spaceDevice, + device: device, + }); + + await this.tagRepository.save(tag); + device.tag = tag; + } + device.subspace = null; const updatedDevice = await this.deviceRepository.save(device); @@ -250,4 +274,19 @@ export class SubspaceDeviceService { ); } } + + async findNextTag(): Promise { + const tags = await this.tagRepository.find({ select: ['tag'] }); + + const tagNumbers = tags + .map((t) => t.tag.match(/^Tag (\d+)$/)) + .filter((match) => match) + .map((match) => parseInt(match[1])) + .sort((a, b) => a - b); + + const nextTagNumber = tagNumbers.length + ? tagNumbers[tagNumbers.length - 1] + 1 + : 1; + return nextTagNumber; + } }