ux improved
This commit is contained in:
68
src/modules/0-ui/controllers/aliases.js
Normal file
68
src/modules/0-ui/controllers/aliases.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import { handleListAliases, handleCreateAlias, handleUpdateAlias, handleDeleteAlias } from "../handlers/aliases.js";
|
||||
|
||||
export const makeListAliases = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const q = req.query.q || "";
|
||||
const woo_product_id = req.query.woo_product_id ? parseInt(req.query.woo_product_id, 10) : null;
|
||||
const limit = req.query.limit || "200";
|
||||
const result = await handleListAliases({ tenantId, q, woo_product_id, limit });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeCreateAlias = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const { alias, woo_product_id, boost, category_hint, metadata } = req.body || {};
|
||||
|
||||
if (!alias || !woo_product_id) {
|
||||
return res.status(400).json({ ok: false, error: "alias_and_woo_product_id_required" });
|
||||
}
|
||||
|
||||
const result = await handleCreateAlias({ tenantId, alias, woo_product_id, boost, category_hint, metadata });
|
||||
res.json({ ok: true, item: result });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
if (err.code === "23505") { // unique violation
|
||||
return res.status(409).json({ ok: false, error: "alias_already_exists" });
|
||||
}
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeUpdateAlias = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const alias = req.params.alias;
|
||||
const { woo_product_id, boost, category_hint, metadata } = req.body || {};
|
||||
|
||||
if (!woo_product_id) {
|
||||
return res.status(400).json({ ok: false, error: "woo_product_id_required" });
|
||||
}
|
||||
|
||||
const result = await handleUpdateAlias({ tenantId, alias, woo_product_id, boost, category_hint, metadata });
|
||||
if (!result) {
|
||||
return res.status(404).json({ ok: false, error: "alias_not_found" });
|
||||
}
|
||||
res.json({ ok: true, item: result });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeDeleteAlias = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const alias = req.params.alias;
|
||||
const result = await handleDeleteAlias({ tenantId, alias });
|
||||
res.json({ ok: true, ...result });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { handleSearchProducts } from "../handlers/products.js";
|
||||
import { handleSearchProducts, handleListProducts, handleGetProduct, handleSyncProducts } from "../handlers/products.js";
|
||||
|
||||
export const makeSearchProducts = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
@@ -14,3 +14,43 @@ export const makeSearchProducts = (tenantIdOrFn) => async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const makeListProducts = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const q = req.query.q || "";
|
||||
const limit = req.query.limit || "2000";
|
||||
const offset = req.query.offset || "0";
|
||||
const result = await handleListProducts({ tenantId, q, limit, offset });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeGetProduct = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const wooProductId = req.params.id;
|
||||
const result = await handleGetProduct({ tenantId, wooProductId });
|
||||
if (!result) {
|
||||
return res.status(404).json({ ok: false, error: "product_not_found" });
|
||||
}
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeSyncProducts = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const result = await handleSyncProducts({ tenantId });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
88
src/modules/0-ui/controllers/recommendations.js
Normal file
88
src/modules/0-ui/controllers/recommendations.js
Normal file
@@ -0,0 +1,88 @@
|
||||
import {
|
||||
handleListRecommendations,
|
||||
handleGetRecommendation,
|
||||
handleCreateRecommendation,
|
||||
handleUpdateRecommendation,
|
||||
handleDeleteRecommendation
|
||||
} from "../handlers/recommendations.js";
|
||||
|
||||
export const makeListRecommendations = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const q = req.query.q || "";
|
||||
const limit = req.query.limit || "200";
|
||||
const result = await handleListRecommendations({ tenantId, q, limit });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeGetRecommendation = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const id = req.params.id;
|
||||
const result = await handleGetRecommendation({ tenantId, id });
|
||||
if (!result) {
|
||||
return res.status(404).json({ ok: false, error: "recommendation_not_found" });
|
||||
}
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeCreateRecommendation = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const { rule_key, trigger, queries, boosts, ask_slots, active, priority } = req.body || {};
|
||||
|
||||
if (!rule_key) {
|
||||
return res.status(400).json({ ok: false, error: "rule_key_required" });
|
||||
}
|
||||
|
||||
const result = await handleCreateRecommendation({
|
||||
tenantId, rule_key, trigger, queries, boosts, ask_slots, active, priority
|
||||
});
|
||||
res.json({ ok: true, item: result });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
if (err.code === "23505") { // unique violation
|
||||
return res.status(409).json({ ok: false, error: "rule_key_already_exists" });
|
||||
}
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeUpdateRecommendation = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const id = req.params.id;
|
||||
const { trigger, queries, boosts, ask_slots, active, priority } = req.body || {};
|
||||
|
||||
const result = await handleUpdateRecommendation({
|
||||
tenantId, id, trigger, queries, boosts, ask_slots, active, priority
|
||||
});
|
||||
if (!result) {
|
||||
return res.status(404).json({ ok: false, error: "recommendation_not_found" });
|
||||
}
|
||||
res.json({ ok: true, item: result });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
export const makeDeleteRecommendation = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const id = req.params.id;
|
||||
const result = await handleDeleteRecommendation({ tenantId, id });
|
||||
res.json({ ok: true, ...result });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user