A free open source IT asset/license management system
Get rid of NotesComposer, moved to SyncAdapter base class
snipe3f0f4fc7d4b3
diff --git a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.phpindex 5a62828865..b5bd408b86 100644--- a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php+++ b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php@@ -495,14 +495,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter$touched[] = $path;}- $composed = $this->composeNotesForPush($asset);- if ($composed !== '') {- $notesTarget = $this->effectiveNotesTarget();- if ($notesTarget !== null) {- Arr::set($payload, $notesTarget, $composed);- $touched[] = $notesTarget;- }- }+ $this->applyComposedNotesToPayload($asset, $payload, $touched);return [$payload, $touched];}diff --git a/app/SyncAdapters/Intune/IntuneAdapter.php b/app/SyncAdapters/Intune/IntuneAdapter.phpindex e261c700e6..cd86eee3ae 100644--- a/app/SyncAdapters/Intune/IntuneAdapter.php+++ b/app/SyncAdapters/Intune/IntuneAdapter.php@@ -193,12 +193,12 @@ class IntuneAdapter extends SyncAdapter implements PushableAdapterreturn;}- $composed = $this->composeNotesForPush($asset);- if ($composed === '') {+ $composedNotes = $this->composeNotesForPush($asset);+ if ($composedNotes === null) {return;}- $payload = [$this->effectiveNotesTarget() => $composed];+ $payload = [$composedNotes['target'] => $composedNotes['value']];if ($this->isPushDryRun()) {Log::channel('sync-adapters')->info(sprintf(@@ -225,7 +225,7 @@ class IntuneAdapter extends SyncAdapter implements PushableAdapter'%s push: updated Intune managed device %s (%s)',$this->name(),$externalSource->external_id,- $this->effectiveNotesTarget(),+ $composedNotes['target'],));}}diff --git a/app/SyncAdapters/Jamf/JamfAdapter.php b/app/SyncAdapters/Jamf/JamfAdapter.phpindex 087d6afd99..05ef220e4a 100644--- a/app/SyncAdapters/Jamf/JamfAdapter.php+++ b/app/SyncAdapters/Jamf/JamfAdapter.php@@ -190,12 +190,7 @@ class JamfAdapter extends SyncAdapter implements PushableAdapter$touched[] = $path;}- $composed = $this->composeNotesForPush($asset);- if ($composed !== '') {- $notesTarget = $this->effectiveNotesTarget();- Arr::set($payload, $notesTarget, $composed);- $touched[] = $notesTarget;- }+ $this->applyComposedNotesToPayload($asset, $payload, $touched);if ($payload === []) {return;diff --git a/app/SyncAdapters/Kandji/KandjiAdapter.php b/app/SyncAdapters/Kandji/KandjiAdapter.phpindex a77c212d5f..edfe7ceada 100644--- a/app/SyncAdapters/Kandji/KandjiAdapter.php+++ b/app/SyncAdapters/Kandji/KandjiAdapter.php@@ -151,9 +151,9 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapter/*** Kandji devices carry a single freeform `notes` field that* admins commonly repurpose for composed asset info from- * Snipe-IT. When pushNotesTemplate() is set, we render it and- * PATCH the notes field alongside any other push-directed- * fields.+ * Snipe-IT. When the admin has set a push_notes_template we+ * render it via composeNotesForPush() and PATCH the notes field+ * alongside any other push-directed fields.*/public function notesFieldTarget(): ?string{@@ -195,14 +195,10 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapter$payload[$mapped] = $value;}- // Composed notes: template + effectiveNotesTarget (admin- // override or adapter default) merge into the same payload- // so admins can push both an asset_tag AND a composed notes- // blob in a single API call.- $composed = $this->composeNotesForPush($asset);- if ($composed !== '') {- $payload[$this->effectiveNotesTarget()] = $composed;- }+ // Composed notes merge into the same payload so admins can+ // push both an asset_tag AND a composed notes blob in a+ // single API call.+ $this->applyComposedNotesToPayload($asset, $payload);if ($payload === []) {return;diff --git a/app/SyncAdapters/Mosyle/MosyleAdapter.php b/app/SyncAdapters/Mosyle/MosyleAdapter.phpindex 562f4da49c..3748292f0e 100644--- a/app/SyncAdapters/Mosyle/MosyleAdapter.php+++ b/app/SyncAdapters/Mosyle/MosyleAdapter.php@@ -244,8 +244,8 @@ class MosyleAdapter extends SyncAdapter implements PushableAdapter*/private function pushComposedNotesField(Asset $asset, string $serial, ?MosyleClient &$client): array{- $composed = $this->composeNotesForPush($asset);- if ($composed === '') {+ $composedNotes = $this->composeNotesForPush($asset);+ if ($composedNotes === null) {return [];}@@ -254,14 +254,14 @@ class MosyleAdapter extends SyncAdapter implements PushableAdapter'%s push [dry-run]: would set Mosyle device serial=%s notes=%s',$this->name(),$serial,- $composed,+ $composedNotes['value'],));return ['notes'];}$client ??= new MosyleClient(baseUrl: $this->url(), token: $this->credential('token'));- $client->updateDeviceNotesBySerial($serial, $composed);+ $client->updateDeviceNotesBySerial($serial, $composedNotes['value']);return ['notes'];}diff --git a/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php b/app/SyncAdapters/NinjaOne/NinjaOneAdapter.phpindex ba44398e00..b2d6bc3e33 100644--- a/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php+++ b/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php@@ -253,10 +253,7 @@ class NinjaOneAdapter extends SyncAdapter implements PushableAdapter$payload[$assetTagFieldName] = $value;}- $composed = $this->composeNotesForPush($asset);- if ($composed !== '') {- $payload[$this->effectiveNotesTarget()] = $composed;- }+ $this->applyComposedNotesToPayload($asset, $payload);return $payload;}diff --git a/app/SyncAdapters/NotesComposer.php b/app/SyncAdapters/NotesComposer.phpdeleted file mode 100644index d9d79649fa..0000000000--- a/app/SyncAdapters/NotesComposer.php+++ /dev/null@@ -1,103 +0,0 @@-<?php--namespace App\SyncAdapters;--use App\Models\Asset;-use App\Models\CustomField;--/**- * Renders a multi-field notes blob from an Asset using a- * placeholder template. Used by PushableAdapter implementations- * whose vendor has a single freeform "notes" field per device and- * admins want to push a composed summary (asset_tag + status +- * assigned user + custom fields, etc.) rather than one Snipe-IT- * field to one vendor field.- *- * Placeholders use `{key}` syntax with a fixed vocabulary. Custom- * fields are addressed via `{custom.field_name}` where `field_name`- * is the exact CustomField name (case-sensitive). Unknown keys and- * null values render as empty strings so a template with references- * to missing custom fields degrades gracefully.- */-class NotesComposer-{- /**- * Compose the notes value for an asset from the template.- * Returns the empty string when the template is blank so- * callers can gate on `''` for "nothing to push".- */- public static function compose(Asset $asset, string $template): string- {- if (trim($template) === '') {- return '';- }-- // Match anything between braces that isn't itself a brace.- // Permissive so custom field names with spaces / punctuation- // ({custom.Cost Center}, {custom.Warranty (extended)}) match- // cleanly. The resolver decides validity.- return preg_replace_callback(- '/\{([^{}]+)\}/',- fn (array $m) => (string) self::resolve($asset, $m[1]),- $template,- ) ?? '';- }-- /**- * Resolve a single placeholder key against the asset. Native- * columns and relationship-derived values are hardcoded. Custom- * fields go through the `custom.field_name` path.- */- private static function resolve(Asset $asset, string $key): string- {- if (str_starts_with($key, 'custom.')) {- return self::resolveCustomField($asset, substr($key, 7));- }-- return match ($key) {- 'asset_tag' => (string) ($asset->asset_tag ?? ''),- 'name' => (string) ($asset->name ?? ''),- 'serial' => (string) ($asset->serial ?? ''),- 'model' => (string) ($asset->model?->name ?? ''),- 'model_number' => (string) ($asset->model?->model_number ?? ''),- 'manufacturer' => (string) ($asset->model?->manufacturer?->name ?? ''),- 'category' => (string) ($asset->model?->category?->name ?? ''),- 'status' => (string) ($asset->status?->name ?? ''),- 'status_type' => (string) ($asset->status?->getStatuslabelType() ?? ''),- 'assigned_to' => (string) ($asset->assignedTo?->present()?->name() ?? ''),- 'assigned_to_email' => (string) ($asset->assignedTo?->email ?? ''),- 'assigned_to_username' => (string) ($asset->assignedTo?->username ?? ''),- 'location' => (string) ($asset->location?->name ?? ''),- 'company' => (string) ($asset->company?->name ?? ''),- 'supplier' => (string) ($asset->supplier?->name ?? ''),- 'last_checkout' => (string) ($asset->last_checkout ?? ''),- 'last_checkin' => (string) ($asset->last_checkin ?? ''),- 'expected_checkin' => (string) ($asset->expected_checkin ?? ''),- 'notes' => (string) ($asset->notes ?? ''),- 'order_number' => (string) ($asset->order_number ?? ''),- 'purchase_date' => (string) ($asset->purchase_date ?? ''),- 'purchase_cost' => (string) ($asset->purchase_cost ?? ''),- 'warranty_months' => (string) ($asset->warranty_months ?? ''),- 'warranty_expires' => (string) ($asset->warranty_expires ?? ''),- default => '',- };- }-- /**- * Look up a custom field value by name. Snipe-IT custom fields- * store per-asset values on the asset row via a `_snipeit_...`- * db_column, so we resolve the CustomField -> db_column ->- * $asset->$dbColumn.- */- private static function resolveCustomField(Asset $asset, string $fieldName): string- {- $field = CustomField::query()->where('name', $fieldName)->first();- if ($field === null || $field->db_column === null) {- return '';- }-- $value = $asset->getAttribute($field->db_column);-- return $value === null ? '' : (string) $value;- }-}diff --git a/app/SyncAdapters/PushableAdapter.php b/app/SyncAdapters/PushableAdapter.phpindex 9d9e4a7723..d00a16fd68 100644--- a/app/SyncAdapters/PushableAdapter.php+++ b/app/SyncAdapters/PushableAdapter.php@@ -37,8 +37,8 @@ interface PushableAdapter* when the vendor has no such field (or when routing composed* notes would be a semantic stretch, e.g. hostname).*- * Push implementations render pushNotesTemplate() through- * NotesComposer and set the composed string on this field's+ * Push implementations call SyncAdapter::composeNotesForPush()+ * and set the returned ['value'] on the returned ['target']* position in the outgoing payload.*/public function notesFieldTarget(): ?string;diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.phpindex 0e6f271afe..1abfde4074 100644--- a/app/SyncAdapters/SyncAdapter.php+++ b/app/SyncAdapters/SyncAdapter.php@@ -2,6 +2,8 @@namespace App\SyncAdapters;+use App\Models\Asset;+use App\Models\CustomField;use App\Models\SyncAdapterConfig;use App\Models\SyncAdapterInstance;use App\Rules\ExternalUrl;@@ -744,51 +746,46 @@ abstract class SyncAdapter}/**- * Template string admins configure to compose a single-field- * notes blob from many Snipe-IT fields (asset_tag + status +- * assigned_to + custom fields, etc.). PushableAdapter- * implementations render this via NotesComposer and push the- * result to their vendor's notes-shape field. Empty string- * means "don't push notes."- */- public function pushNotesTemplate(): string- {- return (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_template');- }-- /**- * Admin-configured vendor field name (or dotted path) for the- * composed notes push. Blank = fall back to the adapter's- * suggested default via PushableAdapter::notesFieldTarget().- * Set this when the vendor exposes multiple candidate targets- * (Custom Attribute name for WS1, Custom Field name for- * NinjaOne, alternative path for Jamf, etc.).- */- public function pushNotesTargetOverride(): ?string- {- $stored = (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_target');-- return $stored === '' ? null : $stored;- }-- /**- * Effective vendor field for composed notes: the admin's- * override if set, else the adapter's suggested default via- * PushableAdapter::notesFieldTarget(). Returns null when- * neither is set (adapter doesn't expose a notes concept AND- * admin didn't name a target field).+ * Valid placeholder keys for the composed-notes push template.+ * Admins wrap these tokens in {curly-braces} inside their+ * push_notes_template to build a single-field notes blob from+ * many Snipe-IT fields. `{custom.Field Name}` accesses any+ * CustomField by its exact display name (case-sensitive).+ * Unknown placeholders and null values render as empty so a+ * template referencing missing fields degrades gracefully.+ *+ * Admin-facing descriptions of each placeholder live in the+ * `push_notes_template_help` translation, not here, so the+ * catalog stays translator-friendly.+ *+ * @var array<int, string>*/- public function effectiveNotesTarget(): ?string- {- $override = $this->pushNotesTargetOverride();- if ($override !== null) {- return $override;- }-- return $this instanceof \App\SyncAdapters\PushableAdapter- ? $this->notesFieldTarget()- : null;- }+ public const NOTES_TEMPLATE_PLACEHOLDERS = [+ 'asset_tag',+ 'name',+ 'serial',+ 'model',+ 'model_number',+ 'manufacturer',+ 'category',+ 'status',+ 'status_type',+ 'assigned_to',+ 'assigned_to_email',+ 'assigned_to_username',+ 'location',+ 'company',+ 'supplier',+ 'last_checkout',+ 'last_checkin',+ 'expected_checkin',+ 'notes',+ 'order_number',+ 'purchase_date',+ 'purchase_cost',+ 'warranty_months',+ 'warranty_expires',+ ];public function directionFor(string $field): string{@@ -837,10 +834,10 @@ abstract class SyncAdapter** @param array<int, string> $changedFields*/- public function pushPrologue(\App\Models\Asset $asset, array $changedFields): ?\App\Models\AssetExternalSource+ public function pushPrologue(Asset $asset, array $changedFields): ?\App\Models\AssetExternalSource{$pushFields = $this->pushDirectedFields();- $notesConfigured = $this->pushNotesTemplate() !== '' && $this->effectiveNotesTarget() !== null;+ $notesConfigured = $this->hasComposedNotesConfigured();if ($pushFields === [] && ! $notesConfigured) {return null;@@ -858,20 +855,171 @@ abstract class SyncAdapter}/**- * Render the composed-notes template against the given asset via- * NotesComposer, or return an empty string when the template or- * target isn't configured. Adapters call this and check for '' to- * decide whether to include composed notes in their push payload.+ * Composed notes payload for a push, or null when nothing to+ * push. Reads the admin's push_notes_template from config,+ * resolves the target vendor field (admin override or+ * PushableAdapter::notesFieldTarget() default), then substitutes+ * every {placeholder} against the given asset.+ *+ * Placeholders are enumerated in NOTES_TEMPLATE_PLACEHOLDERS.+ * `{custom.Field Name}` resolves any CustomField by exact+ * display name. Unknown keys and null values render as empty,+ * so a template referencing missing fields degrades gracefully+ * instead of throwing.+ *+ * Adapter push() implementations call this and check for null:+ * non-null returns give both the vendor field name (`target`)+ * and the rendered string (`value`), so the caller can splice+ * both into its payload in one hit.+ *+ * @return array{target: string, value: string}|null*/- public function composeNotesForPush(\App\Models\Asset $asset): string+ public function composeNotesForPush(Asset $asset): ?array{- $template = $this->pushNotesTemplate();- $target = $this->effectiveNotesTarget();- if ($template === '' || $target === null) {- return '';+ $template = (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_template');+ if (trim($template) === '') {+ return null;+ }++ $target = $this->resolveComposedNotesTarget();+ if ($target === null) {+ return null;+ }++ // Match anything between braces that isn't itself a brace.+ // Permissive so custom-field names with spaces or punctuation+ // ({custom.Cost Center}, {custom.Warranty (extended)}) match+ // cleanly. The inner match block below decides validity.+ $value = preg_replace_callback(+ '/\{([^{}]+)\}/',+ function (array $m) use ($asset): string {+ $key = $m[1];++ if (str_starts_with($key, 'custom.')) {+ $field = CustomField::query()->where('name', substr($key, 7))->first();+ if ($field === null || $field->db_column === null) {+ return '';+ }+ $stored = $asset->getAttribute($field->db_column);++ return $stored === null ? '' : (string) $stored;+ }++ return match ($key) {+ 'asset_tag' => (string) ($asset->asset_tag ?? ''),+ 'name' => (string) ($asset->name ?? ''),+ 'serial' => (string) ($asset->serial ?? ''),+ 'model' => (string) ($asset->model?->name ?? ''),+ 'model_number' => (string) ($asset->model?->model_number ?? ''),+ 'manufacturer' => (string) ($asset->model?->manufacturer?->name ?? ''),+ 'category' => (string) ($asset->model?->category?->name ?? ''),+ 'status' => (string) ($asset->status?->name ?? ''),+ 'status_type' => (string) ($asset->status?->getStatuslabelType() ?? ''),+ 'assigned_to' => (string) ($asset->assignedTo?->present()?->name() ?? ''),+ 'assigned_to_email' => (string) ($asset->assignedTo?->email ?? ''),+ 'assigned_to_username' => (string) ($asset->assignedTo?->username ?? ''),+ 'location' => (string) ($asset->location?->name ?? ''),+ 'company' => (string) ($asset->company?->name ?? ''),+ 'supplier' => (string) ($asset->supplier?->name ?? ''),+ 'last_checkout' => (string) ($asset->last_checkout ?? ''),+ 'last_checkin' => (string) ($asset->last_checkin ?? ''),+ 'expected_checkin' => (string) ($asset->expected_checkin ?? ''),+ 'notes' => (string) ($asset->notes ?? ''),+ 'order_number' => (string) ($asset->order_number ?? ''),+ 'purchase_date' => (string) ($asset->purchase_date ?? ''),+ 'purchase_cost' => (string) ($asset->purchase_cost ?? ''),+ 'warranty_months' => (string) ($asset->warranty_months ?? ''),+ 'warranty_expires' => (string) ($asset->warranty_expires ?? ''),+ default => '',+ };+ },+ $template,+ ) ?? '';++ return ['target' => $target, 'value' => $value];+ }++ /**+ * Splice the composed-notes payload (from composeNotesForPush)+ * into an outgoing vendor payload. Returns true when composed+ * notes were configured and spliced, false when there was+ * nothing to send. Callers that track which fields they pushed+ * (for post-push logging or event dispatch) pass their $touched+ * array by reference. The target field name gets appended to+ * it on success.+ *+ * Uses Arr::set so dotted target paths (Jamf's "general.notes"+ * shape) branch into nested payload structures rather than+ * writing a literal key with a dot in it.+ *+ * @param array<string, mixed> $payload+ * @param array<int, string> $touched+ */+ public function applyComposedNotesToPayload(Asset $asset, array &$payload, array &$touched = []): bool+ {+ $composedNotes = $this->composeNotesForPush($asset);+ if ($composedNotes === null) {+ return false;+ }++ \Illuminate\Support\Arr::set($payload, $composedNotes['target'], $composedNotes['value']);+ $touched[] = $composedNotes['target'];++ return true;+ }++ /**+ * Stored composed-notes template + target-override, exposed for+ * the settings-page form so it can pre-fill the two inputs.+ * Runtime callers use composeNotesForPush() instead, which+ * resolves the effective target (falling back to+ * PushableAdapter::notesFieldTarget()) and does the substitution+ * in one hit.+ *+ * @return array{template: string, target_override: ?string}+ */+ public function storedPushNotesConfig(): array+ {+ $template = (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_template');+ $target = (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_target');++ return [+ 'template' => $template,+ 'target_override' => $target === '' ? null : $target,+ ];+ }++ /**+ * Cheap "should we bother trying to push composed notes?" check+ * used by pushPrologue to gate the vendor call without paying+ * the cost of rendering the template. True when the admin has a+ * non-blank push_notes_template AND a resolvable target field.+ */+ private function hasComposedNotesConfigured(): bool+ {+ $template = (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_template');++ return trim($template) !== '' && $this->resolveComposedNotesTarget() !== null;+ }++ /**+ * Vendor field name for the composed-notes push. Admin's+ * override (push_notes_target config key) wins if set. Falls+ * back to the adapter's suggested default via+ * PushableAdapter::notesFieldTarget(). Null when neither is+ * set (adapter has no notes concept AND admin didn't name a+ * target field).+ */+ private function resolveComposedNotesTarget(): ?string+ {+ $stored = (string) SyncAdapterConfig::get($this->instance->id, 'push_notes_target');+ if ($stored !== '') {+ return $stored;}- return NotesComposer::compose($asset, $template);+ return $this instanceof PushableAdapter+ ? $this->notesFieldTarget()+ : null;}/**diff --git a/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php b/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.phpindex d1fdb4bde9..02492e38c8 100644--- a/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php+++ b/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php@@ -202,15 +202,15 @@ class WorkspaceOneAdapter extends SyncAdapter implements PushableAdapter}$payload = $this->buildDevicePayload($asset);- $composed = $this->composeNotesForPush($asset);+ $composedNotes = $this->composeNotesForPush($asset);- if ($payload === [] && $composed === '') {+ if ($payload === [] && $composedNotes === null) {return;}$client = $this->isPushDryRun() ? null : $this->buildClient();$this->pushDeviceFields($externalSource->external_id, $payload, $client);- $this->pushCustomAttribute($externalSource->external_id, $composed, $client);+ $this->pushCustomAttribute($externalSource->external_id, $composedNotes, $client);}/**@@ -275,31 +275,32 @@ class WorkspaceOneAdapter extends SyncAdapter implements PushableAdapter* /devices/{uuid}/customattributes. Separate endpoint from the* top-level device fields, so this fires independently.*/- private function pushCustomAttribute(string $uuid, string $composed, ?WorkspaceOneClient $client): void+ /**+ * @param array{target: string, value: string}|null $composedNotes+ */+ private function pushCustomAttribute(string $uuid, ?array $composedNotes, ?WorkspaceOneClient $client): void{- if ($composed === '') {+ if ($composedNotes === null) {return;}- $notesTarget = $this->effectiveNotesTarget();-if ($client === null) {Log::channel('sync-adapters')->info(sprintf('%s push [dry-run]: would set Workspace ONE Custom Attribute %s=%s on device %s',$this->name(),- $notesTarget,- $composed,+ $composedNotes['target'],+ $composedNotes['value'],$uuid,));return;}- $client->updateDeviceCustomAttribute($uuid, $notesTarget, $composed);+ $client->updateDeviceCustomAttribute($uuid, $composedNotes['target'], $composedNotes['value']);Log::channel('sync-adapters')->info(sprintf('%s push: set Workspace ONE Custom Attribute "%s" on device %s',$this->name(),- $notesTarget,+ $composedNotes['target'],$uuid,));}diff --git a/resources/views/blade/sync-adapter/form.blade.php b/resources/views/blade/sync-adapter/form.blade.phpindex 1431eab61e..d9fae02567 100644--- a/resources/views/blade/sync-adapter/form.blade.php+++ b/resources/views/blade/sync-adapter/form.blade.php@@ -527,9 +527,12 @@:help_text="trans('admin/settings/sync_adapters.push_notes_target_help')"><x-slot:input>+ @php+ $storedNotesConfig = $adapter->storedPushNotesConfig();+ @endphp<x-input.text:name="$slug . '_push_notes_target'"- :value="old($slug . '_push_notes_target', $adapter->pushNotesTargetOverride() ?? '')"+ :value="old($slug . '_push_notes_target', $storedNotesConfig['target_override'] ?? '')":placeholder="$adapter->notesFieldTarget() ?? trans('admin/settings/sync_adapters.push_notes_target_placeholder_none')":disabled="$locked"/>@@ -549,7 +552,7 @@rows="6"@disabled($locked)style="font-family: monospace; white-space: pre;"- >{{ old($slug . '_push_notes_template', $adapter->pushNotesTemplate()) }}</textarea>+ >{{ old($slug . '_push_notes_template', $storedNotesConfig['template']) }}</textarea></x-slot:input></x-form.row></fieldset>diff --git a/tests/Feature/SyncAdapters/ComposedNotesForPushTest.php b/tests/Feature/SyncAdapters/ComposedNotesForPushTest.phpnew file mode 100644index 0000000000..22ed1ff465--- /dev/null+++ b/tests/Feature/SyncAdapters/ComposedNotesForPushTest.php@@ -0,0 +1,231 @@+<?php++namespace Tests\Feature\SyncAdapters;++use App\Models\Asset;+use App\Models\AssetModel;+use App\Models\Category;+use App\Models\CustomField;+use App\Models\Manufacturer;+use App\Models\Statuslabel;+use App\Models\SyncAdapterConfig;+use App\Models\SyncAdapterInstance;+use App\SyncAdapters\Fleet\FleetAdapter;+use Illuminate\Support\Facades\Crypt;+use PHPUnit\Framework\Attributes\Test;+use Tests\TestCase;++/**+ * Coverage for SyncAdapter::composeNotesForPush(), the folded-onto-+ * base implementation of the composed-notes push template. Previously+ * lived as its own NotesComposer helper class. Test shape mirrors+ * the old NotesComposerTest but hits the base class directly via a+ * concrete adapter, and asserts the new tuple return shape+ * (target + value) instead of a bare string.+ */+class ComposedNotesForPushTest extends TestCase+{+ #[Test]+ public function returns_null_when_no_template_configured(): void+ {+ $adapter = $this->configuredFleetAdapter();+ $asset = $this->makeAsset(['asset_tag' => 'ABC-123']);++ $this->assertNull($adapter->composeNotesForPush($asset));+ }++ #[Test]+ public function returns_null_when_template_is_whitespace_only(): void+ {+ $adapter = $this->configuredFleetAdapter();+ SyncAdapterConfig::put($adapter->getInstanceId(), 'push_notes_template', " \n\t");+ $asset = $this->makeAsset(['asset_tag' => 'ABC-123']);++ $this->assertNull($adapter->composeNotesForPush($asset));+ }++ #[Test]+ public function returns_null_when_no_target_can_be_resolved(): void+ {+ // Fleet doesn't ship a notesFieldTarget default, so with no+ // admin override for push_notes_target we can't resolve one.+ $adapter = $this->configuredFleetAdapter();+ SyncAdapterConfig::put($adapter->getInstanceId(), 'push_notes_template', 'Tag: {asset_tag}');+ $asset = $this->makeAsset(['asset_tag' => 'ABC-123']);++ $this->assertNull($adapter->composeNotesForPush($asset));+ }++ #[Test]+ public function substitutes_native_placeholders_from_asset_columns(): void+ {+ $adapter = $this->configuredFleetAdapterWithNotesTarget();+ SyncAdapterConfig::put(+ $adapter->getInstanceId(),+ 'push_notes_template',+ "Tag: {asset_tag}\nName: {name}\nSerial: {serial}\nStatus: {status}",+ );+ $status = Statuslabel::factory()->rtd()->create(['name' => 'Deployable']);+ $asset = $this->makeAsset([+ 'asset_tag' => 'ABC-1234',+ 'name' => 'workstation-01',+ 'serial' => 'FW-ABC-123',+ 'status_id' => $status->id,+ ]);++ $result = $adapter->composeNotesForPush($asset);++ $this->assertNotNull($result);+ $this->assertSame('notes', $result['target']);+ $this->assertSame(+ "Tag: ABC-1234\nName: workstation-01\nSerial: FW-ABC-123\nStatus: Deployable",+ $result['value'],+ );+ }++ #[Test]+ public function admin_target_override_wins_over_adapter_default(): void+ {+ $adapter = $this->configuredFleetAdapter();+ SyncAdapterConfig::put($adapter->getInstanceId(), 'push_notes_template', 'Tag: {asset_tag}');+ SyncAdapterConfig::put($adapter->getInstanceId(), 'push_notes_target', 'custom.notes_field');+ $asset = $this->makeAsset(['asset_tag' => 'ABC-123']);++ $result = $adapter->composeNotesForPush($asset);++ $this->assertNotNull($result);+ $this->assertSame('custom.notes_field', $result['target']);+ $this->assertSame('Tag: ABC-123', $result['value']);+ }++ #[Test]+ public function unknown_placeholders_render_blank(): void+ {+ $adapter = $this->configuredFleetAdapterWithNotesTarget();+ SyncAdapterConfig::put(+ $adapter->getInstanceId(),+ 'push_notes_template',+ 'Tag: {asset_tag} | Unknown: {not_a_real_field} | End',+ );+ $asset = $this->makeAsset(['asset_tag' => 'ABC-1']);++ $result = $adapter->composeNotesForPush($asset);++ $this->assertNotNull($result);+ $this->assertSame('Tag: ABC-1 | Unknown: | End', $result['value']);+ }++ #[Test]+ public function null_native_values_render_blank_not_literal(): void+ {+ $adapter = $this->configuredFleetAdapterWithNotesTarget();+ SyncAdapterConfig::put(+ $adapter->getInstanceId(),+ 'push_notes_template',+ 'Notes: {notes}!',+ );+ $asset = $this->makeAsset(['asset_tag' => 'X', 'notes' => null]);++ $result = $adapter->composeNotesForPush($asset);++ $this->assertNotNull($result);+ // Null asset->notes renders as empty string, not the literal {notes}.+ $this->assertSame('Notes: !', $result['value']);+ }++ #[Test]+ public function custom_field_placeholder_resolves_by_display_name(): void+ {+ $adapter = $this->configuredFleetAdapterWithNotesTarget();++ $field = CustomField::factory()->create([+ 'element' => 'text',+ 'name' => 'Cost Center',+ ]);+ $asset = $this->makeAsset(['asset_tag' => 'CC-1']);+ $asset->setAttribute($field->db_column, 'FINANCE-42');+ $asset->save();++ SyncAdapterConfig::put(+ $adapter->getInstanceId(),+ 'push_notes_template',+ 'CC: {custom.Cost Center}',+ );++ $result = $adapter->composeNotesForPush($asset);++ $this->assertNotNull($result);+ $this->assertSame('CC: FINANCE-42', $result['value']);+ }++ #[Test]+ public function custom_field_with_unknown_name_renders_blank(): void+ {+ $adapter = $this->configuredFleetAdapterWithNotesTarget();+ SyncAdapterConfig::put(+ $adapter->getInstanceId(),+ 'push_notes_template',+ 'Missing: {custom.Nonexistent Field}!',+ );+ $asset = $this->makeAsset(['asset_tag' => 'X']);++ $result = $adapter->composeNotesForPush($asset);++ $this->assertNotNull($result);+ $this->assertSame('Missing: !', $result['value']);+ }++ // ------------------------------------------------------------+ // Helpers+ // ------------------------------------------------------------++ /**+ * @param array<string, mixed> $overrides+ */+ private function makeAsset(array $overrides = []): Asset+ {+ $status = Statuslabel::factory()->rtd()->create();+ $manufacturer = Manufacturer::factory()->create();+ $category = Category::factory()->assetLaptopCategory()->create();+ $model = AssetModel::factory()->create([+ 'category_id' => $category->id,+ 'manufacturer_id' => $manufacturer->id,+ ]);++ return Asset::factory()->create(array_merge([+ 'model_id' => $model->id,+ 'status_id' => $status->id,+ ], $overrides));+ }++ private function configuredFleetAdapter(): FleetAdapter+ {+ $instance = SyncAdapterInstance::create([+ 'adapter_type' => 'fleet',+ 'label' => 'Test Fleet '.uniqid(),+ 'active' => true,+ ]);+ SyncAdapterConfig::put($instance->id, 'url', 'https://fleet.example.test');+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));++ return new class($instance) extends FleetAdapter+ {+ public function getInstanceId(): int+ {+ return $this->instance->id;+ }+ };+ }++ /**+ * Fleet with an admin-set notes target so composeNotesForPush()+ * can resolve `target` and actually build a payload.+ */+ private function configuredFleetAdapterWithNotesTarget(): FleetAdapter+ {+ $adapter = $this->configuredFleetAdapter();+ SyncAdapterConfig::put($adapter->getInstanceId(), 'push_notes_target', 'notes');++ return $adapter;+ }+}diff --git a/tests/Feature/SyncAdapters/NotesComposerTest.php b/tests/Feature/SyncAdapters/NotesComposerTest.phpdeleted file mode 100644index c4820555fc..0000000000--- a/tests/Feature/SyncAdapters/NotesComposerTest.php+++ /dev/null@@ -1,118 +0,0 @@-<?php--namespace Tests\Feature\SyncAdapters;--use App\Models\Asset;-use App\Models\CustomField;-use App\Models\Statuslabel;-use App\SyncAdapters\NotesComposer;-use Tests\TestCase;--/**- * Coverage for NotesComposer, the helper that renders composed- * notes blobs from Snipe-IT assets using placeholder templates.- * Adapters use this for push-to-notes-field flows (Intune, Kandji,- * Jamf, Mosyle).- */-class NotesComposerTest extends TestCase-{- public function test_renders_native_placeholders()- {- $status = Statuslabel::factory()->rtd()->create(['name' => 'Deployable']);- $asset = Asset::factory()->create([- 'asset_tag' => 'ABC-1234',- 'name' => 'workstation-01',- 'serial' => 'FW-ABC-123',- 'status_id' => $status->id,- ]);-- $out = NotesComposer::compose(- $asset,- "Tag: {asset_tag}\nName: {name}\nSerial: {serial}\nStatus: {status}",- );-- $this->assertSame("Tag: ABC-1234\nName: workstation-01\nSerial: FW-ABC-123\nStatus: Deployable", $out);- }-- public function test_unknown_placeholders_render_blank()- {- $asset = Asset::factory()->create(['asset_tag' => 'ABC-1']);-- $out = NotesComposer::compose(- $asset,- 'Tag: {asset_tag} | Unknown: {not_a_real_field} | End',- );-- $this->assertSame('Tag: ABC-1 | Unknown: | End', $out);- }-- public function test_null_native_values_render_blank_not_literal()- {- $asset = Asset::factory()->create(['asset_tag' => 'X', 'notes' => null]);-- $out = NotesComposer::compose($asset, 'Notes: {notes}!');-- // Null asset->notes renders as empty string, not the literal {notes}.- $this->assertSame('Notes: !', $out);- }-- public function test_custom_field_placeholder_resolves_by_name()- {- $customField = CustomField::factory()->create([- 'element' => 'text',- 'name' => 'Cost Center',- ]);- $asset = Asset::factory()->create(['asset_tag' => 'CC-1']);- // Custom fields are stored on the asset via db_column.- $asset->setAttribute($customField->db_column, 'FINANCE-42');- $asset->save();-- $out = NotesComposer::compose($asset, 'CC: {custom.Cost Center}');-- $this->assertSame('CC: FINANCE-42', $out);- }-- public function test_custom_field_with_unknown_name_renders_blank()- {- $asset = Asset::factory()->create(['asset_tag' => 'X']);-- $out = NotesComposer::compose($asset, 'Missing: {custom.Nonexistent Field}!');-- $this->assertSame('Missing: !', $out);- }-- public function test_blank_template_returns_empty_string()- {- $asset = Asset::factory()->create();-- $this->assertSame('', NotesComposer::compose($asset, ''));- $this->assertSame('', NotesComposer::compose($asset, " \n\t"));- }-- public function test_composed_intune_style_template_matches_brady_script_shape()- {- // Sanity check that a template shaped like Brady Widener's- // community PowerShell (asset tag + status + checkout info)- // composes cleanly. That's the canonical Intune push use case.- $status = Statuslabel::factory()->rtd()->create(['name' => 'Deployable']);- $asset = Asset::factory()->create([- 'asset_tag' => 'IT-42',- 'status_id' => $status->id,- 'last_checkout' => '2026-01-15 10:00:00',- ]);-- $template = <<<'TPL'- [INFO FROM SNIPE-IT]- Asset Tag: {asset_tag}- Status: {status}- Assigned to: {assigned_to}- Last checkout: {last_checkout}- TPL;-- $out = NotesComposer::compose($asset, $template);-- $this->assertStringContainsString('Asset Tag: IT-42', $out);- $this->assertStringContainsString('Status: Deployable', $out);- $this->assertStringContainsString('Last checkout: 2026-01-15', $out);- }-}