@@ -130,66 +130,65 @@ export class Folders extends Effect.Service<Folders>()("Folders", {
130130 folderId : Folder . FolderId ,
131131 data : Folder . FolderUpdate ,
132132 ) {
133- const folder = yield * repo
133+ const folder = yield * ( yield * repo
134134 . getById ( folderId )
135- . pipe (
136- Policy . withPolicy ( policy . canEdit ( folderId ) ) ,
137- Effect . flatMap (
138- Effect . catchTag (
139- "NoSuchElementException" ,
140- ( ) => new Folder . NotFoundError ( ) ,
141- ) ,
142- ) ,
143- ) ;
135+ . pipe ( Policy . withPolicy ( policy . canEdit ( folderId ) ) ) ) . pipe (
136+ Effect . catchTag (
137+ "NoSuchElementException" ,
138+ ( ) => new Folder . NotFoundError ( ) ,
139+ ) ,
140+ ) ;
144141
145142 // If parentId is provided and not null, verify it exists and belongs to the same organization
146- if ( ! data . parentId ) return ;
147- const parentId = data . parentId ;
148-
149- // Check that we're not creating an immediate circular reference
150- if ( parentId === folderId )
151- return yield * new Folder . RecursiveDefinitionError ( ) ;
152-
153- const parentFolder = yield * repo
154- . getById ( parentId , {
155- organizationId : Organisation . OrganisationId . make (
156- folder . organizationId ,
157- ) ,
158- } )
159- . pipe (
160- Policy . withPolicy ( policy . canEdit ( parentId ) ) ,
161- Effect . flatMap (
162- Effect . catchTag (
163- "NoSuchElementException" ,
164- ( ) => new Folder . ParentNotFoundError ( ) ,
143+ if ( data . parentId && Option . isSome ( data . parentId ) ) {
144+ const parentId = data . parentId . value ;
145+ // Check that we're not creating an immediate circular reference
146+ if ( parentId === folderId )
147+ return yield * new Folder . RecursiveDefinitionError ( ) ;
148+
149+ const parentFolder = yield * repo
150+ . getById ( parentId , {
151+ organizationId : Organisation . OrganisationId . make (
152+ folder . organizationId ,
165153 ) ,
166- ) ,
167- ) ;
154+ } )
155+ . pipe (
156+ Policy . withPolicy ( policy . canEdit ( parentId ) ) ,
157+ Effect . flatMap (
158+ Effect . catchTag (
159+ "NoSuchElementException" ,
160+ ( ) => new Folder . ParentNotFoundError ( ) ,
161+ ) ,
162+ ) ,
163+ ) ;
168164
169- // Check for circular references in the folder hierarchy
170- let currentParentId = parentFolder . parentId ;
171- while ( currentParentId ) {
172- if ( currentParentId === folderId )
173- return yield * new Folder . RecursiveDefinitionError ( ) ;
165+ // Check for circular references in the folder hierarchy
166+ let currentParentId = parentFolder . parentId ;
167+ while ( currentParentId ) {
168+ if ( currentParentId === folderId )
169+ return yield * new Folder . RecursiveDefinitionError ( ) ;
174170
175- const parentId = currentParentId ;
176- const nextParent = yield * repo . getById ( parentId , {
177- organizationId : Organisation . OrganisationId . make (
178- folder . organizationId ,
179- ) ,
180- } ) ;
171+ const parentId = currentParentId ;
172+ const nextParent = yield * repo . getById ( parentId , {
173+ organizationId : Organisation . OrganisationId . make (
174+ folder . organizationId ,
175+ ) ,
176+ } ) ;
181177
182- if ( Option . isNone ( nextParent ) ) break ;
183- currentParentId = nextParent . value . parentId ;
178+ if ( Option . isNone ( nextParent ) ) break ;
179+ currentParentId = nextParent . value . parentId ;
180+ }
184181 }
185182
186183 yield * db . execute ( ( db ) =>
187184 db
188185 . update ( Db . folders )
189186 . set ( {
190- ...( data . name ? { name : data . name } : { } ) ,
191- ...( data . color ? { color : data . color } : { } ) ,
192- ...( data . parentId ? { parentId : data . parentId } : { } ) ,
187+ name : data . name ,
188+ color : data . color ,
189+ parentId : data . parentId
190+ ? Option . getOrNull ( data . parentId )
191+ : undefined ,
193192 } )
194193 . where ( Dz . eq ( Db . folders . id , folderId ) ) ,
195194 ) ;
0 commit comments