Skip to content

Commit

Permalink
fix: toggleIsLiveAt
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Feb 3, 2025
1 parent 89ecdde commit 94d1761
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { isIsoDate } from '@adobe/spacecat-shared-utils';
import { getDataAccess, getDynamoClients } from '../test/it/util/db.js';

const config = {
region: 'us-east-1',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN,
},
};
const srcDataAccess = await getDataAccess(config);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mockDataAccess = await getDataAccess({
endpoint: 'http://127.0.0.1:8000',
region: 'local',
credentials: {
accessKeyId: 'o71baq',
secretAccessKey: 'lz7fu',
},
});
const { docClient } = getDynamoClients(config);

const originalSites = await srcDataAccess.Site.all();

for (const site of originalSites) {
// eslint-disable-next-line no-await-in-loop
const params = {
TableName: 'spacecat-services-sites-dev',
IndexName: 'spacecat-services-all-sites-dev',
KeyConditionExpression: 'baseURL = :baseURL',
ExpressionAttributeValues: {
':baseURL': site.getBaseURL(),
},
};

try {
// Directly await the promise returned by query
// eslint-disable-next-line no-await-in-loop
const originalSite = await docClient.query(params);
console.log('Query succeeded.');

Check warning on line 53 in packages/spacecat-shared-data-access/src/migrate-isLivetoggledAt.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
console.log(originalSite);

Check warning on line 54 in packages/spacecat-shared-data-access/src/migrate-isLivetoggledAt.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
if (!originalSite) {
// eslint-disable-next-line no-continue
console.log('Site not found:', site.getBaseURL());

Check warning on line 57 in packages/spacecat-shared-data-access/src/migrate-isLivetoggledAt.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
// eslint-disable-next-line no-continue
continue;
}
const liveToggledAt = originalSite.getIsLiveToggledAt();

if (liveToggledAt && isIsoDate(liveToggledAt)) {
site.setIsLiveToggledAt(liveToggledAt);
} else {
site.setIsLiveToggledAt(undefined);
}
// eslint-disable-next-line no-await-in-loop
const entity = await site.save();
console.log('Updated site:', entity.getIsLiveToggledAt());

Check warning on line 70 in packages/spacecat-shared-data-access/src/migrate-isLivetoggledAt.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
} catch (err) {
console.error('Unable to query. Error:', JSON.stringify(err, null, 2));

Check warning on line 72 in packages/spacecat-shared-data-access/src/migrate-isLivetoggledAt.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
}
}
44 changes: 41 additions & 3 deletions packages/spacecat-shared-data-access/test/it/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,41 @@
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { DynamoDB } from '@aws-sdk/client-dynamodb';

import { createDataAccess } from '../../../src/service/index.js';
import { createDataAccess } from '../../../src/index.js';

export const TEST_DA_CONFIG_DEV = {
indexNameAllImportJobsByDateRange: 'spacecat-services-all-import-jobs-by-date-range-dev',
indexNameAllImportJobsByStatus: 'spacecat-services-all-import-jobs-by-status-dev',
indexNameAllKeyEventsBySiteId: 'spacecat-services-key-events-by-site-id-dev',
indexNameAllLatestAuditScores: 'spacecat-services-all-latest-audit-scores-dev',
indexNameAllOrganizations: 'spacecat-services-all-organizations-dev',
indexNameAllOrganizationsByImsOrgId: 'spacecat-services-all-organizations-by-ims-org-id-dev',
indexNameAllSites: 'spacecat-services-all-sites-dev',
indexNameAllSitesByDeliveryType: 'spacecat-services-all-sites-by-delivery-type-dev',
indexNameAllSitesOrganizations: 'spacecat-services-all-sites-organizations-dev',
indexNameApiKeyByHashedApiKey: 'spacecat-services-api-key-by-hashed-api-key-dev',
indexNameApiKeyByImsUserIdAndImsOrgId: 'spacecat-services-api-key-by-ims-user-id-and-ims-org-id-dev',
indexNameImportUrlsByJobIdAndStatus: 'spacecat-services-all-import-urls-by-job-id-and-status-dev',
pkAllConfigurations: 'ALL_CONFIGURATIONS-dev',
pkAllImportJobs: 'ALL_IMPORT_JOBS-dev',
pkAllLatestAudits: 'ALL_LATEST_AUDITS-dev',
pkAllOrganizations: 'ALL_ORGANIZATIONS',
pkAllSites: 'ALL_SITES',
tableNameApiKeys: 'spacecat-services-api-keys-dev',
tableNameAudits: 'spacecat-services-audits-dev',
tableNameConfigurations: 'spacecat-services-configurations-dev',
tableNameData: 'spacecat-services-data-dev',
tableNameExperiments: 'spacecat-services-experiments-dev',
tableNameImportJobs: 'spacecat-services-import-jobs-dev',
tableNameImportUrls: 'spacecat-services-import-urls-dev',
tableNameKeyEvents: 'spacecat-services-key-events-dev',
tableNameLatestAudits: 'spacecat-services-latest-audits-dev',
tableNameOrganizations: 'spacecat-services-organizations-dev',
tableNameSiteCandidates: 'spacecat-services-site-candidates-dev',
tableNameSiteTopPages: 'spacecat-services-site-top-pages-dev',
tableNameSites: 'spacecat-services-sites-dev',
tableNameSpacecatData: 'spacecat-data-dev',
};

export const TEST_DA_CONFIG = {
indexNameAllImportJobsByDateRange: 'spacecat-services-all-import-jobs-by-date-range',
Expand Down Expand Up @@ -70,9 +104,13 @@ const getDynamoClients = (config = {}) => {
return { dbClient, docClient };
};

export const getDataAccess = (config, logger = console) => {
export const getDataAccess = (config, logger = console, env = 'dev') => {
const { dbClient } = getDynamoClients(config);
return createDataAccess(TEST_DA_CONFIG, logger, dbClient);
if (env === 'prod') {
return createDataAccess(TEST_DA_CONFIG, logger, dbClient);
} else {
return createDataAccess(TEST_DA_CONFIG_DEV, logger, dbClient);
}
};

export { getDynamoClients };

0 comments on commit 94d1761

Please sign in to comment.