fix: prevent creation project with same name

This commit is contained in:
Nicola Zambello 2023-02-18 23:15:58 +01:00
parent f66f0d5e01
commit 6d958d817a
Signed by: nzambello
GPG key ID: 56E4A92C2C1E50BA
2 changed files with 30 additions and 1 deletions

View file

@ -15,6 +15,17 @@ export function getProject({
}); });
} }
export function getProjectByName({
name,
userId
}: Pick<Project, 'name'> & {
userId: User['id'];
}) {
return prisma.project.findFirst({
where: { name, userId }
});
}
export async function getProjects({ export async function getProjects({
userId, userId,
page, page,

View file

@ -20,7 +20,11 @@ import {
} from '@remix-run/react'; } from '@remix-run/react';
import * as React from 'react'; import * as React from 'react';
import { AlertTriangle, RefreshCcw, Save } from 'react-feather'; import { AlertTriangle, RefreshCcw, Save } from 'react-feather';
import { createProject, Project } from '~/models/project.server'; import {
createProject,
getProjectByName,
Project
} from '~/models/project.server';
import { requireUserId } from '~/session.server'; import { requireUserId } from '~/session.server';
export const meta: MetaFunction = () => { export const meta: MetaFunction = () => {
@ -81,6 +85,20 @@ export async function action({ request }: ActionArgs) {
); );
} }
const projectWithSameName = await getProjectByName({ name, userId });
if (projectWithSameName) {
return json(
{
errors: {
name: 'A project with this name already exists',
description: null,
color: null
}
},
{ status: 409 }
);
}
const project = await createProject({ const project = await createProject({
name, name,
description, description,