Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
559 changes: 13 additions & 546 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 21 additions & 5 deletions packages/spacecat-shared-data-access/docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"DataModel": [
{
"TableName": "sites",
"TableName": "spacecat-services-sites",
"KeyAttributes": {
"PartitionKey": {
"AttributeName": "id",
Expand All @@ -22,10 +22,18 @@
"AttributeName": "baseURL",
"AttributeType": "S"
},
{
"AttributeName": "gitHubURL",
"AttributeType": "S"
},
{
"AttributeName": "imsOrgId",
"AttributeType": "S"
},
{
"AttributeName": "isLive",
"AttributeType": "B"
},
{
"AttributeName": "GSI1PK",
"AttributeType": "S"
Expand All @@ -41,7 +49,7 @@
],
"GlobalSecondaryIndexes": [
{
"IndexName": "all_sites",
"IndexName": "spacecat-services-all-sites",
"KeyAttributes": {
"PartitionKey": {
"AttributeName": "GSI1PK",
Expand Down Expand Up @@ -103,7 +111,7 @@
}
},
{
"TableName": "audits",
"TableName": "spacecat-services-audits",
"KeyAttributes": {
"PartitionKey": {
"AttributeName": "siteId",
Expand Down Expand Up @@ -134,6 +142,10 @@
{
"AttributeName": "fullAuditRef",
"AttributeType": "S"
},
{
"AttributeName": "isLive",
"AttributeType": "B"
}
],
"DataAccess": {
Expand Down Expand Up @@ -182,7 +194,7 @@
}
},
{
"TableName": "latest_audits",
"TableName": "spacecat-services-latest-audits",
"KeyAttributes": {
"PartitionKey": {
"AttributeName": "siteId",
Expand Down Expand Up @@ -214,6 +226,10 @@
"AttributeName": "fullAuditRef",
"AttributeType": "S"
},
{
"AttributeName": "isLive",
"AttributeType": "B"
},
{
"AttributeName": "GSI1PK",
"AttributeType": "S"
Expand All @@ -225,7 +241,7 @@
],
"GlobalSecondaryIndexes": [
{
"IndexName": "all_latest_audit_scores",
"IndexName": "spacecat-services-all-latest-audit-scores",
"KeyAttributes": {
"PartitionKey": {
"AttributeName": "GSI1PK",
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"access": "public"
},
"dependencies": {
"@adobe/spacecat-shared-dynamo": "1.1.4",
"@adobe/spacecat-shared-dynamo": "1.2.3",
"@adobe/spacecat-shared-utils": "1.2.0",
"@aws-sdk/client-dynamodb": "3.465.0",
"@aws-sdk/lib-dynamodb": "3.465.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const AuditDto = {
auditType: audit.getAuditType(),
expiresAt: convertDateToEpochSeconds(audit.getExpiresAt()),
fullAuditRef: audit.getFullAuditRef(),
isLive: audit.isLive(),
SK: `${audit.getAuditType()}#${audit.getAuditedAt()}`,
...latestAuditProps,
};
Expand All @@ -62,6 +63,7 @@ export const AuditDto = {
auditType: dynamoItem.auditType,
expiresAt: parseEpochToDate(dynamoItem.expiresAt),
fullAuditRef: dynamoItem.fullAuditRef,
isLive: dynamoItem.isLive,
};

return createAudit(auditData);
Expand Down
4 changes: 4 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const SiteDto = {
toDynamoItem: (site) => ({
id: site.getId(),
baseURL: site.getBaseURL(),
gitHubURL: site.getGitHubURL(),
imsOrgId: site.getImsOrgId(),
isLive: site.isLive(),
createdAt: site.getCreatedAt(),
updatedAt: site.getUpdatedAt(),
GSI1PK: 'ALL_SITES',
Expand All @@ -39,7 +41,9 @@ export const SiteDto = {
const siteData = {
id: dynamoItem.id,
baseURL: dynamoItem.baseURL,
gitHubURL: dynamoItem.gitHubURL,
imsOrgId: dynamoItem.imsOrgId,
isLive: dynamoItem.isLive,
createdAt: dynamoItem.createdAt,
updatedAt: dynamoItem.updatedAt,
};
Expand Down
23 changes: 22 additions & 1 deletion packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ export interface Audit {
getAuditType: () => object;
getExpiresAt: () => Date;
getFullAuditRef: () => string;
isLive: () => boolean;
getScores: () => object;
}

export interface Site {
getId: () => string;
getBaseURL: () => string;
getGitHubURL: () => string;
getImsOrgId: () => string;
getCreatedAt: () => string;
getUpdatedAt: () => string;
getAudits: () => Audit[];
updateImsOrgId: (imsOrgId: string) => Site;
isLive: () => boolean;
setAudits: (audits: Audit[]) => Site;
toggleLive: () => Site;
updateImsOrgId: (imsOrgId: string) => Site;
}

export interface DataAccess {
Expand All @@ -49,6 +53,12 @@ export interface DataAccess {
getLatestAuditsForSite: (
siteId: string,
) => Promise<Audit[]>;
addAudit: (
auditData: object,
) => Promise<Audit>;
removeAuditsForSite: (
siteId: string,
) => Promise<void>;
getSites: () => Promise<Site[]>;
getSitesToAudit: () => Promise<string[]>;
getSitesWithLatestAudit: (
Expand Down Expand Up @@ -82,6 +92,17 @@ export interface DataAccess {
) => Promise<void>;
}

interface DataAccessConfig {
tableNameAudits: string;
tableNameLatestAudits: string;
tableNameSites: string;
indexNameAllSites: string;
indexNameAllLatestAuditScores: string;
pkAllSites: string;
pkAllLatestAudits: string;
}

export function createDataAccess(
config: DataAccessConfig,
logger: object,
): DataAccess;
29 changes: 28 additions & 1 deletion packages/spacecat-shared-data-access/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,38 @@

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

const TABLE_NAME_AUDITS = 'spacecat-services-audits';
const TABLE_NAME_LATEST_AUDITS = 'spacecat-services-latest-audits';
const TABLE_NAME_SITES = 'spacecat-services-sites';

const INDEX_NAME_ALL_SITES = 'spacecat-services-all-sites';
const INDEX_NAME_ALL_LATEST_AUDIT_SCORES = 'spacecat-services-all-latest-audit-scores';

const PK_ALL_SITES = 'ALL_SITES';
const PK_ALL_LATEST_AUDITS = 'ALL_LATEST_AUDITS';

export default function dataAccessWrapper(fn) {
return async (request, context) => {
if (!context.dataAccess) {
const { log } = context;
context.dataAccess = createDataAccess(log);

const {
DYNAMO_TABLE_NAME_AUDITS = TABLE_NAME_AUDITS,
DYNAMO_TABLE_NAME_LATEST_AUDITS = TABLE_NAME_LATEST_AUDITS,
DYNAMO_TABLE_NAME_SITES = TABLE_NAME_SITES,
DYNAMO_INDEX_NAME_ALL_SITES = INDEX_NAME_ALL_SITES,
DYNAMO_INDEX_NAME_ALL_LATEST_AUDIT_SCORES = INDEX_NAME_ALL_LATEST_AUDIT_SCORES,
} = context.env;

context.dataAccess = createDataAccess({
tableNameAudits: DYNAMO_TABLE_NAME_AUDITS,
tableNameLatestAudits: DYNAMO_TABLE_NAME_LATEST_AUDITS,
tableNameSites: DYNAMO_TABLE_NAME_SITES,
indexNameAllSites: DYNAMO_INDEX_NAME_ALL_SITES,
indexNameAllLatestAuditScores: DYNAMO_INDEX_NAME_ALL_LATEST_AUDIT_SCORES,
pkAllSites: PK_ALL_SITES,
pkAllLatestAudits: PK_ALL_LATEST_AUDITS,
}, log);
}

return fn(request, context);
Expand Down
5 changes: 5 additions & 0 deletions packages/spacecat-shared-data-access/src/models/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const Audit = (data = {}) => {
self.getAuditType = () => self.state.auditType.toLowerCase();
self.getExpiresAt = () => self.state.expiresAt;
self.getFullAuditRef = () => self.state.fullAuditRef;
self.isLive = () => self.state.isLive;
self.getScores = () => self.getAuditResult();

return Object.freeze(self);
Expand Down Expand Up @@ -99,5 +100,9 @@ export const createAudit = (data) => {
newState.expiresAt.setDate(newState.expiresAt.getDate() + EXPIRES_IN_DAYS);
}

if (!Object.prototype.hasOwnProperty.call(newState, 'isLive')) {
newState.isLive = false;
}

return Audit(newState);
};
11 changes: 11 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const Site = (data = {}) => {

self.getAudits = () => self.state.audits;
self.getBaseURL = () => self.state.baseURL;
self.getGitHubURL = () => self.state.gitHubURL;
self.getImsOrgId = () => self.state.imsOrgId;
self.isLive = () => self.state.isLive;

// TODO: updating the baseURL is not supported yet, it will require a transact write
// on dynamodb (put then delete) since baseURL is part of the primary key, something like:
Expand Down Expand Up @@ -79,6 +81,11 @@ const Site = (data = {}) => {
return self;
};

self.toggleLive = () => {
self.state.isLive = !self.state.isLive;
return self;
};

return Object.freeze(self);
};

Expand All @@ -95,6 +102,10 @@ export const createSite = (data) => {
throw new Error('Base URL must be a valid URL');
}

if (!Object.prototype.hasOwnProperty.call(newState, 'isLive')) {
newState.isLive = false;
}

if (!Array.isArray(newState.audits)) {
newState.audits = [];
}
Expand Down
Loading