@opengis/fastify-table 2.0.66 → 2.0.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/functions.d.ts +39 -0
  2. package/dist/functions.d.ts.map +1 -0
  3. package/dist/functions.js +412 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +15 -418
  6. package/dist/server/routes/access/index.d.ts +1 -1
  7. package/dist/server/routes/access/index.d.ts.map +1 -1
  8. package/dist/server/routes/access/index.js +1 -1
  9. package/dist/server/routes/auth/index.d.ts +1 -1
  10. package/dist/server/routes/auth/index.d.ts.map +1 -1
  11. package/dist/server/routes/auth/index.js +1 -1
  12. package/dist/server/routes/cron/index.d.ts +1 -1
  13. package/dist/server/routes/cron/index.d.ts.map +1 -1
  14. package/dist/server/routes/cron/index.js +1 -1
  15. package/dist/server/routes/crud/index.d.ts +1 -1
  16. package/dist/server/routes/crud/index.d.ts.map +1 -1
  17. package/dist/server/routes/crud/index.js +1 -1
  18. package/dist/server/routes/dblist/index.d.ts +1 -1
  19. package/dist/server/routes/dblist/index.d.ts.map +1 -1
  20. package/dist/server/routes/dblist/index.js +1 -1
  21. package/dist/server/routes/file/index.d.ts +1 -1
  22. package/dist/server/routes/file/index.d.ts.map +1 -1
  23. package/dist/server/routes/file/index.js +1 -1
  24. package/dist/server/routes/grpc/index.d.ts +1 -1
  25. package/dist/server/routes/grpc/index.d.ts.map +1 -1
  26. package/dist/server/routes/grpc/index.js +1 -1
  27. package/dist/server/routes/logger/index.d.ts +1 -1
  28. package/dist/server/routes/logger/index.d.ts.map +1 -1
  29. package/dist/server/routes/logger/index.js +1 -1
  30. package/dist/server/routes/menu/index.d.ts +1 -1
  31. package/dist/server/routes/menu/index.d.ts.map +1 -1
  32. package/dist/server/routes/menu/index.js +1 -1
  33. package/dist/server/routes/properties/index.d.ts +1 -1
  34. package/dist/server/routes/properties/index.d.ts.map +1 -1
  35. package/dist/server/routes/properties/index.js +1 -1
  36. package/dist/server/routes/table/functions/getData.d.ts.map +1 -1
  37. package/dist/server/routes/table/functions/getData.js +10 -6
  38. package/dist/server/routes/table/index.d.ts +1 -1
  39. package/dist/server/routes/table/index.d.ts.map +1 -1
  40. package/dist/server/routes/table/index.js +1 -1
  41. package/dist/server/routes/templates/index.d.ts +1 -1
  42. package/dist/server/routes/templates/index.d.ts.map +1 -1
  43. package/dist/server/routes/templates/index.js +1 -1
  44. package/dist/server/routes/util/index.d.ts +1 -1
  45. package/dist/server/routes/util/index.d.ts.map +1 -1
  46. package/dist/server/routes/util/index.js +1 -1
  47. package/dist/server/routes/widget/index.d.ts +1 -1
  48. package/dist/server/routes/widget/index.d.ts.map +1 -1
  49. package/dist/server/routes/widget/index.js +1 -1
  50. package/package.json +1 -1
