@mondaydotcomorg/monday-authorization 3.5.1 → 3.5.2-feat-shaime-support-entity-attributes-4-ddec1d3
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/dist/authorization-attributes-ms-service.d.ts +88 -0
- package/dist/authorization-attributes-ms-service.d.ts.map +1 -0
- package/dist/authorization-attributes-ms-service.js +290 -0
- package/dist/authorization-attributes-service.d.ts +25 -47
- package/dist/authorization-attributes-service.d.ts.map +1 -1
- package/dist/authorization-attributes-service.js +32 -171
- package/dist/authorization-attributes-sns-service.d.ts +91 -0
- package/dist/authorization-attributes-sns-service.d.ts.map +1 -0
- package/dist/authorization-attributes-sns-service.js +217 -0
- package/dist/base-attribute-assignment.d.ts +20 -0
- package/dist/base-attribute-assignment.d.ts.map +1 -0
- package/dist/base-attribute-assignment.js +36 -0
- package/dist/constants/sns.d.ts +12 -2
- package/dist/constants/sns.d.ts.map +1 -1
- package/dist/constants/sns.js +22 -2
- package/dist/entity-attribute-assignment.d.ts +20 -0
- package/dist/entity-attribute-assignment.d.ts.map +1 -0
- package/dist/entity-attribute-assignment.js +32 -0
- package/dist/entity-attributes-constants.d.ts +7 -0
- package/dist/entity-attributes-constants.d.ts.map +1 -0
- package/dist/entity-attributes-constants.js +11 -0
- package/dist/errors/argument-error.d.ts +4 -0
- package/dist/errors/argument-error.d.ts.map +1 -0
- package/dist/errors/argument-error.js +11 -0
- package/dist/esm/authorization-attributes-ms-service.d.ts +88 -0
- package/dist/esm/authorization-attributes-ms-service.d.ts.map +1 -0
- package/dist/esm/authorization-attributes-ms-service.mjs +288 -0
- package/dist/esm/authorization-attributes-service.d.ts +25 -47
- package/dist/esm/authorization-attributes-service.d.ts.map +1 -1
- package/dist/esm/authorization-attributes-service.mjs +32 -167
- package/dist/esm/authorization-attributes-sns-service.d.ts +91 -0
- package/dist/esm/authorization-attributes-sns-service.d.ts.map +1 -0
- package/dist/esm/authorization-attributes-sns-service.mjs +211 -0
- package/dist/esm/base-attribute-assignment.d.ts +20 -0
- package/dist/esm/base-attribute-assignment.d.ts.map +1 -0
- package/dist/esm/base-attribute-assignment.mjs +30 -0
- package/dist/esm/constants/sns.d.ts +12 -2
- package/dist/esm/constants/sns.d.ts.map +1 -1
- package/dist/esm/constants/sns.mjs +17 -3
- package/dist/esm/entity-attribute-assignment.d.ts +20 -0
- package/dist/esm/entity-attribute-assignment.d.ts.map +1 -0
- package/dist/esm/entity-attribute-assignment.mjs +30 -0
- package/dist/esm/entity-attributes-constants.d.ts +7 -0
- package/dist/esm/entity-attributes-constants.d.ts.map +1 -0
- package/dist/esm/entity-attributes-constants.mjs +9 -0
- package/dist/esm/errors/argument-error.d.ts +4 -0
- package/dist/esm/errors/argument-error.d.ts.map +1 -0
- package/dist/esm/errors/argument-error.mjs +9 -0
- package/dist/esm/resource-attribute-assignment.d.ts +20 -0
- package/dist/esm/resource-attribute-assignment.d.ts.map +1 -0
- package/dist/esm/resource-attribute-assignment.mjs +30 -0
- package/dist/esm/resource-attributes-constants.d.ts +25 -0
- package/dist/esm/resource-attributes-constants.d.ts.map +1 -0
- package/dist/esm/resource-attributes-constants.mjs +28 -0
- package/dist/esm/testKit/index.d.ts +4 -4
- package/dist/esm/testKit/index.d.ts.map +1 -1
- package/dist/esm/types/authorization-attributes-contracts.d.ts +26 -11
- package/dist/esm/types/authorization-attributes-contracts.d.ts.map +1 -1
- package/dist/esm/types/authorization-attributes-contracts.mjs +6 -6
- package/dist/esm/types/authorization-attributes-service.interface.d.ts +57 -0
- package/dist/esm/types/authorization-attributes-service.interface.d.ts.map +1 -0
- package/dist/esm/types/authorization-attributes-service.interface.mjs +1 -0
- package/dist/esm/utils/validation.d.ts +45 -0
- package/dist/esm/utils/validation.d.ts.map +1 -0
- package/dist/esm/utils/validation.mjs +152 -0
- package/dist/resource-attribute-assignment.d.ts +20 -0
- package/dist/resource-attribute-assignment.d.ts.map +1 -0
- package/dist/resource-attribute-assignment.js +32 -0
- package/dist/resource-attributes-constants.d.ts +25 -0
- package/dist/resource-attributes-constants.d.ts.map +1 -0
- package/dist/resource-attributes-constants.js +31 -0
- package/dist/testKit/index.d.ts +4 -4
- package/dist/testKit/index.d.ts.map +1 -1
- package/dist/types/authorization-attributes-contracts.d.ts +26 -11
- package/dist/types/authorization-attributes-contracts.d.ts.map +1 -1
- package/dist/types/authorization-attributes-contracts.js +5 -5
- package/dist/types/authorization-attributes-service.interface.d.ts +57 -0
- package/dist/types/authorization-attributes-service.interface.d.ts.map +1 -0
- package/dist/types/authorization-attributes-service.interface.js +1 -0
- package/dist/utils/validation.d.ts +45 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +158 -0
- package/package.json +3 -1
- package/src/authorization-attributes-ms-service.ts +438 -0
- package/src/authorization-attributes-service.ts +34 -222
- package/src/authorization-attributes-sns-service.ts +312 -0
- package/src/base-attribute-assignment.ts +46 -0
- package/src/constants/sns.ts +19 -2
- package/src/entity-attribute-assignment.ts +30 -0
- package/src/entity-attributes-constants.ts +7 -0
- package/src/errors/argument-error.ts +7 -0
- package/src/resource-attribute-assignment.ts +38 -0
- package/src/resource-attributes-constants.ts +27 -0
- package/src/testKit/index.ts +5 -5
- package/src/types/authorization-attributes-contracts.ts +34 -11
- package/src/types/authorization-attributes-service.interface.ts +101 -0
- package/src/utils/validation.ts +171 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { Api, HttpClient } from '@mondaydotcomorg/trident-backend-api';
|
|
2
|
+
import { signAuthorizationHeader } from '@mondaydotcomorg/monday-jwt';
|
|
3
|
+
import { ResourceAttributeAssignment } from './resource-attribute-assignment';
|
|
4
|
+
import { EntityAttributeAssignment } from './entity-attribute-assignment';
|
|
5
|
+
import {
|
|
6
|
+
AttributeOperation,
|
|
7
|
+
ResourceAttributeOperation,
|
|
8
|
+
EntityAttributeOperation,
|
|
9
|
+
} from './types/authorization-attributes-contracts';
|
|
10
|
+
import { EntityType } from './entity-attributes-constants';
|
|
11
|
+
import { ArgumentError } from './errors/argument-error';
|
|
12
|
+
import { AuthorizationInternalService, logger } from './authorization-internal-service';
|
|
13
|
+
import { getAttributionsFromApi } from './attributions-service';
|
|
14
|
+
import { APP_NAME } from './constants';
|
|
15
|
+
import { ValidationUtils } from './utils/validation';
|
|
16
|
+
import { AuthorizationAttributesService } from './types/authorization-attributes-service.interface';
|
|
17
|
+
import { Resource } from './types/general';
|
|
18
|
+
import { StringNullableChain } from 'lodash';
|
|
19
|
+
const INTERNAL_APP_NAME = 'internal_ms';
|
|
20
|
+
const UPSERT_RESOURCE_ATTRIBUTES_PATH = '/attributes/{accountId}/resource';
|
|
21
|
+
const DELETE_RESOURCE_ATTRIBUTES_PATH = '/attributes/{accountId}/resource/{resourceType}/{resourceId}';
|
|
22
|
+
const UPSERT_ENTITY_ATTRIBUTES_PATH = '/attributes/{accountId}/entity';
|
|
23
|
+
const DELETE_ENTITY_ATTRIBUTES_PATH = '/attributes/{accountId}/entity/{entityType}/{entityId}';
|
|
24
|
+
|
|
25
|
+
interface DeleteRequestBody {
|
|
26
|
+
keys: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Service class for managing resource attributes in the authorization microservice.
|
|
31
|
+
* Provides synchronous HTTP operations to create/update and delete attributes on resources.
|
|
32
|
+
*/
|
|
33
|
+
export class AuthorizationAttributesMsService implements AuthorizationAttributesService {
|
|
34
|
+
private static LOG_TAG = 'authorization_attributes_ms';
|
|
35
|
+
private static httpClient: HttpClient | null = Api.getPart('httpClient') ?? null;
|
|
36
|
+
|
|
37
|
+
public static destroyHttpClient(): void {
|
|
38
|
+
AuthorizationAttributesMsService.httpClient = null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates or updates resource attributes synchronously.
|
|
43
|
+
* @param accountId The account ID
|
|
44
|
+
* @param resourceAttributeAssignments Array of ResourceAttributeAssignment objects
|
|
45
|
+
* @returns Promise<void>
|
|
46
|
+
*/
|
|
47
|
+
async upsertResourceAttributes(
|
|
48
|
+
accountId: number,
|
|
49
|
+
resourceAttributeAssignments: ResourceAttributeAssignment[],
|
|
50
|
+
_appName?: string,
|
|
51
|
+
_callerActionIdentifier?: string
|
|
52
|
+
): Promise<void> {
|
|
53
|
+
return AuthorizationAttributesMsService.executeUpsertRequest(
|
|
54
|
+
accountId,
|
|
55
|
+
resourceAttributeAssignments,
|
|
56
|
+
UPSERT_RESOURCE_ATTRIBUTES_PATH,
|
|
57
|
+
'resourceAttributeAssignments',
|
|
58
|
+
ResourceAttributeAssignment,
|
|
59
|
+
'resource',
|
|
60
|
+
'upsertResourceAttributes'
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Deletes specific attributes from a resource synchronously.
|
|
66
|
+
* @param accountId The account ID
|
|
67
|
+
* @param resource Object with resourceType (string) and resourceId (number)
|
|
68
|
+
* @param attributeKeys Array of attribute key strings to delete
|
|
69
|
+
* @returns Promise<void>
|
|
70
|
+
*/
|
|
71
|
+
async deleteResourceAttributes(
|
|
72
|
+
accountId: number,
|
|
73
|
+
resource: Resource,
|
|
74
|
+
attributeKeys: string[],
|
|
75
|
+
_appName?: string,
|
|
76
|
+
_callerActionIdentifier?: string
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
ValidationUtils.validateResource(resource);
|
|
79
|
+
ValidationUtils.validateStringArray(attributeKeys);
|
|
80
|
+
return AuthorizationAttributesMsService.executeDeleteRequest(
|
|
81
|
+
accountId,
|
|
82
|
+
DELETE_RESOURCE_ATTRIBUTES_PATH,
|
|
83
|
+
{
|
|
84
|
+
resourceType: resource.type,
|
|
85
|
+
resourceId: resource.id as number,
|
|
86
|
+
},
|
|
87
|
+
attributeKeys,
|
|
88
|
+
'resource',
|
|
89
|
+
'deleteResourceAttributesSync',
|
|
90
|
+
{ resource }
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates or updates entity attributes synchronously.
|
|
96
|
+
* @param accountId The account ID
|
|
97
|
+
* @param entityAttributeAssignments Array of EntityAttributeAssignment objects
|
|
98
|
+
* @returns Promise<void>
|
|
99
|
+
*/
|
|
100
|
+
async upsertEntityAttributes(
|
|
101
|
+
accountId: number,
|
|
102
|
+
entityAttributeAssignments: EntityAttributeAssignment[],
|
|
103
|
+
_appName?: string,
|
|
104
|
+
_callerActionIdentifier?: string
|
|
105
|
+
): Promise<void> {
|
|
106
|
+
return AuthorizationAttributesMsService.executeUpsertRequest(
|
|
107
|
+
accountId,
|
|
108
|
+
entityAttributeAssignments,
|
|
109
|
+
UPSERT_ENTITY_ATTRIBUTES_PATH,
|
|
110
|
+
'entityAttributeAssignments',
|
|
111
|
+
EntityAttributeAssignment,
|
|
112
|
+
'entity',
|
|
113
|
+
'upsertEntityAttributesSync'
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Deletes specific attributes from an entity synchronously.
|
|
119
|
+
* @param accountId The account ID
|
|
120
|
+
* @param entityType The entity type
|
|
121
|
+
* @param entityId The entity ID
|
|
122
|
+
* @param attributeKeys Array of attribute key strings to delete
|
|
123
|
+
* @returns Promise<void>
|
|
124
|
+
*/
|
|
125
|
+
async deleteEntityAttributes(
|
|
126
|
+
accountId: number,
|
|
127
|
+
entityType: EntityType,
|
|
128
|
+
entityId: number,
|
|
129
|
+
attributeKeys: string[],
|
|
130
|
+
_appName?: string,
|
|
131
|
+
_callerActionIdentifier?: string
|
|
132
|
+
): Promise<void> {
|
|
133
|
+
if (!entityType || typeof entityType !== 'string' || entityType.trim() === '') {
|
|
134
|
+
throw new ArgumentError(`entityType must be a non-empty string, got: ${entityType}`);
|
|
135
|
+
}
|
|
136
|
+
ValidationUtils.validateInteger(entityId);
|
|
137
|
+
|
|
138
|
+
return AuthorizationAttributesMsService.executeDeleteRequest(
|
|
139
|
+
accountId,
|
|
140
|
+
DELETE_ENTITY_ATTRIBUTES_PATH,
|
|
141
|
+
{
|
|
142
|
+
entityType,
|
|
143
|
+
entityId,
|
|
144
|
+
},
|
|
145
|
+
attributeKeys,
|
|
146
|
+
'entity',
|
|
147
|
+
'deleteEntityAttributesSync',
|
|
148
|
+
{ entityType, entityId }
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Updates resource attributes (batch operations).
|
|
154
|
+
* Note: MS service does not support batch operations directly.
|
|
155
|
+
* This method processes operations sequentially using upsert/delete methods.
|
|
156
|
+
* @param accountId The account ID
|
|
157
|
+
* @param appName App name (required for interface compatibility, but not used in MS service)
|
|
158
|
+
* @param callerActionIdentifier Action identifier (required for interface compatibility, but not used in MS service)
|
|
159
|
+
* @param resourceAttributeOperations Array of operations to perform
|
|
160
|
+
* @returns Promise<ResourceAttributesOperation[]> Array of processed operations
|
|
161
|
+
*/
|
|
162
|
+
async updateResourceAttributes(
|
|
163
|
+
accountId: number,
|
|
164
|
+
_appName: string,
|
|
165
|
+
_callerActionIdentifier: string,
|
|
166
|
+
resourceAttributeOperations: ResourceAttributeOperation[]
|
|
167
|
+
): Promise<ResourceAttributeOperation[]> {
|
|
168
|
+
const processedOperations: ResourceAttributeOperation[] = [];
|
|
169
|
+
|
|
170
|
+
for (const operation of resourceAttributeOperations) {
|
|
171
|
+
if (operation.operationType === AttributeOperation.UPSERT) {
|
|
172
|
+
if (!operation.resourceId) {
|
|
173
|
+
throw new ArgumentError('resourceId is required for upsert operation');
|
|
174
|
+
}
|
|
175
|
+
await this.upsertResourceAttributes(accountId, [
|
|
176
|
+
new ResourceAttributeAssignment(
|
|
177
|
+
operation.resourceId,
|
|
178
|
+
operation.resourceType,
|
|
179
|
+
operation.key,
|
|
180
|
+
operation.value || ''
|
|
181
|
+
),
|
|
182
|
+
]);
|
|
183
|
+
processedOperations.push(operation);
|
|
184
|
+
} else if (operation.operationType === AttributeOperation.DELETE) {
|
|
185
|
+
if (!operation.resourceId) {
|
|
186
|
+
throw new ArgumentError('resourceId is required for delete operation');
|
|
187
|
+
}
|
|
188
|
+
await this.deleteResourceAttributes(
|
|
189
|
+
accountId,
|
|
190
|
+
{
|
|
191
|
+
type: operation.resourceType,
|
|
192
|
+
id: operation.resourceId,
|
|
193
|
+
},
|
|
194
|
+
[operation.key]
|
|
195
|
+
);
|
|
196
|
+
processedOperations.push(operation);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return processedOperations;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Updates entity attributes (batch operations).
|
|
205
|
+
* Note: MS service does not support batch operations directly.
|
|
206
|
+
* This method processes operations sequentially using upsert/delete methods.
|
|
207
|
+
* @param accountId The account ID
|
|
208
|
+
* @param appName App name (required for interface compatibility, but not used in MS service)
|
|
209
|
+
* @param callerActionIdentifier Action identifier (required for interface compatibility, but not used in MS service)
|
|
210
|
+
* @param entityAttributeOperations Array of operations to perform
|
|
211
|
+
* @returns Promise<EntityAttributesOperation[]> Array of processed operations
|
|
212
|
+
*/
|
|
213
|
+
async updateEntityAttributes(
|
|
214
|
+
accountId: number,
|
|
215
|
+
_appName: string,
|
|
216
|
+
_callerActionIdentifier: string,
|
|
217
|
+
entityAttributeOperations: EntityAttributeOperation[]
|
|
218
|
+
): Promise<EntityAttributeOperation[]> {
|
|
219
|
+
const processedOperations: EntityAttributeOperation[] = [];
|
|
220
|
+
for (const operation of entityAttributeOperations) {
|
|
221
|
+
if (operation.operationType === 'upsert') {
|
|
222
|
+
await this.upsertEntityAttributes(accountId, [
|
|
223
|
+
new EntityAttributeAssignment(operation.entityId, operation.entityType, operation.key, operation.value || ''),
|
|
224
|
+
]);
|
|
225
|
+
processedOperations.push(operation);
|
|
226
|
+
} else if (operation.operationType === 'delete') {
|
|
227
|
+
await this.deleteEntityAttributes(accountId, operation.entityType, operation.entityId, [operation.key]);
|
|
228
|
+
processedOperations.push(operation);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return processedOperations;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Replaces path template parameters with actual values
|
|
237
|
+
* @param template Path template with placeholders like {accountId}
|
|
238
|
+
* @param params Object with parameter names and values
|
|
239
|
+
* @returns Path with all placeholders replaced
|
|
240
|
+
*/
|
|
241
|
+
private static replacePathParams(template: string, params: Record<string, string | number>): string {
|
|
242
|
+
let path = template;
|
|
243
|
+
for (const [key, value] of Object.entries(params)) {
|
|
244
|
+
path = path.replace(`{${key}}`, String(value));
|
|
245
|
+
}
|
|
246
|
+
return path;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Generic helper for executing delete requests
|
|
251
|
+
*/
|
|
252
|
+
private static async executeDeleteRequest(
|
|
253
|
+
accountId: number,
|
|
254
|
+
pathTemplate: string,
|
|
255
|
+
pathParams: Record<string, string | number>,
|
|
256
|
+
keys: string[],
|
|
257
|
+
logPrefix: string,
|
|
258
|
+
methodName: string,
|
|
259
|
+
context: Record<string, any> = {}
|
|
260
|
+
): Promise<void> {
|
|
261
|
+
// Validate inputs
|
|
262
|
+
ValidationUtils.validateInteger(accountId);
|
|
263
|
+
ValidationUtils.validateStringArray(keys);
|
|
264
|
+
|
|
265
|
+
if (!keys.length) {
|
|
266
|
+
logger.warn({ tag: this.LOG_TAG, accountId, ...pathParams }, `${methodName} called with empty keys array`);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const requestBody: DeleteRequestBody = { keys };
|
|
270
|
+
|
|
271
|
+
if (!AuthorizationAttributesMsService.httpClient) {
|
|
272
|
+
throw new Error('AuthorizationAttributesMsService: HTTP client is not initialized');
|
|
273
|
+
}
|
|
274
|
+
const path = AuthorizationAttributesMsService.replacePathParams(pathTemplate, { accountId, ...pathParams });
|
|
275
|
+
const headers = AuthorizationAttributesMsService.getRequestHeaders(accountId);
|
|
276
|
+
|
|
277
|
+
try {
|
|
278
|
+
logger.info(
|
|
279
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, ...pathParams, keys },
|
|
280
|
+
`Deleting ${logPrefix} attributes`
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
await AuthorizationAttributesMsService.httpClient.fetch(
|
|
284
|
+
{
|
|
285
|
+
url: {
|
|
286
|
+
appName: APP_NAME,
|
|
287
|
+
path,
|
|
288
|
+
},
|
|
289
|
+
method: 'DELETE',
|
|
290
|
+
headers,
|
|
291
|
+
body: JSON.stringify(requestBody),
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
295
|
+
retryPolicy: AuthorizationInternalService.getRetriesPolicy(),
|
|
296
|
+
}
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
logger.debug(
|
|
300
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, ...pathParams, keys },
|
|
301
|
+
`Successfully deleted ${logPrefix} attributes`
|
|
302
|
+
);
|
|
303
|
+
} catch (err) {
|
|
304
|
+
logger.error(
|
|
305
|
+
{
|
|
306
|
+
tag: AuthorizationAttributesMsService.LOG_TAG,
|
|
307
|
+
method: methodName,
|
|
308
|
+
accountId,
|
|
309
|
+
...pathParams,
|
|
310
|
+
...context,
|
|
311
|
+
error: err instanceof Error ? err.message : String(err),
|
|
312
|
+
},
|
|
313
|
+
`Failed in ${methodName}`
|
|
314
|
+
);
|
|
315
|
+
throw err;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Gets request headers including Authorization, Content-Type, and optional attribution headers
|
|
321
|
+
*/
|
|
322
|
+
private static getRequestHeaders(accountId: number, userId?: number): Record<string, string> {
|
|
323
|
+
const headers: Record<string, string> = {
|
|
324
|
+
'Content-Type': 'application/json',
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// Generate Authorization token
|
|
328
|
+
const authToken = signAuthorizationHeader({
|
|
329
|
+
appName: INTERNAL_APP_NAME,
|
|
330
|
+
accountId,
|
|
331
|
+
userId,
|
|
332
|
+
});
|
|
333
|
+
headers.Authorization = authToken;
|
|
334
|
+
|
|
335
|
+
// Add attribution headers if available
|
|
336
|
+
const attributionHeaders = getAttributionsFromApi();
|
|
337
|
+
for (const key in attributionHeaders) {
|
|
338
|
+
if (Object.prototype.hasOwnProperty.call(attributionHeaders, key)) {
|
|
339
|
+
headers[key] = attributionHeaders[key];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Add X-REQUEST-ID if available from context
|
|
344
|
+
try {
|
|
345
|
+
const tridentContext = Api.getPart('context');
|
|
346
|
+
if (tridentContext?.runtimeAttributions) {
|
|
347
|
+
const outgoingHeaders = tridentContext.runtimeAttributions.buildOutgoingHeaders('HTTP_INTERNAL');
|
|
348
|
+
if (outgoingHeaders) {
|
|
349
|
+
const attributionHeadersMap: Record<string, string> = {};
|
|
350
|
+
for (const [key, value] of outgoingHeaders) {
|
|
351
|
+
attributionHeadersMap[key] = value;
|
|
352
|
+
}
|
|
353
|
+
if (attributionHeadersMap['x-request-id']) {
|
|
354
|
+
headers['X-REQUEST-ID'] = attributionHeadersMap['x-request-id'];
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
} catch (error) {
|
|
359
|
+
// Silently fail if context is not available
|
|
360
|
+
logger.debug({ tag: AuthorizationAttributesMsService.LOG_TAG, error }, 'Failed to get request ID from context');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Add X-REQUEST-START timestamp
|
|
364
|
+
headers['X-REQUEST-START'] = Math.floor(Date.now() / 1000).toString();
|
|
365
|
+
|
|
366
|
+
return headers;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Generic helper for executing upsert requests
|
|
371
|
+
*/
|
|
372
|
+
private static async executeUpsertRequest<T extends ResourceAttributeAssignment | EntityAttributeAssignment>(
|
|
373
|
+
accountId: number,
|
|
374
|
+
assignments: T[],
|
|
375
|
+
pathTemplate: string,
|
|
376
|
+
requestBodyKey: 'resourceAttributeAssignments' | 'entityAttributeAssignments',
|
|
377
|
+
assignmentClass: abstract new (...args: any[]) => T,
|
|
378
|
+
logPrefix: string,
|
|
379
|
+
methodName: string
|
|
380
|
+
): Promise<void> {
|
|
381
|
+
ValidationUtils.validateInteger(accountId);
|
|
382
|
+
ValidationUtils.validateArrayTypeOf(assignments, assignmentClass);
|
|
383
|
+
if (!assignments.length) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const assignmentDto = assignments.map(assignment => assignment.toDataTransferObject());
|
|
387
|
+
|
|
388
|
+
const requestBody =
|
|
389
|
+
requestBodyKey === 'resourceAttributeAssignments'
|
|
390
|
+
? { resourceAttributeAssignments: assignmentDto }
|
|
391
|
+
: { entityAttributeAssignments: assignmentDto };
|
|
392
|
+
|
|
393
|
+
if (!AuthorizationAttributesMsService.httpClient) {
|
|
394
|
+
throw new Error('AuthorizationAttributesMsService: HTTP client is not initialized');
|
|
395
|
+
}
|
|
396
|
+
const path = AuthorizationAttributesMsService.replacePathParams(pathTemplate, { accountId });
|
|
397
|
+
const headers = AuthorizationAttributesMsService.getRequestHeaders(accountId);
|
|
398
|
+
|
|
399
|
+
try {
|
|
400
|
+
logger.info(
|
|
401
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, count: assignments.length },
|
|
402
|
+
`Upserting ${logPrefix} attributes`
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
await AuthorizationAttributesMsService.httpClient.fetch(
|
|
406
|
+
{
|
|
407
|
+
url: {
|
|
408
|
+
appName: APP_NAME,
|
|
409
|
+
path,
|
|
410
|
+
},
|
|
411
|
+
method: 'POST',
|
|
412
|
+
headers,
|
|
413
|
+
body: JSON.stringify(requestBody),
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
417
|
+
retryPolicy: AuthorizationInternalService.getRetriesPolicy(),
|
|
418
|
+
}
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
logger.debug(
|
|
422
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, count: assignments.length },
|
|
423
|
+
`Successfully upserted ${logPrefix} attributes`
|
|
424
|
+
);
|
|
425
|
+
} catch (err) {
|
|
426
|
+
logger.error(
|
|
427
|
+
{
|
|
428
|
+
tag: AuthorizationAttributesMsService.LOG_TAG,
|
|
429
|
+
method: methodName,
|
|
430
|
+
accountId,
|
|
431
|
+
error: err instanceof Error ? err.message : String(err),
|
|
432
|
+
},
|
|
433
|
+
`Failed in ${methodName}`
|
|
434
|
+
);
|
|
435
|
+
throw err;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|