Esempio n. 1
0
 public static function boot()
 {
     parent::boot();
     Company::created(function ($company) {
         AuthorizationToken::createAuthorizationToken($company);
         Credit::createCredit($company->id);
         Credit::createCredit($company->id, Credit::PAID);
     });
 }
Esempio n. 2
0
 public static function boot()
 {
     parent::boot();
     User::created(function ($user) {
         if ($user->type == User::TYPE_PAYEE) {
             AuthorizationToken::createAuthorizationToken($user);
         }
     });
 }
 protected function processDirectory($dir)
 {
     try {
         $path = $dir['path'];
         $dirName = substr($path, strrpos($path, '/') + 1);
         if ($this->processingEtlExists($dirName)) {
             $this->log($this->processingEtlKey($dirName) . " key exists, directory already processing");
             return;
         }
         $this->lockProcessingEtl($dirName);
         $this->log("locked folder with " . $this->processingEtlKey($dirName));
         if (!$this->doneExists($dirName)) {
             $this->log($this->doneEtlKey($dirName) . " not found directory not processable yet, removed key " . $this->processingEtlKey($dirName));
             //                $this->dropbox->delete($directory . "/.processing_etl");
             $this->deleteProcessingEtl($dirName);
             return;
         }
         $this->log("Processing directory {$path}");
         $dropboxMetadata = $this->dropbox->getMetadataWithChildren($path);
         $fileList = $dropboxMetadata['contents'];
         $this->log('Read data.json');
         $dataFile = $this->getDataFile($path);
         $dataJson = $this->dropboxReadFile($dataFile);
         $data = json_decode($dataJson, true);
         $this->log('check authorization');
         if (isset($data['payeeId'])) {
             $token = $data['payeeId'];
             $authorized = AuthorizationToken::findByToken($token, "User");
         } else {
             $token = isset($data['companyId']) ? $data['companyId'] : $data['publisherId'];
             $authorized = AuthorizationToken::findByToken($token, "Company");
         }
         if (empty($authorized)) {
             $this->log("authorization token failed");
             $this->deleteProcessingEtl($dirName);
             return;
         }
         if (isset($data['payeeId'])) {
             $payee = User::find($authorized->model_id);
             $companyId = $payee->company_id;
             $payeeCode = $payee->code;
         } else {
             $companyId = $authorized->model_id;
             $payeeCode = null;
         }
         $this->log("authorization succeeded");
         $publisherAdmins = User::publisherAdmins($companyId);
         $this->log('check credits');
         if (!Company::hasCredits($companyId)) {
             $this->log("no credits available");
             $this->deleteProcessingEtl($dirName);
             $this->sendEmailToPublisherAdminsNoCredits($publisherAdmins, $companyId);
             return;
         }
         $this->log("company has credits");
         if (true || !isset($data['dealId'])) {
             $this->log('No deal id found, creating deal');
             $dealName = isset($data["dealName"]) && !empty($data["dealName"]) ? $data["dealName"] : $dirName;
             $deal = $this->createDeal($data, $dealName, $companyId, $payeeCode);
             $data['dealId'] = $deal->id;
             $this->log("Will upload to {$dataFile}");
             $this->dropbox->uploadFileFromString($dataFile, \Dropbox\WriteMode::force(), json_encode($data));
         } else {
             $deal = Deal::find($data['dealId']);
         }
         $this->log('Deal id is ' . $deal->id);
         $statementList = $data['files'];
         foreach ($statementList as $filename => $metadata) {
             $this->processFile($filename, $metadata, $fileList, $deal, $path);
             //            break;
         }
         $deal->etl_status = 'processed';
         $deal->save();
         $decremented = Company::decrementCredits($companyId);
         $this->log("decrement credits " . $decremented);
         if (count($publisherAdmins)) {
             $this->sendEmailToPublisherAdmins($publisherAdmins, $deal);
             $this->log("email sent to publisher");
         } else {
             $this->log("no publisher admins found");
         }
         $moveTo = $this->getDropboxProcessedPath() . '/' . $dirName;
         $this->dropbox->move($path, $moveTo);
         $this->log("moved to {$moveTo}");
         $this->log("start executing rollups");
         RollupEvent::EXECUTE_ROLLUP_TABLES($deal->id);
         $this->log("end executing rollups");
         $this->deleteProcessingEtl($dirName);
         $this->log("folder unlocked");
     } catch (Dropbox\Exception $e) {
         $this->log('dropbox error: ' . get_class($e) . ' - ' . $e->getMessage() . " FILE " . $e->getFile() . " LINE " . $e->getLine());
         $this->deleteProcessingEtl($dirName);
         $this->log("folder unlocked");
         $this->sendErrorEmailToAdmin($dir, get_class($e) . ' - ' . $e->getMessage() . " FILE " . $e->getFile() . " LINE " . $e->getLine());
     } catch (Exception $e) {
         $this->log('something bad happened: ' . get_class($e) . ' - ' . $e->getMessage() . " FILE " . $e->getFile() . " LINE " . $e->getLine());
         $this->sendErrorEmailToAdmin($dir, get_class($e) . ' - ' . $e->getMessage() . " FILE " . $e->getFile() . " LINE " . $e->getLine());
     }
 }
 public static function findByToken($token, $model)
 {
     return AuthorizationToken::where('token', '=', $token)->where("model", "=", $model)->first();
 }