Skip to content

Commit

Permalink
fix: add missing org data layer methods (#107)
Browse files Browse the repository at this point in the history
In preparation for adding the API controller to api-service, i am adding
a couple of data layer things that are missing for the organizations
model.
  • Loading branch information
solaris007 authored Jan 24, 2024
1 parent 9525095 commit 0504d8e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ export interface DataAccess {
removeSite: (
siteId: string,
) => Promise<void>;
removeSitesForOrganization: (
organizationId: string,
) => Promise<void>;
getOrganizations:
() => Promise<Organization[]>;
getOrganizationByID: (
Expand All @@ -347,7 +350,7 @@ export interface DataAccess {
updateOrganization: (
organization: Organization,
) => Promise<Organization>;
removeRemoveOrganization: (
removeOrganization: (
organizationId: string,
) => Promise<void>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { isObject } from '@adobe/spacecat-shared-utils';

import { createOrganization } from '../../models/organization.js';
import { OrganizationDto } from '../../dto/organization.js';
import { removeSitesForOrganization } from '../sites/accessPatterns.js';

/**
* Retrieves all organizations.
Expand Down Expand Up @@ -123,6 +124,8 @@ export const removeOrganization = async (
organizationId,
) => {
try {
await removeSitesForOrganization(dynamoClient, config, log, organizationId);

await dynamoClient.removeItem(config.tableNameOrganizations, { id: organizationId });
} catch (error) {
log.error(`Error removing organization: ${error.message}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { isObject } from '@adobe/spacecat-shared-utils';
import {
getAuditsForSite,
getLatestAuditForSite,
getLatestAudits, removeAuditsForSite,
getLatestAudits,
removeAuditsForSite,
} from '../audits/accessPatterns.js';

import { createSite } from '../../models/site.js';
Expand Down Expand Up @@ -369,3 +370,49 @@ export const removeSite = async (
throw error;
}
};

/**
* Removes all given sites.
* @param {DynamoDbClient} dynamoClient - The DynamoDB client.
* @param {DataAccessConfig} config - The data access config.
* @param {Array<Site>} sites - The sites to remove.
* @return {Promise<void>} A promise that resolves when all sites have been removed.
*/
async function removeSites(
dynamoClient,
config,
sites,
) {
const tableName = config.tableNameSites;
// TODO: use batch-remove (needs dynamo client update)
const removeSitePromises = sites.map((site) => dynamoClient.removeItem(
tableName,
{ id: site.getId() },
));

await Promise.all(removeSitePromises);
}

/**
* Removes all sites for an organization.
* @param {DynamoDbClient} dynamoClient - The DynamoDB client.
* @param {DataAccessConfig} config - The data access config.
* @param {Logger} log - The logger.
* @param {string} organizationId - The ID of the organization to remove the sites for.
* @return {Promise<void>} A promise that resolves when all sites for the organization have been
* removed.
*/
export const removeSitesForOrganization = async (
dynamoClient,
config,
log,
organizationId,
) => {
try {
const sites = await getSitesByOrganizationID(dynamoClient, config, organizationId);
await removeSites(dynamoClient, config, sites);
} catch (error) {
log.error(`Error removing sites for organization ${organizationId}: ${error.message}`);
throw error;
}
};
14 changes: 12 additions & 2 deletions packages/spacecat-shared-data-access/src/service/sites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import {
getSiteByBaseURLWithAudits,
getSiteByBaseURLWithLatestAudit,
getSiteByID,
getSites, getSitesByDeliveryType, getSitesByOrganizationID,
getSites,
getSitesByDeliveryType,
getSitesByOrganizationID,
getSitesToAudit,
getSitesWithLatestAudit, removeSite,
getSitesWithLatestAudit,
removeSite,
removeSitesForOrganization,
updateSite,
} from './accessPatterns.js';

Expand Down Expand Up @@ -91,4 +95,10 @@ export const siteFunctions = (dynamoClient, config, log) => ({
addSite: (siteData) => addSite(dynamoClient, config, log, siteData),
updateSite: (site) => updateSite(dynamoClient, config, log, site),
removeSite: (siteId) => removeSite(dynamoClient, config, log, siteId),
removeSitesForOrganization: (organizationId) => removeSitesForOrganization(
dynamoClient,
config,
log,
organizationId,
),
});
10 changes: 9 additions & 1 deletion packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('DynamoDB Integration Test', async () => {
const sites = await dataAccess.getSitesByOrganizationID(organizations[0].getId());

expect(sites.length).to.be.lessThanOrEqual(Math.trunc(NUMBER_OF_SITES / NUMBER_OF_ORGANIZATIONS)
+ (NUMBER_OF_SITES % NUMBER_OF_ORGANIZATIONS));
+ (NUMBER_OF_SITES % NUMBER_OF_ORGANIZATIONS));

sites.forEach((site) => {
checkSite(site);
Expand Down Expand Up @@ -578,4 +578,12 @@ describe('DynamoDB Integration Test', async () => {
updatedSite = await dataAccess.getSiteByID(siteToUpdate.getId());
expect(updatedSite.getAuditConfig().getAuditTypeConfig('type1').disabled()).to.be.false;
});

it('removes organization', async () => {
const organizations = await dataAccess.getOrganizations();
const organization = organizations[0];
await expect(dataAccess.removeOrganization(organization.getId())).to.eventually.be.fulfilled;
const organizationAfterRemoval = await dataAccess.getOrganizationByID(organization.getId());
expect(organizationAfterRemoval).to.be.null;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Data Access Object Tests', () => {
'addSite',
'updateSite',
'removeSite',
'removeSitesForOrganization',
'getSites',
'getSitesByDeliveryType',
'getSitesByOrganizationID',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,5 +420,33 @@ describe('Site Access Pattern Tests', () => {
await expect(exportedFunctions.removeSite('some-id')).to.be.rejectedWith(errorMessage);
expect(mockLog.error.calledOnce).to.be.true;
});

it('removes sites for a given organization', async () => {
const mockSiteData = [{
id: 'site1',
baseURL: 'https://example.com',
}];

mockDynamoClient.query.onFirstCall().resolves(mockSiteData);

await exportedFunctions.removeSitesForOrganization('org1');

expect(mockDynamoClient.removeItem.calledOnce).to.be.true;
});

it('logs an error and reject if the sites remove for organization fails', async () => {
const errorMessage = 'Failed to delete site';
const mockSiteData = [{
id: 'site1',
baseURL: 'https://example.com',
}];

mockDynamoClient.query.onFirstCall().resolves(mockSiteData);

mockDynamoClient.removeItem.rejects(new Error(errorMessage));

await expect(exportedFunctions.removeSitesForOrganization('org1')).to.be.rejectedWith(errorMessage);
expect(mockLog.error.calledOnce).to.be.true;
});
});
});

0 comments on commit 0504d8e

Please sign in to comment.