core: prevent tags with duplicate names

This commit is contained in:
Abdullah Atta
2024-03-22 10:45:22 +05:00
parent e7ee455ee3
commit a1117d77f3
2 changed files with 7 additions and 9 deletions

View File

@@ -49,19 +49,17 @@ export class Tags implements ICollection {
return this.all.find((eb) => eb.and([eb("title", "==", title)]));
}
async add(item: Partial<Tag>) {
item.title = item.title ? Tags.sanitize(item.title) : item.title;
async add(item: Partial<Tag> & { title: string }) {
item.title = Tags.sanitize(item.title);
const id = item.id || getId();
const oldTag = item.id
? await this.tag(item.id)
: item.title
? await this.find(item.title)
: undefined;
const oldTag = item.id ? await this.tag(item.id) : undefined;
if (!item.title && !oldTag?.title) throw new Error("Title is required.");
if (oldTag && item.title === oldTag.title) return oldTag.id;
if (await this.find(item.title))
throw new Error("Tag with this title already exists.");
await this.collection.upsert({
id,
dateCreated: item.dateCreated || oldTag?.dateCreated || Date.now(),

View File

@@ -21,7 +21,7 @@ import { parseHTML } from "./utils/html-parser";
import { decodeHTML5 } from "entities";
import { CURRENT_DATABASE_VERSION } from "./common";
import Database from "./api";
import { getId, makeId } from "./utils/id";
import { makeId } from "./utils/id";
import {
Color,
ContentItem,