@dracul/audit-backend 1.35.2 → 1.35.3

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.
@@ -52,17 +52,18 @@ const resolvers = {
52
52
  Mutation: {
53
53
  createAudit: (_, {
54
54
  action,
55
- resource,
56
- description
55
+ entity,
56
+ details,
57
+ changes
57
58
  }, {
58
59
  user
59
60
  }) => {
60
61
  if (!user) throw new AuthenticationError("Unauthenticated");
61
62
  return createAudit(user, {
62
- user: user.id,
63
63
  action,
64
- resource,
65
- description
64
+ entity,
65
+ details,
66
+ changes
66
67
  });
67
68
  }
68
69
  }
@@ -2,9 +2,10 @@ type Audit{
2
2
  id: ID!
3
3
  user: User!
4
4
  action: String!
5
- resource: String!
5
+ entity: String!
6
6
  createdAt: String,
7
- description: String
7
+ details: String
8
+ changes: [Changes]
8
9
  }
9
10
 
10
11
  type AuditPaginated{
@@ -20,5 +21,17 @@ type Query {
20
21
  }
21
22
 
22
23
  type Mutation {
23
- createAudit(action: String!, resource: String!, description: String): Audit
24
+ createAudit(action: String!, entity: String!, details: String, changes: [ChangesInput]): Audit
24
25
  }
26
+
27
+ input ChangesInput {
28
+ field: String!
29
+ oldValue: JSON
30
+ newValue: JSON
31
+ }
32
+
33
+ type Changes{
34
+ field: String
35
+ oldValue: JSON
36
+ newValue: JSON
37
+ }
@@ -1,11 +1,33 @@
1
1
  "use strict";
2
2
 
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.auditAdmittedActions = exports.Audit = void 0;
3
7
  const {
4
8
  mongoose
5
9
  } = require('@dracul/common-backend');
6
10
  const mongoosePaginate = require('mongoose-paginate-v2');
7
11
  const uniqueValidator = require('mongoose-unique-validator');
12
+ const auditAdmittedActions = exports.auditAdmittedActions = ["CREATE", "READ", "UPDATE", "DELETE"];
8
13
  const Schema = mongoose.Schema;
14
+ const ChangeSchema = new Schema({
15
+ field: {
16
+ type: String,
17
+ required: true
18
+ },
19
+ oldValue: {
20
+ type: Schema.Types.Mixed,
21
+ required: true
22
+ },
23
+ newValue: {
24
+ type: Schema.Types.Mixed,
25
+ required: true
26
+ }
27
+ }, {
28
+ _id: false
29
+ }); // _id: false para evitar que se genere un _id en subdocumentos
30
+
9
31
  const AuditSchema = new Schema({
10
32
  user: {
11
33
  type: mongoose.Schema.Types.ObjectId,
@@ -16,20 +38,27 @@ const AuditSchema = new Schema({
16
38
  },
17
39
  action: {
18
40
  type: String,
41
+ enum: auditAdmittedActions,
19
42
  required: true,
20
43
  index: false
21
44
  },
22
- resource: {
45
+ entity: {
23
46
  type: String,
24
47
  required: true,
25
48
  unique: false,
26
49
  index: false
27
50
  },
28
- description: {
51
+ details: {
29
52
  type: String,
30
53
  required: false,
31
54
  unique: false,
32
55
  index: false
56
+ },
57
+ changes: {
58
+ type: [ChangeSchema],
59
+ required: false,
60
+ unique: false,
61
+ index: false
33
62
  }
34
63
  }, {
35
64
  timestamps: true
@@ -38,5 +67,4 @@ AuditSchema.plugin(mongoosePaginate);
38
67
  AuditSchema.plugin(uniqueValidator, {
39
68
  message: 'validation.unique'
40
69
  });
41
- const Audit = mongoose.model('Audit', AuditSchema);
42
- module.exports = Audit;
70
+ const Audit = exports.Audit = mongoose.model('Audit', AuditSchema);
@@ -1,28 +1,34 @@
1
1
  "use strict";
2
2
 
3
- const Audit = require('../models/AuditModel.js');
4
- const {
5
- UserInputError
6
- } = require('apollo-server-errors');
7
- const findAudit = async function (id) {
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createAudit = createAudit;
7
+ exports.fetchAudit = fetchAudit;
8
+ exports.findAudit = findAudit;
9
+ exports.paginateAudit = paginateAudit;
10
+ var _loggerBackend = require("@dracul/logger-backend");
11
+ var _apolloServerErrors = require("apollo-server-errors");
12
+ var _AuditModel = require("../models/AuditModel.js");
13
+ async function findAudit(id) {
8
14
  try {
9
- const audit = await Audit.findOne({
15
+ const audit = await _AuditModel.Audit.findOne({
10
16
  _id: id
11
17
  }).populate('user').exec();
12
18
  return audit;
13
- } catch (e) {
14
- throw e;
19
+ } catch (error) {
20
+ _loggerBackend.DefaultLogger.error('An error happened at the findAudit method', error.message);
15
21
  }
16
- };
17
- const fetchAudit = async function () {
22
+ }
23
+ async function fetchAudit() {
18
24
  try {
19
- const audit = await Audit.find({}).populate('user').exec();
25
+ const audit = await _AuditModel.Audit.find({}).populate('user').exec();
20
26
  return audit;
21
- } catch (e) {
22
- throw e;
27
+ } catch (error) {
28
+ _loggerBackend.DefaultLogger.error('An error happened at the fetchAudit method', error.message);
23
29
  }
24
- };
25
- const paginateAudit = function (pageNumber = 1, itemsPerPage = 5, search = null, filters = null, orderBy = null, orderDesc = false) {
30
+ }
31
+ function paginateAudit(pageNumber = 1, itemsPerPage = 5, search = null, filters = null, orderBy = null, orderDesc = false) {
26
32
  function qs(search, filters) {
27
33
  let qs = {};
28
34
  if (search) {
@@ -71,7 +77,14 @@ const paginateAudit = function (pageNumber = 1, itemsPerPage = 5, search = null,
71
77
  function getSort(orderBy, orderDesc) {
72
78
  return orderBy ? (orderDesc ? '-' : '') + orderBy : null;
73
79
  }
74
- const populate = ['user'];
80
+ const populate = [{
81
+ path: 'user',
82
+ populate: [{
83
+ path: 'role'
84
+ }, {
85
+ path: 'groups'
86
+ }]
87
+ }];
75
88
  const sort = getSort(orderBy, orderDesc);
76
89
  const query = {
77
90
  ...qs(search),
@@ -84,43 +97,43 @@ const paginateAudit = function (pageNumber = 1, itemsPerPage = 5, search = null,
84
97
  sort
85
98
  };
86
99
  return new Promise((resolve, reject) => {
87
- Audit.paginate(query, params).then(result => {
88
- resolve({
100
+ _AuditModel.Audit.paginate(query, params).then(result => {
101
+ const valueToReturn = {
89
102
  items: result.docs,
90
103
  totalItems: result.totalDocs,
91
104
  page: result.page
92
- });
105
+ };
106
+ _loggerBackend.DefaultLogger.info(`valueToReturn: ${JSON.stringify(valueToReturn, null, 2)}`);
107
+ resolve(valueToReturn);
93
108
  }).catch(err => reject(err));
94
109
  });
95
- };
96
- const createAudit = async function (authUser, {
97
- user,
110
+ }
111
+ async function createAudit(authUser, {
98
112
  action,
99
- resource,
100
- description
113
+ entity,
114
+ details,
115
+ changes
101
116
  }) {
102
117
  try {
103
- const doc = new Audit({
104
- user,
118
+ const doc = new _AuditModel.Audit({
119
+ user: authUser.id,
105
120
  action,
106
- resource,
107
- description
121
+ entity,
122
+ details,
123
+ changes
108
124
  });
109
125
  doc.id = doc._id;
110
- await doc.populate('user');
111
- return await doc.save();
126
+ const createdAuditory = await doc.save();
127
+ await createdAuditory.populate({
128
+ path: 'user'
129
+ });
130
+ return await findAudit(doc.id);
112
131
  } catch (error) {
113
- if (error.name == "ValidationError") {
114
- throw new UserInputError(error.message, {
132
+ if (error.name === "ValidationError") {
133
+ throw new _apolloServerErrors.UserInputError(error.message, {
115
134
  inputErrors: error.errors
116
135
  });
117
136
  }
118
137
  throw error;
119
138
  }
120
- };
121
- module.exports = {
122
- createAudit,
123
- fetchAudit,
124
- paginateAudit,
125
- findAudit
126
- };
139
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dracul/audit-backend",
3
- "version": "1.35.2",
3
+ "version": "1.35.3",
4
4
  "description": "Audit Backend",
5
5
  "author": "Cristian Incarnato <cristian.cdi@gmail.com>",
6
6
  "homepage": "https://github.com/draculjs/modular-framework#readme",
@@ -32,7 +32,7 @@
32
32
  "@dracul/logger-backend": "^1.35.0",
33
33
  "@dracul/notification-backend": "^1.35.0",
34
34
  "@dracul/settings-backend": "^1.34.0",
35
- "@dracul/user-backend": "^1.35.2"
35
+ "@dracul/user-backend": "^1.35.3"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "@graphql-tools/load-files": "^7.0.0",
@@ -45,5 +45,5 @@
45
45
  "mongoose-softdelete": "1.1.0",
46
46
  "mongoose-unique-validator": "^2.0.3 || ^5.0.0"
47
47
  },
48
- "gitHead": "d012954afb63b35bab8e31bc41a4fd737bac4858"
48
+ "gitHead": "d55ebfdfdaab950fc7bae85a0710539c3093ff72"
49
49
  }