Skip to content

Commit 6e110c6

Browse files
tathagat2241Tathagat Sharma
andauthored
feat: add temporary API to replace enabled/disabled lists (#1789)
## Changes - Add `PUT /configurations/latest/handlers/:handlerType/replace-enabled-disabled` endpoint - Add `replaceHandlerEnabledDisabled` controller method - Update tests to include new method and route - Update package-lock.json with new dependency ## API Details **Endpoint:** `PUT /configurations/latest/handlers/:handlerType/replace-enabled-disabled` **Request Body:**n { "enabled": { "sites": ["site-id-1", "site-id-2"], "orgs": ["org-id-1"] }, "disabled": { "sites": ["site-id-3"], "orgs": ["org-id-2"] } } Please ensure your pull request adheres to the following guidelines: - [X] make sure to link the related issues in this description. Or if there's no issue created, make sure you describe here the problem you're solving. - [X] when merging / squashing, make sure the fixed issue references are visible in the commits, for easy compilation of release notes Spacecat-shared PR: adobe/spacecat-shared#1328 If the PR is changing the API specification: - [ ] make sure you add a "Not implemented yet" note the endpoint description, if the implementation is not ready yet. Ideally, return a 501 status code with a message explaining the feature is not implemented yet. - [ ] make sure you add at least one example of the request and response. If the PR is changing the API implementation or an entity exposed through the API: - [ ] make sure you update the API specification and the examples to reflect the changes. If the PR is introducing a new audit type: - [ ] make sure you update the API specification with the type, schema of the audit result and an example ## Related Issues https://jira.corp.adobe.com/browse/SITES-40312 Thanks for contributing! --------- Co-authored-by: Tathagat Sharma <ftathagat+adobe@adobe.com>
1 parent d7b323f commit 6e110c6

8 files changed

Lines changed: 360 additions & 14 deletions

File tree

package-lock.json

Lines changed: 32 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"@adobe/spacecat-shared-brand-client": "1.4.0",
8989
"@adobe/spacecat-shared-cloudflare-client": "1.2.1",
9090
"@adobe/spacecat-shared-content-client": "1.8.24",
91-
"@adobe/spacecat-shared-data-access": "4.7.0",
91+
"@adobe/spacecat-shared-data-access": "4.8.0",
9292
"@adobe/spacecat-shared-drs-client": "1.13.0",
9393
"@adobe/spacecat-shared-gpt-client": "1.6.23",
9494
"@adobe/spacecat-shared-http-utils": "1.34.0",

src/controllers/configuration.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,90 @@ function ConfigurationController(ctx) {
317317
}
318318
};
319319

320+
/**
321+
* Replaces enabled/disabled lists for a handler.
322+
* This is a temporary API that replaces (not merges) the provided arrays.
323+
*
324+
* TEMPORARY API: This endpoint was created to clean up enabled and disabled lists
325+
* by removing unnecessary site IDs. Unlike the existing updateHandler endpoint
326+
* which merges arrays, this endpoint completely replaces the specified arrays.
327+
* This API will be removed once the cleanup task is completed.
328+
*
329+
* @param {UniversalContext} context - Context of the request.
330+
* @return {Promise<Response>} Updated configuration response.
331+
*/
332+
const replaceHandlerEnabledDisabled = async (context) => {
333+
if (!accessControlUtil.hasAdminAccess()) {
334+
return forbidden('Only admins can update handler configuration');
335+
}
336+
337+
const { handlerType } = context.params;
338+
const { data } = context;
339+
340+
if (!hasText(handlerType)) {
341+
return badRequest('Handler type is required');
342+
}
343+
344+
if (!isNonEmptyObject(data)) {
345+
return badRequest('Request body is required and cannot be empty');
346+
}
347+
348+
// Validate that at least one of enabled or disabled is provided
349+
const hasEnabled = data.enabled !== undefined;
350+
const hasDisabled = data.disabled !== undefined;
351+
352+
if (!hasEnabled && !hasDisabled) {
353+
return badRequest('At least one of enabled or disabled must be provided');
354+
}
355+
356+
// Validate that at least one array is provided within enabled/disabled
357+
// (empty arrays are allowed)
358+
if (hasEnabled) {
359+
const hasEnabledSites = data.enabled.sites !== undefined;
360+
const hasEnabledOrgs = data.enabled.orgs !== undefined;
361+
if (!hasEnabledSites && !hasEnabledOrgs) {
362+
return badRequest('At least one of enabled.sites or enabled.orgs must be provided');
363+
}
364+
// Validate that if provided, they must be arrays
365+
if (hasEnabledSites && !Array.isArray(data.enabled.sites)) {
366+
return badRequest('enabled.sites must be an array');
367+
}
368+
if (hasEnabledOrgs && !Array.isArray(data.enabled.orgs)) {
369+
return badRequest('enabled.orgs must be an array');
370+
}
371+
}
372+
373+
if (hasDisabled) {
374+
const hasDisabledSites = data.disabled.sites !== undefined;
375+
const hasDisabledOrgs = data.disabled.orgs !== undefined;
376+
if (!hasDisabledSites && !hasDisabledOrgs) {
377+
return badRequest('At least one of disabled.sites or disabled.orgs must be provided');
378+
}
379+
// Validate that if provided, they must be arrays
380+
if (hasDisabledSites && !Array.isArray(data.disabled.sites)) {
381+
return badRequest('disabled.sites must be an array');
382+
}
383+
if (hasDisabledOrgs && !Array.isArray(data.disabled.orgs)) {
384+
return badRequest('disabled.orgs must be an array');
385+
}
386+
}
387+
388+
try {
389+
const configuration = await Configuration.findLatest();
390+
if (!configuration) {
391+
return notFound('Configuration not found');
392+
}
393+
394+
configuration.replaceHandlerEnabledDisabled(handlerType, data);
395+
setUpdatedBy(configuration, context);
396+
await configuration.save();
397+
398+
return ok(ConfigurationDto.toJSON(configuration));
399+
} catch (error) {
400+
return badRequest(error.message);
401+
}
402+
};
403+
320404
/**
321405
* Updates the entire configuration or specific sections.
322406
* Allows updating handlers, jobs, and/or queues in a single request.
@@ -430,6 +514,7 @@ function ConfigurationController(ctx) {
430514
updateQueues,
431515
updateJob,
432516
updateHandler,
517+
replaceHandlerEnabledDisabled,
433518
updateConfiguration,
434519
restoreVersion,
435520
};

0 commit comments

Comments
 (0)