Example #1
0
 public function onRedeem()
 {
     $id = post('id');
     $user = Auth::getUser();
     RewardManager::redeem($id, $user);
     // Send Flash and kiosk notification
     Postman::send('simple', function (NotificationMessage $notification) use($user) {
         // Set user in the notification
         $notification->to($user, $user->name);
         // Send code and activity just in case we want to use in the template
         $notification->addData([]);
         // Determine the content of the message and type of message
         $message = Session::pull('rewardMessage');
         $type = $message ? 'info' : 'error';
         // Only for flash messages
         if ($type == 'error') {
             $message = Session::pull('rewardError');
         }
         // Set type of flash
         $notification->addViewSettings(['type' => $type]);
         $notification->message($message);
     }, ['flash', 'kiosk']);
     return ['.modal-dialog' => "<script type='text/javascript'>\$('button.close').click();</script>", '#flashMessages' => $this->renderPartial('@flashMessages'), 'span.points' => number_format($user->points)];
 }
 /**
  * @SWG\Definition(
  *      definition="response.redeem",
  *      required={"data"},
  *      @SWG\Property(
  *          property="data",
  *          type="object",
  *          ref="#/definitions/redeem.payload"
  *      )
  * )
  * 
  * @SWG\Definition(
  *      definition="redeem.payload",
  *      required={"success", "message", "user"},
  *      @SWG\Property(
  *          property="success",
  *          type="boolean"
  *      ),
  *      @SWG\Property(
  *          property="message",
  *          type="string"
  *      ),
  *      @SWG\Property(
  *          property="user",
  *          type="object",
  *          ref="#/definitions/user.info.points"
  *      ) 
  * )
  * 
  * 
  * @SWG\GET(
  *     path="rewards/redeem/{reward}/user/{user}",
  *     description="Redeem user points for rewards",
  *     summary="Redeem a reward",
  *     tags={ "rewards"},
  *
  *     @SWG\Parameter(
  *         description="ID of reward to redeem",
  *         format="int64",
  *         in="path",
  *         name="reward",
  *         required=true,
  *         type="integer"
  *     ),
  *     @SWG\Parameter(
  *         description="ID of user",
  *         format="int64",
  *         in="path",
  *         name="user",
  *         required=true,
  *         type="integer"
  *     ),     
  *     @SWG\Response(
  *         response=200,
  *         description="Unsuccessful response",
  *         @SWG\Schema(ref="#/definitions/response.redeem")
  *     ),
  *     @SWG\Response(
  *         response=201,
  *         description="Successful response",
  *         @SWG\Schema(ref="#/definitions/response.redeem")
  *     ),
  *     @SWG\Response(
  *         response=500,
  *         description="Unexpected error",
  *         @SWG\Schema(ref="#/definitions/error500")
  *     ),
  *     @SWG\Response(
  *         response=404,
  *         description="User not found",
  *         @SWG\Schema(ref="#/definitions/UserError404")
  *     )
  * )
  */
 public function redeemByGet($rewardId, $userId)
 {
     if (is_null($user = User::find($userId))) {
         return Response::api()->errorNotFound('User not found');
     }
     if (is_null($reward = Reward::find($rewardId))) {
         return Response::api()->errorNotFound('Reward not found');
     }
     RewardManager::redeem($rewardId, $user);
     // Check if redeem was successful
     $message = Session::pull('rewardMessage');
     $type = $message ? 'info' : 'error';
     $success = true;
     $httpCode = 201;
     if ($type == 'error') {
         $success = false;
         $httpCode = 200;
         $message = Session::pull('rewardError');
     }
     // Get common user points format via UserProfileTransformer
     $userTransformer = new UserProfileTransformer();
     $points = $userTransformer->getUserPoints($user);
     $payload = ['data' => ['success' => $success, 'message' => $message, 'user' => ['id' => $user->getKey(), 'points' => $points]]];
     return Response::api()->setStatusCode($httpCode)->withArray($payload);
 }