@getjones/test-infra 5.2.14 → 5.2.15

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": "@getjones/test-infra",
3
- "version": "5.2.14",
3
+ "version": "5.2.15",
4
4
  "description": "Providing internal and openapi interface",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -1,9 +1,9 @@
1
- const UPLOADER_STATUSES = {
2
- PM: 'pmUploadedCOI',
3
- ICS: 'ICSUploadedCOI',
4
- AUDITOR: 'AuditorUploadedCOI',
5
- VENDOR: 'vendorUploadedCOI',
6
- BOT: 'botUploadedCOI'
7
- };
8
-
9
- module.exports = UPLOADER_STATUSES;
1
+ const COI_UPLOADER_TYPES = {
2
+ PM: 'pmUploadedCOI',
3
+ ICS: 'ICSUploadedCOI',
4
+ AUDITOR: 'AuditorUploadedCOI',
5
+ VENDOR: 'vendorUploadedCOI',
6
+ BOT: 'botUploadedCOI'
7
+ };
8
+
9
+ module.exports = COI_UPLOADER_TYPES;
@@ -1,17 +1,17 @@
1
- const services = require('./api/coi.service');
2
-
3
- const models = {
4
- submit: require('./models/submit.coi.model'),
5
- submitBatch: require('./models/submit.coi.batch.model')
6
- };
7
-
8
- const utils = require('./utils/utils');
9
-
10
- const consts = {
11
- UPLOADER_TYPES: require('./consts/uploaderTypes'),
12
- UPLOADER_STATUSES: require('./consts/uploaderStatuses'),
13
- HASHES: require('./consts/hashes'),
14
- DUPLICATE_HANDLING: require('./consts/duplicateHandling')
15
- };
16
-
17
- module.exports = { services, models, utils, consts };
1
+ const services = require('./api/coi.service');
2
+
3
+ const models = {
4
+ submit: require('./models/submit.coi.model'),
5
+ submitBatch: require('./models/submit.coi.batch.model')
6
+ };
7
+
8
+ const utils = require('./utils/utils');
9
+
10
+ const consts = {
11
+ UPLOADER_TYPES: require('./consts/uploaderTypes'),
12
+ COI_UPLOADER_TYPES: require('./consts/coiUploaderTypes'),
13
+ HASHES: require('./consts/hashes'),
14
+ DUPLICATE_HANDLING: require('./consts/duplicateHandling')
15
+ };
16
+
17
+ module.exports = { services, models, utils, consts };
@@ -1,185 +1,186 @@
1
- const fs = require('fs');
2
-
3
- const services = require('../api/coi.service');
4
- const fileScanService = require('../../fileScan/api/file.scan.service');
5
- const { SubmitCoiModel } = require('../models/submit.coi.model');
6
- const { SubmitCoiBatchModel } = require('../models/submit.coi.batch.model');
7
- const { CombineLastTwoCOIsModel } = require('../models/Combine.last.two.cois.model');
8
- const { getContractorById } = require('../../contractors/api/contractors.service');
9
- const { getIcsCookie } = require('../../auth/api/auth.service');
10
- const timeUtils = require('../../../../common/utils/timeUtils');
11
- const UPLOADER_TYPES = require('../consts/uploaderTypes');
12
-
13
- // Stages the certificate into TEMPCOI/{recordId}/ without submitting it, no audit is created
14
- const uploadFileToStorage = async (recordId, filePath) => {
15
- const fileData = fs.createReadStream(filePath);
16
- const uploadResponse = await services.uploadToS3(recordId, fileData);
17
-
18
- return uploadResponse.data.fileId;
19
- };
20
-
21
- const submitCoiFilesUpload = async ({
22
- recordId,
23
- fileIds,
24
- uploadedBy,
25
- uploaderContext,
26
- uploaderEmail,
27
- recordDateModified,
28
- duplicateHandling
29
- }) => {
30
- const isAnonymousUpload = !uploaderContext && uploadedBy !== UPLOADER_TYPES.ICS;
31
- const sessionContext = uploaderContext || await getIcsCookie();
32
-
33
- const submitCoiModel = new SubmitCoiModel({
34
- recordId,
35
- fileIds,
36
- uploadedBy,
37
- uploaderContext: sessionContext,
38
- uploaderEmail,
39
- recordDateModified,
40
- duplicateHandling
41
- });
42
-
43
- if (isAnonymousUpload) {
44
- return services.submitCoi(submitCoiModel);
45
- }
46
-
47
- return services.submitCoiByUserContext(submitCoiModel, sessionContext);
48
- };
49
-
50
- const uploadCoi = async (
51
- recordId,
52
- filePath,
53
- uploadedBy,
54
- uploaderEmail,
55
- {
56
- markAsScaned,
57
- uploaderContext,
58
- duplicateHandling
59
- } = {}
60
- ) => {
61
- const icsContext = await getIcsCookie();
62
- const recordResponse = await getContractorById(recordId, icsContext); // Auditors can't read the record, same as combineLastTwoCOIs
63
- const recordDateModified = recordResponse.data.dateModified;
64
- const fileId = await uploadFileToStorage(recordId, filePath);
65
-
66
- const submitResponse = fileId && await submitCoiFilesUpload({
67
- recordId,
68
- fileIds: [fileId],
69
- uploadedBy,
70
- uploaderContext,
71
- uploaderEmail,
72
- recordDateModified,
73
- duplicateHandling
74
- });
75
-
76
- const uploadedCoi = !!submitResponse.data.fileName && { id: fileId, name: submitResponse.data.fileName };
77
-
78
- if (uploadedCoi.name && markAsScaned) {
79
- await fileScanService.completeCoiScan(recordId, uploadedCoi.name, icsContext); // Only ICS may update the scan status
80
- }
81
-
82
- return uploadedCoi;
83
- };
84
-
85
- // Stages a certificate the way the UI does before an on-behalf upload: upload, then merge with
86
- // isContractorExist false. The merge writes the file flat to TEMPCOI/ and returns before any
87
- // certificate, audit or history write, so the COI is staged but not attached to the record.
88
- const stageMergedCertificate = async (recordId, filePath, context) => {
89
- const fileId = await uploadFileToStorage(recordId, filePath);
90
- const submitCoiModel = new SubmitCoiModel({ recordId, fileIds: [fileId], isContractorExist: false });
91
- const submitResponse = await services.submitCoiByUserContext(submitCoiModel, context);
92
-
93
- if (!submitResponse.data.fileName) {
94
- throw Error(`Failed to stage an on-behalf certificate for record: ${recordId}`);
95
- }
96
-
97
- return submitResponse.data.fileName;
98
- };
99
-
100
- const waitForAntiVirusRecordCoiScanToFinish = async (recordId, context, { maxSecondToWait = 10 } = {}) => {
101
- let isScanned = false;
102
- const oneSecondInMilliseconds = 1000;
103
- let timeFromTheStart = 0;
104
- let coiLink;
105
- const started = Date.now();
106
-
107
- const { data: record } = await getContractorById(recordId, context);
108
-
109
- while (!isScanned && timeFromTheStart < maxSecondToWait) {
110
- ({ data: coiLink } = await services.getCoiPreSignedUrl(record.certificatePdf));
111
- isScanned = coiLink.isInProgress;
112
- const now = Date.now();
113
- timeFromTheStart = (now - started) / oneSecondInMilliseconds;
114
- await timeUtils.waitFor(1).seconds;
115
- }
116
-
117
- return coiLink.coiPreSignedUrl;
118
- };
119
-
120
- const uploadCoiBatch = async (sourceRecordId, recordIds, filePaths, uploadedBy, uploaderEmail, {
121
- uploaderContext,
122
- duplicateHandling,
123
- expectError
124
- } = {}) => {
125
- if (!uploaderContext) {
126
- uploaderContext = await getIcsCookie();
127
- }
128
-
129
- const fileIds = await Promise.all(
130
- filePaths.map(filePath => uploadFileToStorage(sourceRecordId, filePath))
131
- );
132
-
133
- const records = await Promise.all(
134
- recordIds.map(async recordId => {
135
- const { data: record } = await getContractorById(recordId, uploaderContext);
136
- return { contractorId: recordId, contractorDateModified: record.dateModified };
137
- })
138
- );
139
-
140
- const submitCoiBatchModel = new SubmitCoiBatchModel({
141
- fileIds,
142
- sourceRecordId,
143
- records,
144
- uploadedBy,
145
- uploaderEmail,
146
- duplicateHandling
147
- });
148
-
149
- return services.submitCoiBatch(submitCoiBatchModel, uploaderContext, { expectError });
150
- };
151
-
152
- const combineLastTwoCOIs = async ({
153
- recordId,
154
- fileIds,
155
- uploadedBy,
156
- uploaderEmail,
157
- isContractorExist,
158
- icsContext,
159
- auditorContext,
160
- isNewCoiOnTop
161
- }) => {
162
- const recordResponse = await getContractorById(recordId, icsContext);
163
- const recordDateModified = recordResponse.data.dateModified;
164
-
165
- const combineLastTwoCOIsModel = new CombineLastTwoCOIsModel({
166
- recordId,
167
- fileIds,
168
- uploadedBy,
169
- uploaderEmail,
170
- recordDateModified,
171
- isContractorExist,
172
- isNewCoiOnTop
173
- });
174
- return services.combineLastTwoCOIs(combineLastTwoCOIsModel, auditorContext);
175
- };
176
-
177
- module.exports = {
178
- uploadCoi,
179
- uploadCoiBatch,
180
- uploadFileToStorage,
181
- submitCoiFilesUpload,
182
- stageMergedCertificate,
183
- waitForAntiVirusRecordCoiScanToFinish,
184
- combineLastTwoCOIs
185
- };
1
+ const fs = require('fs');
2
+
3
+ const services = require('../api/coi.service');
4
+ const fileScanService = require('../../fileScan/api/file.scan.service');
5
+ const { SubmitCoiModel } = require('../models/submit.coi.model');
6
+ const { SubmitCoiBatchModel } = require('../models/submit.coi.batch.model');
7
+ const { CombineLastTwoCOIsModel } = require('../models/Combine.last.two.cois.model');
8
+ const { getContractorById } = require('../../contractors/api/contractors.service');
9
+ const { getIcsCookie } = require('../../auth/api/auth.service');
10
+ const timeUtils = require('../../../../common/utils/timeUtils');
11
+ const UPLOADER_TYPES = require('../consts/uploaderTypes');
12
+
13
+ // Stages the certificate into TEMPCOI/{recordId}/ without submitting it, no audit is created
14
+ const uploadFileToStorage = async (recordId, filePath) => {
15
+ const fileData = fs.createReadStream(filePath);
16
+ const uploadResponse = await services.uploadToS3(recordId, fileData);
17
+
18
+ return uploadResponse.data.fileId;
19
+ };
20
+
21
+ const submitCoiFilesUpload = async ({
22
+ recordId,
23
+ fileIds,
24
+ uploadedBy,
25
+ uploaderContext,
26
+ uploaderEmail,
27
+ recordDateModified,
28
+ duplicateHandling
29
+ }) => {
30
+ // ICS is the default uploader, so without a session it falls back to the ICS cookie; other types go out unauthenticated
31
+ const isUnauthenticatedUpload = !uploaderContext && uploadedBy !== UPLOADER_TYPES.ICS;
32
+
33
+ const submitCoiModel = new SubmitCoiModel({
34
+ recordId,
35
+ fileIds,
36
+ uploadedBy,
37
+ uploaderContext,
38
+ uploaderEmail,
39
+ recordDateModified,
40
+ duplicateHandling
41
+ });
42
+
43
+ if (isUnauthenticatedUpload) {
44
+ return services.submitCoi(submitCoiModel);
45
+ }
46
+
47
+ const sessionContext = uploaderContext || await getIcsCookie();
48
+ return services.submitCoiByUserContext(submitCoiModel, sessionContext);
49
+ };
50
+
51
+ const uploadCoi = async (
52
+ recordId,
53
+ filePath,
54
+ uploadedBy,
55
+ uploaderEmail,
56
+ {
57
+ markAsScaned,
58
+ uploaderContext,
59
+ duplicateHandling
60
+ } = {}
61
+ ) => {
62
+ const icsContext = await getIcsCookie();
63
+ const recordResponse = await getContractorById(recordId, icsContext); // Auditors can't read the record, same as combineLastTwoCOIs
64
+ const recordDateModified = recordResponse.data.dateModified;
65
+ const fileId = await uploadFileToStorage(recordId, filePath);
66
+
67
+ const submitResponse = fileId && await submitCoiFilesUpload({
68
+ recordId,
69
+ fileIds: [fileId],
70
+ uploadedBy,
71
+ uploaderContext,
72
+ uploaderEmail,
73
+ recordDateModified,
74
+ duplicateHandling
75
+ });
76
+
77
+ const uploadedCoi = !!submitResponse.data.fileName && { id: fileId, name: submitResponse.data.fileName };
78
+
79
+ if (uploadedCoi.name && markAsScaned) {
80
+ await fileScanService.completeCoiScan(recordId, uploadedCoi.name, icsContext); // Only ICS may update the scan status
81
+ }
82
+
83
+ return uploadedCoi;
84
+ };
85
+
86
+ // Stages a certificate the way the UI does before an on-behalf upload: upload, then merge with
87
+ // isContractorExist false. The merge writes the file flat to TEMPCOI/ and returns before any
88
+ // certificate, audit or history write, so the COI is staged but not attached to the record.
89
+ const stageMergedCertificate = async (recordId, filePath, context) => {
90
+ const fileId = await uploadFileToStorage(recordId, filePath);
91
+ const submitCoiModel = new SubmitCoiModel({ recordId, fileIds: [fileId], isContractorExist: false });
92
+ const submitResponse = await services.submitCoiByUserContext(submitCoiModel, context);
93
+
94
+ if (!submitResponse.data.fileName) {
95
+ throw Error(`Failed to stage an on-behalf certificate for record: ${recordId}`);
96
+ }
97
+
98
+ return submitResponse.data.fileName;
99
+ };
100
+
101
+ const waitForAntiVirusRecordCoiScanToFinish = async (recordId, context, { maxSecondToWait = 10 } = {}) => {
102
+ let isScanned = false;
103
+ const oneSecondInMilliseconds = 1000;
104
+ let timeFromTheStart = 0;
105
+ let coiLink;
106
+ const started = Date.now();
107
+
108
+ const { data: record } = await getContractorById(recordId, context);
109
+
110
+ while (!isScanned && timeFromTheStart < maxSecondToWait) {
111
+ ({ data: coiLink } = await services.getCoiPreSignedUrl(record.certificatePdf));
112
+ isScanned = coiLink.isInProgress;
113
+ const now = Date.now();
114
+ timeFromTheStart = (now - started) / oneSecondInMilliseconds;
115
+ await timeUtils.waitFor(1).seconds;
116
+ }
117
+
118
+ return coiLink.coiPreSignedUrl;
119
+ };
120
+
121
+ const uploadCoiBatch = async (sourceRecordId, recordIds, filePaths, uploadedBy, uploaderEmail, {
122
+ uploaderContext,
123
+ duplicateHandling,
124
+ expectError
125
+ } = {}) => {
126
+ if (!uploaderContext) {
127
+ uploaderContext = await getIcsCookie();
128
+ }
129
+
130
+ const fileIds = await Promise.all(
131
+ filePaths.map(filePath => uploadFileToStorage(sourceRecordId, filePath))
132
+ );
133
+
134
+ const records = await Promise.all(
135
+ recordIds.map(async recordId => {
136
+ const { data: record } = await getContractorById(recordId, uploaderContext);
137
+ return { contractorId: recordId, contractorDateModified: record.dateModified };
138
+ })
139
+ );
140
+
141
+ const submitCoiBatchModel = new SubmitCoiBatchModel({
142
+ fileIds,
143
+ sourceRecordId,
144
+ records,
145
+ uploadedBy,
146
+ uploaderEmail,
147
+ duplicateHandling
148
+ });
149
+
150
+ return services.submitCoiBatch(submitCoiBatchModel, uploaderContext, { expectError });
151
+ };
152
+
153
+ const combineLastTwoCOIs = async ({
154
+ recordId,
155
+ fileIds,
156
+ uploadedBy,
157
+ uploaderEmail,
158
+ isContractorExist,
159
+ icsContext,
160
+ auditorContext,
161
+ isNewCoiOnTop
162
+ }) => {
163
+ const recordResponse = await getContractorById(recordId, icsContext);
164
+ const recordDateModified = recordResponse.data.dateModified;
165
+
166
+ const combineLastTwoCOIsModel = new CombineLastTwoCOIsModel({
167
+ recordId,
168
+ fileIds,
169
+ uploadedBy,
170
+ uploaderEmail,
171
+ recordDateModified,
172
+ isContractorExist,
173
+ isNewCoiOnTop
174
+ });
175
+ return services.combineLastTwoCOIs(combineLastTwoCOIsModel, auditorContext);
176
+ };
177
+
178
+ module.exports = {
179
+ uploadCoi,
180
+ uploadCoiBatch,
181
+ uploadFileToStorage,
182
+ submitCoiFilesUpload,
183
+ stageMergedCertificate,
184
+ waitForAntiVirusRecordCoiScanToFinish,
185
+ combineLastTwoCOIs
186
+ };