Skip to content

Commit

Permalink
[ADMIN] Better error handling when creating duplicate collaborators
Browse files Browse the repository at this point in the history
  • Loading branch information
samtgarson committed Apr 26, 2024
1 parent a8f261b commit 546ed5e
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions admin/lib/collections/books.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { inngest } from '@books-about-food/core/jobs'
import { fetchProfiles } from '@books-about-food/core/services/profiles/fetch-profiles'
import { AppError } from '@books-about-food/core/services/utils/errors'
import prisma, { Prisma } from '@books-about-food/database'
import { appUrl } from '@books-about-food/shared/utils/app-url'
import { imageUrl } from '@books-about-food/shared/utils/image-url'
Expand Down Expand Up @@ -318,16 +319,28 @@ export const customiseBooks = (
profileId = profileIdOrName
}

await prisma.contribution.create({
data: {
bookId,
profileId,
jobId,
tag
}
})
try {
await prisma.contribution.create({
data: {
bookId,
profileId,
jobId,
tag
}
})

return result.success('🎉 Collaborator added')
return result.success('🎉 Collaborator added')
} catch (e) {
const parsed = AppError.fromError(e)
switch (parsed.type) {
case 'UniqueConstraintViolation':
return result.error(
'This collaborator is already added in that role'
)
default:
return result.error('Something went wrong, devs have been notified')
}
}
}
})

Expand Down

0 comments on commit 546ed5e

Please sign in to comment.