<?php
require_once('../../../api/v1/config/constants.php');
$thisDIR = getcwd()."/";
try{
    $data = file_get_contents('php://input');
    if (!empty($data)) {
    	$hookContent = json_decode($data, true);
    	$thisLogFile = $thisDIR.$hookContent['type']."_log.json";
    	$oldLog = json_decode(file_get_contents($thisLogFile), true);
    	if ($hookContent['retries'] == 0) {
    		$oldLog[$hookContent['created']] = array(
    			'timestamp' => $hookContent['created'],
    			'type' => $hookContent['type'],
    			'status' => "processing",
    		);
    		file_put_contents($thisLogFile, json_encode($oldLog));
    		// Do curl here for update of store inventory

    		// send status 200 if operation is small, it will work else next time
    		header('HTTP/1.0 200 OK');exit();
    	}else{
    		if ($oldLog[$hookContent['created']]['status'] == "done") {
    			header('HTTP/1.0 200 OK');exit();
    		}
    	}
    }
}catch(Exception $e){
     $log.= 'error: '.$e->getMessage();
     error_log($log);
}


?>