Skip to content

Commit

Permalink
feat: Add success/error activity logs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed Feb 22, 2024
1 parent ad02509 commit 6189299
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 28 deletions.
8 changes: 7 additions & 1 deletion src/jobs/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\queue\BaseJob;
use craft\web\UploadedFile;
use fostercommerce\variantmanager\Plugin;
use fostercommerce\variantmanager\records\Activity;
use League\Csv\UnableToProcessCsv;
use yii\base\Exception;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -46,7 +47,12 @@ public static function fromFilename(string $filename, ?string $productTypeHandle
*/
public function execute($queue): void
{
Plugin::getInstance()->csv->import($this->filename, $this->csvData, $this->productTypeHandle);
try {
Plugin::getInstance()->csv->import($this->filename, $this->csvData, $this->productTypeHandle);
} catch (\Throwable $throwable) {
Activity::log("Failed to import <strong>{$this->filename}</strong>: {$throwable->getMessage()}", 'error');
throw $throwable;
}
}

protected function defaultDescription(): ?string
Expand Down
1 change: 1 addition & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function safeUp(): bool
$this->createTable(Activity::TABLE_NAME, [
'id' => $this->primaryKey(),
'message' => $this->text()->notNull(),
'type' => $this->string()->notNull(),
'userId' => $this->integer()->notNull(),
'username' => $this->string()->notNull(),
'dateCreated' => $this->dateTime()->notNull(),
Expand Down
36 changes: 36 additions & 0 deletions src/migrations/m240222_084504_add_activity_error_col.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace fostercommerce\variantmanager\migrations;

use Craft;
use craft\db\Migration;
use fostercommerce\variantmanager\records\Activity;

/**
* m240222_084504_add_activity_error_col migration.
*/
class m240222_084504_add_activity_error_col extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// Place migration code here...
$this->addColumn(Activity::TABLE_NAME, 'type', $this->string()->after('message'));
$this->update(Activity::TABLE_NAME, ['type' => 'success'], '', [], false);
$this->alterColumn(Activity::TABLE_NAME, 'type', $this->string()->after('message')->notNull());

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
$this->dropColumn(Activity::TABLE_NAME, 'type');

return true;
}
}
16 changes: 16 additions & 0 deletions src/records/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace fostercommerce\variantmanager\records;

use Craft;
use craft\db\ActiveRecord;
use craft\helpers\Db;

/**
* @property int $id
* @property string $message
* @property string $type
* @property string|null $dateCreated
*/
class Activity extends ActiveRecord
Expand All @@ -17,4 +20,17 @@ public static function tableName(): string
{
return self::TABLE_NAME;
}

public static function log(string $message, string $type = 'success'): void
{
$currentUser = Craft::$app->getUser()->identity;
$activity = new Activity([
'userId' => $currentUser->id,
'username' => $currentUser->username,
'type' => $type,
'message' => $message,
'dateCreated' => Db::prepareDateForDb(new \DateTime()),
]);
$activity->save();
}
}
19 changes: 6 additions & 13 deletions src/services/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use craft\commerce\models\ProductType;
use craft\commerce\Plugin as CommercePlugin;
use craft\errors\ElementNotFoundException;
use craft\helpers\Db;
use DateTime;
use fostercommerce\variantmanager\fields\VariantAttributesField;
use fostercommerce\variantmanager\helpers\FieldHelper;
use fostercommerce\variantmanager\Plugin;
Expand Down Expand Up @@ -52,13 +50,6 @@ class Csv extends Component
*/
public function import(string $filename, string $csvData, ?string $productTypeHandle): void
{
$currentUser = Craft::$app->getUser()->identity;
$activity = new Activity([
'userId' => $currentUser->id,
'username' => $currentUser->username,
'dateCreated' => Db::prepareDateForDb(new DateTime()),
]);

$tabularDataReader = $this->read($csvData);
$titleRecord = array_filter($tabularDataReader->fetchOne());
if ($titleRecord === [] || count($titleRecord) > 1) {
Expand Down Expand Up @@ -97,12 +88,14 @@ public function import(string $filename, string $csvData, ?string $productTypeHa

// Do this after save so that we can get the correct edit URL from a new product
if ($product->isNewForSite) {
$activity->message = "Imported new product <a class=\"go\" href=\"{$product->getCpEditUrl()}\">{$product->title}</a> into {$product->type->name}";
Activity::log(
"Imported new product <a class=\"go\" href=\"{$product->getCpEditUrl()}\">{$product->title}</a> into {$product->type->name}",
);
} else {
$activity->message = "Imported existing product <a class=\"go\" href=\"{$product->getCpEditUrl()}\">{$product->title}</a> into {$product->type->name}";
Activity::log(
"Imported existing product <a class=\"go\" href=\"{$product->getCpEditUrl()}\">{$product->title}</a> into {$product->type->name}",
);
}

$activity->save();
}

public function export(string $productId, array $options = []): array|bool
Expand Down
2 changes: 1 addition & 1 deletion src/templates/dashboard/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{% endif %}
</td>
<td data-title="{{ "Message"|t('variant-manager') }}">
{{ activity.message | raw }}
<span class="status {% if activity.type == 'success' %}green{% else %}red{% endif %}"></span>{{ activity.message | raw }}
</td>
<td data-title="{{ "Date"|t('variant-manager') }}">
{{ activity.dateCreated }}
Expand Down
14 changes: 1 addition & 13 deletions src/templates/fields/variant_upload.twig
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,18 @@
modal.show();
}).catch((error) => {
modal.hide();
uploadSpinner.classList.add('hidden');
btn.disabled = false;
btn.classList.remove('disabled');
try {
error = JSON.parse(error.message);
} catch (err) {
error = err;
}
confirmBtn.classList.add("hidden");
if (error.statusText) {
Craft.cp.displayError(`An error occurred: ${error.statusText}`);
} else if (error.message) {
Craft.cp.displayError(error.message);
}
modal.show();
});
return true;
Expand Down Expand Up @@ -231,11 +224,6 @@
uploadSpinner.classList.add('hidden');
primaryActions.classList.remove('hidden');
completeActions.classList.add('hidden');
try {
error = JSON.parse(error.message);
} catch (err) {
error = err;
}
confirmBtn.classList.add("hidden");
productTypeContainer.classList.add("hidden");
Expand Down

0 comments on commit 6189299

Please sign in to comment.