auto generate tag if not there

This commit is contained in:
hannathkadher
2025-01-31 12:06:16 +04:00
parent a8f966a221
commit 96852b3d91

View File

@ -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; device.subspace = subspace;
const newDevice = await this.deviceRepository.save(device); 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; device.subspace = null;
const updatedDevice = await this.deviceRepository.save(device); const updatedDevice = await this.deviceRepository.save(device);
@ -250,4 +274,19 @@ export class SubspaceDeviceService {
); );
} }
} }
async findNextTag(): Promise<number> {
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;
}
} }