feat: better error messages
This commit is contained in:
34
server/utils/links.ts
Normal file
34
server/utils/links.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { db } from "#server/db";
|
||||
import * as tables from "#server/db/schema";
|
||||
import { and, eq, ne } from "drizzle-orm";
|
||||
|
||||
const UNIQUE_FIELDS = ["name", "path", "url"] as const;
|
||||
type UniqueField = (typeof UNIQUE_FIELDS)[number];
|
||||
|
||||
export const joinFields = (fields: string[]): string => {
|
||||
if (fields.length <= 1) return fields.join("");
|
||||
return `${fields.slice(0, -1).join(", ")} and ${fields.at(-1)}`;
|
||||
};
|
||||
|
||||
export const findConflictingFields = async (
|
||||
values: Partial<Record<UniqueField, string>>,
|
||||
excludeId?: number,
|
||||
): Promise<UniqueField[]> => {
|
||||
const conflicts: UniqueField[] = [];
|
||||
|
||||
for (const field of UNIQUE_FIELDS) {
|
||||
const value = values[field];
|
||||
if (!value) continue;
|
||||
|
||||
const conflict = await db.query.links.findFirst({
|
||||
where: and(
|
||||
eq(tables.links[field], value),
|
||||
excludeId !== undefined ? ne(tables.links.id, excludeId) : undefined,
|
||||
),
|
||||
});
|
||||
|
||||
if (conflict) conflicts.push(field);
|
||||
}
|
||||
|
||||
return conflicts;
|
||||
};
|
||||
Reference in New Issue
Block a user