69 lines
2.6 KiB
JavaScript
69 lines
2.6 KiB
JavaScript
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" });
|
|
}
|
|
};
|