@plusscommunities/pluss-maintenance-aws-feedback 2.1.19-auth.0 → 2.1.19

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": "@plusscommunities/pluss-maintenance-aws-feedback",
3
- "version": "2.1.19-auth.0",
3
+ "version": "2.1.19",
4
4
  "description": "Extension package to enable maintenance on Pluss Communities Platform",
5
5
  "scripts": {
6
6
  "gc": "node ../../tools/gc ./",
@@ -10,9 +10,6 @@
10
10
  "deploy": "npm run gc && npm run gs && serverless deploy",
11
11
  "betaupload": "rm -rf .serverless && npm i && npm publish --access public --tag beta",
12
12
  "betaupload:p": "npm run betapatch && npm run betaupload",
13
- "authpatch": "npm version prepatch --preid=auth",
14
- "authupload": "npm i && npm i && npm publish --access public --tag auth",
15
- "authupload:p": "npm run authpatch && npm run authupload",
16
13
  "upload": "rm -rf .serverless && npm i && npm publish --access public",
17
14
  "upload:p": "npm run patch && npm run upload",
18
15
  "copy:add": "run(){ ext=${1:-default}; test -f values.config.$ext.js || cp values.config.default.js values.config.$ext.js; }; run",
@@ -20,7 +17,6 @@
20
17
  "copy:set": "run(){ target='\\@plusscommunities\\/pluss-maintenance-aws'; ext=${1:-default}; [ $ext == 'default' ] && replace=$target || replace=$target'-'$ext; echo 'Setting target to '$replace; test -f values.config.$ext.js && cp -f values.config.$ext.js values.config.js; sed -i '' -e 's/'$target'.*\"/'$replace'\"/g' package.json; }; run",
21
18
  "copy:deploy": "for file in `ls ./values.config.*.js`; do dup=`echo $file | sed 's/.*values\\.config\\.\\(.*\\)\\.js/\\1/'`; npm run copy:set $dup; npm run deploy; done; npm run copy:set; npm run gs;",
22
19
  "copy:betaupload": "npm run betapatch; for file in `ls ./values.config.*.js`; do dup=`echo $file | sed 's/.*values\\.config\\.\\(.*\\)\\.js/\\1/'`; npm run copy:set $dup; npm run betaupload; done; npm run copy:set;",
23
- "copy:authupload": "npm run authpatch; for file in `ls ./values.config.*.js`; do dup=`echo $file | sed 's/.*values\\.config\\.\\(.*\\)\\.js/\\1/'`; npm run copy:set $dup; npm run authupload; done; npm run copy:set;",
24
20
  "copy:upload": "npm run patch; for file in `ls ./values.config.*.js`; do dup=`echo $file | sed 's/.*values\\.config\\.\\(.*\\)\\.js/\\1/'`; npm run copy:set $dup; npm run upload; done; npm run copy:set;",
25
21
  "test": "jest tests -i"
26
22
  },
@@ -28,7 +24,7 @@
28
24
  "license": "ISC",
29
25
  "dependencies": {
30
26
  "@aws/dynamodb-auto-marshaller": "^0.7.1",
31
- "@plusscommunities/pluss-core-aws": "2.0.24-auth.0",
27
+ "@plusscommunities/pluss-core-aws": "2.0.23",
32
28
  "amazon-cognito-identity-js": "^2.0.19",
33
29
  "aws-sdk": "^2.1591.0",
34
30
  "axios": "^1.6.8",
@@ -0,0 +1,121 @@
1
+ const editRef = require("@plusscommunities/pluss-core-aws/db/common/editRef");
2
+ const updateRef = require("@plusscommunities/pluss-core-aws/db/common/updateRef");
3
+ const getRef = require("@plusscommunities/pluss-core-aws/db/common/getRef");
4
+ const validateMasterAuth = require("@plusscommunities/pluss-core-aws/helper/auth/validateMasterAuth");
5
+ const getUserPreviewFromReq = require("@plusscommunities/pluss-core-aws/helper/getUserPreviewFromReq");
6
+ const moment = require("moment");
7
+ const { values } = require("../values.config");
8
+ const { log } = require("@plusscommunities/pluss-core-aws/helper");
9
+
10
+ module.exports = async (event, data) => {
11
+ const action = "setExternalJobId";
12
+ const logId = log(action, "Input", data);
13
+
14
+ try {
15
+ // 1. Validate required fields
16
+ if (!data.id || !data.externalId || !data.systemType) {
17
+ return {
18
+ status: 422,
19
+ data: {
20
+ error: "Missing required fields: id, externalId, systemType",
21
+ },
22
+ };
23
+ }
24
+
25
+ // 2. Find the job by ID (UUID)
26
+ let job;
27
+
28
+ try {
29
+ job = await getRef(values.tableNameMaintenance, "id", data.id);
30
+ } catch (error) {
31
+ log(action, "Error:JobNotFound", { error: error.message }, logId);
32
+ return { status: 404, data: { error: "Job not found" } };
33
+ }
34
+
35
+ if (!job) {
36
+ log(action, "JobNotFound", { id: data.id }, logId);
37
+ return { status: 404, data: { error: "Job not found" } };
38
+ }
39
+
40
+ // 3. Validate authorization using the job's site
41
+ const authorised = await validateMasterAuth(
42
+ event,
43
+ values.permissionMaintenanceTracking,
44
+ job.site
45
+ );
46
+
47
+ if (!authorised) {
48
+ log(action, "NotAuthorised", { site: job.site }, logId);
49
+ return { status: 403, data: { error: "Not authorised" } };
50
+ }
51
+
52
+ // 4. Get user preview for history tracking
53
+ const user = await getUserPreviewFromReq(event);
54
+
55
+ // 5. Create entity type identifier
56
+ const entityType = `${values.serviceKey}_${data.systemType}`;
57
+ const rowId = `${entityType}_${data.externalId}`;
58
+
59
+ log(action, "CreatingExternalEntity", { entityType, rowId }, logId);
60
+
61
+ // 6. Create/update external entity mapping
62
+ const externalEntity = await updateRef("externalentities", {
63
+ RowId: rowId,
64
+ EntityType: entityType,
65
+ InternalId: job.id,
66
+ ExternalId: data.externalId,
67
+ LastUpdated: moment().valueOf(),
68
+ TrackedData: data.trackedData || {},
69
+ Site: job.site, // Store for site-specific queries
70
+ });
71
+
72
+ log(action, "ExternalEntityCreated", externalEntity, logId);
73
+
74
+ // 7. Add history entry
75
+ if (!job.history) job.history = [];
76
+ job.history.push({
77
+ timestamp: moment.utc().valueOf(),
78
+ action: "ExternalIDSet",
79
+ externalId: data.externalId,
80
+ user,
81
+ });
82
+
83
+ // 8. Update job with history and optionally update job number for system parity
84
+ let updatedJob = job;
85
+
86
+ if (!isNaN(data.externalId)) {
87
+ log(
88
+ action,
89
+ "UpdatingJobNumber",
90
+ {
91
+ oldJobNo: job.jobNo,
92
+ newJobNo: data.externalId,
93
+ },
94
+ logId
95
+ );
96
+
97
+ updatedJob = await editRef(values.tableNameMaintenance, "id", job.id, {
98
+ jobNo: Number(data.externalId),
99
+ jobId: Number(data.externalId) + "",
100
+ history: job.history,
101
+ });
102
+
103
+ log(action, "JobNumberUpdated", updatedJob, logId);
104
+ }
105
+
106
+ return {
107
+ status: 200,
108
+ data: {
109
+ success: true,
110
+ job: updatedJob,
111
+ externalEntity: externalEntity,
112
+ },
113
+ };
114
+ } catch (error) {
115
+ log(action, "InternalError", error, logId);
116
+ return {
117
+ status: 500,
118
+ data: { error: "Internal error", message: error.message },
119
+ };
120
+ }
121
+ };
package/updateData.js CHANGED
@@ -4,6 +4,7 @@ const { init } = require("@plusscommunities/pluss-core-aws/config");
4
4
  const config = require("./config.json");
5
5
  const assignRequest = require("./requests/assignRequest");
6
6
  const updatePriority = require("./requests/updatePriority");
7
+ const setExternalJobId = require("./requests/setExternalJobId");
7
8
 
8
9
  module.exports.updateData = async (event, context, callback) => {
9
10
  init(config);
@@ -21,7 +22,11 @@ module.exports.updateData = async (event, context, callback) => {
21
22
  case "priority":
22
23
  response = await updatePriority(event, data);
23
24
  break;
25
+ case "externalid":
26
+ response = await setExternalJobId(event, data);
27
+ break;
24
28
  default:
29
+ response = { status: 404, data: { error: "Action not found" } };
25
30
  break;
26
31
  }
27
32
  } catch (err) {
@@ -33,5 +38,9 @@ module.exports.updateData = async (event, context, callback) => {
33
38
 
34
39
  log(action, "Response", response, logId);
35
40
 
41
+ if (!response) {
42
+ response = { status: 500, data: { error: "No response generated" } };
43
+ }
44
+
36
45
  return callback(null, generateJsonResponse(response.status, response.data));
37
46
  };