11import type { Prisma , PrismaClient } from "../../../generated/prisma/client.ts"
2+ import { classifyPrismaTransactionError } from "../../../integrations/prisma/classify-transaction-error/index.ts"
23import { createReviewCommentRepository } from "../../../repositories/review-comments/create-review-comment-repository/index.ts"
34import type { ReviewPersistenceFailure } from "../../../repositories/review-comments/types.ts"
5+ import { runWithBoundedRetries } from "../../identity/run-with-bounded-retries/index.ts"
46import { ReviewCommentError } from "../errors.ts"
57import { parseReviewAnchor } from "../parse-review-anchor/index.ts"
68import { parseReviewBody } from "../parse-review-body/index.ts"
@@ -38,6 +40,20 @@ function persistenceError(reason: ReviewPersistenceFailure): ReviewCommentError
3840 }
3941}
4042
43+ async function runReviewMutation < Result > ( operation : ( ) => Promise < Result > ) : Promise < Result > {
44+ try {
45+ return await runWithBoundedRetries (
46+ ( ) => operation ( ) ,
47+ ( error ) => classifyPrismaTransactionError ( error ) === "retryable" ,
48+ )
49+ } catch ( error ) {
50+ if ( classifyPrismaTransactionError ( error ) === "retryable" ) {
51+ throw new ReviewCommentError ( "conflict" , "The review thread changed. Try again." )
52+ }
53+ throw error
54+ }
55+ }
56+
4157export function createReviewCommentService (
4258 database : Pick < PrismaClient , "$transaction" > ,
4359) : ReviewCommentService {
@@ -49,37 +65,33 @@ export function createReviewCommentService(
4965 return result . value . map ( serializeReviewThread )
5066 } ,
5167 async createThread ( input ) {
52- const result = await repository . createThread ( {
53- ... input ,
54- body : parseReviewBody ( input . body ) ,
55- anchor : parseReviewAnchor ( input . anchor ) as unknown as Prisma . InputJsonValue ,
56- } )
68+ const body = parseReviewBody ( input . body )
69+ const anchor = parseReviewAnchor ( input . anchor ) as unknown as Prisma . InputJsonValue
70+ const result = await runReviewMutation ( ( ) =>
71+ repository . createThread ( { ... input , body , anchor } ) ,
72+ )
5773 if ( ! result . ok ) throw persistenceError ( result . reason )
5874 return serializeReviewThread ( result . value )
5975 } ,
6076 async createReply ( input ) {
61- const result = await repository . createReply ( {
62- ...input ,
63- body : parseReviewBody ( input . body ) ,
64- } )
77+ const body = parseReviewBody ( input . body )
78+ const result = await runReviewMutation ( ( ) => repository . createReply ( { ...input , body } ) )
6579 if ( ! result . ok ) throw persistenceError ( result . reason )
6680 return serializeReviewThread ( result . value )
6781 } ,
6882 async editComment ( input ) {
69- const result = await repository . editComment ( {
70- ...input ,
71- body : parseReviewBody ( input . body ) ,
72- } )
83+ const body = parseReviewBody ( input . body )
84+ const result = await runReviewMutation ( ( ) => repository . editComment ( { ...input , body } ) )
7385 if ( ! result . ok ) throw persistenceError ( result . reason )
7486 return serializeReviewThread ( result . value )
7587 } ,
7688 async deleteComment ( input ) {
77- const result = await repository . deleteComment ( input )
89+ const result = await runReviewMutation ( ( ) => repository . deleteComment ( input ) )
7890 if ( ! result . ok ) throw persistenceError ( result . reason )
7991 return serializeReviewThread ( result . value )
8092 } ,
8193 async setResolution ( input ) {
82- const result = await repository . setResolution ( input )
94+ const result = await runReviewMutation ( ( ) => repository . setResolution ( input ) )
8395 if ( ! result . ok ) throw persistenceError ( result . reason )
8496 return serializeReviewThread ( result . value )
8597 } ,
0 commit comments