fix: prevent creation project with same name
This commit is contained in:
parent
f66f0d5e01
commit
6d958d817a
|
|
@ -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({
|
||||
userId,
|
||||
page,
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ import {
|
|||
} from '@remix-run/react';
|
||||
import * as React from 'react';
|
||||
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';
|
||||
|
||||
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({
|
||||
name,
|
||||
description,
|
||||
|
|
|
|||
Loading…
Reference in a new issue