@@ -29,6 +29,8 @@ import { parseServiceRevisionSpec } from "@/lib/service-revision-changes";
2929
3030const MAX_CANVAS_COORDINATE = 2_147_483_647 ;
3131
32+ class CanvasServiceNotFoundError extends Error { }
33+
3234const canvasPositionsSchema = z . object ( {
3335 positions : z
3436 . array (
@@ -64,66 +66,70 @@ export async function PATCH(
6466 return Response . json ( { error : "Invalid positions" } , { status : 400 } ) ;
6567 }
6668
67- const savedPositions = await db . transaction ( async ( tx ) => {
68- const serviceIds = parsed . data . positions . map (
69- ( position ) => position . serviceId ,
70- ) ;
71- const activeServices = await tx
72- . select ( { id : services . id } )
73- . from ( services )
74- . where (
75- and (
76- eq ( services . projectId , projectId ) ,
77- inArray ( services . id , serviceIds ) ,
78- isNull ( services . deletedAt ) ,
79- ) ,
69+ try {
70+ const savedPositions = await db . transaction ( async ( tx ) => {
71+ const serviceIds = parsed . data . positions . map (
72+ ( position ) => position . serviceId ,
8073 ) ;
81-
82- if ( activeServices . length !== parsed . data . positions . length ) {
83- return null ;
84- }
85-
86- const positions : Array < {
87- id : string ;
88- canvasX : number | null ;
89- canvasY : number | null ;
90- } > = [ ] ;
91-
92- for ( const position of parsed . data . positions ) {
93- const [ savedPosition ] = await tx
94- . update ( services )
95- . set ( {
96- canvasX : position . canvasX ,
97- canvasY : position . canvasY ,
98- } )
74+ const activeServices = await tx
75+ . select ( { id : services . id } )
76+ . from ( services )
9977 . where (
10078 and (
101- eq ( services . id , position . serviceId ) ,
10279 eq ( services . projectId , projectId ) ,
80+ inArray ( services . id , serviceIds ) ,
10381 isNull ( services . deletedAt ) ,
10482 ) ,
105- )
106- . returning ( {
107- id : services . id ,
108- canvasX : services . canvasX ,
109- canvasY : services . canvasY ,
110- } ) ;
83+ ) ;
11184
112- if ( ! savedPosition ) {
113- tx . rollback ( ) ;
85+ if ( activeServices . length !== parsed . data . positions . length ) {
86+ throw new CanvasServiceNotFoundError ( ) ;
11487 }
11588
116- positions . push ( savedPosition ) ;
117- }
89+ const positions : Array < {
90+ id : string ;
91+ canvasX : number | null ;
92+ canvasY : number | null ;
93+ } > = [ ] ;
11894
119- return positions ;
120- } ) ;
95+ for ( const position of parsed . data . positions ) {
96+ const [ savedPosition ] = await tx
97+ . update ( services )
98+ . set ( {
99+ canvasX : position . canvasX ,
100+ canvasY : position . canvasY ,
101+ } )
102+ . where (
103+ and (
104+ eq ( services . id , position . serviceId ) ,
105+ eq ( services . projectId , projectId ) ,
106+ isNull ( services . deletedAt ) ,
107+ ) ,
108+ )
109+ . returning ( {
110+ id : services . id ,
111+ canvasX : services . canvasX ,
112+ canvasY : services . canvasY ,
113+ } ) ;
121114
122- if ( ! savedPositions ) {
123- return Response . json ( { error : "Service not found" } , { status : 404 } ) ;
124- }
115+ if ( ! savedPosition ) {
116+ throw new CanvasServiceNotFoundError ( ) ;
117+ }
118+
119+ positions . push ( savedPosition ) ;
120+ }
121+
122+ return positions ;
123+ } ) ;
125124
126- return Response . json ( savedPositions ) ;
125+ return Response . json ( savedPositions ) ;
126+ } catch ( error ) {
127+ if ( error instanceof CanvasServiceNotFoundError ) {
128+ return Response . json ( { error : "Service not found" } , { status : 404 } ) ;
129+ }
130+
131+ throw error ;
132+ }
127133}
128134
129135export async function GET (
0 commit comments