aloux-iam 0.0.115 → 0.0.117

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,97 +1,97 @@
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, permission) => {
20
- try {
21
- if (_createdBy) {
22
- await History.updateOne(
23
- { _id: _history },
24
- { response: response, _createdBy: _createdBy, permission: permission }
25
- );
26
- } else {
27
- await History.updateOne(
28
- { _id: _history },
29
- { response: response, permission: permission }
30
- );
31
- }
32
- return true;
33
- } catch (error) {
34
- return error;
35
- }
36
- };
37
-
38
- self.retrieve = async (req, res) => {
39
- try {
40
- let query = { $and: [] };
41
- const response = { items: [], count: 0 };
42
-
43
- if (req.body?.filter?.method) {
44
- query.$and.push({ method: req.body.filter.method });
45
- }
46
-
47
- if (req.body?.filter?.description) {
48
- query.$and.push({
49
- permission: { $regex: req.body.filter.description, $options: "i" },
50
- });
51
- }
52
-
53
- if (req.body?.filter?._user) {
54
- query.$and.push({ _createdBy: req.body.filter._user });
55
- }
56
-
57
- if (req.body.filter?.dateStart > 0 && req.body.filter?.dateEnd > 0) {
58
- query.$and.push({
59
- createdAt: {
60
- $gte: req.body.filter.dateStart,
61
- $lte: req.body.filter.dateEnd,
62
- },
63
- });
64
- }
65
-
66
- if (query.$and.length === 0) {
67
- query = {};
68
- }
69
-
70
- response.count = await History.countDocuments(query);
71
- response.items = await History.find(query)
72
- .populate({ path: "_createdBy", select: { name: 1, lastName: 1 } })
73
- .sort({ createdAt: -1 })
74
- .skip((req.body.config.page - 1) * req.body.config.itemsPerPage)
75
- .limit(req.body.config.itemsPerPage)
76
- .lean();
77
-
78
- res.status(200).send(response);
79
- } catch (error) {
80
- res.status(400).send(error);
81
- }
82
- };
83
-
84
- self.detail = async (req, res) => {
85
- try {
86
- const history = await History.findOne({ _id: req.params.HISTORY_ID });
87
- if (!history) {
88
- return res.status(404).send({
89
- error: "No se encontró el elemento",
90
- suggestion: "Verifica que el id del registro sea correcto",
91
- });
92
- }
93
- res.status(200).send(history);
94
- } catch (error) {
95
- res.status(400).send(error);
96
- }
97
- };
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, permission) => {
20
+ try {
21
+ if (_createdBy) {
22
+ await History.updateOne(
23
+ { _id: _history },
24
+ { response: response, _createdBy: _createdBy, permission: permission }
25
+ );
26
+ } else {
27
+ await History.updateOne(
28
+ { _id: _history },
29
+ { response: response, permission: permission }
30
+ );
31
+ }
32
+ return true;
33
+ } catch (error) {
34
+ return error;
35
+ }
36
+ };
37
+
38
+ self.retrieve = async (req, res) => {
39
+ try {
40
+ let query = { $and: [] };
41
+ const response = { items: [], count: 0 };
42
+
43
+ if (req.body?.filter?.method) {
44
+ query.$and.push({ method: req.body.filter.method });
45
+ }
46
+
47
+ if (req.body?.filter?.description) {
48
+ query.$and.push({
49
+ permission: { $regex: req.body.filter.description, $options: "i" },
50
+ });
51
+ }
52
+
53
+ if (req.body?.filter?._user) {
54
+ query.$and.push({ _createdBy: req.body.filter._user });
55
+ }
56
+
57
+ if (req.body.filter?.dateStart > 0 && req.body.filter?.dateEnd > 0) {
58
+ query.$and.push({
59
+ createdAt: {
60
+ $gte: req.body.filter.dateStart,
61
+ $lte: req.body.filter.dateEnd,
62
+ },
63
+ });
64
+ }
65
+
66
+ if (query.$and.length === 0) {
67
+ query = {};
68
+ }
69
+
70
+ response.count = await History.countDocuments(query);
71
+ response.items = await History.find(query)
72
+ .populate({ path: "_createdBy", select: { name: 1, lastName: 1 } })
73
+ .sort({ createdAt: -1 })
74
+ .skip((req.body.config.page - 1) * req.body.config.itemsPerPage)
75
+ .limit(req.body.config.itemsPerPage)
76
+ .lean();
77
+
78
+ res.status(200).send(response);
79
+ } catch (error) {
80
+ res.status(400).send(error);
81
+ }
82
+ };
83
+
84
+ self.detail = async (req, res) => {
85
+ try {
86
+ const history = await History.findOne({ _id: req.params.HISTORY_ID });
87
+ if (!history) {
88
+ return res.status(404).send({
89
+ error: "No se encontró el elemento",
90
+ suggestion: "Verifica que el id del registro sea correcto",
91
+ });
92
+ }
93
+ res.status(200).send(history);
94
+ } catch (error) {
95
+ res.status(400).send(error);
96
+ }
97
+ };
@@ -1,82 +1,82 @@
1
- const Label = require("../models/Label");
2
- const self = module.exports;
3
-
4
- self.create = async (req, res) => {
5
- try {
6
- const label = new Label(req.body);
7
- label.createdAt = new Date().getTime();
8
- label.status = "Activo";
9
- await label.save();
10
- res.status(201).send(label);
11
- } catch (error) {
12
- res.status(400).send({ error: error.message });
13
- }
14
- };
15
-
16
- self.update = async (req, resp) => {
17
- try {
18
- await new Label(req.body).validate();
19
- const _id = req.params.LABEL_ID;
20
- const count = await Label.findOne({ _id }).countDocuments();
21
- if (!count) throw new Error("Upss! No se encontró el registro");
22
- req.body.lastUpdate = new Date().getTime();
23
- const result = await Label.updateOne({ _id }, req.body);
24
- resp.status(200).send(req.body);
25
- } catch (error) {
26
- resp.status(400).send({ error: error.message });
27
- }
28
- };
29
- self.status = async (req, resp) => {
30
- try {
31
- const _id = req.params.LABEL_ID;
32
- const user = await Label.findOne({ _id });
33
- if (!user) throw new Error("Upss! No se encontró el Elemento");
34
- user.status = req.body.status;
35
- user.lastUpdate = new Date().getTime();
36
- const result = await user.save();
37
- resp.status(200).send(result);
38
- } catch (error) {
39
- resp.status(400).send({ error: error.message });
40
- }
41
- };
42
- self.retrieve = async (req, res) => {
43
- try {
44
- const consulta = await Label.find({}).sort({ index: 1 });
45
- if (!consulta) res.status(404).send();
46
- res.status(200).send(consulta);
47
- } catch (error) {
48
- res.status(400).send(error);
49
- }
50
- };
51
-
52
- self.get = async (req, res) => {
53
- try {
54
- const _id = req.params.LABEL_ID;
55
- const label = await Label.findOne({ _id });
56
- if (!label) res.status(404).send();
57
- res.status(200).send(label);
58
- } catch (error) {
59
- res.status(400).send(error);
60
- }
61
- };
62
-
63
- self.delete = async (req, res) => {
64
- try {
65
- const _id = req.params.LABEL_ID;
66
- const response = await Label.deleteOne({ _id });
67
- if (!response.deletedCount)
68
- res.status(404).send({ error: "El registro no existe" });
69
- else res.status(200).send({});
70
- } catch (error) {
71
- res.status(400).send({ error: error.message });
72
- }
73
- };
74
-
75
- self.count = async (req, res) => {
76
- try {
77
- let result = await Label.find({}).countDocuments();
78
- res.status(200).send({ count: result });
79
- } catch (error) {
80
- res.status(400).send({ error: error.message });
81
- }
82
- };
1
+ const Label = require("../models/Label");
2
+ const self = module.exports;
3
+
4
+ self.create = async (req, res) => {
5
+ try {
6
+ const label = new Label(req.body);
7
+ label.createdAt = new Date().getTime();
8
+ label.status = "Activo";
9
+ await label.save();
10
+ res.status(201).send(label);
11
+ } catch (error) {
12
+ res.status(400).send({ error: error.message });
13
+ }
14
+ };
15
+
16
+ self.update = async (req, resp) => {
17
+ try {
18
+ await new Label(req.body).validate();
19
+ const _id = req.params.LABEL_ID;
20
+ const count = await Label.findOne({ _id }).countDocuments();
21
+ if (!count) throw new Error("Upss! No se encontró el registro");
22
+ req.body.lastUpdate = new Date().getTime();
23
+ const result = await Label.updateOne({ _id }, req.body);
24
+ resp.status(200).send(req.body);
25
+ } catch (error) {
26
+ resp.status(400).send({ error: error.message });
27
+ }
28
+ };
29
+ self.status = async (req, resp) => {
30
+ try {
31
+ const _id = req.params.LABEL_ID;
32
+ const user = await Label.findOne({ _id });
33
+ if (!user) throw new Error("Upss! No se encontró el Elemento");
34
+ user.status = req.body.status;
35
+ user.lastUpdate = new Date().getTime();
36
+ const result = await user.save();
37
+ resp.status(200).send(result);
38
+ } catch (error) {
39
+ resp.status(400).send({ error: error.message });
40
+ }
41
+ };
42
+ self.retrieve = async (req, res) => {
43
+ try {
44
+ const consulta = await Label.find({}).sort({ index: 1 });
45
+ if (!consulta) res.status(404).send();
46
+ res.status(200).send(consulta);
47
+ } catch (error) {
48
+ res.status(400).send(error);
49
+ }
50
+ };
51
+
52
+ self.get = async (req, res) => {
53
+ try {
54
+ const _id = req.params.LABEL_ID;
55
+ const label = await Label.findOne({ _id });
56
+ if (!label) res.status(404).send();
57
+ res.status(200).send(label);
58
+ } catch (error) {
59
+ res.status(400).send(error);
60
+ }
61
+ };
62
+
63
+ self.delete = async (req, res) => {
64
+ try {
65
+ const _id = req.params.LABEL_ID;
66
+ const response = await Label.deleteOne({ _id });
67
+ if (!response.deletedCount)
68
+ res.status(404).send({ error: "El registro no existe" });
69
+ else res.status(200).send({});
70
+ } catch (error) {
71
+ res.status(400).send({ error: error.message });
72
+ }
73
+ };
74
+
75
+ self.count = async (req, res) => {
76
+ try {
77
+ let result = await Label.find({}).countDocuments();
78
+ res.status(200).send({ count: result });
79
+ } catch (error) {
80
+ res.status(400).send({ error: error.message });
81
+ }
82
+ };