@opengis/fastify-table 2.0.54 → 2.0.55

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.
@@ -1 +1 @@
1
- {"version":3,"file":"checkPolicy.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/policy/funcs/checkPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,wBAAwB,CAAC;AAKhC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,YAAY,oSA6K5E"}
1
+ {"version":3,"file":"checkPolicy.d.ts","sourceRoot":"","sources":["../../../../../server/plugins/policy/funcs/checkPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,wBAAwB,CAAC;AAKhC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,YAAY,oSA8K5E"}
@@ -22,7 +22,8 @@ export default function checkPolicy(req, reply) {
22
22
  config.admin ||
23
23
  hostname.startsWith("admin");
24
24
  const { role, referer, policy = [], } = (routeOptions?.config || {});
25
- const isRole = (role && user?.user_type !== role) ||
25
+ // role=admin|regular
26
+ const isRole = (role && (!user?.user_type || !role?.includes?.(user?.user_type))) ||
26
27
  (policy.includes("admin") && user?.user_type !== "admin");
27
28
  const allowExtPublic = [".png", ".jpg", ".svg"];
28
29
  const ext = path.toLowerCase().substr(-4);
@@ -1 +1 @@
1
- {"version":3,"file":"code.generator.d.ts","sourceRoot":"","sources":["../../../../../server/routes/util/controllers/code.generator.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAazD,wBAA8B,aAAa,CACzC,EACE,EAAqB,EACrB,MAAW,EACX,IAAS,EACT,KAAU,GACX,EAAE;IACD,EAAE,EAAE,UAAU,CAAC;IACf,MAAM,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5C,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,KAAK,EAAE;QAAE,IAAI,CAAC,EAAE,SAAS,CAAA;KAAE,CAAC;CAC7B,EACD,KAAK,EAAE,YAAY,kBA8FpB"}
1
+ {"version":3,"file":"code.generator.d.ts","sourceRoot":"","sources":["../../../../../server/routes/util/controllers/code.generator.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AA0EzD,wBAA8B,aAAa,CACzC,EACE,EAAqB,EACrB,MAAW,EACX,IAAS,EACT,KAAU,GACX,EAAE;IACD,EAAE,EAAE,UAAU,CAAC;IACf,MAAM,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5C,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,KAAK,EAAE;QAAE,IAAI,CAAC,EAAE,SAAS,CAAA;KAAE,CAAC;CAC7B,EACD,KAAK,EAAE,YAAY,kBA+GpB"}
@@ -11,6 +11,54 @@ function dayOfTheYear(date) {
11
11
  const day = Math.floor(diff / oneDay);
12
12
  return day;
13
13
  }
14
+ const optionsKeys = [
15
+ "HH",
16
+ "HH12",
17
+ "HH24",
18
+ "MI",
19
+ "SS",
20
+ "YYYY",
21
+ "YY",
22
+ "MONTH",
23
+ "MON",
24
+ "MM",
25
+ "D",
26
+ "DD",
27
+ "DDD",
28
+ // "NUMM",
29
+ // "NUMY",
30
+ ];
31
+ const regexp = new RegExp(`\\b(${optionsKeys.join("|")}|NUM[MY]\\s?\\d?)\\b`, "gi");
32
+ function getOptions(data = {}, pattern = "", numYear = 0, nextNum = 0) {
33
+ const date = new Date();
34
+ const options = {
35
+ HH: date.getHours(), // hours 24h: 14:00 = 14
36
+ HH12: (date.getHours() + 24) % 12 || 12, // hours 12h: 14:00 = 2
37
+ HH24: date.getHours(), // hours 24h: 14:00 = 14
38
+ MI: date.getMinutes(), // minutes
39
+ SS: date.getSeconds(), // seconds
40
+ YYYY: date.getFullYear(), // full year: 2025
41
+ YY: date.getFullYear().toString().substring(2, 4), // last 2 digits of year: 25
42
+ MONTH: date.toLocaleString("en", { month: "long" }).toUpperCase(), // month name: MARCH
43
+ MON: date
44
+ .toLocaleString("en", { month: "long" })
45
+ .substring(0, 3)
46
+ .toUpperCase(), // month name abbrev: MAR
47
+ MM: date.getMonth() + 1, // month number: 1 - january, 12 - december
48
+ D: date.getDay(), // day of the week: 1 - monday, 7 - sunday
49
+ DD: date.getDate(), // day of the month: january 4 = 4
50
+ DDD: dayOfTheYear(date), // day of the year: march 4 = 63
51
+ ...data,
52
+ };
53
+ const template = pattern.match(/NUM[M|Y] ?\d?/g)?.reduce((acc, curr) => {
54
+ const value = handlebarsSync.compile("{{paddingNumber value padding}}")({
55
+ padding: curr.substring(4, curr.length)?.trim?.() || 6,
56
+ value: curr.startsWith("NUMY") ? +numYear : +nextNum,
57
+ });
58
+ return acc.replace(curr.startsWith("{{{") ? `{{{${curr}}}}` : `{{${curr}}}`, value);
59
+ }, pattern) || pattern;
60
+ return { template, options };
61
+ }
14
62
  export default async function codeGenerator({ pg = pgClients.client, params = {}, user = {}, query = {}, }, reply) {
15
63
  const { token, column } = params;
16
64
  const data = query.data?.split?.(";") || [];
@@ -21,6 +69,30 @@ export default async function codeGenerator({ pg = pgClients.client, params = {}
21
69
  return reply.status(401).send("unauthorized");
22
70
  }
23
71
  const tokenData = (await getToken({ token, uid: user?.uid, json: 1 })) || {};
72
+ const currentYear = new Date().getFullYear();
73
+ // unique entity per table, table as key - property_text = pattern as yyyy-mm-dd/numy
74
+ if (pg?.pk?.[token]) {
75
+ const pattern1 = pg
76
+ ? await pg
77
+ .query(`select property_text from admin.properties where property_entity=$1 and property_key=$2`, [`code:${column.replace(/'/g, "''")}`, token])
78
+ .then((el) => el.rows?.[0]?.property_text)
79
+ : null;
80
+ // force pattern keys to upperCase for handlebars compile
81
+ const pattern = (pattern1 || column).replace(regexp, (_, m) => `{{${m.toUpperCase()}}}`);
82
+ const count = pg
83
+ ? await pg
84
+ .query(`select count(*) + 1 as count from ${token}`)
85
+ .then((el) => el.rows?.[0]?.count || 0)
86
+ : 0;
87
+ const numYear = pg
88
+ ? await pg
89
+ .query(`select count(*) + 1 as count from ${token} where date_part('year', created_at)=$1`, [currentYear])
90
+ .then((el) => el.rows?.[0]?.count || 0)
91
+ : 0;
92
+ const { template, options } = getOptions(data, pattern, numYear, count);
93
+ const result = handlebarsSync.compile(template)(options);
94
+ return reply.status(200).send(result);
95
+ }
24
96
  if (!tokenData?.form || !tokenData?.table) {
25
97
  return reply.status(401).send("token not allow");
26
98
  }
@@ -41,35 +113,17 @@ export default async function codeGenerator({ pg = pgClients.client, params = {}
41
113
  if (!pg.pk?.[table]) {
42
114
  return reply.status(404).send("table pk not found");
43
115
  }
44
- const { count = 0 } = await pg
45
- .query(`select count(*) from ${table} where ${columnValue ? `${column}::text = '${columnValue}'` : "true"} limit 1`)
46
- .then((el) => el.rows?.[0] || {});
47
- const { NUMY = 0 } = await pg
48
- .query(`select ${column} as "NUMY" from ${table} where ${column} is not null and date_part('year', cdate) = $1 order by cdate desc limit 1`, [new Date().getFullYear()])
49
- .then((el) => el.rows?.[0] || {});
50
- const date = new Date();
51
- const template = schema[column].template.match(/NUM[M|Y] \d/g).reduce((acc, curr) => acc.replace(curr.startsWith("{{{") ? `{{{${curr}}}}` : `{{${curr}}}`, handlebarsSync.compile("{{paddingNumber value padding}}")({
52
- padding: curr.substring(4, curr.length)?.trim?.(),
53
- value: curr.startsWith("NUMY") ? NUMY : +count + 1,
54
- })), schema[column].template);
55
- const result = handlebarsSync.compile(template)({
56
- HH: date.getHours(), // hours 24h: 14:00 = 14
57
- HH12: (date.getHours() + 24) % 12 || 12, // hours 12h: 14:00 = 2
58
- HH24: date.getHours(), // hours 24h: 14:00 = 14
59
- MI: date.getMinutes(), // minutes
60
- SS: date.getSeconds(), // seconds
61
- YYYY: date.getFullYear(), // full year: 2025
62
- YY: date.getFullYear().toString().substring(2, 4), // last 2 digits of year: 25
63
- MONTH: date.toLocaleString("en", { month: "long" }).toUpperCase(), // month name: MARCH
64
- MON: date
65
- .toLocaleString("en", { month: "long" })
66
- .substring(0, 3)
67
- .toUpperCase(), // month name abbrev: MAR
68
- MM: date.getMonth() + 1, // month number: 1 - january, 12 - december
69
- D: date.getDay(), // day of the week: 1 - monday, 7 - sunday
70
- DD: date.getDate(), // day of the month: january 4 = 4
71
- DDD: dayOfTheYear(date), // day of the year: march 4 = 63
72
- ...data,
73
- });
116
+ const count = pg
117
+ ? await pg
118
+ .query(`select count(*) + 1 as count from ${table} where ${columnValue ? `${column}::text = '${columnValue}'` : "true"} limit 1`)
119
+ .then((el) => el.rows?.[0]?.count || 0)
120
+ : 0;
121
+ const numYear = pg
122
+ ? await pg
123
+ .query(`select ${column} + 1 as count from ${table} where ${column} is not null and date_part('year', cdate) = $1 order by cdate desc limit 1`, [currentYear])
124
+ .then((el) => el.rows?.[0]?.count || 0)
125
+ : 0;
126
+ const { template, options } = getOptions(data, schema[column].template, numYear, count);
127
+ const result = handlebarsSync.compile(template)(options);
74
128
  return reply.status(200).send(result);
75
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.0.54",
3
+ "version": "2.0.55",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [