Skip to content

Commit

Permalink
feat: migrate to v2 data access (#654)
Browse files Browse the repository at this point in the history
Co-authored-by: Damian Zehnder <[email protected]>
  • Loading branch information
solaris007 and dzehnder authored Jan 3, 2025
1 parent ba76aa1 commit 5e369a0
Show file tree
Hide file tree
Showing 69 changed files with 1,733 additions and 1,404 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Build

permissions:
id-token: write
Expand All @@ -24,6 +24,9 @@ jobs:
with:
node-version: 22.12

- name: Update NPM
run: npm install -g [email protected]

- name: Install dependencies
run: npm ci

Expand Down Expand Up @@ -58,6 +61,9 @@ jobs:
with:
node-version: 22.12

- name: Update NPM
run: npm install -g [email protected]

- name: Install dependencies
run: npm ci

Expand Down Expand Up @@ -100,6 +106,9 @@ jobs:
with:
node-version: 22.12

- name: Update NPM
run: npm install -g [email protected]

- name: Install dependencies
run: npm ci

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "src/index.js",
"type": "module",
"engines": {
"node": ">=20.0.0 <23.0.0",
"npm": ">=10.0.0 <12.0.0"
"node": ">=22.0.0 <23.0.0",
"npm": ">=10.9.0 <12.0.0"
},
"scripts": {
"start": "nodemon",
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/api-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function ApiKeyController(context) {
const {
dataAccess, log, env, attributes, imsClient,
} = context;
const { ApiKey } = dataAccess;

let apiKeyConfiguration = {};
try {
Expand Down Expand Up @@ -159,7 +160,7 @@ function ApiKeyController(context) {

// Check whether the user has already created the maximum number of
// active API keys for the given imsOrgId.
const apiKeys = await dataAccess.getApiKeysByImsUserIdAndImsOrgId(imsUserId, imsOrgId);
const apiKeys = await ApiKey.allByImsOrgIdAndImsUserId(imsOrgId, imsUserId);

const validApiKeys = apiKeys.filter(
(apiKey) => apiKey.isValid(),
Expand Down Expand Up @@ -200,8 +201,7 @@ function ApiKeyController(context) {
const expiresAt = new Date();
expiresAt.setMonth(expiresAt.getMonth() + 6);

const apiKeyEntity = await dataAccess.createNewApiKey({
id: crypto.randomUUID(),
const apiKeyEntity = await ApiKey.create({
name: data.name,
scopes,
createdAt: new Date().toISOString(),
Expand Down Expand Up @@ -230,7 +230,7 @@ function ApiKeyController(context) {
try {
const imsUserToken = getImsUserToken(headers);
await validateImsOrgId(imsOrgId, imsUserToken);
const apiKeyEntity = await dataAccess.getApiKeyById(id);
const apiKeyEntity = await ApiKey.findById(id);
const { authInfo: { profile } } = attributes;

const imsUserId = getImsUserIdFromProfile(profile);
Expand All @@ -239,9 +239,9 @@ function ApiKeyController(context) {
throw new ErrorWithStatusCode('Invalid request: API key not found', STATUS_NOT_FOUND);
}

apiKeyEntity.updateDeletedAt(new Date().toISOString());
apiKeyEntity.setDeletedAt(new Date().toISOString());

await dataAccess.updateApiKey(apiKeyEntity);
await apiKeyEntity.save();
return createResponse({}, STATUS_NO_CONTENT);
} catch (error) {
log.error(`Failed to delete the api key with id: ${id} - ${error.message}`);
Expand All @@ -264,7 +264,7 @@ function ApiKeyController(context) {
const { authInfo: { profile } } = attributes;

const imsUserId = getImsUserIdFromProfile(profile);
const apiKeys = await dataAccess.getApiKeysByImsUserIdAndImsOrgId(imsUserId, imsOrgId);
const apiKeys = await ApiKey.allByImsOrgIdAndImsUserId(imsOrgId, imsUserId);
return ok(apiKeys.map((apiKey) => ApiKeyDto.toJSON(apiKey)));
} catch (error) {
log.error(`Failed to retrieve the api keys - ${error.message}`);
Expand Down
28 changes: 17 additions & 11 deletions src/controllers/audits.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function AuditsController(dataAccess) {
throw new Error('Data access required');
}

const {
Audit, Configuration, LatestAudit, Site,
} = dataAccess;

/**
* Gets all audits for a given site and audit type. If no audit type is specified,
* all audits are returned.
Expand All @@ -40,14 +44,16 @@ function AuditsController(dataAccess) {
const getAllForSite = async (context) => {
const siteId = context.params?.siteId;
const auditType = context.params?.auditType || undefined;
const ascending = context.data?.ascending === 'true' || false;
const order = context.data?.ascending === 'true' ? 'asc' : 'desc';

if (!hasText(siteId)) {
return badRequest('Site ID required');
}

const audits = (await dataAccess.getAuditsForSite(siteId, auditType, ascending))
.map((audit) => AuditDto.toAbbreviatedJSON(audit));
const method = auditType
? Audit.allBySiteIdAndAuditType(siteId, auditType, { order })
: Audit.allBySiteId(siteId, { order });
const audits = ((await method).map((audit) => AuditDto.toAbbreviatedJSON(audit)));

return ok(audits);
};
Expand All @@ -60,13 +66,13 @@ function AuditsController(dataAccess) {
*/
const getAllLatest = async (context) => {
const auditType = context.params?.auditType;
const ascending = context.data?.ascending === 'true' || false;
const order = context.data?.ascending === 'true' ? 'asc' : 'desc';

if (!hasText(auditType)) {
return badRequest('Audit type required');
}

const audits = (await dataAccess.getLatestAudits(auditType, ascending))
const audits = (await LatestAudit.allByAuditType(auditType, { order }))
.map((audit) => AuditDto.toAbbreviatedJSON(audit));

return ok(audits);
Expand All @@ -83,7 +89,7 @@ function AuditsController(dataAccess) {
return badRequest('Site ID required');
}

const audits = (await dataAccess.getLatestAuditsForSite(siteId))
const audits = (await LatestAudit.allBySiteId(siteId))
.map((audit) => AuditDto.toJSON(audit));

return ok(audits);
Expand All @@ -105,7 +111,7 @@ function AuditsController(dataAccess) {
return badRequest('Audit type required');
}

const audit = await dataAccess.getLatestAuditForSite(siteId, auditType);
const audit = await LatestAudit.allBySiteIdAndAuditType(siteId, auditType);
if (!audit) {
return notFound('Audit not found');
}
Expand Down Expand Up @@ -150,12 +156,12 @@ function AuditsController(dataAccess) {
const { excludedURLs, manualOverwrites, groupedURLs } = context.data;
let hasUpdates = false;

const site = await dataAccess.getSiteByID(siteId);
const site = await Site.findById(siteId);
if (!site) {
return notFound('Site not found');
}

const configuration = await dataAccess.getConfiguration();
const configuration = await Configuration.findLatest();
const registeredAudits = configuration.getHandlers();
if (!registeredAudits[auditType]) {
return notFound(`The "${auditType}" is not present in the configuration. List of allowed audits:`
Expand Down Expand Up @@ -231,8 +237,8 @@ function AuditsController(dataAccess) {

if (hasUpdates) {
const configObj = Config.toDynamoItem(siteConfig);
site.updateConfig(configObj);
await dataAccess.updateSite(site);
site.setConfig(configObj);
await site.save();
const auditConfig = siteConfig.getHandlerConfig(auditType);
return ok(auditConfig);
}
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ function ConfigurationController(dataAccess) {
throw new Error('Data access required');
}

const { Configuration } = dataAccess;

/**
* Retrieves all configurations (all versions).
* @return {Promise<Response>} Array of configurations.
*/
const getAll = async () => {
const configurations = (await dataAccess.getConfigurations())
const configurations = (await Configuration.all())
.map((configuration) => ConfigurationDto.toJSON(configuration));
return ok(configurations);
};
Expand All @@ -49,7 +51,7 @@ function ConfigurationController(dataAccess) {
return badRequest('Configuration version required to be an integer');
}

const configuration = await dataAccess.getConfigurationByVersion(configurationVersion);
const configuration = await Configuration.findByVersion(configurationVersion);
if (!configuration) {
return notFound('Configuration not found');
}
Expand All @@ -62,7 +64,7 @@ function ConfigurationController(dataAccess) {
* @return {Promise<Response>} Configuration response.
*/
const getLatest = async () => {
const configuration = await dataAccess.getConfiguration();
const configuration = await Configuration.findLatest();
if (!configuration) {
return notFound('Configuration not found');
}
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function ExperimentsController(dataAccess) {
throw new Error('Data access required');
}

const { Experiment } = dataAccess;

/**
* Gets all experiments for a given site
*
Expand All @@ -42,7 +44,7 @@ function ExperimentsController(dataAccess) {
return badRequest('Site ID required');
}

const experiments = (await dataAccess.getExperiments(siteId))
const experiments = (await Experiment.allBySiteId(siteId))
.map((experiment) => ExperimentDto.toJSON(experiment));

return ok(experiments);
Expand Down
13 changes: 7 additions & 6 deletions src/controllers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ const getConfigFromContext = (lambdaContext) => {
*/
function HooksController(lambdaContext) {
const { dataAccess } = lambdaContext;
const { Site, SiteCandidate } = dataAccess;
const config = getConfigFromContext(lambdaContext);

async function processSiteCandidate(domain, source, log, hlxConfig = {}) {
Expand All @@ -345,7 +346,7 @@ function HooksController(lambdaContext) {
hlxConfig,
};

const site = await dataAccess.getSiteByBaseURL(siteCandidate.baseURL);
const site = await Site.findByBaseURL(siteCandidate.baseURL);

// discard the site candidate if the site exists in sites db with deliveryType=aem_edge
if (site && site.getDeliveryType() === DELIVERY_TYPES.AEM_EDGE) {
Expand All @@ -358,8 +359,8 @@ function HooksController(lambdaContext) {
const hlxConfigChanged = !deepEqual(siteHlxConfig, candidateHlxConfig);

if (hlxConfigChanged) {
site.updateHlxConfig(siteCandidate.hlxConfig);
await dataAccess.updateSite(site);
site.setHlxConfig(siteCandidate.hlxConfig);
await site.save();

const action = siteHasHlxConfig && hlxConfigChanged ? 'updated' : 'added';
log.info(`HLX config ${action} for existing site: *<${baseURL}|${baseURL}>*${getHlxConfigMessagePart(hlxConfig)}`);
Expand All @@ -369,12 +370,12 @@ function HooksController(lambdaContext) {
}

// discard the site candidate if previously evaluated
const isPreviouslyEvaluated = await dataAccess.siteCandidateExists(siteCandidate.baseURL);
if (isPreviouslyEvaluated) {
const isPreviouslyEvaluated = await SiteCandidate.findByBaseURL(siteCandidate.baseURL);
if (isPreviouslyEvaluated !== null) {
throw new InvalidSiteCandidate('Site candidate previously evaluated', baseURL);
}

await dataAccess.upsertSiteCandidate(siteCandidate);
await SiteCandidate.create(siteCandidate);

return baseURL;
}
Expand Down
Loading

0 comments on commit 5e369a0

Please sign in to comment.