public function addDeviceToken(Request $request)
{
$account = Auth::user()->account;
//scan if this user has a token already registered (tokens can change, so we need to use the users email as key)
$devices = json_decode($account->devices, TRUE);
for ($x = 0; $x < count($devices); $x++) {
if ($devices[$x]['email'] == Auth::user()->username) {
$devices[$x]['token'] = $request->token;
//update
$account->devices = json_encode($devices);
$account->save();
$devices[$x]['account_key'] = $account->account_key;
return $this->response($devices[$x]);
}
}
//User does not have a device, create new record
$newDevice = ['token' => $request->token, 'email' => $request->email, 'device' => $request->device, 'account_key' => $account->account_key, 'notify_sent' => TRUE, 'notify_viewed' => TRUE, 'notify_approved' => TRUE, 'notify_paid' => TRUE];
$devices[] = $newDevice;
$account->devices = json_encode($devices);
$account->save();
return $this->response($newDevice);
}