mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
core: prevent tags with duplicate names
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user