@@ -394,17 +394,17 @@ export const routes = (router: KoaRouter) => {
394394 Images,
395395 } = ctx . request . body
396396
397- // Filter out workOrders that are not related to laundry rooms
398- const laundryRoomWorkOrderRequests = Rows . filter (
399- ( workOrder : any ) => workOrder . LocationCode === 'TV'
397+ // Filter out workOrders that are not handled by onecore
398+ const onecoreWorkOrderRequests = Rows . filter ( ( workOrder : any ) =>
399+ [ 'TV' , 'BWC' , 'KÖ' ] . includes ( workOrder . LocationCode )
400400 )
401401
402402 const reason = ! ContactCode
403403 ? 'ContactCode is missing'
404404 : ! RentalObjectCode
405405 ? 'RentalObjectCode is missing'
406- : laundryRoomWorkOrderRequests . length === 0
407- ? 'No work orders on laundry rooms found in request'
406+ : onecoreWorkOrderRequests . length === 0
407+ ? 'No supported work orders found in request'
408408 : null
409409
410410 if ( reason ) {
@@ -428,20 +428,6 @@ export const routes = (router: KoaRouter) => {
428428 return
429429 }
430430
431- // Check if rental property has laundry room access
432- const propertyHasLaundryRoomAccess =
433- rentalPropertyInfo . maintenanceUnits ?. find (
434- ( unit ) => unit . type . toUpperCase ( ) === 'TVÄTTSTUGA'
435- )
436- if ( ! propertyHasLaundryRoomAccess ) {
437- ctx . status = 404
438- ctx . body = {
439- reason : 'No laundry room found for rental property' ,
440- ...metadata ,
441- }
442- return
443- }
444-
445431 // Get tenant with leases by contact code
446432 const tenant = await leasingAdapter . getTenantByContactCode ( ContactCode )
447433 if ( ! tenant . ok ) {
@@ -466,34 +452,39 @@ export const routes = (router: KoaRouter) => {
466452 return
467453 }
468454
469- for ( let workOrderRequest of laundryRoomWorkOrderRequests ) {
470- workOrderRequest = {
471- rentalPropertyInfo : rentalPropertyInfo ,
472- tenant : tenant . data ,
473- lease : lease ,
474- details : {
475- ContactCode,
476- RentalObjectCode,
477- AccessOptions,
478- HearingImpaired,
479- Pet,
480- Images,
481- Rows : [ workOrderRequest ] ,
482- } ,
483- }
484- workOrderAdapter . createWorkOrder ( workOrderRequest )
485- }
455+ const results = await Promise . all (
456+ onecoreWorkOrderRequests . map ( ( workOrderRequest : any ) =>
457+ workOrderAdapter . createWorkOrder ( {
458+ rentalPropertyInfo : rentalPropertyInfo ,
459+ tenant : tenant . data ,
460+ lease : lease ,
461+ details : {
462+ ContactCode,
463+ RentalObjectCode,
464+ AccessOptions,
465+ HearingImpaired,
466+ Pet,
467+ Images,
468+ Rows : [ workOrderRequest ] ,
469+ } ,
470+ } )
471+ )
472+ )
486473
487474 ctx . status = 200
488475 ctx . body = {
489- message : `Work order created` ,
476+ message : `Work orders created` ,
477+ // TODO better handling/response when there is an error with creating one or more work orders in the batch
478+ errors : results
479+ . filter ( ( result ) => ! result . ok )
480+ . map ( ( result ) => result . err ) ,
490481 ...metadata ,
491482 }
492483 } catch ( error ) {
493- logger . error ( error , 'Error creating new work order ' )
484+ logger . error ( error , 'Error creating new work orders ' )
494485 ctx . status = 500
495486 ctx . body = {
496- error : 'Failed to create a new work order ' ,
487+ error : 'Failed to create new work orders ' ,
497488 ...metadata ,
498489 }
499490 }
0 commit comments