mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-09-01 19:49:54 +02:00
core: prevent duplicate colors based on color code
This commit is contained in:
@@ -50,6 +50,10 @@ export class Colors implements ICollection {
|
||||
return this.collection.get(id);
|
||||
}
|
||||
|
||||
find(colorCode: string) {
|
||||
return this.all.find((eb) => eb.and([eb("colorCode", "==", colorCode)]));
|
||||
}
|
||||
|
||||
// async merge(remoteColor: MaybeDeletedItem<Color>) {
|
||||
// if (!remoteColor) return;
|
||||
|
||||
@@ -62,14 +66,22 @@ export class Colors implements ICollection {
|
||||
if (item.remote)
|
||||
throw new Error("Please use db.colors.merge to merge remote colors.");
|
||||
|
||||
const id = item.id || getId(item.dateCreated);
|
||||
const oldColor = await this.color(id);
|
||||
|
||||
item.title = item.title ? Tags.sanitize(item.title) : item.title;
|
||||
const oldColor = item.id
|
||||
? await this.color(item.id)
|
||||
: item.colorCode
|
||||
? await this.find(item.colorCode)
|
||||
: undefined;
|
||||
|
||||
if (!item.title && !oldColor?.title) throw new Error("Title is required.");
|
||||
if (!item.colorCode && !oldColor?.colorCode)
|
||||
throw new Error("Color code is required.");
|
||||
|
||||
const id =
|
||||
oldColor && item.colorCode === oldColor.colorCode
|
||||
? oldColor.id
|
||||
: item.id || getId(item.dateCreated);
|
||||
|
||||
await this.collection.upsert({
|
||||
id,
|
||||
dateCreated: item.dateCreated || oldColor?.dateCreated || Date.now(),
|
||||
|
||||
Reference in New Issue
Block a user