/** * Api function for get currencies listing json. * * @return Response */ public function getListCurrencies() { $data = get(); // Validator get params. $valids = Validator::make($data, ['type' => 'required'], EXRHelper::getMessage()); // Response error message if not valids if ($valids->fails()) { return Response::json(array('status' => 'error', 'data' => $valids->messages())); } extract($data); $types = array(); switch ($type) { default: case 'ty-gia': $types = ['CURRENCY', 'VCB']; break; case 'vang': $types = ['GOLD']; break; } /*$page = intval($page); $limit = EXR_PAGINATE; $skip = $page * $limit;*/ // Get list currency ids $records = Currency::where('status', true)->whereRaw('status = ?', array(true))->whereIn('type', $types)->orderBy('name', 'DESC')->get(array('id', 'code', 'type', 'name', 'flag', 'created_at', 'updated_at')); return Response::json(array('status' => 'success', 'data' => $records)); }
/** * Api function for crawl send notification to devices. * * @return Response */ public function getSendNotificationDevices() { $data = get(); // Valid get params. $valids = Validator::make($data, ['token' => 'required|in:' + EXR_TOKEN], EXRHelper::getMessage()); // Check valid. if ($valids->fails()) { return Response::json(array('status' => 'Error', 'data' => $valids->messages()), 403); } // Get Currency $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get(); if (!count($currencies)) { Currency::where('is_send', true)->update(array('is_send' => false)); $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get(); } foreach ($currencies as $currency) { $message = $tye = ''; if (Cache::has('exr_notification_msg_' . $currency->id)) { $data = Cache::get('exr_notification_msg_' . $currency->id); $message = isset($data['message']) ? $data['message'] : ''; $type = isset($data['type']) ? $data['type'] : ''; } else { // Get today currency $today = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) = CURDATE()', array($currency->id))->first(); if (!$today) { return Response::json(array('status' => 'error', 'data' => 'Error'), 500); } $mimax = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) >= NOW() - INTERVAL ? WEEK', array($currency->id, EXR_LIMIT_WEEK))->select(DB::raw('MIN(buy) as min, MAX(buy) as max'))->first(); if ($mimax->min > $today->buy) { $message = sprintf(EXR_MIN_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy); $tye = 'currency_min'; } else { if ($mimax->max < $today->buy) { $message = sprintf(EXR_MAX_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy); $type = 'currency_max'; } else { $message = ''; $type = ''; } } if ($message) { Cache::put('exr_notification_msg_' . $currency->id, array('message' => $message, 'type' => $type), EXR_CACHE_MINUTE); } } if (!$message) { return Response::json(array('status' => 'error', 'data' => 'Error'), 500); } // Get 1000 devices $records = Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ?', array($currency->id, $type, false))->take(EXR_LIMIT_DEVICES)->get(); if (!count($records)) { // Update is_send for currency when send all devices. $currency->is_send = true; $currency->save(); // Clear if crawl end devices. Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ? AND DATE(updated_at) != CURDATE()', array($currency->id, $type, true))->update(array('is_send', false)); } // Convert devices registations ids to array. $deviceIds = $records->lists('device_id'); $response = ''; if (count($deviceIds) <= EXR_LIMIT_DEVICES && !empty($message)) { // Send to notification to it $response = Notification::sendNtfsToDevices($message, $deviceIds); } } return Response::json(array('status' => 'success', 'data' => $response)); }