core: prevent duplicate colors based on color code

This commit is contained in:
Abdullah Atta
2023-11-24 10:29:59 +05:00
parent 0febeec85f
commit 7fce76017d

View File

@@ -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(),