@opengis/fastify-table
Version:
core-plugins
33 lines (32 loc) • 1.27 kB
JavaScript
import pgClients from "../../../plugins/pg/pgClients.js";
import getSelectMeta from "../../../plugins/table/funcs/getSelectMeta.js";
import { addUserCls, addCustomCls, editUserCls, getUserCls, } from "../../../plugins/usercls/index.js";
export default async function editUserClsApi({ pg = pgClients.client, body, params, user }, reply) {
const { name } = params;
const { data, table, description, alias } = body;
const t1 = Date.now();
if (!user?.uid) {
return reply.status(401).send({ error: "unauthorized", code: 401 });
}
if (table) {
await addCustomCls({ name, data, description, alias, table, uid: user.uid }, pg);
return reply
.status(200)
.send({ time: Date.now() - t1, name, type: "custom" });
}
const exists = await getUserCls(name, pg);
if (exists) {
await editUserCls({ name, data, description, alias }, pg);
}
else {
await addUserCls({ name, data, description, alias }, pg);
}
// refresh metadata to avoid suggest API cache
const test = await getSelectMeta({
pg,
name,
nocache: 1,
});
const result = await getUserCls(name, pg);
return reply.status(200).send({ time: Date.now() - t1, name, ...result });
}