Changed labels to be unique on the same instance

This commit is contained in:
Judson Cairo 2024-08-12 21:35:31 -03:00
parent dbb6ea9147
commit 48febec36a
3 changed files with 32 additions and 10 deletions

View File

@ -0,0 +1,11 @@
/*
Warnings:
- A unique constraint covering the columns `[labelId,instanceId]` on the table `Label` will be added. If there are existing duplicate values, this will fail.
*/
-- DropIndex
DROP INDEX "Label_labelId_key";
-- CreateIndex
CREATE UNIQUE INDEX "Label_labelId_instanceId_key" ON "Label"("labelId", "instanceId");

View File

@ -216,7 +216,7 @@ model Chatwoot {
model Label { model Label {
id String @id @default(cuid()) id String @id @default(cuid())
labelId String? @unique @db.VarChar(100) labelId String? @db.VarChar(100)
name String @db.VarChar(100) name String @db.VarChar(100)
color String @db.VarChar(100) color String @db.VarChar(100)
predefinedId String? @db.VarChar(100) predefinedId String? @db.VarChar(100)
@ -224,6 +224,8 @@ model Label {
updatedAt DateTime @updatedAt @db.Timestamp updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
@@unique([labelId, instanceId])
} }
model Proxy { model Proxy {

View File

@ -1455,7 +1455,7 @@ export class BaileysStartupService extends ChannelStartupService {
const savedLabel = labelsRepository.find((l) => l.labelId === label.id); const savedLabel = labelsRepository.find((l) => l.labelId === label.id);
if (label.deleted && savedLabel) { if (label.deleted && savedLabel) {
await this.prismaRepository.label.delete({ await this.prismaRepository.label.delete({
where: { instanceId: this.instanceId, labelId: label.id }, where: { labelId_instanceId: { instanceId: this.instanceId, labelId: label.id } },
}); });
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name }); this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
return; return;
@ -1463,16 +1463,25 @@ export class BaileysStartupService extends ChannelStartupService {
const labelName = label.name.replace(/[^\x20-\x7E]/g, ''); const labelName = label.name.replace(/[^\x20-\x7E]/g, '');
if (!savedLabel || savedLabel.color !== `${label.color}` || savedLabel.name !== labelName) { if (!savedLabel || savedLabel.color !== `${label.color}` || savedLabel.name !== labelName) {
if (this.configService.get<Database>('DATABASE').SAVE_DATA.LABELS) if (this.configService.get<Database>('DATABASE').SAVE_DATA.LABELS) {
await this.prismaRepository.label.create({ const labelData = {
data: { color: `${label.color}`,
color: `${label.color}`, name: labelName,
name: labelName, labelId: label.id,
labelId: label.id, predefinedId: label.predefinedId,
predefinedId: label.predefinedId, instanceId: this.instanceId,
instanceId: this.instanceId, };
await this.prismaRepository.label.upsert({
where: {
labelId_instanceId: {
instanceId: labelData.instanceId,
labelId: labelData.labelId,
},
}, },
update: labelData,
create: labelData,
}); });
}
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name }); this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
} }
}, },