@plusscommunities/pluss-maintenance-aws-forms 2.1.18 → 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 +1 -1
- package/requests/setExternalJobId.js +121 -0
- package/updateData.js +9 -0
- package/package-lock.json +0 -7662
package/package.json
CHANGED
|
@@ -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
|
};
|