@opengis/admin 0.2.132 → 0.2.134

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,21 @@
1
1
  const table = 'admin.properties';
2
2
 
3
- import { setSettings } from '../../../../utils.js';
3
+ import { dataInsert } from '@opengis/fastify-table/utils.js';
4
+
5
+ function checkValueType(val) {
6
+ if (val) {
7
+ if (typeof val === 'object') {
8
+ return 'property_json';
9
+ }
10
+ if (typeof val === 'number' && (!/\D/.test(val.toString()) && val.toString().length < 10)) {
11
+ return 'property_int';
12
+ }
13
+ }
14
+ return 'property_text';
15
+ }
4
16
 
5
17
  export default async function postSettingsApp({
6
- pg, user = {}, body = {},
18
+ pg, user = {}, body = {}, uid
7
19
  }) {
8
20
  if (!user?.user_type?.includes?.('admin')) {
9
21
  return { message: 'access restricted', status: 403 };
@@ -16,11 +28,20 @@ export default async function postSettingsApp({
16
28
  if (!pg?.pk?.[table]) {
17
29
  return { message: 'table not found', status: 404 };
18
30
  }
31
+ const keys = Object.keys(body);
32
+ await pg.query(`delete from ${table} where property_key=any($1)`, [keys]);
33
+
34
+ await Promise.all(keys.map(async (el) => {
35
+ const columnType = table === 'admin.user_properties' ? 'property_json' : checkValueType(body[el]);
36
+ const data = { property_key: el, [columnType]: body[el], uid };
37
+ if (!body[el]) return;
38
+ await dataInsert({
39
+ pg, table, data, uid,
40
+ });
41
+
42
+ }));
43
+
19
44
 
20
- const res = await setSettings({
21
- pg, key, val, body,
22
- });
23
- if (res?.error) return res;
24
45
 
25
- return { message: res, status: 200 };
46
+ return { message: 'ok', status: 200 };
26
47
  }
@@ -1,11 +1,8 @@
1
1
  const table = 'admin.user_properties';
2
2
 
3
- import { getRedis } from '@opengis/fastify-table/utils.js';
4
-
5
- import { getSettings } from '../../../../utils.js';
6
3
 
7
4
  export default async function getSettingsUser({
8
- pg = {}, params = {}, query = {}, session = {},
5
+ pg = {}, session = {},
9
6
  }) {
10
7
  const { uid } = session.passport?.user || {};
11
8
  if (!uid) {
@@ -16,15 +13,7 @@ export default async function getSettingsUser({
16
13
  return { message: 'table not found', status: 404 };
17
14
  }
18
15
 
19
- const redis = getRedis();
20
- const keyCache = `${pg.options?.database}:settings:${params?.key || 'all'}:${query?.json ? 'json' : 'plain'}:user`;
21
-
22
- const cache = await redis.get(keyCache);
23
- if (cache) return JSON.parse(cache);
24
16
 
25
- const res = await getSettings({
26
- pg, key: params.key, json: query.json, redis, table, uid,
27
- });
28
17
 
29
- return { message: res, status: 200 };
18
+ return { message: 'user', status: 200 };
30
19
  }
@@ -1,30 +1,10 @@
1
1
  const table = 'admin.user_properties';
2
2
 
3
- import { setSettings } from '../../../../utils.js';
3
+
4
4
 
5
5
  export default async function postSettingsUser({
6
6
  pg, session = {}, body = {},
7
7
  }) {
8
- const { uid } = session.passport?.user || {};
9
-
10
- if (!uid) {
11
- return { message: 'access restricted', status: 403 };
12
- }
13
-
14
- const { key, val } = body;
15
-
16
- if ((!key || !val) && !Object.keys(body).length) {
17
- return { message: 'not enough params', status: 400 };
18
- }
19
-
20
- if (!pg?.pk?.[table]) {
21
- return { message: 'table not found', status: 404 };
22
- }
23
-
24
- const res = await setSettings({
25
- pg, key, val, body, table, uid,
26
- });
27
- if (res?.error) return res;
28
8
 
29
- return { message: res, status: 200 };
9
+ return { message: 'ok', status: 200 };
30
10
  }
@@ -1,8 +1,8 @@
1
1
  import getSettingsApp from './controllers/admin.properties.get.js';
2
2
  import postSettingsApp from './controllers/admin.properties.post.js';
3
3
 
4
- import getSettingsUser from './controllers/user.properties.get.js';
5
- import postSettingsUser from './controllers/user.properties.post.js';
4
+ //import getSettingsUser from './controllers/user.properties.get.js';
5
+ //import postSettingsUser from './controllers/user.properties.post.js';
6
6
 
7
7
  import getSettingsTable from './controllers/table.properties.get.js';
8
8
  import postSettingsTable from './controllers/table.properties.post.js';
@@ -13,8 +13,8 @@ export default async function route(fastify) {
13
13
  fastify.get('/settings-app/:key?', { scheme: propertiesSchema }, getSettingsApp);
14
14
  fastify.post('/settings-app', { config: { policy: ['admin'] } }, postSettingsApp);
15
15
 
16
- fastify.get('/settings-user/:key?', { scheme: propertiesSchema }, getSettingsUser);
17
- fastify.post('/settings-user', {}, postSettingsUser);
16
+ //fastify.get('/settings-user/:key?', { scheme: propertiesSchema }, getSettingsUser);
17
+ // fastify.post('/settings-user', {}, postSettingsUser);
18
18
 
19
19
  fastify.get('/settings-table/:table/:entity?', {}, getSettingsTable);
20
20
  fastify.post('/settings-table/:table', {}, postSettingsTable);
package/utils.js CHANGED
@@ -1,14 +1,13 @@
1
1
  import yamlSafe from 'js-yaml';
2
2
 
3
- import getSettings from './server/routes/properties/funcs/getSettings.js';
4
- import setSettings from './server/routes/properties/funcs/setSettings.js';
3
+
5
4
 
6
5
  import addNotification from './server/routes/notifications/funcs/addNotification.js';
7
6
  import sendNotification from './server/routes/notifications/funcs/sendNotification.js';
8
7
 
9
8
  import getAdminAccess from './server/plugins/access/funcs/getAdminAccess.js';
10
9
 
11
- function loadSafe (yml) {
10
+ function loadSafe(yml) {
12
11
  try {
13
12
  return yamlSafe.load(yml);
14
13
  } catch (err) {
@@ -21,8 +20,6 @@ Object.assign(yamlSafe, { loadSafe });
21
20
  export default null;
22
21
  export {
23
22
  yamlSafe,
24
- getSettings,
25
- setSettings,
26
23
  addNotification,
27
24
  sendNotification,
28
25
  getAdminAccess,
@@ -1,104 +0,0 @@
1
- import { _ as g, u as f, e as u } from "./import-file-CztGvKRc.js";
2
- import { resolveComponent as x, openBlock as h, createElementBlock as w, createElementVNode as n, createBlock as y, normalizeStyle as k, createCommentVNode as v } from "vue";
3
- const V = {
4
- data() {
5
- return {
6
- formValues: {},
7
- scheme: null,
8
- table: "",
9
- token: "",
10
- api: "",
11
- style: null
12
- };
13
- },
14
- mounted() {
15
- this.getFormScheme();
16
- },
17
- methods: {
18
- flattenMenu(t) {
19
- const e = [];
20
- return t.forEach((a) => {
21
- a.menu ? e.push(...this.flattenMenu(a.menu)) : e.push(a);
22
- }), e;
23
- },
24
- async getFormScheme() {
25
- var i, o;
26
- const t = this.flattenMenu(f.value);
27
- t != null && t.length || this.$router.replace("/404");
28
- const e = t == null ? void 0 : t.find((s) => {
29
- var r, l;
30
- return (s == null ? void 0 : s.path) == ((l = (r = this.$route) == null ? void 0 : r.query) == null ? void 0 : l.table);
31
- });
32
- e || this.$router.replace("/404");
33
- const a = e == null ? void 0 : e.table;
34
- this.table = a;
35
- try {
36
- const s = await u.get(`/api/table-data/${a}`), { data: r } = await u.get(
37
- `/api/template/form/${(i = s == null ? void 0 : s.data) == null ? void 0 : i.form}`
38
- );
39
- this.scheme = (r == null ? void 0 : r.schema) || r, this.style = (r == null ? void 0 : r.style) || null, this.api = (r == null ? void 0 : r.api) || "", this.token = (o = s == null ? void 0 : s.data) == null ? void 0 : o.addToken;
40
- } catch {
41
- }
42
- },
43
- async createObject() {
44
- var e, a, i, o, s, r, l, m, b;
45
- const t = this.$refs.form;
46
- try {
47
- await t.doValidation(), await u.post(
48
- this.api || `/api/table/${this.token}`,
49
- this.formValues
50
- ), await this.$router.back(), await this.$notify({
51
- title: "Успішно!",
52
- message: "Об'єкт успішно створено",
53
- type: "success"
54
- });
55
- } catch (c) {
56
- const p = ((e = c == null ? void 0 : c.response) == null ? void 0 : e.data) || "";
57
- let d = ((s = (o = (i = (a = Object.entries(t == null ? void 0 : t.formErrors)) == null ? void 0 : a[0]) == null ? void 0 : i[1]) == null ? void 0 : o[0]) == null ? void 0 : s.message) === "Це поле обов'язкове" ? "Заповніть обов'язкові поля" : (b = (m = (l = (r = Object.entries(t == null ? void 0 : t.formErrors)) == null ? void 0 : r[0]) == null ? void 0 : l[1]) == null ? void 0 : m[0]) == null ? void 0 : b.message;
58
- this.$notify({
59
- title: "Помилка!",
60
- message: p || d || "Сталася помилка валідаціі",
61
- type: "error"
62
- });
63
- }
64
- }
65
- }
66
- }, _ = { class: "bg-gray-50 lg:w-[calc(100vw-260px)] w-[100vw]" }, $ = { class: "h-[76px] mt-[15px] flex items-center justify-between mx-[20px] px-[20px] bg-white border rounded-xl" }, j = { class: "flex items-center gap-[6px]" }, E = {
67
- style: { height: "calc(100vh - 165px)" },
68
- class: "bg-gray-50 p-[20px] flex lg:w-[calc(100vw-260px)] w-[100vw]"
69
- }, O = { class: "bg-white w-full rounded-xl border p-[20px] overflow-auto [&::-webkit-scrollbar]:w-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar:horizontal]:h-[8px] [&::-webkit-scrollbar-track]:bg-gray-100 [&::-webkit-scrollbar-thumb]:bg-gray-300 dark:[&::-webkit-scrollbar-track]:bg-neutral-700 dark:[&::-webkit-scrollbar-thumb]:bg-neutral-500" };
70
- function C(t, e, a, i, o, s) {
71
- const r = x("VsForm");
72
- return h(), w("div", _, [
73
- n("div", $, [
74
- e[3] || (e[3] = n("h2", { class: "text-xl font-medium" }, "Створити", -1)),
75
- n("div", j, [
76
- n("button", {
77
- onClick: e[0] || (e[0] = (l) => t.$router.back()),
78
- class: "flex items-center px-3 py-2 text-sm font-medium text-gray-800 duration-300 bg-white border border-gray-100 rounded-lg shadow gap-x-2 focus:outline-none hover:bg-gray-50 hover:border-gray-100"
79
- }, " Скасувати "),
80
- n("button", {
81
- onClick: e[1] || (e[1] = (...l) => s.createObject && s.createObject(...l)),
82
- class: "inline-flex items-center px-3 py-2 text-sm font-medium text-white duration-300 bg-blue-600 border border-transparent rounded-lg gap-x-2 hover:bg-blue-700 hover:text-white"
83
- }, " Зберегти ")
84
- ])
85
- ]),
86
- n("div", E, [
87
- n("div", O, [
88
- o.scheme ? (h(), y(r, {
89
- key: 0,
90
- ref: "form",
91
- scheme: o.scheme,
92
- style: k(o.style),
93
- modelValue: o.formValues,
94
- "onUpdate:modelValue": e[2] || (e[2] = (l) => o.formValues = l),
95
- class: "p-0 mt-[20px]"
96
- }, null, 8, ["scheme", "style", "modelValue"])) : v("", !0)
97
- ])
98
- ])
99
- ]);
100
- }
101
- const B = /* @__PURE__ */ g(V, [["render", C]]);
102
- export {
103
- B as default
104
- };
@@ -1,125 +0,0 @@
1
- import { _ as g, u as f, e as b } from "./import-file-CztGvKRc.js";
2
- import { resolveComponent as y, openBlock as p, createElementBlock as x, createElementVNode as u, createBlock as w, normalizeStyle as k, createCommentVNode as v } from "vue";
3
- const V = {
4
- data() {
5
- return {
6
- formValues: {},
7
- scheme: null,
8
- table: "",
9
- token: "",
10
- style: null
11
- };
12
- },
13
- mounted() {
14
- this.getFormScheme();
15
- },
16
- methods: {
17
- flattenMenu(e) {
18
- const t = [];
19
- return e.forEach((l) => {
20
- l.menu ? t.push(...this.flattenMenu(l.menu)) : t.push(l);
21
- }), t;
22
- },
23
- async getFormScheme() {
24
- var i, r;
25
- const e = this.flattenMenu(f.value);
26
- e != null && e.length || this.$router.replace("/404");
27
- const t = e == null ? void 0 : e.find((o) => {
28
- var a, s;
29
- return (o == null ? void 0 : o.path) == ((s = (a = this.$route) == null ? void 0 : a.query) == null ? void 0 : s.table);
30
- });
31
- t || this.$router.replace("/404");
32
- const l = t == null ? void 0 : t.table;
33
- this.table = l;
34
- try {
35
- const {
36
- data: { form: o }
37
- } = await b.get(`/api/template/table/${l}`), { data: a } = await b.get(
38
- `/api/table/${this.table}/${(r = (i = this.$route) == null ? void 0 : i.query) == null ? void 0 : r.id}`
39
- );
40
- this.token = (a == null ? void 0 : a.token) || "", this.formValues = a || {};
41
- const { data: s } = await b.get(`/api/template/form/${o}`);
42
- this.scheme = (s == null ? void 0 : s.schema) || s, this.style = (s == null ? void 0 : s.style) || null;
43
- } catch {
44
- }
45
- },
46
- async onlyEditObject() {
47
- var t, l, i, r, o, a, s, n, m;
48
- const e = this.$refs.form;
49
- try {
50
- await e.doValidation(), await b.put(`/api/table/${this.token}`, this.formValues), await this.$notify({
51
- title: "Успішно!",
52
- message: "Об'єкт успішно створено",
53
- type: "success"
54
- });
55
- } catch (c) {
56
- const d = ((t = c == null ? void 0 : c.response) == null ? void 0 : t.data) || "";
57
- let h = ((o = (r = (i = (l = Object.entries(e == null ? void 0 : e.formErrors)) == null ? void 0 : l[0]) == null ? void 0 : i[1]) == null ? void 0 : r[0]) == null ? void 0 : o.message) === "Це поле обов'язкове" ? "Заповніть обов'язкові поля" : (m = (n = (s = (a = Object.entries(e == null ? void 0 : e.formErrors)) == null ? void 0 : a[0]) == null ? void 0 : s[1]) == null ? void 0 : n[0]) == null ? void 0 : m.message;
58
- this.$notify({
59
- title: "Помилка!",
60
- message: d || h || "Сталася помилка валідаціі",
61
- type: "error"
62
- });
63
- }
64
- },
65
- async editObjectAndRedirect() {
66
- var t, l, i, r, o, a, s, n, m;
67
- const e = this.$refs.form;
68
- try {
69
- await e.doValidation(), await b.put(`/api/table/${this.token}`, this.formValues), await this.$router.back(), await this.$notify({
70
- title: "Успішно!",
71
- message: "Об'єкт успішно створено",
72
- type: "success"
73
- });
74
- } catch (c) {
75
- const d = ((t = c == null ? void 0 : c.response) == null ? void 0 : t.data) || "";
76
- let h = ((o = (r = (i = (l = Object.entries(e == null ? void 0 : e.formErrors)) == null ? void 0 : l[0]) == null ? void 0 : i[1]) == null ? void 0 : r[0]) == null ? void 0 : o.message) === "Це поле обов'язкове" ? "Заповніть обов'язкові поля" : (m = (n = (s = (a = Object.entries(e == null ? void 0 : e.formErrors)) == null ? void 0 : a[0]) == null ? void 0 : s[1]) == null ? void 0 : n[0]) == null ? void 0 : m.message;
77
- this.$notify({
78
- title: "Помилка!",
79
- message: d || h || "Сталася помилка валідаціі",
80
- type: "error"
81
- });
82
- }
83
- }
84
- }
85
- }, $ = { class: "bg-gray-50 lg:w-[calc(100vw-260px)] w-[100vw]" }, _ = { class: "h-[76px] mt-[15px] flex items-center justify-between mx-[20px] px-[20px] bg-white border rounded-xl" }, E = { class: "flex items-center gap-[6px]" }, j = {
86
- style: { height: "calc(100vh - 155px)" },
87
- class: "bg-gray-50 p-[20px] flex lg:w-[calc(100vw-260px)] w-[100vw]"
88
- }, O = { class: "bg-white w-full rounded-xl border p-[20px] overflow-auto [&::-webkit-scrollbar]:w-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar:horizontal]:h-[8px] [&::-webkit-scrollbar-track]:bg-gray-100 [&::-webkit-scrollbar-thumb]:bg-gray-300 dark:[&::-webkit-scrollbar-track]:bg-neutral-700 dark:[&::-webkit-scrollbar-thumb]:bg-neutral-500" };
89
- function C(e, t, l, i, r, o) {
90
- var s;
91
- const a = y("VsForm");
92
- return p(), x("div", $, [
93
- u("div", _, [
94
- t[3] || (t[3] = u("h2", { class: "text-xl font-medium" }, "Редагувати", -1)),
95
- u("div", E, [
96
- u("button", {
97
- onClick: t[0] || (t[0] = (n) => e.$router.back()),
98
- class: "flex items-center px-3 py-2 text-sm font-medium text-gray-800 duration-300 bg-white border border-gray-100 rounded-lg shadow gap-x-2 focus:outline-none hover:bg-gray-50 hover:border-gray-100"
99
- }, " Скасувати "),
100
- u("button", {
101
- onClick: t[1] || (t[1] = (...n) => o.editObjectAndRedirect && o.editObjectAndRedirect(...n)),
102
- class: "inline-flex items-center px-3 py-2 text-sm font-medium text-white duration-300 bg-blue-600 border border-transparent rounded-lg gap-x-2 hover:bg-blue-700 hover:text-white"
103
- }, " Зберегти ")
104
- ])
105
- ]),
106
- u("div", j, [
107
- u("div", O, [
108
- r.scheme ? (p(), w(a, {
109
- key: 0,
110
- ref: "form",
111
- scheme: r.scheme,
112
- columns: (s = r.style) == null ? void 0 : s.columns,
113
- style: k(r.style),
114
- modelValue: r.formValues,
115
- "onUpdate:modelValue": t[2] || (t[2] = (n) => r.formValues = n),
116
- class: "p-0 mt-[20px]"
117
- }, null, 8, ["scheme", "columns", "style", "modelValue"])) : v("", !0)
118
- ])
119
- ])
120
- ]);
121
- }
122
- const A = /* @__PURE__ */ g(V, [["render", C]]);
123
- export {
124
- A as default
125
- };
@@ -1,56 +0,0 @@
1
- import { getMeta } from '@opengis/fastify-table/utils.js';
2
-
3
- const match = {
4
- property_key: 'key',
5
- property_json: 'json',
6
- property_int: 'int',
7
- property_text: 'text',
8
- };
9
-
10
- function getQuery({
11
- table, columns = [], key, uid,
12
- }) {
13
- const columnList = columns?.filter((el) => match[el?.name]).map((el) => el?.name).map((el) => `${el} as ${match[el]}`);
14
- const sql = `select ${columnList.join(',')} from ${table} where 1=1`;
15
-
16
- if (table === 'admin.user_properties') {
17
- const args = [uid, key].filter((el) => el);
18
- const q = sql + (uid ? ' and uid=$1' : '') + (key ? ` and property_key=$${args.indexOf(key) + 1}` : '');
19
- return { q, args };
20
- }
21
- const args = [key].filter((el) => el);
22
- const q = sql + (key ? ` and property_key=$${args.indexOf(key) + 1}` : '');
23
- return { q, args };
24
- }
25
-
26
- export default async function getSettings({
27
- pg, redis, json, key, table = 'admin.properties', uid,
28
- }) {
29
- const { columns = [] } = await getMeta({ table });
30
-
31
- const { q, args } = getQuery({
32
- table, columns, key, uid,
33
- });
34
-
35
- const { rows } = await pg.query(q, args);
36
- const data = rows?.reduce((acc, curr) => Object.assign(acc, { [curr.key]: curr.json || curr.int || curr.text }), {});
37
-
38
- const jsonData = Object.keys(data || {}).reduce((acc, curr) => {
39
- const [ckey, csubkey] = curr.split('.');
40
- if (!csubkey) return Object.assign(acc, { [curr]: data[curr] });
41
- if (!acc[ckey]) Object.assign(acc, { [ckey]: {} });
42
- Object.assign(acc[ckey], { [csubkey]: data[curr] });
43
- return acc;
44
- }, {});
45
-
46
- // save both
47
- if (redis) {
48
- const keyCacheJSON = `${pg.options?.database}:settings:${key || 'all'}:json:${table}`;
49
- await redis.set(keyCacheJSON, JSON.stringify(jsonData));
50
-
51
- const keyCachePlain = `${pg.options?.database}:settings:${key || 'all'}:plain:${table}`;
52
- await redis.set(keyCachePlain, JSON.stringify(data));
53
- }
54
-
55
- return json ? jsonData : data;
56
- }
@@ -1,44 +0,0 @@
1
- import { getRedis } from '@opengis/fastify-table/utils.js';
2
-
3
- import { getSettings } from '../../../../utils.js';
4
- import dataInsert from '../utils/dataInsert.js';
5
-
6
- function checkValueType(val) {
7
- if (val) {
8
- if (typeof val === 'object') {
9
- return 'property_json';
10
- }
11
- if (typeof val === 'number' && (!/\D/.test(val.toString()) && val.toString().length < 10)) {
12
- return 'property_int';
13
- }
14
- }
15
- return 'property_text';
16
- }
17
-
18
- export default async function setSettings({
19
- pg, key, val, body = {}, table = 'admin.properties', uid,
20
- }) {
21
- const body1 = key && val ? { [key]: val } : body;
22
- const keys = Object.keys(body1);
23
- try {
24
- const redis = getRedis();
25
- await pg.query(`delete from ${table} where property_key=any($1)`, [keys]);
26
-
27
- await Promise.all(keys.map(async (el) => {
28
- const columnType = table === 'admin.user_properties' ? 'property_json' : checkValueType(body1[el]);
29
- const data = { property_key: el, [columnType]: body1[el], uid };
30
- const { rows } = await dataInsert({
31
- pg, table, data, uid,
32
- });
33
- return { key: el, rows: { val: body[el], data: rows?.[0] } };
34
- }));
35
-
36
- const res = await getSettings({
37
- pg, redis, table, uid,
38
- });
39
- return res;
40
- }
41
- catch (err) {
42
- return { error: err.toString(), status: 500 };
43
- }
44
- }