@getjones/test-infra 1.8.1 → 1.11.0

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.
Files changed (124) hide show
  1. package/package.json +29 -29
  2. package/src/common/factory/index.js +3 -0
  3. package/src/common/factory/records/records.js +45 -0
  4. package/src/common/httpResponseStatusCodes.json +11 -11
  5. package/src/common/requests/apiKeyAuth/requests.js +38 -38
  6. package/src/common/requests/cookieAuth/requests.js +40 -40
  7. package/src/common/requests/requestHandlers.js +60 -60
  8. package/src/common/utils/numberUtils.js +22 -22
  9. package/src/common/utils/timeUtils.js +147 -0
  10. package/src/common/validations/errorValidation.js +16 -16
  11. package/src/common/validations/typeValidations.js +36 -36
  12. package/src/env.js +45 -44
  13. package/src/index.js +67 -65
  14. package/src/saas/internal-api/accounts/api/core/accounts.core.api.js +28 -28
  15. package/src/saas/internal-api/accounts/api/core/accounts.core.service.js +77 -77
  16. package/src/saas/internal-api/accounts/api/openapi/accounts.openapi.api.js +48 -48
  17. package/src/saas/internal-api/accounts/api/openapi/accounts.openapi.service.js +74 -74
  18. package/src/saas/internal-api/accounts/api/settings/settings.api.js +21 -21
  19. package/src/saas/internal-api/accounts/api/settings/settings.service.js +24 -24
  20. package/src/saas/internal-api/accounts/index.js +20 -20
  21. package/src/saas/internal-api/accounts/models/core/create.account.model.js +15 -15
  22. package/src/saas/internal-api/accounts/models/settings/update.settings.model.js +19 -19
  23. package/src/saas/internal-api/accounts/utils/utils.js +32 -32
  24. package/src/saas/internal-api/auth/api/auth.api.js +12 -12
  25. package/src/saas/internal-api/auth/api/auth.service.js +47 -47
  26. package/src/saas/internal-api/auth/index.js +3 -3
  27. package/src/saas/internal-api/coi/api/coi.api.js +34 -34
  28. package/src/saas/internal-api/coi/api/coi.service.js +22 -22
  29. package/src/saas/internal-api/coi/consts/uploaderTypes.js +7 -7
  30. package/src/saas/internal-api/coi/index.js +13 -13
  31. package/src/saas/internal-api/coi/models/submit.coi.model.js +9 -9
  32. package/src/saas/internal-api/coi/utils/utils.js +48 -48
  33. package/src/saas/internal-api/compliance/api/audit/audit.api.js +40 -40
  34. package/src/saas/internal-api/compliance/api/audit/audit.service.js +33 -33
  35. package/src/saas/internal-api/compliance/api/convert/convert.api.js +17 -17
  36. package/src/saas/internal-api/compliance/api/convert/convert.service.js +11 -11
  37. package/src/saas/internal-api/compliance/api/coverage/coverage.api.js +33 -33
  38. package/src/saas/internal-api/compliance/api/coverage/coverage.service.js +24 -24
  39. package/src/saas/internal-api/compliance/api/waive/waive.api.js +21 -21
  40. package/src/saas/internal-api/compliance/api/waive/waive.service.js +15 -15
  41. package/src/saas/internal-api/compliance/index.js +22 -22
  42. package/src/saas/internal-api/compliance/models/audit/audit.model.js +110 -110
  43. package/src/saas/internal-api/compliance/models/waive/waive.model.js +45 -45
  44. package/src/saas/internal-api/contractors/api/contractors.api.js +159 -121
  45. package/src/saas/internal-api/contractors/api/contractors.service.js +136 -101
  46. package/src/saas/internal-api/contractors/index.js +8 -8
  47. package/src/saas/internal-api/contractors/models/create.contractor.model.js +22 -22
  48. package/src/saas/internal-api/contractors/models/edit.contractor.model.js +69 -91
  49. package/src/saas/internal-api/contractors/models/verification.contractor.model.js +27 -33
  50. package/src/saas/internal-api/dbEmails/api/db.emails.api.js +25 -0
  51. package/src/saas/internal-api/dbEmails/api/db.emails.service.js +19 -0
  52. package/src/saas/internal-api/dbEmails/index.js +3 -0
  53. package/src/saas/internal-api/dbEmails/models/db.emails.model.js +21 -0
  54. package/src/saas/internal-api/globalRecord/api/global.record.api.js +76 -76
  55. package/src/saas/internal-api/globalRecord/api/global.record.service.js +83 -83
  56. package/src/saas/internal-api/globalRecord/consts.js +20 -20
  57. package/src/saas/internal-api/globalRecord/index.js +6 -6
  58. package/src/saas/internal-api/globalRecord/models/global.record.model.js +43 -43
  59. package/src/saas/internal-api/globalRecord/utils/util.js +148 -148
  60. package/src/saas/internal-api/homepageCharts/api/charts.api.js +12 -12
  61. package/src/saas/internal-api/homepageCharts/api/charts.service.js +7 -7
  62. package/src/saas/internal-api/homepageCharts/index.js +3 -3
  63. package/src/saas/internal-api/projects/api/core/projects.api.js +39 -39
  64. package/src/saas/internal-api/projects/api/core/projects.service.js +65 -65
  65. package/src/saas/internal-api/projects/api/settings/project.settings.api.js +18 -18
  66. package/src/saas/internal-api/projects/api/settings/project.settings.service.js +17 -17
  67. package/src/saas/internal-api/projects/consts/defaultProject.js +3 -3
  68. package/src/saas/internal-api/projects/index.js +24 -24
  69. package/src/saas/internal-api/projects/models/core/create.project.model.js +41 -41
  70. package/src/saas/internal-api/projects/models/core/update.project.model.js +47 -47
  71. package/src/saas/internal-api/projects/models/settings/update.project.settings.model.js +39 -39
  72. package/src/saas/internal-api/projects/utils/utils.js +82 -82
  73. package/src/saas/internal-api/recordNotes/api/record.notes.api.js +38 -38
  74. package/src/saas/internal-api/recordNotes/api/record.notes.service.js +29 -29
  75. package/src/saas/internal-api/recordNotes/index.js +3 -3
  76. package/src/saas/internal-api/reports/api/reports.api.js +39 -39
  77. package/src/saas/internal-api/reports/api/reports.service.js +34 -34
  78. package/src/saas/internal-api/reports/consts/consts.js +63 -63
  79. package/src/saas/internal-api/reports/index.js +15 -15
  80. package/src/saas/internal-api/reports/models/report.model.js +27 -27
  81. package/src/saas/internal-api/requests/api/requests.api.js +28 -0
  82. package/src/saas/internal-api/requests/api/requests.service.js +19 -0
  83. package/src/saas/internal-api/requests/index.js +11 -0
  84. package/src/saas/internal-api/requests/models/edit.request.model.js +31 -0
  85. package/src/saas/internal-api/requests/models/tenant.request.model.js +9 -0
  86. package/src/saas/internal-api/requests/models/validation.models.js +8 -0
  87. package/src/saas/internal-api/requirements/api/requirements.api.js +27 -27
  88. package/src/saas/internal-api/requirements/api/requirements.service.js +23 -23
  89. package/src/saas/internal-api/requirements/index.js +6 -6
  90. package/src/saas/internal-api/requirements/models/create.requirement.model.js +45 -45
  91. package/src/saas/internal-api/serviceArea/api/coordinates.api.js +12 -12
  92. package/src/saas/internal-api/serviceArea/api/coordinates.service.js +7 -7
  93. package/src/saas/internal-api/serviceArea/index.js +4 -4
  94. package/src/saas/internal-api/serviceArea/utils/utils.js +8 -8
  95. package/src/saas/internal-api/tabs/api/tabs.api.js +32 -32
  96. package/src/saas/internal-api/tabs/api/tabs.service.js +54 -54
  97. package/src/saas/internal-api/tabs/index.js +3 -3
  98. package/src/saas/internal-api/tabs/models/tabs.model.js +18 -18
  99. package/src/saas/internal-api/thread-scheduler/runner.js +13 -13
  100. package/src/saas/internal-api/users/api/users.api.js +78 -78
  101. package/src/saas/internal-api/users/api/users.service.js +101 -101
  102. package/src/saas/internal-api/users/index.js +4 -4
  103. package/src/saas/internal-api/users/models/users.model.js +31 -31
  104. package/src/saas/internal-api/users/utils/util.js +14 -14
  105. package/src/saas/internal-api/vendorProfile/api/vendor.profile.api.js +20 -20
  106. package/src/saas/internal-api/vendorProfile/api/vendor.profile.service.js +14 -14
  107. package/src/saas/internal-api/vendorProfile/index.js +3 -3
  108. package/src/saas/openapi/accounts/accounts.api.js +17 -17
  109. package/src/saas/openapi/accounts/accounts.service.js +11 -11
  110. package/src/saas/openapi/contractors/contractors.api.js +20 -20
  111. package/src/saas/openapi/contractors/contractors.service.js +18 -18
  112. package/src/saas/openapi/contractors/index.js +6 -6
  113. package/src/saas/openapi/contractors/models/invite.contractor.model.js +37 -37
  114. package/src/saas/openapi/projects/projects.api.js +59 -59
  115. package/src/saas/openapi/projects/projects.service.js +44 -44
  116. package/src/saas/openapi/requirements/index.js +6 -6
  117. package/src/saas/openapi/requirements/models/copy.requirement.model.js +10 -10
  118. package/src/saas/openapi/requirements/requirements.api.js +12 -12
  119. package/src/saas/openapi/requirements/requirements.service.js +20 -20
  120. package/src/saas/openapi/tabs/tabs.api.js +13 -13
  121. package/src/saas/openapi/tabs/tabs.service.js +7 -7
  122. package/tests/env.test.js +40 -40
  123. package/tests/validation.test.js +83 -83
  124. package/src/common/utils/dateUtils.js +0 -6
