@@ -240,6 +240,48 @@ export default ( db, req, res ) => {
240240 }
241241 ,
242242 } ,
243+ {
244+ route : 'worldsById[{keys:ids}].books.remove' ,
245+ call : ( { ids : [ world_id ] } , [ { id} ] ) => db
246+ ::getBooksFromWorld ( world_id , user . _id )
247+ . toArray ( )
248+ . map ( array => ( {
249+ length : array . length ,
250+ position : array . findIndex ( element => element === id )
251+ } ) )
252+ . flatMap ( ( { position, length} ) => {
253+ if ( position === - 1 ) {
254+ throw new Error ( 'Could not find the book to delete' ) ;
255+ }
256+ return db
257+ ::archiveDocument ( 'books' , id , user . _id )
258+ . flatMap ( ( document ) => {
259+ if ( document . archived !== true )
260+ throw new Error ( 'Could not delete the book' ) ;
261+ return db
262+ ::archiveNode ( 'Book' , document . _id , user . _id )
263+ . flatMap ( node =>
264+ db ::archiveRelationship ( 'IN' , document . _id , world_id , user . _id )
265+ )
266+ . flatMap ( ( ) => {
267+ return [
268+ {
269+ path : [ 'booksById' , id ] ,
270+ invalidated : true
271+ } ,
272+ {
273+ path : [ 'worldsById' , world_id , 'books' , 'length' ] ,
274+ value : length - 1 ,
275+ } ,
276+ {
277+ path : [ 'worldsById' , world_id , 'books' , { from : position , to : length } ] ,
278+ invalidated : true ,
279+ } ,
280+ ]
281+ } )
282+ } )
283+ } )
284+ } ,
243285
244286 /**
245287 * Outlines
0 commit comments