Finished sync script
This commit is contained in:
parent
dfe833d670
commit
fb1f63d4bc
1 changed files with 49 additions and 6 deletions
|
@ -70,30 +70,48 @@ class SyncCustomers extends Command
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt_array($ch, $opts);
|
curl_setopt_array($ch, $opts);
|
||||||
if ($result = curl_exec($ch)) {
|
if ($result = curl_exec($ch)) {
|
||||||
|
$hosts = [];
|
||||||
|
|
||||||
$customers = json_decode($result);
|
$customers = json_decode($result);
|
||||||
|
|
||||||
$bar = $this->output->createProgressBar(count($customers));
|
$bar = $this->output->createProgressBar(count($customers));
|
||||||
$bar->start();
|
$bar->start();
|
||||||
|
|
||||||
|
|
||||||
// Getting existing tasks
|
// Getting existing tasks
|
||||||
$query = app('db')->select('SELECT * FROM tasks');
|
$tasks_flat = [];
|
||||||
foreach ($query as $t) {
|
$tasks = app('db')->select('SELECT * FROM tasks');
|
||||||
$tasks[$t->id] = preg_replace('~^https?://~', '', $t->host);
|
foreach ($tasks as $t) {
|
||||||
|
$tasks_flat[$t->id] = preg_replace('~^https?://~', '', trim($t->host));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getting existing contacts
|
// Getting existing contacts
|
||||||
$contacts = app('db')->select('SELECT * FROM contacts');
|
$contacts = app('db')->select('SELECT * FROM contacts');
|
||||||
|
|
||||||
|
// Getting existing groups
|
||||||
|
$groups_flat = [];
|
||||||
|
$groups = app('db')->select('SELECT * FROM `groups`');
|
||||||
|
foreach ($groups as $g) {
|
||||||
|
$groups_flat[$g->id] = $g->name;
|
||||||
|
}
|
||||||
|
|
||||||
// First we insert new customers
|
// First we insert new customers
|
||||||
foreach($customers as $c) {
|
foreach($customers as $c) {
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
if (false === $key = array_search($c->domain, $tasks)) {
|
|
||||||
|
$hosts[] = 'https://'.trim($c->domain);
|
||||||
|
|
||||||
|
// Checking group existence
|
||||||
|
if (empty($groups_flat[$c->id])) {
|
||||||
|
app('db')->insert('INSERT INTO `groups` (`id`, `name`) VALUE (?, ?)', [ $c->id, $c->name ]);
|
||||||
|
$groups_flat[$c->id] = $c->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false === array_search(trim($c->domain), $tasks_flat)) {
|
||||||
$ret = app('db')->insert('
|
$ret = app('db')->insert('
|
||||||
INSERT INTO tasks (`host`, `type`, `params`, `creation_date`, `frequency`, `active`, `group_id`)
|
INSERT INTO tasks (`host`, `type`, `params`, `creation_date`, `frequency`, `active`, `group_id`)
|
||||||
VALUES(:host, :type, :params, :creation_date, :frequency, :active, :group_id)
|
VALUES(:host, :type, :params, :creation_date, :frequency, :active, :group_id)
|
||||||
', [
|
', [
|
||||||
'host' => 'https://'.$c->domain,
|
'host' => 'https://'.trim($c->domain),
|
||||||
'type' => 'http',
|
'type' => 'http',
|
||||||
'params' => 'propulsé par',
|
'params' => 'propulsé par',
|
||||||
'creation_date' => date('Y-m-d H:i:s'),
|
'creation_date' => date('Y-m-d H:i:s'),
|
||||||
|
@ -101,13 +119,38 @@ class SyncCustomers extends Command
|
||||||
'active' => 1,
|
'active' => 1,
|
||||||
'group_id' => $c->id
|
'group_id' => $c->id
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if ($ret === true) {
|
||||||
|
$task_id = app('db')->getPdo()->lastInsertId();
|
||||||
|
|
||||||
|
// Inserting contacts
|
||||||
|
foreach ($contacts as $c) {
|
||||||
|
app('db')->insert('INSERT INTO notifications (`task_id`, `contact_id`) VALUES (:task_id, :contact_id)', [
|
||||||
|
'task_id' => $task_id,
|
||||||
|
'contact_id' => $c->id
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$bar->finish();
|
||||||
|
|
||||||
|
$this->newLine(2);
|
||||||
|
$this->line('Checking tasks to delete');
|
||||||
|
$bar = $this->output->createProgressBar(count($tasks));
|
||||||
|
$bar->start();
|
||||||
|
|
||||||
// Then we delete old customers
|
// Then we delete old customers
|
||||||
foreach ($tasks as $t) {
|
foreach ($tasks as $t) {
|
||||||
|
$bar->advance();
|
||||||
|
|
||||||
|
if (false === array_search($t->host, $hosts)) {
|
||||||
|
// Must delete task
|
||||||
|
//$this->line('must delete '.$t->host);
|
||||||
|
app('db')->delete('DELETE FROM `tasks` WHERE host = ?', [$t->host]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
$bar->finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue