@plusscommunities/pluss-maintenance-aws 1.2.4 → 1.2.6

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,7 +1,7 @@
1
1
  const moment = require("moment");
2
2
  const _ = require("lodash");
3
3
  const uuid = require("uuid");
4
- const getTableCount = require("@plusscommunities/pluss-core-aws/db/common/getTableCount");
4
+ const indexQuery = require("@plusscommunities/pluss-core-aws/db/common/indexQuery");
5
5
  const updateRef = require("@plusscommunities/pluss-core-aws/db/common/updateRef");
6
6
  const { getConfig } = require("@plusscommunities/pluss-core-aws/config");
7
7
 
@@ -63,8 +63,20 @@ module.exports = async (
63
63
  requestToSave.id = uuid.v1();
64
64
  requestToSave.createdTime = moment.utc().toISOString();
65
65
  requestToSave.createdUnix = moment.utc().valueOf();
66
- const result = await getTableCount("maintenance");
67
- requestToSave.jobId = ++result.Count + "";
66
+ const query = {
67
+ IndexName: "MaintenanceSiteJobIdIndex",
68
+ KeyConditionExpression: "site = :site",
69
+ ExpressionAttributeValues: {
70
+ ":site": requestToSave.site,
71
+ },
72
+ Limit: 1,
73
+ ScanIndexForward: false,
74
+ };
75
+ const { Items } = await indexQuery("maintenance", query);
76
+ requestToSave.jobId =
77
+ !_.isEmpty(Items) && Items[0].jobId
78
+ ? Number.parseInt(Items[0].jobId) + 1 + ""
79
+ : "0";
68
80
 
69
81
  await updateRef("maintenance", requestToSave);
70
82
  return requestToSave.id;
package/feature.config.js CHANGED
@@ -156,6 +156,15 @@ exports.serverless = {
156
156
  path: "tickets/get/{id}",
157
157
  method: "get",
158
158
  },
159
+ {
160
+ name: "getData",
161
+ file: "getData",
162
+ function: "getData",
163
+ memorySize: 256,
164
+ timeout: 10,
165
+ path: "get/{action}",
166
+ method: "get",
167
+ },
159
168
  ],
160
169
  triggers: [
161
170
  {
@@ -195,6 +204,7 @@ exports.serverless = {
195
204
  { name: "id", type: "S" },
196
205
  { name: "site", type: "S" },
197
206
  { name: "jobId", type: "S" },
207
+ { name: "userID", type: "S" },
198
208
  ],
199
209
  id: "id",
200
210
  indexes: [
@@ -209,6 +219,13 @@ exports.serverless = {
209
219
  { name: "jobId", type: "RANGE" },
210
220
  ],
211
221
  },
222
+ {
223
+ name: "MaintenanceSiteUserIdIndex",
224
+ keys: [
225
+ { name: "site", type: "HASH" },
226
+ { name: "userID", type: "RANGE" },
227
+ ],
228
+ },
212
229
  ],
213
230
  },
