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)]));
|
return this.all.find((eb) => eb.and([eb("title", "==", title)]));
|
||||||
}
|
}
|
||||||
|
|
||||||
async add(item: Partial<Tag>) {
|
async add(item: Partial<Tag> & { title: string }) {
|
||||||
item.title = item.title ? Tags.sanitize(item.title) : item.title;
|
item.title = Tags.sanitize(item.title);
|
||||||
|
|
||||||
const id = item.id || getId();
|
const id = item.id || getId();
|
||||||
const oldTag = item.id
|
const oldTag = item.id ? await this.tag(item.id) : undefined;
|
||||||
? await this.tag(item.id)
|
|
||||||
: item.title
|
|
||||||
? await this.find(item.title)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
if (!item.title && !oldTag?.title) throw new Error("Title is required.");
|
|
||||||
if (oldTag && item.title === oldTag.title) return oldTag.id;
|
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({
|
await this.collection.upsert({
|
||||||
id,
|
id,
|
||||||
dateCreated: item.dateCreated || oldTag?.dateCreated || Date.now(),
|
dateCreated: item.dateCreated || oldTag?.dateCreated || Date.now(),
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import { parseHTML } from "./utils/html-parser";
|
|||||||
import { decodeHTML5 } from "entities";
|
import { decodeHTML5 } from "entities";
|
||||||
import { CURRENT_DATABASE_VERSION } from "./common";
|
import { CURRENT_DATABASE_VERSION } from "./common";
|
||||||
import Database from "./api";
|
import Database from "./api";
|
||||||
import { getId, makeId } from "./utils/id";
|
import { makeId } from "./utils/id";
|
||||||
import {
|
import {
|
||||||
Color,
|
Color,
|
||||||
ContentItem,
|
ContentItem,
|
||||||
|
|||||||
Reference in New Issue
Block a user