@opengis/fastify-table
Version:
core-plugins
127 lines (126 loc) • 4.71 kB
JavaScript
import { config, applyHook, getTemplate, pgClients, getAccess, setToken, getSelectMeta, getToken, } from "../../../../utils.js";
import getEditData from "../../crud/controllers/table.js";
import formatSchema from "./utils/formatSchema.js";
const q = `select
property_key as key,
property_json as json,
property_int as int,
property_text as text
from admin.properties
where 1=1`;
export default async function getForm({ pg = pgClients.client, params, user = {}, query = {}, }, reply) {
const time = Date.now();
const edit = query.edit || params.id;
if (!user?.uid) {
return reply.status(401).send("unauthorized");
}
const hookData = (await applyHook("preForm", {
table: params.name,
user,
}));
if (hookData?.message && hookData?.status) {
return reply.status(hookData?.status).send(hookData?.message);
}
const tokenData = (await getToken({ token: params.name, uid: user?.uid, json: 1 })) || {};
const table = tokenData?.table || hookData?.table || params.name;
const loadTable = (await getTemplate("table", table)) || {};
const { title = loadTable.ua, description } = loadTable;
const form = tokenData?.form || hookData?.form || loadTable.form;
if (!form) {
// return reply.status(404).send("form not found");
}
const { actions = [] } = (await getAccess({ table, form, user }, pg)) || {};
const loadTemplate = await getTemplate("form", form || params.name);
if (!loadTemplate) {
return reply.status(404).send("form template not found");
}
const { schema = loadTemplate } = loadTemplate;
await Promise.all(Object.keys(schema || {})
.filter((key) => schema[key]?.data)
.map(async (key) => {
const { arr } = (await getSelectMeta({ name: schema[key].data, pg })) || {};
if (arr) {
schema[key].data = undefined;
schema[key].options = arr;
}
}));
if (loadTemplate && tokenData.obj) {
const obj = tokenData.obj.split("#").reduce((p, el) => ({
...p,
[el.split("=")[0] || ""]: el.split("=")[1],
}), {});
Object.assign(loadTemplate, { obj });
}
const res = {
mode: "form",
time: 0,
meta: { title, description },
...loadTemplate,
schema,
};
if (user.uid) {
// form in form addToken
formatSchema(schema, user, loadTemplate?.obj);
const id = edit || tokenData?.id;
const isAllowedByTemplate = id
? actions.includes("edit")
: actions.includes("add");
if (!isAllowedByTemplate &&
!config.local &&
process.env.NODE_ENV !== "test" &&
!(tokenData?.form || hookData?.form) &&
form) {
return reply.status(403).send("access restricted: actions");
}
const token = setToken({
ids: [JSON.stringify({ id, table, form })],
uid: user.uid,
array: 1,
})?.[0];
if (!id) {
const nextId = await pg
.query("select next_id() as id")
.then((el) => el.rows[0].id);
Object.assign(res, {
token,
mode: "add",
id: nextId,
});
}
else {
const data = await getEditData({
pg,
params: { table, id },
user,
}, reply, true);
Object.assign(res, {
token,
data,
mode: "edit",
id,
});
}
}
// replace settings
const arr = JSON.stringify(loadTemplate).match(/{{settings.([^}]*)}}/g);
if (arr?.length) {
const string = JSON.stringify(loadTemplate);
const rows = pg?.pk?.["admin.properties"]
? await pg.query(q).then((el) => el.rows || [])
: [];
const settings = rows.reduce((acc, curr) => Object.assign(acc, { [curr.key]: curr.json || curr.int || curr.text }), {});
const match = arr.reduce((acc, curr) => Object.assign(acc, {
[curr]: settings[curr.replace(/^{{settings./g, "").replace(/}}$/, "")],
}), {});
const result = Object.keys(match).reduce((s, m) => s.replace(m, match[m]), string);
Object.assign(res, { time: Date.now() - time, schema: JSON.parse(result) });
return res;
}
Object.assign(res, { time: Date.now() - time });
const res1 = await applyHook("afterForm", {
table: hookData?.table || params.name,
payload: res,
user,
});
return reply.status(200).send(res1 || res);
}