Skip to content

feat: add site competitors array to site entity #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
317ca32
feat: add rum api calls to shared
alinarublea Dec 13, 2023
2ce1404
feat: merge main changes
alinarublea Jan 22, 2024
5c2390e
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jan 24, 2024
641f735
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jan 24, 2024
c9fc3d1
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jan 29, 2024
f007a5f
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jan 29, 2024
f09c2f6
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jan 30, 2024
966d79d
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 6, 2024
6136548
chore: merge main
alinarublea Feb 12, 2024
d7918e3
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 13, 2024
78baaae
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 26, 2024
cac54f9
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 26, 2024
3a34590
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 27, 2024
6073613
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 27, 2024
ad0bb8b
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Feb 28, 2024
81204ae
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Mar 1, 2024
2594513
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Mar 1, 2024
f81d056
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Mar 19, 2024
5354fc8
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Apr 23, 2024
aaae74c
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea May 30, 2024
305a0a0
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jun 21, 2024
4eb43c3
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jul 9, 2024
835366c
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jul 15, 2024
f68704f
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jul 22, 2024
8371649
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jul 22, 2024
a1dac87
fix: naming
alinarublea Jul 24, 2024
39f0a8d
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Aug 28, 2024
8dc69f6
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Sep 12, 2024
d6b853f
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Oct 1, 2024
3299821
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Oct 10, 2024
6c7c883
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Oct 28, 2024
abbab5c
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Nov 7, 2024
3f27400
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Nov 11, 2024
904b1cc
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Nov 12, 2024
0e0664c
Merge branch 'main' of github.com:adobe/spacecat-shared
alinarublea Jan 22, 2025
d4ed2a6
feat: add site competitors entity
alinarublea Jan 24, 2025
4996666
Merge branch 'main' into site-competitors
alinarublea Jan 24, 2025
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
2 changes: 1 addition & 1 deletion package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface Site extends BaseModel {
getOrganization(): Promise<Organization>;
getOrganizationId(): string;
getSiteCandidates(): Promise<SiteCandidate[]>;
getSiteCompetitors(): Promise<object>;
getSiteTopPages(): Promise<SiteTopPage[]>;
getSiteTopPagesBySource(source: string): Promise<SiteTopPage[]>;
getSiteTopPagesBySourceAndGeo(source: string, geo: string): Promise<SiteTopPage[]>;
Expand All @@ -65,6 +66,7 @@ export interface Site extends BaseModel {
setIsLive(isLive: boolean): Site;
setIsLiveToggledAt(isLiveToggledAt: string): Site;
setOrganizationId(organizationId: string): Site;
setSiteCompetitors(siteCompetitors: object): Site;
toggleLive(): Site;
}

Expand All @@ -77,4 +79,6 @@ export interface SiteCollection extends BaseCollection<Organization> {
findByBaseURL(baseURL: string): Promise<Site | null>;
findByDeliveryType(deliveryType: string): Promise<Site | null>;
findByOrganizationId(organizationId: string): Promise<Site | null>;
addSiteCompetitorBySiteId(siteId: string, siteCompetitorBaseUrl: string): Promise<void>;
removeSiteCompetitorBySiteId(siteId: string, siteCompetitorBaseUrl: string): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ class SiteCollection extends BaseCollection {

return orderedSites;
}

async addSiteCompetitorBySiteId(siteId, siteCompetitorBaseURL) {
const site = await this.findById(siteId);
if (!site) {
throw new DataAccessError(`Site with id ${siteId} not found`, this);
}
const currentSiteCompetitors = site.getSiteCompetitors();
if (!currentSiteCompetitors.includes(siteCompetitorBaseURL)) {
currentSiteCompetitors.push(siteCompetitorBaseURL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not work, you'd have to call site.setSiteCompetitors(updatedCompetitors) - please validate in IT test

also, you could use a set to make things easier:

  const competitors = new Set(site.getSiteCompetitors());
  if (!competitors.has(siteCompetitorBaseURL)) {
    competitors.add(siteCompetitorBaseURL);
    site.setSiteCompetitors([...competitors]);
    await site.save();
  }

await site.save();
}
return site;
}

async removeSiteCompetitorBySiteId(siteId, siteCompetitorBaseURL) {
const site = await this.findById(siteId);
if (!site) {
throw new DataAccessError(`Site with id ${siteId} not found`, this);
}
const currentSiteCompetitors = site.getSiteCompetitors();
const indexOfSiteCompetitor = currentSiteCompetitors.indexOf(siteCompetitorBaseURL);
if (indexOfSiteCompetitor !== -1) {
currentSiteCompetitors.splice(indexOfSiteCompetitor, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not work, you'd have to call site.setSiteCompetitors(updatedCompetitors) - please validate in IT test

you could use a set to make things easier:

  const competitors = new Set(site.getSiteCompetitors());
  if (competitors.has(siteCompetitorBaseURL)) {
    competitors.delete(siteCompetitorBaseURL);
    site.setSiteCompetitors([...competitors]);
    await site.save();
  }

await site.save();
}
return site;
}
}

export default SiteCollection;
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ const schema = new SchemaBuilder(Site, SiteCollection)
default: {},
validate: (value) => isObject(value),
})
.addAttribute('siteCompetitors', {
type: 'list',
default: [],
validate: (value) => Array.isArray(value),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the attribute is not required, we should not check for array unless a value is present.

})
.addAttribute('isLive', {
type: 'boolean',
required: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function checkSite(site) {
expect(site.getGitHubURL()).to.be.a('string');
expect(site.getHlxConfig()).to.be.an('object');
expect(site.getOrganizationId()).to.be.a('string');
expect(site.getSiteCompetitors()).to.be.an('array');
expect(isIsoDate(site.getCreatedAt())).to.be.true;
expect(isIsoDate(site.getUpdatedAt())).to.be.true;

Expand Down Expand Up @@ -287,6 +288,7 @@ describe('Site IT', async () => {
},
hlxVersion: 5,
},
siteCompetitors: ['https://competitor1.com', 'https://competitor2.com'],
organizationId: sampleData.organizations[0].getId(),
isLive: true,
isLiveToggledAt: '2024-12-06T08:35:24.125Z',
Expand All @@ -304,6 +306,7 @@ describe('Site IT', async () => {
await checkSite(newSite);

expect(newSite.getBaseURL()).to.equal(newSiteData.baseURL);
expect(newSite.getSiteCompetitors()).to.deep.equal(newSiteData.siteCompetitors);
});

it('updates a site', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,67 @@ describe('SiteCollection', () => {
expect(instance.allByDeliveryType).to.have.been.calledOnce;
});
});
describe('addSiteCompetitorBySiteId', () => {
it('adds a site competitor by site ID', async () => {
const siteId = 's12345';
const siteCompetitorBaseURL = 'https://competitor.com';
const siteCompetitors = [];
const mockSite = {
siteCompetitors: {},
getId: () => siteId,
getSiteCompetitors: () => (siteCompetitors),
save: stub().resolves(),
};

instance.findById = stub().resolves(mockSite);

const result = await instance.addSiteCompetitorBySiteId(siteId, siteCompetitorBaseURL);

expect(result).to.equal(mockSite);
expect(mockSite.getSiteCompetitors()).to.have.deep.equal([siteCompetitorBaseURL]);
expect(mockSite.save).to.have.been.calledOnce;
});

it('throws an error if site is not found', async () => {
const siteId = 's12345';
const siteCompetitorBaseURL = 'https://competitor.com';

instance.findById = stub().resolves(null);

await expect(instance.addSiteCompetitorBySiteId(siteId, siteCompetitorBaseURL))
.to.be.rejectedWith(`Site with id ${siteId} not found`);
});
});

describe('removeSiteCompetitorBySiteId', () => {
it('removes a site competitor by site ID', async () => {
const siteId = 's12345';
const siteCompetitorBaseURL1 = 'https://competitor1.com';
const siteCompetitorBaseURL2 = 'https://competitor2.com';
const siteCompetitors = [siteCompetitorBaseURL1, siteCompetitorBaseURL2];
const mockSite = {
getId: () => siteId,
getSiteCompetitors: () => (siteCompetitors),
save: stub().resolves(),
};

instance.findById = stub().resolves(mockSite);

const result = await instance.removeSiteCompetitorBySiteId(siteId, siteCompetitorBaseURL1);

expect(result).to.equal(mockSite);
expect(mockSite.getSiteCompetitors()).to.deep.equal([siteCompetitorBaseURL2]);
expect(mockSite.save).to.have.been.calledOnce;
});

it('throws an error if site is not found', async () => {
const siteId = 's12345';
const siteCompetitorBaseURL = 'https://competitor.com';

instance.findById = stub().resolves(null);

await expect(instance.removeSiteCompetitorBySiteId(siteId, siteCompetitorBaseURL))
.to.be.rejectedWith(`Site with id ${siteId} not found`);
});
});
});
Loading