@ejfdelgado/ejflab-back 1.26.11 → 1.27.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-back",
3
- "version": "1.26.11",
3
+ "version": "1.27.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ejfdelgado/ejflab-back.git"
@@ -18,7 +18,7 @@
18
18
  "license": "ISC",
19
19
  "private": false,
20
20
  "dependencies": {
21
- "@ejfdelgado/ejflab-common": "1.14.4",
21
+ "@ejfdelgado/ejflab-common": "1.16.0",
22
22
  "@google-cloud/compute": "^4.7.0",
23
23
  "@google-cloud/firestore": "^7.9.0",
24
24
  "@google-cloud/storage": "^7.11.3",
@@ -10,7 +10,8 @@ import { ModuloDatoSeguroBack } from "@ejfdelgado/ejflab-common/src/ModuloDatoSe
10
10
  import { MyConstants } from "@ejfdelgado/ejflab-common/src/MyConstants.js";
11
11
 
12
12
  export class MainHandler {
13
- static LOCAL_FOLDER = path.resolve() + "/dist/bundle";
13
+ static LOCAL_FOLDER1 = path.resolve() + "/src";// First try
14
+ static LOCAL_FOLDER = path.resolve() + "/dist/bundle";// Finall try
14
15
  static async handle(req, res, next) {
15
16
  const originalUrl = req.getUrl();
16
17
  const theUrl = url.parse(originalUrl);
@@ -97,37 +98,54 @@ export class MainHandler {
97
98
  return null;
98
99
  }
99
100
 
100
- static async resolveLocalFileSingle(filename, encoding, rootFolder = MainHandler.LOCAL_FOLDER) {
101
+ static async resolveLocalFileSingle(filename, encoding, rootFolder) {
101
102
  return new Promise((resolve, reject) => {
102
- const somePath = path.join(rootFolder, filename);
103
103
 
104
- fs.access(somePath, (err) => {
105
- if (err) {
106
- resolve(null);
107
- } else {
108
- if (!fs.lstatSync(somePath).isFile()) {
109
- resolve(null);
104
+ // Try
105
+ const options = [];
106
+ if (rootFolder) {
107
+ options.push(path.join(rootFolder, filename));
108
+ }
109
+ options.push(path.join(MainHandler.LOCAL_FOLDER1, filename));
110
+ options.push(path.join(MainHandler.LOCAL_FOLDER, filename));
111
+
112
+ // Check which exists first, if not resolve null with log
113
+ let selected = null;
114
+ for (let i = 0; i < options.length; i++) {
115
+ const actual = options[i];
116
+ if (fs.existsSync(actual)) {
117
+ selected = actual;
118
+ break;
119
+ }
120
+ }
121
+
122
+ if (!selected) {
123
+ console.log(`Files not found ${options}`);
124
+ resolve(null);
125
+ }
126
+
127
+ const somePath = selected;
128
+ if (!fs.lstatSync(somePath).isFile()) {
129
+ resolve(null);
130
+ return;
131
+ }
132
+ if (typeof encoding == "string") {
133
+ fs.readFile(somePath, encoding, function (err, data) {
134
+ if (err) {
135
+ reject(err);
110
136
  return;
111
137
  }
112
- if (typeof encoding == "string") {
113
- fs.readFile(somePath, encoding, function (err, data) {
114
- if (err) {
115
- reject(err);
116
- return;
117
- }
118
- resolve(data);
119
- });
120
- } else {
121
- fs.readFile(somePath, function (err, data) {
122
- if (err) {
123
- reject(err);
124
- return;
125
- }
126
- resolve(data);
127
- });
138
+ resolve(data);
139
+ });
140
+ } else {
141
+ fs.readFile(somePath, function (err, data) {
142
+ if (err) {
143
+ reject(err);
144
+ return;
128
145
  }
129
- }
130
- });
146
+ resolve(data);
147
+ });
148
+ }
131
149
  });
132
150
  }
133
151
 
package/srv/MyPdf.mjs CHANGED
@@ -5,6 +5,7 @@ import { MyTemplate } from "@ejfdelgado/ejflab-common/src/MyTemplate.js";
5
5
  import { CsvFormatterFilters } from "@ejfdelgado/ejflab-common/src/CsvFormatterFilters.js";
6
6
  import { MyUtilities } from '@ejfdelgado/ejflab-common/src/MyUtilities.js';
7
7
  import MyDatesBack from '@ejfdelgado/ejflab-common/src/MyDatesBack.mjs';
8
+ import { TranslateSrv } from "./TranslateSrv.mjs";
8
9
 
9
10
  export class MyPdf {
10
11
  static async localRender(template, model = {}, format = "pdf", extra = [], configuration, launch) {
@@ -17,6 +18,18 @@ export class MyPdf {
17
18
  renderer.registerFunction("epoch2date", (value, ...args) => {
18
19
  return MyDatesBack.formatDateCompleto(new Date(value), ...args);
19
20
  });
21
+ renderer.registerFunction("age", (value, ...args) => {
22
+ const model = MyDatesBack.age(value);
23
+ let level = typeof (args[1]) == "number" ? args[1] : 0;
24
+ if ([0, 1, 2].indexOf(level) < 0) {
25
+ level = 0;
26
+ }
27
+ const folderName = args[0];
28
+ return TranslateSrv.translate(`pipes.age.${level}`, [folderName, model], 'en');
29
+ });
30
+ renderer.registerFunction("translate", (value, ...args) => {
31
+ return TranslateSrv.translate(value, args, 'en');
32
+ });
20
33
  for (let i = 0; i < extra.length; i++) {
21
34
  const extraFile = extra[i];
22
35
  const path = MyUtilities.removeRepeatedSlash(`./src/assets/templates/pdf/${extraFile.path}`);
@@ -36,6 +49,7 @@ export class MyPdf {
36
49
  }
37
50
  }
38
51
  } else {
52
+ console.log(`Doesn't exits ${path}`);
39
53
  model.extra[extraFile.alias] = "";
40
54
  }
41
55
  }
@@ -0,0 +1,58 @@
1
+ import fs from "fs";
2
+ import { MyTemplate } from "@ejfdelgado/ejflab-common/src/MyTemplate.js";
3
+ import { MyUtilities } from '@ejfdelgado/ejflab-common/src/MyUtilities.js';
4
+ import { SimpleObj } from '@ejfdelgado/ejflab-common/src/SimpleObj.js';
5
+
6
+ export class TranslateSrv {
7
+ static keyPromises = {};// this is the cache
8
+ static renderer = new MyTemplate();
9
+
10
+ static loadLanguageDB(args, currentLang) {
11
+ // Read query param
12
+ const key = `${args[0]}/${currentLang}`;
13
+ let promesa = TranslateSrv.keyPromises[key];
14
+ if (!promesa) {
15
+ const path = MyUtilities.removeRepeatedSlash(`./src/assets/lang/${key}.json`);
16
+ const exists = fs.existsSync(path);
17
+ if (exists) {
18
+ TranslateSrv.keyPromises[key] = JSON.parse(fs.readFileSync(path));
19
+ } else {
20
+ TranslateSrv.keyPromises[key] = {};
21
+ }
22
+ promesa = TranslateSrv.keyPromises[key];
23
+ }
24
+ const valor = promesa;
25
+ return valor;
26
+ }
27
+ /**
28
+ *
29
+ * @param key
30
+ * @param args Can be a string, referencing the folder name in assets/lang/FOLDER/en.json or a JSON object {es: {}, en: {}}
31
+ * @returns
32
+ */
33
+ static translate(key, args, currentLang = "en") {
34
+ const def = key;
35
+ if (args.length > 0) {
36
+ let valor = {};
37
+ const args0 = args[0];
38
+ if (typeof args0 == 'string') {
39
+ valor = TranslateSrv.loadLanguageDB(args, currentLang);
40
+ } else if (
41
+ args0 !== undefined &&
42
+ args0 !== null &&
43
+ typeof args0 == 'object'
44
+ ) {
45
+ if (currentLang in args0) {
46
+ valor = args0[currentLang];
47
+ }
48
+ }
49
+ let raw = SimpleObj.getValue(valor, key, def);
50
+ if (args.length >= 2) {
51
+ raw = TranslateSrv.renderer.render(raw, args[1]);
52
+ }
53
+ return raw;
54
+ } else {
55
+ return def;
56
+ }
57
+ }
58
+ }
@@ -7,7 +7,12 @@ import { MalaPeticionException } from "../MyError.mjs";
7
7
  import { Buffer } from 'buffer';
8
8
 
9
9
  const AUTH_PROVIDER = process.env.AUTH_PROVIDER;
10
- const groupIdMap = JSON.parse("AUTH_GROUP_ID_MAP" in process.env ? Buffer.from(process.env.AUTH_GROUP_ID_MAP, 'base64').toString("utf8") : "{}");
10
+ let groupIdMap = {}
11
+ try {
12
+ groupIdMap = JSON.parse("AUTH_GROUP_ID_MAP" in process.env ? Buffer.from(process.env.AUTH_GROUP_ID_MAP, 'base64').toString("utf8") : "{}");
13
+ } catch (err) {
14
+ console.log(err);
15
+ }
11
16
  const USER_TYPE = "user";
12
17
 
13
18
  export class Usuario {