feat: enhance createSport functionality with conflict handling and result management

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Jose Selesan
2026-04-23 16:46:46 -03:00
parent 20d6018836
commit 21d0c27f4d
2 changed files with 15 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
import { Prisma, Sport } from '@/generated/prisma/client';
import { Errors } from '@/lib/errors';
import { db } from '@/lib/prisma';
import { ok, Result } from '@/lib/result';
import { err, ok, Result } from '@/lib/result';
import { v7 as uuidv7 } from 'uuid';
export type CreateSportInput = {
@@ -62,7 +63,15 @@ export async function listSports(): Promise<Result<Sport[]>> {
export async function createSport(input: CreateSportInput) {
const slug = await buildUniqueSlug(input.name);
return db.sport.create({
const existing = await db.sport.count({
where: { slug },
});
if (existing > 0) {
return err(Errors.conflict('Ya existe un deporte con ese nombre.'));
}
var created = db.sport.create({
data: {
id: uuidv7(),
name: input.name.trim(),
@@ -70,6 +79,8 @@ export async function createSport(input: CreateSportInput) {
isActive: true,
},
});
return ok(created);
}
export async function getSportById(id: string) {