mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 09:34:54 +00:00
auto generate tag if not there
This commit is contained in:
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user