zenv item sync for admins?

This commit is contained in:
24c02 2026-01-15 17:53:50 -05:00
parent c64c9a7a3c
commit 433597368b
4 changed files with 79 additions and 43 deletions

View file

@ -1,48 +1,12 @@
module Admin module Admin
module Warehouse module Warehouse
class SKUsController < Admin::ApplicationController class SKUsController < Admin::ApplicationController
# Overwrite any of the RESTful controller actions to implement custom behavior def sync_to_zenventory
# For example, you may want to send an email after a foo is updated. requested_resource.sync_to_zenventory!
# redirect_to [:admin, requested_resource], notice: "Synced to Zenventory"
# def update rescue Zenventory::ZenventoryError => e
# super redirect_to [:admin, requested_resource], alert: "Zenventory sync failed: #{e.message}"
# send_foo_updated_email(requested_resource) end
# end
# Override this method to specify custom lookup behavior.
# This will be used to set the resource for the `show`, `edit`, and `update`
# actions.
#
# def find_resource(param)
# Foo.find_by!(slug: param)
# end
# The result of this lookup will be available as `requested_resource`
# Override this if you have certain roles that require a subset
# this will be used to set the records shown on the `index` action.
#
# def scoped_resource
# if current_user.super_admin?
# resource_class
# else
# resource_class.with_less_stuff
# end
# end
# Override `resource_params` if you want to transform the submitted
# data before it's persisted. For example, the following would turn all
# empty values into nil values. It uses other APIs such as `resource_class`
# and `dashboard`:
#
# def resource_params
# params.require(resource_class.model_name.param_key).
# permit(dashboard.permitted_attributes(action_name)).
# transform_values { |value| value == "" ? nil : value }
# end
# See https://administrate-demo.herokuapp.com/customizing_controller_actions
# for more information
end end
end end
end end

View file

@ -61,4 +61,23 @@ class Warehouse::SKU < ApplicationRecord
} }
has_zenventory_url "https://app.zenventory.com/admin/item-details/%s/basic", :zenventory_id has_zenventory_url "https://app.zenventory.com/admin/item-details/%s/basic", :zenventory_id
def sync_to_zenventory!
params = {
sku: sku,
description: name,
category: category&.to_s&.humanize,
active: enabled || false,
unitCost: declared_unit_cost,
userField1: country_of_origin,
userField2: hs_code,
}.compact
if zenventory_id.present?
Zenventory.update_item(zenventory_id, params)
else
response = Zenventory.create_item(params)
update!(zenventory_id: response[:id].to_s)
end
end
end end

View file

@ -0,0 +1,49 @@
<% content_for(:title) { t("administrate.actions.show_resource", name: page.page_title) } %>
<header class="main-content__header">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to(
t("administrate.actions.edit_resource", name: page.page_title),
[:edit, namespace, page.resource],
class: "button",
) if accessible_action?(page.resource, :edit) %>
<%= button_to(
"Sync to Zenventory",
sync_to_zenventory_admin_warehouse_sku_path(page.resource),
method: :post,
class: "button button--secondary",
form: { style: "display: inline" },
) %>
<% if page.resource.zenventory_url.present? %>
<%= link_to(
"View on Zenventory",
page.resource.zenventory_url,
class: "button button--secondary",
target: "_blank",
) %>
<% end %>
<%= link_to(
t("administrate.actions.destroy"),
[namespace, page.resource],
class: "button button--danger",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) if accessible_action?(page.resource, :destroy) %>
</div>
</header>
<section class="main-content__body">
<dl>
<% page.attributes.each do |attribute| %>
<dt class="attribute-label" id="<%= attribute.name %>">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: page.resource.class.human_attribute_name(attribute.name),
) %>
</dt>
<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
><%= render_field attribute, page: page %></dd>
<% end %>
</dl>
</section>

View file

@ -504,7 +504,11 @@ Rails.application.routes.draw do
namespace :warehouse do namespace :warehouse do
resources :templates resources :templates
resources :orders resources :orders
resources :skus resources :skus do
member do
post :sync_to_zenventory
end
end
end end
namespace :usps do namespace :usps do