@@ -18,10 +18,30 @@ export const insertFacilityAssignment = dbAction(
1818 } )
1919 . from ( schema . facility )
2020 . where ( and ( eq ( schema . facility . id , data . facilityId ) , isNull ( schema . facility . deletedAt ) ) )
21- . limit ( 1 ) ;
21+ . limit ( 1 )
22+ . for ( "update" ) ;
2223
2324 if ( facilityInfo == null ) return ;
2425
26+ const [ duplicate ] = await tx
27+ . select ( { id : schema . eventFacility . id } )
28+ . from ( schema . eventFacility )
29+ . where (
30+ and (
31+ eq ( schema . eventFacility . eventId , eventId ) ,
32+ eq ( schema . eventFacility . facilityId , data . facilityId ) ,
33+ data . venueAllotmentId != null
34+ ? eq ( schema . eventFacility . venueAllotmentId , data . venueAllotmentId )
35+ : isNull ( schema . eventFacility . venueAllotmentId ) ,
36+ isNull ( schema . eventFacility . deletedAt ) ,
37+ ) ,
38+ )
39+ . limit ( 1 ) ;
40+
41+ if ( duplicate != null ) {
42+ return { success : false as const , conflict : { event : { id : eventId } } } ;
43+ }
44+
2545 if ( facilityInfo . overlapPolicy === "exclusive" ) {
2646 const [ overlap ] = await tx
2747 . select ( {
@@ -45,7 +65,7 @@ export const insertFacilityAssignment = dbAction(
4565 if ( overlap != null ) return { success : false as const , conflict : overlap } ;
4666 }
4767
48- const [ assignment ] = await db
68+ const [ assignment ] = await tx
4969 . insert ( schema . eventFacility )
5070 . values ( {
5171 eventId : eventId ,
0 commit comments