@@ -0,0 +1,39 @@
1
+ export declare function onListen1(): Promise<void>;
2
+ export declare function onListen2(): Promise<void>;
3
+ export declare function preTemplate({ name, type, user, }: {
4
+ name?: string;
5
+ type?: string;
6
+ user: Record<string, any>;
7
+ }): Promise<{
8
+ name: any;
9
+ } | undefined>;
10
+ export declare function preForm({ form, user }: {
11
+ form: string;
12
+ user: any;
13
+ }): Promise<any>;
14
+ export declare function afterTable({ table, res, payload: rows, user, }: {
15
+ table: string;
16
+ res: Record<string, any>;
17
+ payload: any[];
18
+ user: Record<string, any>;
19
+ }): Promise<void>;
20
+ export declare function afterTemplate({ name, type, payload: data, user, }: {
21
+ name?: string;
22
+ type: string;
23
+ payload: Record<string, any>;
24
+ user?: Record<string, any>;
25
+ }): Promise<null | undefined>;
26
+ export declare function afterUpdate({ table, body, payload: res, user, }: {
27
+ table: string;
28
+ body: Record<string, any>;
29
+ payload: Record<string, any>;
30
+ user: Record<string, any>;
31
+ }): Promise<any>;
32
+ export declare function afterInsert({ table, body, payload: res, user, }: {
33
+ table: string;
34
+ body: Record<string, any>;
35
+ payload: Record<string, any>;
36
+ user: Record<string, any>;
37
+ }): Promise<any>;
38
+ export declare function onReady(): Promise<void>;
39
+ //# sourceMappingURL=functions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../functions.ts"],"names":[],"mappings":"AAgCA,wBAAsB,SAAS,kBAkH9B;AAED,wBAAsB,SAAS,kBAiJ9B;AAED,wBAAsB,WAAW,CAAC,EAChC,IAAI,EACJ,IAAI,EACJ,IAAS,GACV,EAAE;IACD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC3B;;eAqBA;AAED,wBAAsB,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,GAAG,CAAA;CAAE,gBASxE;AAED,wBAAsB,UAAU,CAAC,EAC/B,KAAK,EACL,GAAQ,EACR,OAAO,EAAE,IAAS,EAClB,IAAS,GACV,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACzB,OAAO,EAAE,GAAG,EAAE,CAAC;IACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC3B,iBA+FA;AAED,wBAAsB,aAAa,CAAC,EAClC,IAAI,EACJ,IAAI,EACJ,OAAO,EAAE,IAAS,EAClB,IAAS,GACV,EAAE;IACD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC5B,6BAiEA;AAED,wBAAsB,WAAW,CAAC,EAChC,KAAK,EACL,IAAS,EACT,OAAO,EAAE,GAAQ,EACjB,IAAS,GACV,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC3B,gBA6CA;AAED,wBAAsB,WAAW,CAAC,EAChC,KAAK,EACL,IAAI,EACJ,OAAO,EAAE,GAAQ,EACjB,IAAS,GACV,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC3B,gBA2CA;AAED,wBAAsB,OAAO,kBAmC5B"}
@@ -0,0 +1,412 @@
1
+ import path from "node:path";
2
+ import { existsSync, readdirSync, readFileSync, mkdirSync, writeFileSync, } from "node:fs";
3
+ import { createHash } from "node:crypto";
4
+ import { fileURLToPath } from "node:url";
5
+ import config from "./config.js";
6
+ import getTemplatePath from "./server/plugins/table/funcs/getTemplatePath.js";
7
+ import getToken from "./server/plugins/crud/funcs/getToken.js";
8
+ import getTemplate from "./server/plugins/table/funcs/getTemplate.js";
9
+ import getRedis from "./server/plugins/redis/funcs/getRedis.js";
10
+ import logger from "./server/plugins/logger/getLogger.js";
11
+ import getMenu from "./server/routes/menu/controllers/getMenu.js";
12
+ import getSelectVal from "./server/plugins/table/funcs/metaFormat/getSelectVal.js";
13
+ import locales from "./server/routes/table/controllers/utils/locales.js";
14
+ import pgClients from "./server/plugins/pg/pgClients.js";
15
+ const { client } = pgClients;
16
+ const rclient = getRedis();
17
+ // core templates && cls
18
+ const filename = fileURLToPath(import.meta.url);
19
+ const cwd = path.dirname(filename);
20
+ export async function onListen1() {
21
+ const json = await getMenu({ user: { uid: "1" }, pg: client }, null);
22
+ // insert interface list to db (user access management)
23
+ if (client?.pk?.["admin.routes"] && json?.menus?.length) {
24
+ const menuList = json?.menus?.filter?.((el) => el?.table ||
25
+ el?.component ||
26
+ el?.menu?.length /*&& el?.ua || el?.en || el?.name*/) || [];
27
+ // skip dupes
28
+ //admin_route_menu_id_fkey
29
+ menuList.forEach((el) => Object.assign(el, { ua: el?.ua || el?.en || el?.name }));
30
+ const uniqueList = menuList.filter((el, idx, arr) => el?.ua &&
31
+ arr.map((item) => item?.ua).indexOf(el?.ua) === idx);
32
+ const q = `insert into admin.menu(name, ord) values${uniqueList
33
+ .map((el, i) => `('${el?.ua.replace(/'/g, "''")}', ${i}) `)
34
+ .join(",")}
35
+ on conflict (name) do update set ord=excluded.ord, enabled=true returning name, menu_id`;
36
+ const { rows = [] } = uniqueList.length ? await client.query(q) : {};
37
+ const menus = rows.reduce((acc, curr) => Object.assign(acc, {
38
+ [curr.menu_id]: uniqueList.find((item) => item?.ua === curr.name),
39
+ }), {});
40
+ const values = Object.entries(menus).reduce((acc, curr) => {
41
+ if (curr[1]?.table || curr[1]?.component) {
42
+ acc.push({ ...curr[1], menuId: curr[0] });
43
+ }
44
+ curr[1]?.menu?.forEach?.((el) => acc.push({ ...el, menuId: curr[0] }));
45
+ return acc;
46
+ }, []);
47
+ await Promise.all(values
48
+ .filter((el) => el?.table)
49
+ .map(async (el) => {
50
+ const loadTable = await getTemplate("table", el.table);
51
+ Object.assign(el, {
52
+ table1: loadTable?.table || el.table,
53
+ actions: loadTable?.actions,
54
+ access: loadTable?.access,
55
+ });
56
+ }));
57
+ // console.log(values)
58
+ const q1 = `insert into admin.routes(route_id, alias, title, menu_id, table_name, actions, access, query)
59
+ values ${values
60
+ .filter((el) => el?.path)
61
+ .map((el) => `('${el.path}', ${el.table ? `'${el.table}'` : null},
62
+ ${el.title || el.ua
63
+ ? `'${(el.title || el.ua).replace(/'/g, "''")}'`
64
+ : null},
65
+ ${el.menuId ? `'${el.menuId}'` : null}, ${el.table1 ? `'${el.table1}'` : null},
66
+ ${el.actions?.length ? `'{ ${el.actions} }'::text[]` : null}, ${el.access ? `'${el.access}'` : null},
67
+ ${el.query ? `'${el.query.replace(/'/g, "''")}'` : "'1=1'"})`)
68
+ .join(",")}
69
+ on conflict (route_id) do update set menu_id=excluded.menu_id, alias=excluded.alias, title=excluded.title, enabled=true, query=excluded.query,
70
+ table_name=excluded.table_name, actions=excluded.actions, access=excluded.access returning route_id, table_name`;
71
+ try {
72
+ console.log("admin/hook routes sql start");
73
+ const { rowCount: menuCount } = await client.query(`delete from admin.menu
74
+ where not array[menu_id] <@ $1::text[] and menu_id not in (select menu_id from admin.routes)`, [rows.map((el) => el.menu_id)]);
75
+ console.log("delete deprecated menus ok", menuCount);
76
+ const { rowCount: interfaceCount } = await client.query(`delete from admin.routes
77
+ where not array[route_id] <@ $1::text[] and route_id not in (select route_id from admin.role_access)`, [values.filter((el) => el?.path)]);
78
+ console.log("delete deprecated interfaces ok", interfaceCount);
79
+ const { rowCount } = values?.length ? await client.query(q1) : {};
80
+ console.log("insert interfaces ok", rowCount);
81
+ }
82
+ catch (err) {
83
+ console.log("admin/hook routes sql error", values, q1, err);
84
+ }
85
+ }
86
+ }
87
+ export async function onListen2() {
88
+ const clsQuery = [];
89
+ if (!client?.pk && client?.init) {
90
+ await client.init();
91
+ }
92
+ if (!client?.pk?.["admin.cls"])
93
+ return;
94
+ const selectList = getTemplatePath("select");
95
+ const clsList = getTemplatePath("cls")?.filter((el) => !(selectList?.map((el) => el?.[0]) || []).includes(el[0]));
96
+ const cls = (selectList || []).concat(clsList || [])?.map((el) => ({
97
+ name: el[0],
98
+ module: path.basename(path.dirname(path.dirname(el[1]))),
99
+ type: { json: "cls", sql: "select" }[el[2]],
100
+ }));
101
+ if (!cls?.length)
102
+ return;
103
+ try {
104
+ const hashes = await rclient
105
+ .hgetall("cls-insert-hashes")
106
+ .then((obj) => Object.keys(obj));
107
+ const dbdata = await client
108
+ .query(`select json_object_agg(name, hash) from admin.cls where parent is null`)
109
+ .then((el) => el.rows?.[0]?.json_object_agg || {});
110
+ const names = Object.keys(dbdata);
111
+ console.log("admin/hook cls promise start");
112
+ const qHashes = await Promise.all(cls
113
+ .filter((el, idx, arr) => arr.map((item) => item.name).indexOf(el.name) === idx)
114
+ .map(async (el) => {
115
+ const { name, module, type } = el;
116
+ const loadTemplate = await getTemplate(type, name);
117
+ el.hash = createHash("md5")
118
+ .update(type === "cls"
119
+ ? JSON.stringify(loadTemplate)
120
+ : loadTemplate?.sql || loadTemplate || "")
121
+ .digest("hex");
122
+ el.dbhash = dbdata[name];
123
+ // check for changes by redis hash / dropped from db / changed at git project
124
+ el.update =
125
+ !hashes.includes(el.hash) ||
126
+ !names.includes(name) ||
127
+ el.hash !== el.dbhash;
128
+ if (type === "select" &&
129
+ (loadTemplate?.sql || loadTemplate) &&
130
+ el.update) {
131
+ clsQuery.push(`insert into admin.cls(name,type,data,module,hash) values('${name}','sql','${(loadTemplate?.sql || loadTemplate)?.replace(/'/g, "''")}', '${module?.replace(/'/g, "''")}','${el.hash}')`);
132
+ if (config.trace)
133
+ console.log(name, type, "insert fresh select");
134
+ return el.hash;
135
+ }
136
+ else if (type === "cls" && loadTemplate?.length && el.update) {
137
+ clsQuery.push(`insert into admin.cls(name,type, module,hash) values('${name}','json', '${module?.replace(/'/g, "''")}','${el.hash}');
138
+ insert into admin.cls(code,name,parent,icon,color,data)
139
+ select value->>'id',value->>'text','${name}',value->>'icon',value->>'color',value->>'data'
140
+ from json_array_elements('${JSON.stringify(loadTemplate).replace(/'/g, "''")}'::json) on conflict (code,parent) do update set color=excluded.color;`);
141
+ if (config.trace)
142
+ console.log(name, type, "insert fresh cls");
143
+ return el.hash;
144
+ }
145
+ else if (hashes.includes(el.hash)) {
146
+ if (config.trace)
147
+ console.log(name, type, names.includes(name)
148
+ ? "skip equal hash"
149
+ : "insert missing cls");
150
+ return el.hash;
151
+ }
152
+ else {
153
+ if (config.trace)
154
+ console.log(name, type, "empty");
155
+ return el.hash;
156
+ }
157
+ }));
158
+ // debug
159
+ const logDir = path.join(cwd, "log/migration");
160
+ mkdirSync(logDir, { recursive: true });
161
+ writeFileSync(path.join(logDir, `${path.basename(cwd)}-${client.options?.database}-cls.sql`), clsQuery.filter(Boolean).join(";"));
162
+ writeFileSync(path.join(logDir, `${path.basename(cwd)}-${client.options?.database}-cls.json`), JSON.stringify(cls));
163
+ const { rowCount = 0 } = await client.query("delete from admin.cls where $1::text[] && array[name,parent]", [cls.filter((el) => el.update).map((el) => el.name)]);
164
+ console.log("fastify-table/hook old cls deleted", rowCount);
165
+ if (clsQuery.filter((el) => el).length) {
166
+ console.log("fastify-table cls sql start", clsQuery?.length);
167
+ await client.query(clsQuery.filter((el) => el).join(";"));
168
+ await Promise.all(qHashes
169
+ .filter(Boolean)
170
+ .map(async (el) => rclient.hset("cls-insert-hashes", el, 1)));
171
+ logger.file("migration/hash", { list: qHashes.filter(Boolean) });
172
+ console.log("fastify-table/hook cls sql finish", clsQuery?.length);
173
+ }
174
+ console.log("fastify-table/hook cls promise finish", rowCount);
175
+ }
176
+ catch (err) {
177
+ console.error("fastify-table/hook cls sql error", err.toString());
178
+ console.trace(err);
179
+ }
180
+ }
181
+ export async function preTemplate({ name, type, user = {}, }) {
182
+ if (!name || !type)
183
+ return;
184
+ const { uid } = config?.auth?.disable || process.env.NODE_ENV !== "admin"
185
+ ? { uid: "1" }
186
+ : user;
187
+ const tokenData = (await getToken({
188
+ uid,
189
+ token: name,
190
+ mode: "w",
191
+ json: 1,
192
+ })) || // edit?
193
+ (await getToken({
194
+ uid,
195
+ token: name,
196
+ mode: "a",
197
+ json: 1,
198
+ })) ||
199
+ {}; // add?
200
+ return { name: tokenData?.[type] };
201
+ }
202
+ export async function preForm({ form, user }) {
203
+ if (!user?.uid)
204
+ return null;
205
+ const opt = await getToken({
206
+ mode: "w",
207
+ token: form,
208
+ uid: user.uid,
209
+ json: 1,
210
+ });
211
+ return opt;
212
+ }
213
+ export async function afterTable({ table, res = {}, payload: rows = [], user = {}, }) {
214
+ const loadTable = await getTemplate("table", table);
215
+ const { uid } = config?.auth?.disable || process.env.NODE_ENV !== "admin"
216
+ ? { uid: "1" }
217
+ : user;
218
+ if (!uid ||
219
+ !table ||
220
+ !client?.pk?.[table] ||
221
+ !rows.length ||
222
+ !loadTable?.table ||
223
+ !client?.pk?.["crm.extra_data"] ||
224
+ !client?.pk?.["admin.custom_column"])
225
+ return;
226
+ // admin.custom_column - user column data
227
+ const { rows: properties = [] } = await client.query(`select column_id, name, title, format, data from admin.custom_column
228
+ where _table and entity=$1 and uid=$2`, [table, uid]);
229
+ const extraColumnList = properties.map((row) => ({
230
+ id: row.column_id,
231
+ name: row.name,
232
+ title: row.title,
233
+ format: row.format,
234
+ data: row.data,
235
+ }));
236
+ if (!extraColumnList?.length)
237
+ return;
238
+ if (Array.isArray(res?.columns) && res?.columns?.length) {
239
+ extraColumnList.forEach((col) => res.columns.push(col));
240
+ }
241
+ const { rows: extraData = [] } = await client.query(`select object_id, json_object_agg( property_id, coalesce(value_date::text,value_text) ) as extra from crm.extra_data
242
+ where property_entity=$1 and property_id=any($2) and object_id=any($3) group by object_id`, [
243
+ table,
244
+ extraColumnList?.map((el) => el.id),
245
+ rows.map((el) => el.id),
246
+ ]);
247
+ if (!extraData?.length) {
248
+ // Object.assign(rows?.[0] || {}, { ...extraColumnList.reduce((acc, curr) => Object.assign(acc, { [curr.name]: null }), {}) });
249
+ return;
250
+ }
251
+ rows
252
+ .filter((row) => extraData.map((el) => el?.object_id).includes(row.id))
253
+ .forEach((row) => {
254
+ const { extra = {} } = extraData.find((el) => el.object_id === row.id);
255
+ Object.assign(row, {
256
+ ...Object.fromEntries(Object.entries(extra).map((el) => [
257
+ extraColumnList.find((col) => col.id === el[0])
258
+ .name,
259
+ el[1],
260
+ ])),
261
+ });
262
+ });
263
+ // admin.custom_column - metaFormat
264
+ await Promise.all(extraColumnList
265
+ .filter((el) => el?.data)
266
+ .map(async (attr) => {
267
+ const values = [
268
+ ...new Set(rows?.map((el) => el[attr.name]).flat()),
269
+ ].filter((el) => el);
270
+ if (!values.length)
271
+ return;
272
+ const cls = await getSelectVal({ name: attr.data, values });
273
+ if (!cls)
274
+ return;
275
+ rows.forEach((el) => {
276
+ const val = el[attr.name]?.map?.((c) => cls[c] || c) ||
277
+ cls[el[attr.name]] ||
278
+ el[attr.name];
279
+ if (!val)
280
+ return;
281
+ Object.assign(el, {
282
+ [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: val.color
283
+ ? val
284
+ : val.text || val,
285
+ });
286
+ });
287
+ }));
288
+ }
289
+ export async function afterTemplate({ name, type, payload: data = {}, user = {}, }) {
290
+ const { uid } = config?.auth?.disable || process.env.NODE_ENV !== "admin"
291
+ ? { uid: "1" }
292
+ : user;
293
+ // extract table from form token for user columns - p.1 - assign (refactor to global token)
294
+ if (!uid ||
295
+ !data ||
296
+ type !== "form" ||
297
+ !name ||
298
+ !client?.pk?.["admin.custom_column"])
299
+ return null;
300
+ const { form, id, table } = (await getToken({
301
+ uid,
302
+ token: name,
303
+ mode: "w",
304
+ json: 1,
305
+ })) || // edit?
306
+ (await getToken({
307
+ uid,
308
+ token: name,
309
+ mode: "a",
310
+ json: 1,
311
+ })) ||
312
+ {}; // add?
313
+ const { rows: properties = [] } = await client.query(`select name, title, format, data from admin.custom_column
314
+ where entity=$1 and uid=$2`, [table || name, uid]);
315
+ await Promise.all(properties.map(async (el) => {
316
+ const clsData = el.data
317
+ ? await getTemplate(["cls", "select"], el.data)
318
+ : undefined;
319
+ const type = clsData
320
+ ? "Select"
321
+ : { date: "DatePicker" }[el.format || ""] || "Text";
322
+ Object.assign(data?.schema || data || {}, {
323
+ [el.name]: {
324
+ type,
325
+ ua: el.title,
326
+ data: el.data,
327
+ options: type === "Select" && Array.isArray(clsData) && clsData?.length
328
+ ? clsData
329
+ : undefined,
330
+ extra: 1,
331
+ },
332
+ });
333
+ }));
334
+ }
335
+ export async function afterUpdate({ table, body = {}, payload: res = {}, user = {}, }) {
336
+ const { uid } = config?.auth?.disable || process.env.NODE_ENV !== "admin"
337
+ ? { uid: "1" }
338
+ : user;
339
+ if (!uid ||
340
+ !table ||
341
+ !Object.keys(body)?.length ||
342
+ !client?.pk?.["crm.extra_data"] ||
343
+ !client?.pk?.["admin.custom_column"])
344
+ return null;
345
+ const loadTable = await getTemplate("table", table);
346
+ if (!client?.pk?.[loadTable?.table || table])
347
+ return null;
348
+ const pk = client?.pk?.[loadTable?.table || table];
349
+ const id = res[pk];
350
+ const { rows: properties = [] } = await client.query(`select column_id, name, title, format, data from admin.custom_column
351
+ where entity=$1 and uid=$2`, [table, uid]);
352
+ if (!id || !properties?.length || !client.pk?.["crm.extra_data"])
353
+ return null;
354
+ const q = `delete from crm.extra_data where property_entity='${table}' and object_id='${id}';${properties
355
+ .filter((el) => Object.keys(body).includes(el.name))
356
+ .map((el) => `insert into crm.extra_data(property_id,property_key,property_entity,object_id,${el.format?.toLowerCase() === "date" ? "value_date" : "value_text"})
357
+ select '${el.column_id}', '${el.name}', '${table}', '${id}', ${el.format?.toLowerCase() === "date"
358
+ ? `'${body[el.name]}'::timestamp without time zone`
359
+ : `'${body[el.name]}'::text`}`)
360
+ .join(";\n") || ""}`;
361
+ return client.query(q);
362
+ }
363
+ export async function afterInsert({ table, body, payload: res = {}, user = {}, }) {
364
+ const { uid } = config?.auth?.disable || process.env.NODE_ENV !== "admin"
365
+ ? { uid: "1" }
366
+ : user;
367
+ if (!uid ||
368
+ !table ||
369
+ !Object.keys(body)?.length ||
370
+ !client?.pk?.["crm.extra_data"] ||
371
+ !client?.pk?.["admin.custom_column"])
372
+ return null;
373
+ const loadTable = await getTemplate("table", table);
374
+ if (!client?.pk?.[loadTable?.table || table])
375
+ return null;
376
+ const pk = client?.pk?.[loadTable?.table || table];
377
+ const id = res.rows?.[0]?.[pk];
378
+ const { rows: properties = [] } = await client.query(`select column_id, name, title, format, data from admin.custom_column
379
+ where entity=$1 and uid=$2`, [table, uid]);
380
+ if (!id || !properties?.length)
381
+ return null;
382
+ const q = properties
383
+ .filter((el) => Object.keys(body).includes(el.name))
384
+ .map((el) => `insert into crm.extra_data(property_id,property_key,property_entity,object_id,${el.format?.toLowerCase() === "date" ? "value_date" : "value_text"})
385
+ select '${el.column_id}', '${el.name}', '${table}', '${id}', ${el.format?.toLowerCase() === "date"
386
+ ? `'${body[el.name]}'::timestamp without time zone`
387
+ : `'${body[el.name]}'::text`}`)
388
+ .join(";\n");
389
+ return client.query(q);
390
+ }
391
+ export async function onReady() {
392
+ if (existsSync("locales")) {
393
+ const subdirs = readdirSync("locales", { withFileTypes: true }).filter((el) => el.isDirectory());
394
+ const res = subdirs.reduce((acc, curr) => {
395
+ const content = readdirSync(`locales/${curr.name}`, {
396
+ withFileTypes: true,
397
+ });
398
+ const obj = content
399
+ .filter((el) => el.isFile())
400
+ .reduce((acc1, file) => ({
401
+ ...acc1,
402
+ ...JSON.parse(readFileSync(`locales/${curr.name}/${file.name}`, "utf-8").replace(/[\u200B-\u200D\uFEFF]/g, "")),
403
+ }), {});
404
+ Object.keys(obj).reduce((acc1, curr1) => Object.assign(acc, {
405
+ [curr1]: { ...acc[curr1], [curr.name]: obj?.[curr1] },
406
+ }), {});
407
+ return acc;
408
+ }, {});
409
+ Object.assign(locales, res);
410
+ console.log("locales loaded");
411
+ }
412
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AA6aA,iBAAS,MAAM,CAAC,OAAO,EAAE,GAAG,QA6a3B;;AACD,wBAA0B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AA2EA,iBAAS,MAAM,CAAC,OAAO,EAAE,GAAG,QAyH3B;;AACD,wBAA0B"}