@mondaydotcomorg/monday-authorization 3.5.3-feat-shaime-support-entity-attributes-in-authorization-sdk-e355942 → 3.5.3-feat-shaime-support-entity-attributes-in-authorization-sdk-ade64f6
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 +17 -22
- package/dist/authorization-attributes-ms-service.d.ts.map +1 -1
- package/dist/authorization-attributes-ms-service.js +201 -194
- package/dist/authorization-attributes-sns-service.d.ts +1 -2
- package/dist/authorization-attributes-sns-service.d.ts.map +1 -1
- package/dist/authorization-attributes-sns-service.js +1 -1
- package/dist/esm/authorization-attributes-ms-service.d.ts +17 -22
- package/dist/esm/authorization-attributes-ms-service.d.ts.map +1 -1
- package/dist/esm/authorization-attributes-ms-service.mjs +201 -194
- package/dist/esm/authorization-attributes-sns-service.d.ts +1 -2
- package/dist/esm/authorization-attributes-sns-service.d.ts.map +1 -1
- package/dist/esm/authorization-attributes-sns-service.mjs +1 -1
- package/dist/esm/prometheus-service.d.ts.map +1 -1
- package/dist/esm/resource-attribute-assignment.d.ts +0 -10
- package/dist/esm/resource-attribute-assignment.d.ts.map +1 -1
- package/dist/esm/resource-attribute-assignment.mjs +0 -12
- package/dist/esm/resource-attributes-constants.d.ts +1 -1
- package/dist/esm/resource-attributes-constants.d.ts.map +1 -1
- package/dist/esm/resource-attributes-constants.mjs +11 -11
- package/dist/esm/types/authorization-attributes-service.interface.d.ts +1 -2
- package/dist/esm/types/authorization-attributes-service.interface.d.ts.map +1 -1
- package/dist/prometheus-service.d.ts.map +1 -1
- package/dist/resource-attribute-assignment.d.ts +0 -10
- package/dist/resource-attribute-assignment.d.ts.map +1 -1
- package/dist/resource-attribute-assignment.js +0 -12
- package/dist/resource-attributes-constants.d.ts +1 -1
- package/dist/resource-attributes-constants.d.ts.map +1 -1
- package/dist/resource-attributes-constants.js +11 -11
- package/dist/types/authorization-attributes-service.interface.d.ts +1 -2
- package/dist/types/authorization-attributes-service.interface.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/authorization-attributes-ms-service.ts +335 -332
- package/src/authorization-attributes-sns-service.ts +2 -2
- package/src/prometheus-service.ts +0 -1
- package/src/resource-attribute-assignment.ts +0 -14
- package/src/resource-attributes-constants.ts +12 -12
- package/src/types/authorization-attributes-service.interface.ts +2 -1
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { Api, HttpClient } from '@mondaydotcomorg/trident-backend-api';
|
|
2
2
|
import { signAuthorizationHeader } from '@mondaydotcomorg/monday-jwt';
|
|
3
|
-
import { HttpFetcherError } from '@mondaydotcomorg/monday-fetch-api';
|
|
4
3
|
import { z } from 'zod';
|
|
5
4
|
import { ResourceAttributeAssignment } from './resource-attribute-assignment';
|
|
6
5
|
import { EntityAttributeAssignment } from './entity-attribute-assignment';
|
|
7
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
AttributeOperation,
|
|
8
|
+
EntityType,
|
|
9
|
+
ResourceAttributeOperation,
|
|
10
|
+
EntityAttributeOperation,
|
|
11
|
+
} from './types/authorization-attributes-contracts';
|
|
8
12
|
import { ArgumentError } from './errors/argument-error';
|
|
9
13
|
import { AuthorizationInternalService, logger } from './authorization-internal-service';
|
|
10
14
|
import { getAttributionsFromApi } from './attributions-service';
|
|
@@ -12,7 +16,6 @@ import { APP_NAME } from './constants';
|
|
|
12
16
|
import { ValidationUtils } from './utils/validation';
|
|
13
17
|
import { IAuthorizationAttributesService } from './types/authorization-attributes-service.interface';
|
|
14
18
|
import { Resource } from './types/general';
|
|
15
|
-
import { ResourceAttributeOperation, EntityAttributeOperation } from './types/authorization-attributes-contracts';
|
|
16
19
|
const INTERNAL_APP_NAME = 'internal_ms';
|
|
17
20
|
const UPSERT_RESOURCE_ATTRIBUTES_PATH = '/attributes/{accountId}/resource';
|
|
18
21
|
const DELETE_RESOURCE_ATTRIBUTES_PATH = '/attributes/{accountId}/resource/{resourceType}/{resourceId}';
|
|
@@ -30,12 +33,297 @@ interface DeleteRequestBody {
|
|
|
30
33
|
export class AuthorizationAttributesMsService implements IAuthorizationAttributesService {
|
|
31
34
|
private static LOG_TAG = 'authorization_attributes_ms';
|
|
32
35
|
private static httpClient: HttpClient | undefined = Api.getPart('httpClient');
|
|
36
|
+
/**
|
|
37
|
+
* Creates or updates resource attributes synchronously.
|
|
38
|
+
* @param accountId The account ID
|
|
39
|
+
* @param resourceAttributeAssignments Array of ResourceAttributeAssignment objects
|
|
40
|
+
* @returns Promise<void>
|
|
41
|
+
*/
|
|
42
|
+
async upsertResourceAttributes(
|
|
43
|
+
accountId: number,
|
|
44
|
+
resourceAttributeAssignments: ResourceAttributeAssignment[],
|
|
45
|
+
_appName?: string,
|
|
46
|
+
_callerActionIdentifier?: string
|
|
47
|
+
): Promise<void> {
|
|
48
|
+
return AuthorizationAttributesMsService.executeUpsertRequest(
|
|
49
|
+
accountId,
|
|
50
|
+
resourceAttributeAssignments,
|
|
51
|
+
UPSERT_RESOURCE_ATTRIBUTES_PATH,
|
|
52
|
+
'resourceAttributeAssignments',
|
|
53
|
+
ResourceAttributeAssignment,
|
|
54
|
+
'resource',
|
|
55
|
+
'upsertResourceAttributes'
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Deletes specific attributes from a resource synchronously.
|
|
61
|
+
* @param accountId The account ID
|
|
62
|
+
* @param resource Object with resourceType (string) and resourceId (number)
|
|
63
|
+
* @param attributeKeys Array of attribute key strings to delete
|
|
64
|
+
* @returns Promise<void>
|
|
65
|
+
*/
|
|
66
|
+
async deleteResourceAttributes(
|
|
67
|
+
accountId: number,
|
|
68
|
+
resource: Resource,
|
|
69
|
+
attributeKeys: string[],
|
|
70
|
+
_appName?: string,
|
|
71
|
+
_callerActionIdentifier?: string
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
// Validate resource object
|
|
74
|
+
if (!resource || typeof resource !== 'object') {
|
|
75
|
+
throw new ArgumentError('resource must be an object');
|
|
76
|
+
}
|
|
77
|
+
if (!resource.id) {
|
|
78
|
+
throw new ArgumentError('resource.id is required');
|
|
79
|
+
}
|
|
80
|
+
ValidationUtils.validateInteger(resource.id, 'resource.id');
|
|
81
|
+
ValidationUtils.validateString(resource.type, 'resource.type');
|
|
82
|
+
|
|
83
|
+
return AuthorizationAttributesMsService.executeDeleteRequest(
|
|
84
|
+
accountId,
|
|
85
|
+
DELETE_RESOURCE_ATTRIBUTES_PATH,
|
|
86
|
+
{
|
|
87
|
+
resourceType: resource.type,
|
|
88
|
+
resourceId: resource.id,
|
|
89
|
+
},
|
|
90
|
+
attributeKeys,
|
|
91
|
+
'resource',
|
|
92
|
+
'deleteResourceAttributesSync',
|
|
93
|
+
{ resource }
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Creates or updates entity attributes synchronously.
|
|
99
|
+
* @param accountId The account ID
|
|
100
|
+
* @param entityAttributeAssignments Array of EntityAttributeAssignment objects
|
|
101
|
+
* @returns Promise<void>
|
|
102
|
+
*/
|
|
103
|
+
async upsertEntityAttributes(
|
|
104
|
+
accountId: number,
|
|
105
|
+
entityAttributeAssignments: EntityAttributeAssignment[],
|
|
106
|
+
_appName?: string,
|
|
107
|
+
_callerActionIdentifier?: string
|
|
108
|
+
): Promise<void> {
|
|
109
|
+
return AuthorizationAttributesMsService.executeUpsertRequest(
|
|
110
|
+
accountId,
|
|
111
|
+
entityAttributeAssignments,
|
|
112
|
+
UPSERT_ENTITY_ATTRIBUTES_PATH,
|
|
113
|
+
'entityAttributeAssignments',
|
|
114
|
+
EntityAttributeAssignment,
|
|
115
|
+
'entity',
|
|
116
|
+
'upsertEntityAttributesSync'
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Deletes specific attributes from an entity synchronously.
|
|
122
|
+
* @param accountId The account ID
|
|
123
|
+
* @param entityType The entity type
|
|
124
|
+
* @param entityId The entity ID
|
|
125
|
+
* @param attributeKeys Array of attribute key strings to delete
|
|
126
|
+
* @returns Promise<void>
|
|
127
|
+
*/
|
|
128
|
+
async deleteEntityAttributes(
|
|
129
|
+
accountId: number,
|
|
130
|
+
entityType: EntityType | string,
|
|
131
|
+
entityId: number,
|
|
132
|
+
attributeKeys: string[],
|
|
133
|
+
_appName?: string,
|
|
134
|
+
_callerActionIdentifier?: string
|
|
135
|
+
): Promise<void> {
|
|
136
|
+
if (!entityType || typeof entityType !== 'string' || entityType.trim() === '') {
|
|
137
|
+
throw new ArgumentError(`entityType must be a non-empty string, got: ${entityType}`);
|
|
138
|
+
}
|
|
139
|
+
ValidationUtils.validateInteger(entityId, 'entityId');
|
|
140
|
+
|
|
141
|
+
return AuthorizationAttributesMsService.executeDeleteRequest(
|
|
142
|
+
accountId,
|
|
143
|
+
DELETE_ENTITY_ATTRIBUTES_PATH,
|
|
144
|
+
{
|
|
145
|
+
entityType,
|
|
146
|
+
entityId,
|
|
147
|
+
},
|
|
148
|
+
attributeKeys,
|
|
149
|
+
'entity',
|
|
150
|
+
'deleteEntityAttributesSync',
|
|
151
|
+
{ entityType, entityId }
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Updates resource attributes (batch operations).
|
|
157
|
+
* Note: MS service does not support batch operations directly.
|
|
158
|
+
* This method processes operations sequentially using upsert/delete methods.
|
|
159
|
+
* @param accountId The account ID
|
|
160
|
+
* @param appName App name (required for interface compatibility, but not used in MS service)
|
|
161
|
+
* @param callerActionIdentifier Action identifier (required for interface compatibility, but not used in MS service)
|
|
162
|
+
* @param resourceAttributeOperations Array of operations to perform
|
|
163
|
+
* @returns Promise<ResourceAttributesOperation[]> Array of processed operations
|
|
164
|
+
*/
|
|
165
|
+
async updateResourceAttributes(
|
|
166
|
+
accountId: number,
|
|
167
|
+
_appName: string,
|
|
168
|
+
_callerActionIdentifier: string,
|
|
169
|
+
resourceAttributeOperations: ResourceAttributeOperation[]
|
|
170
|
+
): Promise<ResourceAttributeOperation[]> {
|
|
171
|
+
const processedOperations: ResourceAttributeOperation[] = [];
|
|
172
|
+
|
|
173
|
+
for (const operation of resourceAttributeOperations) {
|
|
174
|
+
if (operation.operationType === AttributeOperation.UPSERT) {
|
|
175
|
+
if (!operation.resourceId) {
|
|
176
|
+
throw new ArgumentError('resourceId is required for upsert operation');
|
|
177
|
+
}
|
|
178
|
+
await this.upsertResourceAttributes(accountId, [
|
|
179
|
+
new ResourceAttributeAssignment(
|
|
180
|
+
operation.resourceId,
|
|
181
|
+
operation.resourceType,
|
|
182
|
+
operation.key,
|
|
183
|
+
operation.value || ''
|
|
184
|
+
),
|
|
185
|
+
]);
|
|
186
|
+
processedOperations.push(operation);
|
|
187
|
+
} else if (operation.operationType === AttributeOperation.DELETE) {
|
|
188
|
+
if (!operation.resourceId) {
|
|
189
|
+
throw new ArgumentError('resourceId is required for delete operation');
|
|
190
|
+
}
|
|
191
|
+
await this.deleteResourceAttributes(
|
|
192
|
+
accountId,
|
|
193
|
+
{
|
|
194
|
+
type: operation.resourceType,
|
|
195
|
+
id: operation.resourceId,
|
|
196
|
+
},
|
|
197
|
+
[operation.key]
|
|
198
|
+
);
|
|
199
|
+
processedOperations.push(operation);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return processedOperations;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Updates entity attributes (batch operations).
|
|
208
|
+
* Note: MS service does not support batch operations directly.
|
|
209
|
+
* This method processes operations sequentially using upsert/delete methods.
|
|
210
|
+
* @param accountId The account ID
|
|
211
|
+
* @param appName App name (required for interface compatibility, but not used in MS service)
|
|
212
|
+
* @param callerActionIdentifier Action identifier (required for interface compatibility, but not used in MS service)
|
|
213
|
+
* @param entityAttributeOperations Array of operations to perform
|
|
214
|
+
* @returns Promise<EntityAttributesOperation[]> Array of processed operations
|
|
215
|
+
*/
|
|
216
|
+
async updateEntityAttributes(
|
|
217
|
+
accountId: number,
|
|
218
|
+
_appName: string,
|
|
219
|
+
_callerActionIdentifier: string,
|
|
220
|
+
entityAttributeOperations: EntityAttributeOperation[]
|
|
221
|
+
): Promise<EntityAttributeOperation[]> {
|
|
222
|
+
const processedOperations: EntityAttributeOperation[] = [];
|
|
223
|
+
for (const operation of entityAttributeOperations) {
|
|
224
|
+
if (operation.operationType === 'upsert') {
|
|
225
|
+
await this.upsertEntityAttributes(accountId, [
|
|
226
|
+
new EntityAttributeAssignment(operation.entityId, operation.entityType, operation.key, operation.value || ''),
|
|
227
|
+
]);
|
|
228
|
+
processedOperations.push(operation);
|
|
229
|
+
} else if (operation.operationType === 'delete') {
|
|
230
|
+
await this.deleteEntityAttributes(accountId, operation.entityType, operation.entityId, [operation.key]);
|
|
231
|
+
processedOperations.push(operation);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return processedOperations;
|
|
236
|
+
}
|
|
33
237
|
|
|
34
238
|
/**
|
|
35
|
-
*
|
|
239
|
+
* Replaces path template parameters with actual values
|
|
240
|
+
* @param template Path template with placeholders like {accountId}
|
|
241
|
+
* @param params Object with parameter names and values
|
|
242
|
+
* @returns Path with all placeholders replaced
|
|
36
243
|
*/
|
|
37
|
-
private static
|
|
38
|
-
|
|
244
|
+
private static replacePathParams(template: string, params: Record<string, string | number>): string {
|
|
245
|
+
let path = template;
|
|
246
|
+
for (const [key, value] of Object.entries(params)) {
|
|
247
|
+
path = path.replace(`{${key}}`, String(value));
|
|
248
|
+
}
|
|
249
|
+
return path;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Generic helper for executing delete requests
|
|
254
|
+
*/
|
|
255
|
+
private static async executeDeleteRequest(
|
|
256
|
+
accountId: number,
|
|
257
|
+
pathTemplate: string,
|
|
258
|
+
pathParams: Record<string, string | number>,
|
|
259
|
+
keys: string[],
|
|
260
|
+
logPrefix: string,
|
|
261
|
+
methodName: string,
|
|
262
|
+
context: Record<string, any> = {}
|
|
263
|
+
): Promise<void> {
|
|
264
|
+
// Validate inputs
|
|
265
|
+
ValidationUtils.validateInteger(accountId, 'accountId');
|
|
266
|
+
ValidationUtils.validateArray(keys, 'attributeKeys');
|
|
267
|
+
|
|
268
|
+
if (!keys.length) {
|
|
269
|
+
logger.warn({ tag: this.LOG_TAG, accountId, ...pathParams }, `${methodName} called with empty keys array`);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Validate all keys are strings
|
|
274
|
+
ValidationUtils.validateStringArray(keys, 'attributeKeys');
|
|
275
|
+
|
|
276
|
+
// Build request body
|
|
277
|
+
const requestBody: DeleteRequestBody = {
|
|
278
|
+
keys,
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
if (!AuthorizationAttributesMsService.httpClient) {
|
|
282
|
+
throw new Error('AuthorizationAttributesMsService: HTTP client is not initialized');
|
|
283
|
+
}
|
|
284
|
+
const path = AuthorizationAttributesMsService.replacePathParams(pathTemplate, { accountId, ...pathParams });
|
|
285
|
+
const headers = AuthorizationAttributesMsService.getRequestHeaders(accountId);
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
logger.info(
|
|
289
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, ...pathParams, keys },
|
|
290
|
+
`Deleting ${logPrefix} attributes`
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
await AuthorizationAttributesMsService.httpClient.fetch(
|
|
294
|
+
{
|
|
295
|
+
url: {
|
|
296
|
+
appName: APP_NAME,
|
|
297
|
+
path,
|
|
298
|
+
},
|
|
299
|
+
method: 'DELETE',
|
|
300
|
+
headers,
|
|
301
|
+
body: JSON.stringify(requestBody),
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
305
|
+
retryPolicy: AuthorizationInternalService.getRetriesPolicy(),
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
logger.info(
|
|
310
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, ...pathParams, keys },
|
|
311
|
+
`Successfully deleted ${logPrefix} attributes`
|
|
312
|
+
);
|
|
313
|
+
} catch (err) {
|
|
314
|
+
logger.error(
|
|
315
|
+
{
|
|
316
|
+
tag: AuthorizationAttributesMsService.LOG_TAG,
|
|
317
|
+
method: methodName,
|
|
318
|
+
accountId,
|
|
319
|
+
...pathParams,
|
|
320
|
+
...context,
|
|
321
|
+
error: err instanceof Error ? err.message : String(err),
|
|
322
|
+
},
|
|
323
|
+
`Failed in ${methodName}`
|
|
324
|
+
);
|
|
325
|
+
throw err;
|
|
326
|
+
}
|
|
39
327
|
}
|
|
40
328
|
|
|
41
329
|
/**
|
|
@@ -154,115 +442,57 @@ export class AuthorizationAttributesMsService implements IAuthorizationAttribute
|
|
|
154
442
|
pathTemplate: string,
|
|
155
443
|
requestBodyKey: 'resourceAttributeAssignments' | 'entityAttributeAssignments',
|
|
156
444
|
assignmentClass: abstract new (...args: any[]) => T,
|
|
157
|
-
logPrefix: string,
|
|
158
|
-
methodName: string
|
|
159
|
-
): Promise<void> {
|
|
160
|
-
// Validate inputs
|
|
161
|
-
ValidationUtils.validateInteger(accountId, 'accountId');
|
|
162
|
-
ValidationUtils.validateArray(assignments, 'assignments');
|
|
163
|
-
|
|
164
|
-
if (!assignments.length) {
|
|
165
|
-
logger.warn(
|
|
166
|
-
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId },
|
|
167
|
-
`${methodName} called with empty array`
|
|
168
|
-
);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Validate all assignments are instances of the correct class
|
|
173
|
-
AuthorizationAttributesMsService.validateMessages(assignments, assignmentClass);
|
|
174
|
-
|
|
175
|
-
const requestBody =
|
|
176
|
-
requestBodyKey === 'resourceAttributeAssignments'
|
|
177
|
-
? { resourceAttributeAssignments: assignments }
|
|
178
|
-
: { entityAttributeAssignments: assignments };
|
|
179
|
-
|
|
180
|
-
if (!AuthorizationAttributesMsService.httpClient) {
|
|
181
|
-
throw new Error('AuthorizationAttributesMsService: HTTP client is not initialized');
|
|
182
|
-
}
|
|
183
|
-
const path = AuthorizationAttributesMsService.replacePathParams(pathTemplate, { accountId });
|
|
184
|
-
const headers = AuthorizationAttributesMsService.getRequestHeaders(accountId);
|
|
185
|
-
|
|
186
|
-
try {
|
|
187
|
-
logger.info(
|
|
188
|
-
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, count: assignments.length },
|
|
189
|
-
`Upserting ${logPrefix} attributes`
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
await AuthorizationAttributesMsService.httpClient.fetch(
|
|
193
|
-
{
|
|
194
|
-
url: {
|
|
195
|
-
appName: APP_NAME,
|
|
196
|
-
path,
|
|
197
|
-
},
|
|
198
|
-
method: 'POST',
|
|
199
|
-
headers,
|
|
200
|
-
body: JSON.stringify(requestBody),
|
|
201
|
-
},
|
|
202
|
-
{
|
|
203
|
-
timeout: AuthorizationInternalService.getRequestTimeout(),
|
|
204
|
-
retryPolicy: AuthorizationInternalService.getRetriesPolicy(),
|
|
205
|
-
}
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
logger.debug(
|
|
209
|
-
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, count: assignments.length },
|
|
210
|
-
`Successfully upserted ${logPrefix} attributes`
|
|
211
|
-
);
|
|
212
|
-
} catch (err) {
|
|
213
|
-
const error = err as Error;
|
|
214
|
-
logger.error(
|
|
215
|
-
{
|
|
216
|
-
tag: AuthorizationAttributesMsService.LOG_TAG,
|
|
217
|
-
method: methodName,
|
|
218
|
-
accountId,
|
|
219
|
-
error: error instanceof Error ? error.message : String(error),
|
|
220
|
-
},
|
|
221
|
-
`Failed in ${methodName}`
|
|
222
|
-
);
|
|
223
|
-
throw error;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Generic helper for executing delete requests
|
|
229
|
-
*/
|
|
230
|
-
private static async executeDeleteRequest(
|
|
231
|
-
accountId: number,
|
|
232
|
-
pathTemplate: string,
|
|
233
|
-
pathParams: Record<string, string | number>,
|
|
234
|
-
keys: string[],
|
|
235
|
-
logPrefix: string,
|
|
236
|
-
methodName: string,
|
|
237
|
-
context: Record<string, any> = {}
|
|
445
|
+
logPrefix: string,
|
|
446
|
+
methodName: string
|
|
238
447
|
): Promise<void> {
|
|
239
448
|
// Validate inputs
|
|
240
449
|
ValidationUtils.validateInteger(accountId, 'accountId');
|
|
241
|
-
ValidationUtils.validateArray(
|
|
450
|
+
ValidationUtils.validateArray(assignments, 'assignments');
|
|
242
451
|
|
|
243
|
-
if (!
|
|
244
|
-
logger.warn(
|
|
452
|
+
if (!assignments.length) {
|
|
453
|
+
logger.warn(
|
|
454
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId },
|
|
455
|
+
`${methodName} called with empty array`
|
|
456
|
+
);
|
|
245
457
|
return;
|
|
246
458
|
}
|
|
247
459
|
|
|
248
|
-
// Validate all
|
|
249
|
-
|
|
460
|
+
// Validate all assignments are instances of the correct class
|
|
461
|
+
AuthorizationAttributesMsService.validateMessages(assignments, assignmentClass);
|
|
462
|
+
const compatibileAssignments = assignments.map(assignment => {
|
|
463
|
+
if (assignment instanceof ResourceAttributeAssignment) {
|
|
464
|
+
return {
|
|
465
|
+
resourceId: assignment.resourceId,
|
|
466
|
+
resourceType: assignment.resourceType,
|
|
467
|
+
key: assignment.attributeKey,
|
|
468
|
+
value: assignment.attributeValue,
|
|
469
|
+
};
|
|
470
|
+
} else {
|
|
471
|
+
return {
|
|
472
|
+
entityId: assignment.entityId,
|
|
473
|
+
entityType: assignment.entityType,
|
|
474
|
+
key: assignment.attributeKey,
|
|
475
|
+
value: assignment.attributeValue,
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
return assignment;
|
|
479
|
+
});
|
|
250
480
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
481
|
+
const requestBody =
|
|
482
|
+
requestBodyKey === 'resourceAttributeAssignments'
|
|
483
|
+
? { resourceAttributeAssignments: compatibileAssignments }
|
|
484
|
+
: { entityAttributeAssignments: compatibileAssignments };
|
|
255
485
|
|
|
256
486
|
if (!AuthorizationAttributesMsService.httpClient) {
|
|
257
487
|
throw new Error('AuthorizationAttributesMsService: HTTP client is not initialized');
|
|
258
488
|
}
|
|
259
|
-
const path = AuthorizationAttributesMsService.replacePathParams(pathTemplate, { accountId
|
|
489
|
+
const path = AuthorizationAttributesMsService.replacePathParams(pathTemplate, { accountId });
|
|
260
490
|
const headers = AuthorizationAttributesMsService.getRequestHeaders(accountId);
|
|
261
491
|
|
|
262
492
|
try {
|
|
263
493
|
logger.info(
|
|
264
|
-
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId,
|
|
265
|
-
`
|
|
494
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, count: assignments.length },
|
|
495
|
+
`Upserting ${logPrefix} attributes`
|
|
266
496
|
);
|
|
267
497
|
|
|
268
498
|
await AuthorizationAttributesMsService.httpClient.fetch(
|
|
@@ -271,7 +501,7 @@ export class AuthorizationAttributesMsService implements IAuthorizationAttribute
|
|
|
271
501
|
appName: APP_NAME,
|
|
272
502
|
path,
|
|
273
503
|
},
|
|
274
|
-
method: '
|
|
504
|
+
method: 'POST',
|
|
275
505
|
headers,
|
|
276
506
|
body: JSON.stringify(requestBody),
|
|
277
507
|
},
|
|
@@ -281,248 +511,21 @@ export class AuthorizationAttributesMsService implements IAuthorizationAttribute
|
|
|
281
511
|
}
|
|
282
512
|
);
|
|
283
513
|
|
|
284
|
-
logger.
|
|
285
|
-
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId,
|
|
286
|
-
`Successfully
|
|
514
|
+
logger.debug(
|
|
515
|
+
{ tag: AuthorizationAttributesMsService.LOG_TAG, accountId, count: assignments.length },
|
|
516
|
+
`Successfully upserted ${logPrefix} attributes`
|
|
287
517
|
);
|
|
288
518
|
} catch (err) {
|
|
289
|
-
const error = err as Error;
|
|
290
519
|
logger.error(
|
|
291
520
|
{
|
|
292
521
|
tag: AuthorizationAttributesMsService.LOG_TAG,
|
|
293
522
|
method: methodName,
|
|
294
523
|
accountId,
|
|
295
|
-
|
|
296
|
-
...context,
|
|
297
|
-
error: error instanceof Error ? error.message : String(error),
|
|
524
|
+
error: err instanceof Error ? err.message : String(err),
|
|
298
525
|
},
|
|
299
526
|
`Failed in ${methodName}`
|
|
300
527
|
);
|
|
301
|
-
throw
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Creates or updates resource attributes synchronously.
|
|
307
|
-
* @param accountId The account ID
|
|
308
|
-
* @param resourceAttributeAssignments Array of ResourceAttributeAssignment objects
|
|
309
|
-
* @returns Promise<void>
|
|
310
|
-
*/
|
|
311
|
-
async upsertResourceAttributes(
|
|
312
|
-
accountId: number,
|
|
313
|
-
resourceAttributeAssignments: ResourceAttributeAssignment[],
|
|
314
|
-
_appName?: string,
|
|
315
|
-
_callerActionIdentifier?: string
|
|
316
|
-
): Promise<void> {
|
|
317
|
-
return AuthorizationAttributesMsService.executeUpsertRequest(
|
|
318
|
-
accountId,
|
|
319
|
-
resourceAttributeAssignments,
|
|
320
|
-
UPSERT_RESOURCE_ATTRIBUTES_PATH,
|
|
321
|
-
'resourceAttributeAssignments',
|
|
322
|
-
ResourceAttributeAssignment,
|
|
323
|
-
'resource',
|
|
324
|
-
'upsertResourceAttributesSync'
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Deletes specific attributes from a resource synchronously.
|
|
330
|
-
* @param accountId The account ID
|
|
331
|
-
* @param resource Object with resourceType (string) and resourceId (number)
|
|
332
|
-
* @param attributeKeys Array of attribute key strings to delete
|
|
333
|
-
* @returns Promise<void>
|
|
334
|
-
*/
|
|
335
|
-
async deleteResourceAttributes(
|
|
336
|
-
accountId: number,
|
|
337
|
-
resource: Resource,
|
|
338
|
-
attributeKeys: string[],
|
|
339
|
-
_appName?: string,
|
|
340
|
-
_callerActionIdentifier?: string
|
|
341
|
-
): Promise<void> {
|
|
342
|
-
// Validate resource object
|
|
343
|
-
if (!resource || typeof resource !== 'object') {
|
|
344
|
-
throw new ArgumentError('resource must be an object');
|
|
345
|
-
}
|
|
346
|
-
if (!resource.id) {
|
|
347
|
-
throw new ArgumentError('resource.id is required');
|
|
348
|
-
}
|
|
349
|
-
ValidationUtils.validateInteger(resource.id, 'resource.id');
|
|
350
|
-
ValidationUtils.validateString(resource.type, 'resource.type');
|
|
351
|
-
|
|
352
|
-
return AuthorizationAttributesMsService.executeDeleteRequest(
|
|
353
|
-
accountId,
|
|
354
|
-
DELETE_RESOURCE_ATTRIBUTES_PATH,
|
|
355
|
-
{
|
|
356
|
-
resourceType: resource.type,
|
|
357
|
-
resourceId: resource.id,
|
|
358
|
-
},
|
|
359
|
-
attributeKeys,
|
|
360
|
-
'resource',
|
|
361
|
-
'deleteResourceAttributesSync',
|
|
362
|
-
{ resource }
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Creates or updates entity attributes synchronously.
|
|
368
|
-
* @param accountId The account ID
|
|
369
|
-
* @param entityAttributeAssignments Array of EntityAttributeAssignment objects
|
|
370
|
-
* @returns Promise<void>
|
|
371
|
-
*/
|
|
372
|
-
async upsertEntityAttributes(
|
|
373
|
-
accountId: number,
|
|
374
|
-
entityAttributeAssignments: EntityAttributeAssignment[],
|
|
375
|
-
_appName?: string,
|
|
376
|
-
_callerActionIdentifier?: string
|
|
377
|
-
): Promise<void> {
|
|
378
|
-
return AuthorizationAttributesMsService.executeUpsertRequest(
|
|
379
|
-
accountId,
|
|
380
|
-
entityAttributeAssignments,
|
|
381
|
-
UPSERT_ENTITY_ATTRIBUTES_PATH,
|
|
382
|
-
'entityAttributeAssignments',
|
|
383
|
-
EntityAttributeAssignment,
|
|
384
|
-
'entity',
|
|
385
|
-
'upsertEntityAttributesSync'
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Deletes specific attributes from an entity synchronously.
|
|
391
|
-
* @param accountId The account ID
|
|
392
|
-
* @param entityType The entity type
|
|
393
|
-
* @param entityId The entity ID
|
|
394
|
-
* @param attributeKeys Array of attribute key strings to delete
|
|
395
|
-
* @returns Promise<void>
|
|
396
|
-
*/
|
|
397
|
-
async deleteEntityAttributes(
|
|
398
|
-
accountId: number,
|
|
399
|
-
entityType: EntityType | string,
|
|
400
|
-
entityId: number,
|
|
401
|
-
attributeKeys: string[],
|
|
402
|
-
_appName?: string,
|
|
403
|
-
_callerActionIdentifier?: string
|
|
404
|
-
): Promise<void> {
|
|
405
|
-
if (!entityType || typeof entityType !== 'string' || entityType.trim() === '') {
|
|
406
|
-
throw new ArgumentError(`entityType must be a non-empty string, got: ${entityType}`);
|
|
407
|
-
}
|
|
408
|
-
ValidationUtils.validateInteger(entityId, 'entityId');
|
|
409
|
-
|
|
410
|
-
return AuthorizationAttributesMsService.executeDeleteRequest(
|
|
411
|
-
accountId,
|
|
412
|
-
DELETE_ENTITY_ATTRIBUTES_PATH,
|
|
413
|
-
{
|
|
414
|
-
entityType,
|
|
415
|
-
entityId,
|
|
416
|
-
},
|
|
417
|
-
attributeKeys,
|
|
418
|
-
'entity',
|
|
419
|
-
'deleteEntityAttributesSync',
|
|
420
|
-
{ entityType, entityId }
|
|
421
|
-
);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* Updates resource attributes (batch operations).
|
|
426
|
-
* Note: MS service does not support batch operations directly.
|
|
427
|
-
* This method processes operations sequentially using upsert/delete methods.
|
|
428
|
-
* @param accountId The account ID
|
|
429
|
-
* @param appName App name (required for interface compatibility, but not used in MS service)
|
|
430
|
-
* @param callerActionIdentifier Action identifier (required for interface compatibility, but not used in MS service)
|
|
431
|
-
* @param resourceAttributeOperations Array of operations to perform
|
|
432
|
-
* @returns Promise<ResourceAttributesOperation[]> Array of processed operations
|
|
433
|
-
*/
|
|
434
|
-
async updateResourceAttributes(
|
|
435
|
-
accountId: number,
|
|
436
|
-
_appName: string,
|
|
437
|
-
_callerActionIdentifier: string,
|
|
438
|
-
resourceAttributeOperations: ResourceAttributeOperation[]
|
|
439
|
-
): Promise<ResourceAttributeOperation[]> {
|
|
440
|
-
const processedOperations: ResourceAttributeOperation[] = [];
|
|
441
|
-
|
|
442
|
-
for (const operation of resourceAttributeOperations) {
|
|
443
|
-
if (operation.operationType === AttributeOperation.UPSERT) {
|
|
444
|
-
if (!operation.resourceId) {
|
|
445
|
-
throw new ArgumentError('resourceId is required for upsert operation');
|
|
446
|
-
}
|
|
447
|
-
await this.upsertResourceAttributes(accountId, [
|
|
448
|
-
new ResourceAttributeAssignment(
|
|
449
|
-
operation.resourceId,
|
|
450
|
-
operation.resourceType,
|
|
451
|
-
operation.key,
|
|
452
|
-
operation.value || ''
|
|
453
|
-
),
|
|
454
|
-
]);
|
|
455
|
-
processedOperations.push(operation);
|
|
456
|
-
} else if (operation.operationType === AttributeOperation.DELETE) {
|
|
457
|
-
if (!operation.resourceId) {
|
|
458
|
-
throw new ArgumentError('resourceId is required for delete operation');
|
|
459
|
-
}
|
|
460
|
-
await this.deleteResourceAttributes(
|
|
461
|
-
accountId,
|
|
462
|
-
{
|
|
463
|
-
type: operation.resourceType,
|
|
464
|
-
id: operation.resourceId,
|
|
465
|
-
},
|
|
466
|
-
[operation.key]
|
|
467
|
-
);
|
|
468
|
-
processedOperations.push(operation);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
return processedOperations;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* Updates entity attributes (batch operations).
|
|
477
|
-
* Note: MS service does not support batch operations directly.
|
|
478
|
-
* This method processes operations sequentially using upsert/delete methods.
|
|
479
|
-
* @param accountId The account ID
|
|
480
|
-
* @param appName App name (required for interface compatibility, but not used in MS service)
|
|
481
|
-
* @param callerActionIdentifier Action identifier (required for interface compatibility, but not used in MS service)
|
|
482
|
-
* @param entityAttributeOperations Array of operations to perform
|
|
483
|
-
* @returns Promise<EntityAttributesOperation[]> Array of processed operations
|
|
484
|
-
*/
|
|
485
|
-
async updateEntityAttributes(
|
|
486
|
-
accountId: number,
|
|
487
|
-
_appName: string,
|
|
488
|
-
_callerActionIdentifier: string,
|
|
489
|
-
entityAttributeOperations: EntityAttributeOperation[]
|
|
490
|
-
): Promise<EntityAttributeOperation[]> {
|
|
491
|
-
const processedOperations: EntityAttributeOperation[] = [];
|
|
492
|
-
for (const operation of entityAttributeOperations) {
|
|
493
|
-
if (operation.operationType === 'upsert') {
|
|
494
|
-
const upsertOp = operation as any;
|
|
495
|
-
await this.upsertEntityAttributes(accountId, [
|
|
496
|
-
new EntityAttributeAssignment(
|
|
497
|
-
operation.entityId,
|
|
498
|
-
operation.entityType as string,
|
|
499
|
-
operation.key,
|
|
500
|
-
upsertOp.value || ''
|
|
501
|
-
),
|
|
502
|
-
]);
|
|
503
|
-
processedOperations.push(operation);
|
|
504
|
-
} else if (operation.operationType === 'delete') {
|
|
505
|
-
await this.deleteEntityAttributes(accountId, operation.entityType as EntityType, operation.entityId, [
|
|
506
|
-
operation.key,
|
|
507
|
-
]);
|
|
508
|
-
processedOperations.push(operation);
|
|
509
|
-
}
|
|
528
|
+
throw err;
|
|
510
529
|
}
|
|
511
|
-
|
|
512
|
-
return processedOperations;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/**
|
|
516
|
-
* Replaces path template parameters with actual values
|
|
517
|
-
* @param template Path template with placeholders like {accountId}
|
|
518
|
-
* @param params Object with parameter names and values
|
|
519
|
-
* @returns Path with all placeholders replaced
|
|
520
|
-
*/
|
|
521
|
-
private static replacePathParams(template: string, params: Record<string, string | number>): string {
|
|
522
|
-
let path = template;
|
|
523
|
-
for (const [key, value] of Object.entries(params)) {
|
|
524
|
-
path = path.replace(`{${key}}`, String(value));
|
|
525
|
-
}
|
|
526
|
-
return path;
|
|
527
530
|
}
|
|
528
531
|
}
|