214
231
  {
package/getData.js ADDED
@@ -0,0 +1,114 @@
1
+ const indexQuery = require("@plusscommunities/pluss-core-aws/db/common/indexQuery");
2
+ const config = require("./config.json");
3
+ const { init } = require("@plusscommunities/pluss-core-aws/config");
4
+ const { log } = require("@plusscommunities/pluss-core-aws/helper");
5
+ const getSessionUser = require("@plusscommunities/pluss-core-aws/helper/auth/getSessionUser");
6
+ const validateMasterAuth = require("@plusscommunities/pluss-core-aws/helper/auth/validateMasterAuth");
7
+ const validateSiteAccess = require("@plusscommunities/pluss-core-aws/helper/auth/validateSiteAccess");
8
+ const generateJsonResponse = require("@plusscommunities/pluss-core-aws/helper/generateJsonResponse");
9
+
10
+ const getRequests = async (event) => {
11
+ const qParams = event.queryStringParameters;
12
+ const logId = log("getRequests", "Params", qParams);
13
+
14
+ // insufficient input
15
+ if (!qParams.site) {
16
+ return { status: 422, data: { error: "Insufficient input" } };
17
+ }
18
+ log("getRequests", "SufficientInput", true, logId);
19
+
20
+ // no access to site
21
+ const valid = await validateSiteAccess(event, qParams.site);
22
+ log("getRequests", "valid", valid, logId);
23
+ if (!valid) {
24
+ return { status: 403, data: { error: "Not authorised" } };
25
+ }
26
+
27
+ // check auth level to determine whether to fetch all requests or only matching requests
28
+ const authorised = await validateMasterAuth(
29
+ event,
30
+ "maintenanceTracking",
31
+ qParams.site
32
+ );
33
+ log("getRequests", "authorised", authorised, logId);
34
+ const userId = authorised
35
+ ? null
36
+ : await getSessionUser(event.headers.authkey);
37
+
38
+ log("getRequests", "userId", userId, logId);
39
+
40
+ const query = userId
41
+ ? {
42
+ IndexName: "MaintenanceSiteUserIdIndex",
43
+ KeyConditionExpression: "site = :site AND userID = :userId",
44
+ ExpressionAttributeValues: {
45
+ ":site": qParams.site,
46
+ ":userId": userId,
47
+ },
48
+ }
49
+ : {
50
+ IndexName: "MaintenanceSiteIndex",
51
+ KeyConditionExpression: "site = :site",
52
+ ExpressionAttributeValues: {
53
+ ":site": qParams.site,
54
+ },
55
+ };
56
+ log("getRequests", "query", query, logId);
57
+
58
+ // check whether pagination is applied
59
+ if (qParams.lastKey) {
60
+ try {
61
+ query.ExclusiveStartKey = JSON.parse(qParams.lastKey);
62
+ } catch (e) {}
63
+ }
64
+
65
+ // get jobs
66
+ const result = await indexQuery("maintenance", query);
67
+ let jobs = result.Items;
68
+
69
+ log("getRequests", "LastEvaluatedKey", result.LastEvaluatedKey, logId);
70
+ log("getRequests", "JobsLength", jobs.length, logId);
71
+
72
+ // filter on status
73
+ if (qParams.status) {
74
+ jobs = jobs.filter((j) => qParams.status.includes(j.status));
75
+ log("getRequests", "FilteredOnStatus", jobs.length, logId);
76
+ }
77
+
78
+ // filter on type
79
+ if (qParams.type) {
80
+ jobs = jobs.filter((j) => qParams.type.includes(j.type));
81
+ log("getRequests", "FilteredOnType", jobs.length, logId);
82
+ }
83
+
84
+ // compile results
85
+ const results = { Items: jobs, LastKey: result.LastEvaluatedKey };
86
+ log("getRequests", "Done", true, logId);
87
+ return { status: 200, data: results };
88
+ };
89
+
90
+ module.exports.getData = async (event, context, callback) => {
91
+ init(config);
92
+ const action = event.pathParameters.action;
93
+ const logId = log(action, "Input", event.queryStringParameters);
94
+
95
+ let response;
96
+
97
+ try {
98
+ switch (action) {
99
+ case "requests":
100
+ response = await getRequests(event);
101
+ log(action, "ResponseLength", response.data.Items.length, logId);
102
+ break;
103
+ default:
104
+ break;
105
+ }
106
+ } catch (err) {
107
+ log(action, "InternalError", err, logId);
108
+ if (!response) {
109
+ response = { status: 500, data: { error: "Internal Error" } };
110
+ }
111
+ }
112
+
113
+ return callback(null, generateJsonResponse(response.status, response.data));
114
+ };
package/getJobs.js CHANGED
@@ -42,7 +42,7 @@ module.exports.getJobs = async (event, context, callback) => {
42
42
  if (data.status)
43
43
  jobs = jobs.filter((j) => data.status.includes(j.status));
44
44
  if (data.type) jobs = jobs.filter((j) => data.type.includes(j.type));
45
- console.log("jobs", jobs);
45
+ console.log("jobs count", jobs?.length);
46
46
  return callback(null, generateJsonResponse(200, jobs));
47
47
  } catch (error1) {
48
48
  return callback(
package/package-lock.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-maintenance-aws",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "lockfileVersion": 1,
5
5
  "requires": true,
6
6
  "dependencies": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-maintenance-aws",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Extension package to enable maintenance on Pluss Communities Platform",
5
5
  "scripts": {
6
6
  "gc": "node ../../tools/gc ./",