aloux-iam 0.0.102 → 0.0.103

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/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const IAMrouter = require("./lib/router");
2
2
  const IAMauth = require("./lib/middleware");
3
3
  const awsAloux = require("./lib/controllers/operationsAWS");
4
+ const historyAloux = require("./lib/controllers/history");
4
5
  const awsBQ = require("./lib/services/bigQuery");
5
6
  const YAML = require("yamljs");
6
7
  const path = require("path");
@@ -33,4 +34,5 @@ module.exports = {
33
34
 
34
35
  AlouxAWS: awsAloux,
35
36
  AlouxBQ: awsBQ,
37
+ AlouxHistory: historyAloux,
36
38
  };
@@ -0,0 +1,33 @@
1
+ const History = require("../models/History");
2
+ const self = module.exports;
3
+
4
+ self.create = async (data) => {
5
+ try {
6
+ const history = new History({
7
+ method: data.originalMethod,
8
+ path: data.route.path,
9
+ payload: data.body,
10
+ createdAt: Date.now(),
11
+ });
12
+ const newHistory = await history.save();
13
+ return newHistory;
14
+ } catch (error) {
15
+ return error;
16
+ }
17
+ };
18
+
19
+ self.response = async (_history, response, _createdBy) => {
20
+ try {
21
+ if (_createdBy) {
22
+ await History.updateOne(
23
+ { _id: _history },
24
+ { response: response, _createdBy: _createdBy }
25
+ );
26
+ } else {
27
+ await History.updateOne({ _id: _history }, { response: response });
28
+ }
29
+ return true;
30
+ } catch (error) {
31
+ return error;
32
+ }
33
+ };
package/lib/middleware.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const jwt = require("jsonwebtoken");
2
2
  const User = require("./models/User");
3
3
  const Permission = require("./models/Permission");
4
+ const historyController = require("./controllers/history");
4
5
 
5
6
  const getAccess = (user, resource) => {
6
7
  for (let i in user._functions) {
@@ -21,14 +22,10 @@ const getAccess = (user, resource) => {
21
22
 
22
23
  const auth = async (req, res, next) => {
23
24
  try {
24
- if (process.env.LOG === "true") {
25
- console.log(
26
- new Date().toISOString() +
27
- "\t" +
28
- req.originalMethod +
29
- "\t" +
30
- req.route.path
31
- );
25
+ let newHistory;
26
+ if (process.env.HISTORY === "true") {
27
+ //Guardar registro en la colección del Historial
28
+ newHistory = await historyController.create(req);
32
29
  }
33
30
 
34
31
  let token =
@@ -126,6 +123,7 @@ const auth = async (req, res, next) => {
126
123
 
127
124
  req.user = user;
128
125
  req.token = token;
126
+ req.history = newHistory._id.toString();
129
127
  next();
130
128
  } catch (error) {
131
129
  let obj = error;
@@ -0,0 +1,14 @@
1
+ const mongoose = require("mongoose");
2
+ const ObjectId = mongoose.Schema.Types.ObjectId;
3
+
4
+ const historySchema = mongoose.Schema({
5
+ method: { type: String, default: false },
6
+ path: { type: String, default: true },
7
+ payload: { type: Object, default: {} }, // Guarda cualquier estructura JSON
8
+ response: { type: Object, default: {} }, // Guarda cualquier estructura JSON
9
+ _createdBy: { type: ObjectId, required: false },
10
+ createdAt: { type: Number, required: true },
11
+ });
12
+
13
+ const History = mongoose.model("History", historySchema);
14
+ module.exports = History;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aloux-iam",
3
- "version": "0.0.102",
3
+ "version": "0.0.103",
4
4
  "description": "Aloux IAM for APIs ",
5
5
  "main": "index.js",
6
6
  "scripts": {