mirror of
https://github.com/documenso/documenso.git
synced 2026-07-26 01:45:08 +10:00
fix: handle duplicate organization URL update errors gracefully (#2808)
This commit is contained in:
@@ -3,6 +3,8 @@ import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|||||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
import { stripe } from '@documenso/lib/server-only/stripe';
|
||||||
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
import { Prisma } from '@prisma/client';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { authenticatedProcedure } from '../trpc';
|
import { authenticatedProcedure } from '../trpc';
|
||||||
import { ZUpdateOrganisationRequestSchema, ZUpdateOrganisationResponseSchema } from './update-organisation.types';
|
import { ZUpdateOrganisationRequestSchema, ZUpdateOrganisationResponseSchema } from './update-organisation.types';
|
||||||
@@ -36,15 +38,33 @@ export const updateOrganisationRoute = authenticatedProcedure
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedOrganisation = await prisma.organisation.update({
|
const updatedOrganisation = await prisma.organisation
|
||||||
where: {
|
.update({
|
||||||
id: organisationId,
|
where: {
|
||||||
},
|
id: organisationId,
|
||||||
data: {
|
},
|
||||||
name: data.name,
|
data: {
|
||||||
url: data.url,
|
name: data.name,
|
||||||
},
|
url: data.url,
|
||||||
});
|
},
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
if (!(err instanceof Prisma.PrismaClientKnownRequestError)) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
const target = z.array(z.string()).safeParse(err.meta?.target);
|
||||||
|
|
||||||
|
if (err.code === 'P2002' && target.success && target.data.includes('url')) {
|
||||||
|
throw new AppError(AppErrorCode.ALREADY_EXISTS, {
|
||||||
|
message: 'Organisation URL already exists.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
|
||||||
if (updatedOrganisation.customerId) {
|
if (updatedOrganisation.customerId) {
|
||||||
await stripe.customers.update(updatedOrganisation.customerId, {
|
await stripe.customers.update(updatedOrganisation.customerId, {
|
||||||
|
|||||||
Reference in New Issue
Block a user