@@ -4,23 +4,37 @@ import { BaseService, ServiceValidationError } from "./baseService.server";
44
55export class PromptService extends BaseService {
66 async promoteVersion ( promptId : string , versionId : string , options ?: { sourceGuard ?: boolean } ) {
7- if ( options ?. sourceGuard ) {
8- const target = await this . _prisma . promptVersion . findUnique ( {
9- where : { id : versionId } ,
10- } ) ;
11- if ( ! target ) {
12- throw new ServiceValidationError ( "Version not found" , 404 ) ;
13- }
14- if ( target . source !== "code" ) {
15- throw new ServiceValidationError (
16- "Only code-sourced versions can be promoted. Use the override API instead." ,
17- 400
18- ) ;
19- }
7+ const target = await this . _prisma . promptVersion . findUnique ( {
8+ where : { id : versionId } ,
9+ } ) ;
10+
11+ if ( ! target ) {
12+ throw new ServiceValidationError ( "Version not found" , 404 ) ;
13+ }
14+
15+ if ( target . promptId !== promptId ) {
16+ throw new ServiceValidationError ( "Version does not belong to this prompt" , 400 ) ;
17+ }
18+
19+ if ( options ?. sourceGuard && target . source !== "code" ) {
20+ throw new ServiceValidationError (
21+ "Only code-sourced versions can be promoted. Use the override API instead." ,
22+ 400
23+ ) ;
2024 }
2125
22- await this . #removeLabel( promptId , "current" ) ;
23- await this . #addLabel( versionId , "current" ) ;
26+ await prisma . $transaction ( async ( tx ) => {
27+ await tx . $executeRaw `
28+ UPDATE "prompt_versions"
29+ SET "labels" = array_remove("labels", 'current')
30+ WHERE "promptId" = ${ promptId } AND 'current' = ANY("labels")
31+ ` ;
32+ await tx . $executeRaw `
33+ UPDATE "prompt_versions"
34+ SET "labels" = array_append("labels", 'current')
35+ WHERE "id" = ${ versionId } AND NOT ('current' = ANY("labels"))
36+ ` ;
37+ } ) ;
2438 }
2539
2640 async createOverride (
0 commit comments