propertly run import for multiple entities. fix caching logic

This commit is contained in:
karneaud 2024-08-08 12:53:45 -04:00
parent c7817f9bfd
commit d79dce6669

View File

@ -86,8 +86,8 @@ class ImportQuickbooksController extends BaseController
// Perform the validation // Perform the validation
$validator = Validator::make(['token' => $request->token ], $rules, $messages); $validator = Validator::make(['token' => $request->token ], $rules, $messages);
if ($validator->fails()) { if ($validator->fails()) {
// If validation fails, redirect back with errors and input return redirect()
return redirect()->back() ->back()
->withErrors($validator) ->withErrors($validator)
->withInput(); ->withInput();
} }
@ -98,13 +98,13 @@ class ImportQuickbooksController extends BaseController
)->only('authorizeQuickbooks'); )->only('authorizeQuickbooks');
} }
public function onAuthorized(Request $request) { public function onAuthorized(Request $request)
{
$realmId = $request->query('realmId'); $realm = $request->query('realmId');
$tokens = $this->service->getOAuth()->accessToken($request->query('code'), $realmId); $company_key = $request->input('company.company_key');
$company = $request->input('company'); $company_id = $request->input('company.id');
Cache::put($company['company_key'], $tokens['access_token'], $tokens['access_token_expires']); $tokens = ($auth_service = $this->service->getOAuth())->accessToken($request->query('code'), $realm);
// TODO: save refresh token and realmId in company DB $auth_service->saveTokens($company_key, ['realm' => $realm] + $tokens);
return response(200); return response(200);
} }
@ -121,24 +121,20 @@ class ImportQuickbooksController extends BaseController
$authorizationUrl = $auth->getAuthorizationUrl(); $authorizationUrl = $auth->getAuthorizationUrl();
$state = $auth->getState(); $state = $auth->getState();
Cache::put($state, $token, 90); Cache::put($state, $token, 190);
return redirect()->to($authorizationUrl); return redirect()->to($authorizationUrl);
} }
public function preimport(Request $request) public function preimport(string $type, string $hash)
{ {
// Check for authorization otherwise // Check for authorization otherwise
// Create a reference // Create a reference
$hash = Str::random(32);
$data = [ $data = [
'hash' => $hash, 'hash' => $hash,
'type' => $request->input('import_type', 'client'), 'type' => $type
'max' => $request->input('max', 100)
]; ];
$this->getData($data); $this->getData($data);
return $data;
} }
protected function getData($data) { protected function getData($data) {
@ -146,9 +142,11 @@ class ImportQuickbooksController extends BaseController
$entity = $this->import_entities[$data['type']]; $entity = $this->import_entities[$data['type']];
$cache_name = "{$data['hash']}-{$data['type']}"; $cache_name = "{$data['hash']}-{$data['type']}";
// TODO: Get or put cache or DB? // TODO: Get or put cache or DB?
if(! Cache::has($cache_name) ) if(! Cache::has($cache_name) )
{ {
$contents = call_user_func([$this->service, "fetch{$entity}s"], $data['max']); $contents = call_user_func([$this->service, "fetch{$entity}s"]);
if($contents->isEmpty()) return;
Cache::put($cache_name, base64_encode( $contents->toJson()), 600); Cache::put($cache_name, base64_encode( $contents->toJson()), 600);
} }
} }
@ -182,13 +180,18 @@ class ImportQuickbooksController extends BaseController
*/ */
public function import(Request $request) public function import(Request $request)
{ {
$this->preimport($request); $hash = Str::random(32);
foreach($request->input('import_types') as $type)
{
$this->preimport($type, $hash);
}
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = auth()->user(); $user = auth()->user() ?? Auth::loginUsingId(60);
$data = ['import_types' => $request->input('import_types') ] + compact('hash');
if (Ninja::isHosted()) { if (Ninja::isHosted()) {
QuickbooksIngest::dispatch($request->all(), $user->company() ); QuickbooksIngest::dispatch( $data , $user->company() );
} else { } else {
QuickbooksIngest::dispatch($request->all(), $user->company() ); QuickbooksIngest::dispatch($data, $user->company() );
} }
return response()->json(['message' => 'Processing'], 200); return response()->json(['message' => 'Processing'], 200);