@@ -1,36 +1,36 @@
1
- const validateType = (input, expectedType) => {
2
- if (!(input instanceof expectedType)) {
3
- throw TypeError(`Type should be: ${expectedType}`);
4
- }
5
- };
6
-
7
- const validateDateString = dateString => {
8
- if (isNaN(Date.parse(dateString))) {
9
- throw TypeError(`Invalid date string '${dateString}'`);
10
- }
11
- };
12
-
13
- const validateInteger = (number, { min, max } = {}) => {
14
- if (!Number.isInteger(number)) {
15
- throw TypeError(`Invalid number '${number}'`);
16
- }
17
-
18
- const exceedsMin = !isNaN(min) && number < min;
19
- const exceedsMax = !isNaN(max) && number > max;
20
-
21
- if (exceedsMin && exceedsMax) {
22
- throw RangeError('Number is not in range.');
23
- } else if (exceedsMin) {
24
- throw RangeError(`Number is less than ${min}.`);
25
- } else if (exceedsMax) {
26
- throw RangeError(`Number is greater than ${max}.`);
27
- }
28
- };
29
-
30
- const validateArray = obj => {
31
- if (!Array.isArray(obj)) {
32
- throw TypeError(`Invalid Array '${JSON.stringify(obj)}'`);
33
- }
34
- };
35
-
36
- module.exports = { validateType, validateDateString, validateInteger, validateArray };
1
+ const validateType = (input, expectedType) => {
2
+ if (!(input instanceof expectedType)) {
3
+ throw TypeError(`Type should be: ${expectedType}`);
4
+ }
5
+ };
6
+
7
+ const validateDateString = dateString => {
8
+ if (isNaN(Date.parse(dateString))) {
9
+ throw TypeError(`Invalid date string '${dateString}'`);
10
+ }
11
+ };
12
+
13
+ const validateInteger = (number, { min, max } = {}) => {
14
+ if (!Number.isInteger(number)) {
15
+ throw TypeError(`Invalid number '${number}'`);
16
+ }
17
+
18
+ const exceedsMin = !isNaN(min) && number < min;
19
+ const exceedsMax = !isNaN(max) && number > max;
20
+
21
+ if (exceedsMin && exceedsMax) {
22
+ throw RangeError('Number is not in range.');
23
+ } else if (exceedsMin) {
24
+ throw RangeError(`Number is less than ${min}.`);
25
+ } else if (exceedsMax) {
26
+ throw RangeError(`Number is greater than ${max}.`);
27
+ }
28
+ };
29
+
30
+ const validateArray = obj => {
31
+ if (!Array.isArray(obj)) {
32
+ throw TypeError(`Invalid Array '${JSON.stringify(obj)}'`);
33
+ }
34
+ };
35
+
36
+ module.exports = { validateType, validateDateString, validateInteger, validateArray };
package/src/env.js CHANGED
@@ -1,44 +1,45 @@
1
- let privateMembers = {
2
- backendUrl: null,
3
- openapiUrl: null,
4
- nodeBackendUrl: null,
5
- credentials: null,
6
- environment: null,
7
- accountCreationSecret: null,
8
- threadSchedulerSecret: null,
9
- nodeBackendSecret: null
10
- };
11
-
12
- const init = conf => {
13
- privateMembers = Object.assign(privateMembers, conf);
14
- requiredValidation({
15
- backendUrl: privateMembers.backendUrl,
16
- openapiUrl: privateMembers.openapiUrl,
17
- nodeBackendUrl: privateMembers.nodeBackendUrl,
18
- environment: privateMembers.environment
19
- });
20
- };
21
-
22
- const requiredValidation = object => {
23
- const invalidKeys = [];
24
- Object.keys(object).forEach(key => {
25
- if (!object[key]) {
26
- invalidKeys.push(` '${key}'`);
27
- }
28
- });
29
-
30
- if (invalidKeys.length > 0) {
31
- throw Error(`Missing required configuration on your environment variables.\nThe missing env-vars are:${invalidKeys}`);
32
- }
33
- };
34
-
35
- const isLocalhost = () => {
36
- return privateMembers.backendUrl.includes('localhost');
37
- };
38
-
39
- module.exports = {
40
- init,
41
- getEnv: () => privateMembers,
42
- requiredValidation,
43
- isLocalhost
44
- };
1
+ let privateMembers = {
2
+ backendUrl: null,
3
+ openapiUrl: null,
4
+ nodeBackendUrl: null,
5
+ credentials: null,
6
+ environment: null,
7
+ accountCreationSecret: null,
8
+ threadSchedulerSecret: null,
9
+ nodeBackendSecret: null,
10
+ dbEmailsSecrets: null
11
+ };
12
+
13
+ const init = conf => {
14
+ privateMembers = Object.assign(privateMembers, conf);
15
+ requiredValidation({
16
+ backendUrl: privateMembers.backendUrl,
17
+ openapiUrl: privateMembers.openapiUrl,
18
+ nodeBackendUrl: privateMembers.nodeBackendUrl,
19
+ environment: privateMembers.environment
20
+ });
21
+ };
22
+
23
+ const requiredValidation = object => {
24
+ const invalidKeys = [];
25
+ Object.keys(object).forEach(key => {
26
+ if (!object[key]) {
27
+ invalidKeys.push(` '${key}'`);
28
+ }
29
+ });
30
+
31
+ if (invalidKeys.length > 0) {
32
+ throw Error(`Missing required configuration on your environment variables.\nThe missing env-vars are:${invalidKeys}`);
33
+ }
34
+ };
35
+
36
+ const isLocalhost = () => {
37
+ return privateMembers.backendUrl.includes('localhost');
38
+ };
39
+
40
+ module.exports = {
41
+ init,
42
+ getEnv: () => privateMembers,
43
+ requiredValidation,
44
+ isLocalhost
45
+ };
package/src/index.js CHANGED
@@ -1,65 +1,67 @@
1
- const env = require('./env');
2
-
3
- const init = conf => {
4
- env.init(conf);
5
- const internalAPI = getInternalAPI();
6
- const openAPI = getOpenAPI();
7
-
8
- return { internalAPI, openAPI };
9
- };
10
-
11
- const getInternalAPI = () => {
12
- const auth = require('./saas/internal-api/auth');
13
- const accounts = require('./saas/internal-api/accounts');
14
-
15
- const projects = require('./saas/internal-api/projects');
16
- const tabs = require('./saas/internal-api/tabs');
17
-
18
- const requirements = require('./saas/internal-api/requirements');
19
- const contractors = require('./saas/internal-api/contractors');
20
-
21
- const coi = require('./saas/internal-api/coi');
22
- const compliance = require('./saas/internal-api/compliance');
23
- const users = require('./saas/internal-api/users');
24
-
25
- const reports = require('./saas/internal-api/reports');
26
- const charts = require('./saas/internal-api/homepageCharts');
27
-
28
- const globalRecord = require('./saas/internal-api/globalRecord');
29
- const vendorProfile = require('./saas/internal-api/vendorProfile');
30
- const serviceArea = require('./saas/internal-api/serviceArea');
31
-
32
- const threadScheduler = require('./saas/internal-api/thread-scheduler/runner');
33
-
34
- const internalAPI = {
35
- auth,
36
- accounts,
37
- projects,
38
- tabs,
39
- requirements,
40
- contractors,
41
- coi,
42
- compliance,
43
- reports,
44
- users,
45
- charts,
46
- globalRecord,
47
- vendorProfile,
48
- serviceArea,
49
- threadScheduler
50
- };
51
-
52
- return internalAPI;
53
- };
54
-
55
- const getOpenAPI = () => {
56
- const accounts = require('./saas/openapi/accounts/accounts.service');
57
- const tabs = require('./saas/openapi/tabs/tabs.service');
58
- const contractors = require('./saas/openapi/contractors');
59
- const projects = require('./saas/openapi/projects/projects.service');
60
- const requirements = require('./saas/openapi/requirements');
61
- const openAPI = { accounts, tabs, contractors, projects, requirements };
62
- return openAPI;
63
- };
64
-
65
- module.exports = init;
1
+ const env = require('./env');
2
+
3
+ const init = conf => {
4
+ env.init(conf);
5
+ const internalAPI = getInternalAPI();
6
+ const openAPI = getOpenAPI();
7
+
8
+ return { internalAPI, openAPI };
9
+ };
10
+
11
+ const getInternalAPI = () => {
12
+ const auth = require('./saas/internal-api/auth');
13
+ const accounts = require('./saas/internal-api/accounts');
14
+
15
+ const projects = require('./saas/internal-api/projects');
16
+ const tabs = require('./saas/internal-api/tabs');
17
+
18
+ const requirements = require('./saas/internal-api/requirements');
19
+ const contractors = require('./saas/internal-api/contractors');
20
+ const requests = require('./saas/internal-api/requests');
21
+
22
+ const coi = require('./saas/internal-api/coi');
23
+ const compliance = require('./saas/internal-api/compliance');
24
+ const users = require('./saas/internal-api/users');
25
+
26
+ const reports = require('./saas/internal-api/reports');
27
+ const charts = require('./saas/internal-api/homepageCharts');
28
+
29
+ const globalRecord = require('./saas/internal-api/globalRecord');
30
+ const vendorProfile = require('./saas/internal-api/vendorProfile');
31
+ const serviceArea = require('./saas/internal-api/serviceArea');
32
+
33
+ const threadScheduler = require('./saas/internal-api/thread-scheduler/runner');
34
+
35
+ const internalAPI = {
36
+ auth,
37
+ accounts,
38
+ projects,
39
+ tabs,
40
+ requirements,
41
+ contractors,
42
+ requests,
43
+ coi,
44
+ compliance,
45
+ reports,
46
+ users,
47
+ charts,
48
+ globalRecord,
49
+ vendorProfile,
50
+ serviceArea,
51
+ threadScheduler
52
+ };
53
+
54
+ return internalAPI;
55
+ };
56
+
57
+ const getOpenAPI = () => {
58
+ const accounts = require('./saas/openapi/accounts/accounts.service');
59
+ const tabs = require('./saas/openapi/tabs/tabs.service');
60
+ const contractors = require('./saas/openapi/contractors');
61
+ const projects = require('./saas/openapi/projects/projects.service');
62
+ const requirements = require('./saas/openapi/requirements');
63
+ const openAPI = { accounts, tabs, contractors, projects, requirements };
64
+ return openAPI;
65
+ };
66
+
67
+ module.exports = init;
@@ -1,28 +1,28 @@
1
- const requests = require('../../../../../common/requests/cookieAuth/requests');
2
- const { backendUrl } = require('../../../../../env').getEnv();
3
-
4
- const Endpoints = {
5
- getAccount: `${backendUrl}/saas/accounts`,
6
- create: `${backendUrl}/saas/createaccount`,
7
- delete: `${backendUrl}/saas/account`,
8
- edit: accountId => `${backendUrl}/saas/account/${accountId}/settings`
9
- };
10
-
11
- const create = (account, context) => {
12
- return requests.postRequestHandler(Endpoints.create, account, context);
13
- };
14
-
15
- const deleteAccount = (accountId, context) => {
16
- return requests.deleteRequestHandler(Endpoints.delete, context, {}, { 'userId': accountId });
17
- };
18
-
19
- const updateAccountSettings = (accountId, settings, context) => {
20
- return requests.putRequestHandler(Endpoints.edit(accountId), { accountId, ... settings }, context);
21
- };
22
-
23
- const getByName = (accountName, context) => {
24
- const params = { name: accountName };
25
- return requests.getRequestHandler(Endpoints.getAccount, params, context);
26
- };
27
-
28
- module.exports = { create, getByName, deleteAccount, updateAccountSettings };
1
+ const requests = require('../../../../../common/requests/cookieAuth/requests');
2
+ const { backendUrl } = require('../../../../../env').getEnv();
3
+
4
+ const Endpoints = {
5
+ getAccount: `${backendUrl}/saas/accounts`,
6
+ create: `${backendUrl}/saas/createaccount`,
7
+ delete: `${backendUrl}/saas/account`,
8
+ edit: accountId => `${backendUrl}/saas/account/${accountId}/settings`
9
+ };
10
+
11
+ const create = (account, context) => {
12
+ return requests.postRequestHandler(Endpoints.create, account, context);
13
+ };
14
+
15
+ const deleteAccount = (accountId, context) => {
16
+ return requests.deleteRequestHandler(Endpoints.delete, context, {}, { 'userId': accountId });
17
+ };
18
+
19
+ const updateAccountSettings = (accountId, settings, context) => {
20
+ return requests.putRequestHandler(Endpoints.edit(accountId), { accountId, ... settings }, context);
21
+ };
22
+
23
+ const getByName = (accountName, context) => {
24
+ const params = { name: accountName };
25
+ return requests.getRequestHandler(Endpoints.getAccount, params, context);
26
+ };
27
+
28
+ module.exports = { create, getByName, deleteAccount, updateAccountSettings };
@@ -1,77 +1,77 @@
1
- const env = require('../../../../../env');
2
- const accountsAPI = require('./accounts.core.api');
3
- const CreateAccountModel = require('../../models/core/create.account.model');
4
- const { validateType } = require('../../../../../common/validations/typeValidations');
5
-
6
- const { accountCreationSecret } = env.getEnv();
7
-
8
- const createAccount = async (createAccountModel, context) => {
9
- validateType(createAccountModel, CreateAccountModel);
10
-
11
- env.requiredValidation({
12
- accountCreationSecret: accountCreationSecret,
13
- 'credentials.strongPassword': createAccountModel.password
14
- });
15
-
16
- return accountsAPI.create(createAccountModel, context);
17
- };
18
-
19
- const getAccount = async (accountName, context) => {
20
- const response = await accountsAPI.getByName(accountName, context);
21
-
22
- if (!response.data || !response.data.length) {
23
- return null;
24
- }
25
-
26
- if (response.data.length > 1) {
27
- throw new Error(`Got more than one account with the name: ${accountName}. IDs: ${existingAccounts.map(item => item.userId).join(', ')}`);
28
- }
29
-
30
- return response.data[0];
31
- };
32
-
33
- const getOrCreateAccount = async (createAccountModel, context) => {
34
- validateType(createAccountModel, CreateAccountModel);
35
- const existingAccount = await getAccount(createAccountModel.companyName, context);
36
-
37
- if (existingAccount) {
38
- return existingAccount;
39
- }
40
-
41
- const response = await accountsAPI.create(createAccountModel, context);
42
-
43
- if (!response.data) {
44
- throw new Error(`Account by the name ${createAccountModel.companyName} did not crate`);
45
- }
46
-
47
- return response.data;
48
- };
49
-
50
- const getAccountId = async (accountName, context) => {
51
- const account = await getAccount(accountName, context);
52
-
53
- if (!account) {
54
- console.error(`Account ${accountName} does not exist`);
55
- return null;
56
- }
57
-
58
- return account.userId;
59
- };
60
-
61
- const deleteAccountByName = async (accountName, context) => {
62
- const accountId = await getAccountId(accountName, context);
63
- return accountsAPI.deleteAccount(accountId, context);
64
- };
65
-
66
- const deleteAccountById = async (accountId, context) => {
67
- return accountsAPI.deleteAccount(accountId, context);
68
- };
69
-
70
- module.exports = {
71
- getAccount,
72
- getOrCreateAccount,
73
- getAccountId,
74
- createAccount,
75
- deleteAccountByName,
76
- deleteAccountById
77
- };
1
+ const env = require('../../../../../env');
2
+ const accountsAPI = require('./accounts.core.api');
3
+ const CreateAccountModel = require('../../models/core/create.account.model');
4
+ const { validateType } = require('../../../../../common/validations/typeValidations');
5
+
6
+ const { accountCreationSecret } = env.getEnv();
7
+
8
+ const createAccount = async (createAccountModel, context) => {
9
+ validateType(createAccountModel, CreateAccountModel);
10
+
11
+ env.requiredValidation({
12
+ accountCreationSecret: accountCreationSecret,
13
+ 'credentials.strongPassword': createAccountModel.password
14
+ });
15
+
16
+ return accountsAPI.create(createAccountModel, context);
17
+ };
18
+
19
+ const getAccount = async (accountName, context) => {
20
+ const response = await accountsAPI.getByName(accountName, context);
21
+
22
+ if (!response.data || !response.data.length) {
23
+ return null;
24
+ }
25
+
26
+ if (response.data.length > 1) {
27
+ throw new Error(`Got more than one account with the name: ${accountName}. IDs: ${existingAccounts.map(item => item.userId).join(', ')}`);
28
+ }
29
+
30
+ return response.data[0];
31
+ };
32
+
33
+ const getOrCreateAccount = async (createAccountModel, context) => {
34
+ validateType(createAccountModel, CreateAccountModel);
35
+ const existingAccount = await getAccount(createAccountModel.companyName, context);
36
+
37
+ if (existingAccount) {
38
+ return existingAccount;
39
+ }
40
+
41
+ const response = await accountsAPI.create(createAccountModel, context);
42
+
43
+ if (!response.data) {
44
+ throw new Error(`Account by the name ${createAccountModel.companyName} did not crate`);
45
+ }
46
+
47
+ return response.data;
48
+ };
49
+
50
+ const getAccountId = async (accountName, context) => {
51
+ const account = await getAccount(accountName, context);
52
+
53
+ if (!account) {
54
+ console.error(`Account ${accountName} does not exist`);
55
+ return null;
56
+ }
57
+
58
+ return account.userId;
59
+ };
60
+
61
+ const deleteAccountByName = async (accountName, context) => {
62
+ const accountId = await getAccountId(accountName, context);
63
+ return accountsAPI.deleteAccount(accountId, context);
64
+ };
65
+
66
+ const deleteAccountById = async (accountId, context) => {
67
+ return accountsAPI.deleteAccount(accountId, context);
68
+ };
69
+
70
+ module.exports = {
71
+ getAccount,
72
+ getOrCreateAccount,
73
+ getAccountId,
74
+ createAccount,
75
+ deleteAccountByName,
76
+ deleteAccountById
77
+ };
@@ -1,48 +1,48 @@
1
- const requests = require('../../../../../common/requests/cookieAuth/requests');
2
- const { backendUrl } = require('../../../../../env').getEnv();
3
-
4
- const Endpoints = {
5
- createApiKey: `${backendUrl}/saas/api-keys`,
6
- getStaticApiKeyByAccountId: accountId => `${backendUrl}/saas/accounts/${accountId}/static-api-keys`,
7
- getApiKeyByKeyValue: `${backendUrl}/saas/api-keys`,
8
- removeStaticApiKey: accountId => `${backendUrl}/saas/accounts/${accountId}/static-api-keys`,
9
- dynamicApiKey: accountId => `${backendUrl}/saas/accounts/${accountId}/dynamic-api-key`
10
- };
11
-
12
- const createStaticApiKey = (data, context) => {
13
- return requests.postRequestHandler(Endpoints.createApiKey, data, context);
14
- };
15
-
16
- const createGeneratorApiKey = (data, context) => {
17
- return requests.postRequestHandler(Endpoints.createApiKey, data, context);
18
- };
19
-
20
- const getStaticApiKeyByAccountId = (accountId, context) => {
21
- return requests.getRequestHandler(Endpoints.getStaticApiKeyByAccountId(accountId), {}, context);
22
- };
23
-
24
- const getApiKeyDetails = (apiKeyValue, context) => {
25
- return requests.getRequestHandler(Endpoints.getApiKeyByKeyValue, { apiKeyValue }, context);
26
- };
27
-
28
- const deleteStaticApiKey = (accountId, context) => {
29
- return requests.deleteRequestHandler(Endpoints.removeStaticApiKey(accountId), context);
30
- };
31
-
32
- const deleteDynamicApiKey = (accountId, context) => {
33
- return requests.deleteRequestHandler(Endpoints.dynamicApiKey(accountId), context);
34
- };
35
-
36
- const makeDynamicApiKeyExpired = (accountId, context) => {
37
- return requests.patchRequestHandler(Endpoints.dynamicApiKey(accountId), context, {});
38
- };
39
-
40
- module.exports = {
41
- createStaticApiKey,
42
- createGeneratorApiKey,
43
- getStaticApiKeyByAccountId,
44
- getApiKeyDetails,
45
- makeDynamicApiKeyExpired,
46
- deleteStaticApiKey,
47
- deleteDynamicApiKey
48
- };
1
+ const requests = require('../../../../../common/requests/cookieAuth/requests');
2
+ const { backendUrl } = require('../../../../../env').getEnv();
3
+
4
+ const Endpoints = {
5
+ createApiKey: `${backendUrl}/saas/api-keys`,
6
+ getStaticApiKeyByAccountId: accountId => `${backendUrl}/saas/accounts/${accountId}/static-api-keys`,
7
+ getApiKeyByKeyValue: `${backendUrl}/saas/api-keys`,
8
+ removeStaticApiKey: accountId => `${backendUrl}/saas/accounts/${accountId}/static-api-keys`,
9
+ dynamicApiKey: accountId => `${backendUrl}/saas/accounts/${accountId}/dynamic-api-key`
10
+ };
11
+
12
+ const createStaticApiKey = (data, context) => {
13
+ return requests.postRequestHandler(Endpoints.createApiKey, data, context);
14
+ };
15
+
16
+ const createGeneratorApiKey = (data, context) => {
17
+ return requests.postRequestHandler(Endpoints.createApiKey, data, context);
18
+ };
19
+
20
+ const getStaticApiKeyByAccountId = (accountId, context) => {
21
+ return requests.getRequestHandler(Endpoints.getStaticApiKeyByAccountId(accountId), {}, context);
22
+ };
23
+
24
+ const getApiKeyDetails = (apiKeyValue, context) => {
25
+ return requests.getRequestHandler(Endpoints.getApiKeyByKeyValue, { apiKeyValue }, context);
26
+ };
27
+
28
+ const deleteStaticApiKey = (accountId, context) => {
29
+ return requests.deleteRequestHandler(Endpoints.removeStaticApiKey(accountId), context);
30
+ };
31
+
32
+ const deleteDynamicApiKey = (accountId, context) => {
33
+ return requests.deleteRequestHandler(Endpoints.dynamicApiKey(accountId), context);
34
+ };
35
+
36
+ const makeDynamicApiKeyExpired = (accountId, context) => {
37
+ return requests.patchRequestHandler(Endpoints.dynamicApiKey(accountId), context, {});
38
+ };
39
+
40
+ module.exports = {
41
+ createStaticApiKey,
42
+ createGeneratorApiKey,
43
+ getStaticApiKeyByAccountId,
44
+ getApiKeyDetails,
45
+ makeDynamicApiKeyExpired,
46
+ deleteStaticApiKey,
47
+ deleteDynamicApiKey
48
+ };