import { Types } from "mongoose"; import { Product } from "@/lib/server/models/Product"; /** * Two products with the same name (ignoring case) would be impossible to * tell apart on the terminal and in reports, so names are unique per * business — including deactivated products, which can be reactivated. */ export async function labelTakenError( businessId: Types.ObjectId | string, label: string, exceptId?: string ): Promise { const filter: Record = { businessId, label: label.trim() }; if (exceptId) filter._id = { $ne: exceptId }; const clash = await Product.findOne(filter).collation({ locale: "en", strength: 2 }).select("label isActive").lean(); if (!clash) return null; return clash.isActive ? `A product called "${clash.label}" already exists.` : `A deactivated product called "${clash.label}" already exists. Reactivate it instead.`; }