import { rest } from 'msw' ; interface Payload { data ? : Record < string , any > | Record < string , any > [ ] | boolean | string ; message ? : string ; } interface WateredTree { tree_id : string ; time : string ; uuid : string ; amount : string ; timestamp : string ; username : string ; } let location = '' ; if ( process . env . NODE_ENV === 'test' ) { location = '' ; } else { if ( process . env . NODE_ENV === 'development' ) { location = process . env . API_ENDPOINT_DEV ? process . env . API_ENDPOINT_DEV : '' ; } else if ( process . env . NODE_ENV === 'production' ) { location = process . env . API_ENDPOINT_PROD ? process . env . API_ENDPOINT_PROD : '' ; } else { console . log ( 'NODE_ENV is not defiend' ) ; location = '' ; } } let adoptedTreeIds = [ ] ; const wateredAndAdopted = [ ] ; const treesWatered = [ ] ; /**
 * Extracts top level properties from an object or URLSearchParams. Only looks for strings. with a given key. All other types are ignored and returned as undefined
 *
 */ export function getProperty ( item , key ) { if ( item instanceof URLSearchParams ) { const res = item . get ( key ) ; return res ? res : '' ; } return typeof item [ key ] === 'string' ? item [ key ] : '' ; } export const handlers = [ rest . post ( `${ location }/login` , async ( _req , res , ctx ) => { return res ( ctx . status ( 201 ) ) ; } ) , rest . delete ( `${ location }/delete` , ( req , res , ctx ) => { const json = { } ; let body = { } ; if ( typeof req . body === 'string' ) { body = JSON . parse ( req . body ) ; } else { body = req . body ? req . body : { } ; } const queryType = body ? getProperty ( body , 'queryType' ) : '' ; switch ( queryType ) { case 'unadopt' : { break ; } case undefined : case null : { console . log ( ' queryType is undefined or null' ) ; break ; } default : { console . log ( 'no default case for delete action defiend' ) ; } } return res ( ctx . status ( 201 ) , ctx . json ( json ) ) ; } ) , rest . post ( `${ location }/post` , ( req , res , ctx ) => { let json = { } ; let body = { } ; if ( typeof req . body === 'string' ) { body = JSON . parse ( req . body ) ; } else { body = req . body ? req . body : { } ; } const queryType = body ? getProperty ( body , 'queryType' ) : '' ; switch ( queryType ) { case 'water' : { treesWatered . push ( { tree_id : getProperty ( body , 'tree_id' ) , time : new Date ( ) . toLocaleString ( ) , timestamp : new Date ( ) . toLocaleString ( ) , uuid : getProperty ( body , 'uuid' ) , username : getProperty ( body , 'username' ) , amount : body . amount ? body . amount : 0 , } ) ; let found = false ; for ( let i = 0 ; i < wateredAndAdopted . length ; i ++ ) { if ( body . tree_id === wateredAndAdopted [ i ] . tree_id ) { wateredAndAdopted [ i ] . watered = `${ parseInt ( wateredAndAdopted [ i ] . watered ) + 1 }` ; found = true ; break ; } } if ( found === false ) { wateredAndAdopted . push ( { tree_id : body . tree_id , adopted : '0' , watered : '1' , } ) ; } json = { data : 'watered tree' , message : `${ queryType }` } ; break ; } case 'adopt' : { adoptedTreeIds = [ ... new Set ( [ body . tree_id , ... adoptedTreeIds ] ) ] ; let found = false ; for ( let i = 0 ; i < wateredAndAdopted . length ; i ++ ) { if ( body . tree_id === wateredAndAdopted [ i ] . tree_id ) { wateredAndAdopted [ i ] . adopted = `${ parseInt ( wateredAndAdopted [ i ] . adopted ) + 1 }` ; found = true ; break ; } } if ( found === false ) { wateredAndAdopted . push ( { tree_id : body . tree_id , adopted : '1' , watered : '0' , } ) ; } json = { data : 'tree was adopted' , message : `${ queryType }` } ; break ; } case undefined : case null : { console . error ( 'queryType is not defiend' ) ; break ; } default : { json = { data : [ ] , message : `default case for post ${ body }` } ; } } return res ( ctx . status ( 201 ) , ctx . json ( json ) ) ; } ) , rest . get ( `${ location }/` , async ( _req , res , ctx ) => { return res ( ctx . status ( 200 ) , ctx . json ( { foo : 'bar' } ) ) ; } ) , rest . get ( `${ location }/get` , async ( req , res , ctx ) => { let json = { } ; const queryType = getProperty ( req . url . searchParams , 'queryType' ) ; switch ( queryType ) { case 'treesWateredByUser' : { json = { data : treesWatered , message : `${ queryType }` } ; break ; } case 'treesbyids' : { const originalResponse = await ctx . fetch ( req ) ; json = { ... originalResponse } ; break ; } case 'byid' : { const originalResponse = await ctx . fetch ( req ) ; json = { ... originalResponse } ; if ( process . env . NODE_ENV === 'test' ) { json = { data : [ { id : '_abc' } ] } ; } break ; } case 'adopted' : { json = { data : [ ... adoptedTreeIds ] , message : `${ queryType }` } ; break ; } case 'istreeadopted' : { const id = getProperty ( req . url . searchParams , 'id' ) ; json = { data : adoptedTreeIds . includes ( id ) ? true : false , message : `${ queryType }` , } ; break ; } case 'wateredbyuser' : { json = { data : treesWatered , message : `${ queryType }` } ; break ; } case 'wateredandadopted' : { json = { data : wateredAndAdopted , message : `${ queryType }` } ; break ; } case 'lastwatered' : { const id = getProperty ( req . url . searchParams , 'id' ) ; const lastWateredByUser = treesWatered . map ( tree => { if ( tree . tree_id === id ) { return tree ; } else { return ; } } ) ; const lastWateredByUserFiltered = lastWateredByUser . filter ( Boolean ) ; json = { data : lastWateredByUserFiltered , message : `${ queryType }` } ; break ; } case 'watered' : { const watered = treesWatered . map ( tree => tree . tree_id ) ; json = { data : { watered } , message : `${ queryType }` } ; break ; } default : { const originalResponse = await ctx . fetch ( req ) ; json = { data : [ ] , url : req . url , message : `case ${ queryType } with url "${ req . url }" in default case. Not yet defined and passed through` , ... originalResponse , } ; break ; } } return res ( ctx . status ( 200 ) , ctx . json ( json ) ) ; } ) , rest . get ( 'https://tsb-trees-api-user-management.now.sh/api/user' , ( req , res , ctx ) => { const userid = getProperty ( req . url . searchParams , 'id' ) ; const json = { data : { user_id : userid , email : 'test@gdk.de' , email_verified : true , name : 'gdkboss123' , nickname : 'the GDK boss' , username : 'GDK OG' , } , } ; return res ( ctx . status ( 200 ) , ctx . json ( json ) ) ; } ) , ] ;