resource id

This commit is contained in:
Maxime Renou 2020-05-13 12:38:26 +02:00
parent f22b7b12ba
commit 34cb9f8625
3 changed files with 66 additions and 22 deletions

View File

@ -77,7 +77,15 @@ $connect->setSynchronized([
]); ]);
``` ```
Your entities must use `HasConnectSync` trait. This trait allows you to customize the syncing behavior. Your models must use `HasConnectSync` trait. This trait allows you to customize the syncing behavior.
You also need to add this column to your synced tables:
```
$table->unsignedBigInteger('connect_resource_id')->unique();
```
_You can customize this column name and the syncing behavior in your model. Take a look at `HasConnectSync`._
Finally, use this command to sync everything: Finally, use this command to sync everything:

View File

@ -22,6 +22,14 @@ class Connect
'UserTeam' 'UserTeam'
]; ];
protected static $foreignKeys = [
'role_id' => 'Role',
'company_id' => 'Company',
'team_id' => 'Team',
'user_id' => 'User',
'user_teams_id' => 'UserTeam'
];
protected $app; protected $app;
protected $synchronized = []; protected $synchronized = [];
@ -177,16 +185,22 @@ class Connect
$model_data = $this->getUserData($connect_data['access_token']); $model_data = $this->getUserData($connect_data['access_token']);
$model = config('bconnect.model'); $model = config('bconnect.model');
if (in_array($model, $this->synchronized)) {
$user = $model::findConnectResource($model_data['id']) ?? new $model;
}
else {
$user = $model::where('email', $model_data['email'])->first() ?? new $model; $user = $model::where('email', $model_data['email'])->first() ?? new $model;
$user->fill($model_data); }
$user->fill($model_data); // TODO
if (in_array($model, $this->synchronized)) if (in_array($model, $this->synchronized))
$user->id = $model_data['id']; $user->{$model::$connectColumnId} = $model_data['id'];
$user->save(); $user->save();
if (in_array($model, $this->synchronized)) if (in_array($model, $this->synchronized))
$user = $model::findOrFail($model_data['id']); $user = $model::findConnectResource($model_data['id']);
$this->updateUserConnectData($user, $connect_data); $this->updateUserConnectData($user, $connect_data);
@ -320,6 +334,8 @@ class Connect
$data = $data['connectResourceData']; $data = $data['connectResourceData'];
} }
$data = $this->convertForeignKeys($data);
$model::$method($data['id'], $data); $model::$method($data['id'], $data);
return true; return true;
@ -362,7 +378,7 @@ class Connect
foreach ($model::all() as $item) foreach ($model::all() as $item)
{ {
if (!in_array(intval($item->id), $identifiers)) if (!in_array(intval($item->{$model::$connectColumnId}), $identifiers))
$model::onConnectResourceDoesNotExist($item); $model::onConnectResourceDoesNotExist($item);
} }
} }
@ -377,9 +393,11 @@ class Connect
} }
$model = $this->synchronized[$resourceType]; $model = $this->synchronized[$resourceType];
$item = $model::find($resourceId); $item = $model::findConnectResource($resourceId);
$method = $this->getEventMethod($item ? 'updated' : 'created'); $method = $this->getEventMethod($item ? 'updated' : 'created');
$model::$method($resourceId, $resourceData);
$data = $this->convertForeignKeys($resourceData);
$model::$method($resourceId, $data);
} }
// Routing // Routing
@ -428,4 +446,20 @@ class Connect
{ {
return config('bconnect.url') ?? 'https://connect.bluesquare.io'; return config('bconnect.url') ?? 'https://connect.bluesquare.io';
} }
protected function convertForeignKeys($data)
{
foreach (self::$foreignKeys as $key => $resourceType)
{
if (!array_key_exists($key, $data)) continue;
$model = $this->resolveResourceModel($resourceType);
if (!in_array(HasConnectSync::class, class_uses($model))) continue;
$record = $model::findConnectResource($data[$key]);
$data[$key] = $record ? $record->id : null;
}
return $data;
}
} }

View File

@ -10,32 +10,34 @@ trait HasConnectSync
public static $connectResource; public static $connectResource;
public static $connectColumnId = 'connect_resource_id';
public static function findConnectResource($id)
{
return self::query()->where(self::$connectColumnId, $id)->first();
}
public static function onConnectResourceCreated($id, $data) public static function onConnectResourceCreated($id, $data)
{ {
$record = new self; $record = self::findConnectResource($id) ?? new self;
$record->fill($data); $record->fill($data); // TODO
$record->id = $id; $record->{self::$connectColumnId} = $id;
$record->save(); return $record->save();
return true;
} }
public static function onConnectResourceUpdated($id, $data) public static function onConnectResourceUpdated($id, $data)
{ {
$record = self::find($id) ?? new self; return self::onConnectResourceCreated($id, $data);
$record->fill($data);
$record->id = $id;
$record->save();
return true;
} }
public static function onConnectResourceDeleted($id, $data) public static function onConnectResourceDeleted($id, $data = null)
{ {
$record = self::find($id); $record = self::findConnectResource($id);
return $record ? $record->delete() : false; return $record ? $record->forceDelete() : false;
} }
public static function onConnectResourceDoesNotExist($record) public static function onConnectResourceDoesNotExist($record)
{ {
return $record->delete(); return $record->forceDelete();
} }
} }