web: delete dashboards

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-12-12 21:55:42 +01:00
parent 61561e65c9
commit fc141a80e0
9 changed files with 129 additions and 31 deletions

View File

@@ -3,6 +3,8 @@ import { db } from '@/server/db';
import { getDashboardBySlug } from '@/server/services/dashboard.service';
import { getProjectBySlug } from '@/server/services/project.service';
import { slug } from '@/utils/slug';
import { Prisma } from '@prisma/client';
import { PrismaError } from 'prisma-error-enum';
import { z } from 'zod';
export const dashboardRouter = createTRPCRouter({
@@ -58,4 +60,30 @@ export const dashboardRouter = createTRPCRouter({
},
});
}),
delete: protectedProcedure
.input(
z.object({
id: z.string(),
})
)
.mutation(async ({ input: { id } }) => {
try {
await db.dashboard.delete({
where: {
id,
},
});
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
switch (error.code) {
case PrismaError.ForeignConstraintViolation:
throw new Error(
'Cannot delete dashboard with associated reports'
);
default:
throw new Error('Unknown error deleting dashboard');
}
}
}
}),
});