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
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ export const configSchema = Joi.object({
manual: Joi.array().items(Joi.object({
url: Joi.string().uri().required(),
})).optional().default([]),
moneyPages: Joi.array().items(Joi.object({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@nitinja , Let's make sure we can update individual auditTargetURLs keys separately without affecting each other, for example:

% curl -X PATCH \
-d '{
  "config": {
    "auditTargetURLs": {
      "manual": [
        {
          "url": "https://example.com/path1"
        },
        {
          "url": "https://example.com/path2"
        }
      ]
    }
  }
}' \
"https://SPACECAT_API/sites/SITE_ID"

% curl -X PATCH \
-d '{
  "config": {
    "auditTargetURLs": {
      "moneyPages": [
        {
          "url": "https://example.com/money-page1"
        },
        {
          "url": "https://example.com/money-page2"
        }
      ]
    }
  }
}' \
"https://SPACECAT_API/sites/SITE_ID"

In the database, we should have both manual and moneyPages.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

^^^ That way, the API will be more resilient, and we will have less payload input data. Otherwise, to update any individual key, you would need to include all keys to preserve them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have taken care of merge here in this PR https://github.com/adobe/spacecat-api-service/pull/2167/changes

url: Joi.string().uri().required(),
})).optional().default([]),
}).options({ stripUnknown: true }).optional(),
handlers: Joi.object().pattern(Joi.string(), Joi.object({
mentions: Joi.object().pattern(Joi.string(), Joi.array().items(Joi.string())),
Expand Down Expand Up @@ -515,7 +518,7 @@ export const Config = (data = {}) => {
self.getEdgeOptimizeConfig = () => state?.edgeOptimizeConfig;
self.getOnboardConfig = () => state?.onboardConfig;
self.getCommerceLlmoConfig = () => state?.commerceLlmoConfig;
const AUDIT_TARGET_SOURCES = ['manual'];
const AUDIT_TARGET_SOURCES = ['manual', 'moneyPages'];
const auditTargetEntrySchema = Joi.object({
url: Joi.string().uri().required(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export interface LlmoCustomerIntent {
value: string;
}

export type AuditTargetSource = 'manual';
export type AuditTargetSource = 'manual' | 'moneyPages';

export interface AuditTargetEntry {
url: string;
Expand All @@ -122,6 +122,7 @@ export interface AuditTargetEntryWithSource extends AuditTargetEntry {

export interface AuditTargetURLs {
manual?: AuditTargetEntry[];
moneyPages?: AuditTargetEntry[];
}

export interface SiteConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3041,6 +3041,20 @@ describe('Config Tests', () => {
expect(result[1].source).to.equal('manual');
});

it('returns entries from both manual and moneyPages with correct source tags', () => {
const config = Config({
auditTargetURLs: {
manual: [{ url: 'https://example.com/manual1' }],
moneyPages: [{ url: 'https://example.com/money1' }, { url: 'https://example.com/money2' }],
},
});
const result = config.getAuditTargetURLs();
expect(result).to.have.lengthOf(3);
expect(result[0]).to.deep.equal({ url: 'https://example.com/manual1', source: 'manual' });
expect(result[1]).to.deep.equal({ url: 'https://example.com/money1', source: 'moneyPages' });
expect(result[2]).to.deep.equal({ url: 'https://example.com/money2', source: 'moneyPages' });
});

describe('getAuditTargetURLsBySource', () => {
it('returns URLs for a specific source', () => {
const config = Config({
Expand All @@ -3054,9 +3068,22 @@ describe('Config Tests', () => {
expect(manual[1].url).to.equal('https://example.com/m2');
});

it('returns moneyPages URLs for moneyPages source', () => {
const config = Config({
auditTargetURLs: {
moneyPages: [{ url: 'https://example.com/mp1' }, { url: 'https://example.com/mp2' }],
},
});
const moneyPages = config.getAuditTargetURLsBySource('moneyPages');
expect(moneyPages).to.have.lengthOf(2);
expect(moneyPages[0].url).to.equal('https://example.com/mp1');
expect(moneyPages[1].url).to.equal('https://example.com/mp2');
});

it('returns empty array for source with no entries', () => {
const config = Config();
expect(config.getAuditTargetURLsBySource('manual')).to.deep.equal([]);
expect(config.getAuditTargetURLsBySource('moneyPages')).to.deep.equal([]);
});

it('rejects invalid source', () => {
Expand Down Expand Up @@ -3096,6 +3123,22 @@ describe('Config Tests', () => {
config.updateAuditTargetURLs('manual', []);
expect(config.getAuditTargetURLsBySource('manual')).to.deep.equal([]);
});

it('replaces URLs for moneyPages source', () => {
const config = Config({
auditTargetURLs: {
moneyPages: [{ url: 'https://old.com' }],
},
});
config.updateAuditTargetURLs('moneyPages', [
{ url: 'https://new1.com' },
{ url: 'https://new2.com' },
]);
const result = config.getAuditTargetURLsBySource('moneyPages');
expect(result).to.have.lengthOf(2);
expect(result[0].url).to.equal('https://new1.com');
expect(result[1].url).to.equal('https://new2.com');
});
});

describe('addAuditTargetURL', () => {
Expand Down Expand Up @@ -3129,6 +3172,25 @@ describe('Config Tests', () => {
const config = Config();
expect(() => config.addAuditTargetURL('invalid', { url: 'https://example.com' })).to.throw('Invalid audit target source');
});

it('appends a new URL to moneyPages source', () => {
const config = Config();
config.addAuditTargetURL('moneyPages', { url: 'https://example.com/mp1' });
config.addAuditTargetURL('moneyPages', { url: 'https://example.com/mp2' });
expect(config.getAuditTargetURLsBySource('moneyPages')).to.have.lengthOf(2);
expect(config.getAuditTargetURLsBySource('moneyPages')[1].url).to.equal('https://example.com/mp2');
});

it('deduplicates across manual and moneyPages sources', () => {
const config = Config({
auditTargetURLs: {
manual: [{ url: 'https://example.com/shared' }],
},
});
config.addAuditTargetURL('moneyPages', { url: 'https://example.com/shared' });
expect(config.getAuditTargetURLsBySource('moneyPages')).to.have.lengthOf(0);
expect(config.getAuditTargetURLs()).to.have.lengthOf(1);
});
});

describe('removeAuditTargetURL', () => {
Expand Down Expand Up @@ -3161,6 +3223,17 @@ describe('Config Tests', () => {
const config = Config();
expect(() => config.removeAuditTargetURL('invalid', 'https://example.com')).to.throw('Invalid audit target source');
});

it('removes by url string from moneyPages source', () => {
const config = Config({
auditTargetURLs: {
moneyPages: [{ url: 'https://example.com/mp1' }, { url: 'https://example.com/mp2' }],
},
});
config.removeAuditTargetURL('moneyPages', 'https://example.com/mp2');
expect(config.getAuditTargetURLsBySource('moneyPages')).to.have.lengthOf(1);
expect(config.getAuditTargetURLsBySource('moneyPages')[0].url).to.equal('https://example.com/mp1');
});
});

describe('serialization', () => {
Expand All @@ -3173,6 +3246,7 @@ describe('Config Tests', () => {
const item = Config.toDynamoItem(config);
expect(item.auditTargetURLs).to.deep.equal({
manual: [{ url: 'https://example.com/page1' }],
moneyPages: [],
});
});

Expand All @@ -3189,6 +3263,31 @@ describe('Config Tests', () => {
const restored = Config.fromDynamoItem(item);
expect(restored.getAuditTargetURLs()).to.deep.equal(config.getAuditTargetURLs());
});

it('includes moneyPages in toDynamoItem conversion', () => {
const config = Config({
auditTargetURLs: {
moneyPages: [{ url: 'https://example.com/mp1' }],
},
});
const item = Config.toDynamoItem(config);
expect(item.auditTargetURLs).to.deep.equal({
manual: [],
moneyPages: [{ url: 'https://example.com/mp1' }],
});
});

it('round-trips both manual and moneyPages through serialization', () => {
const config = Config({
auditTargetURLs: {
manual: [{ url: 'https://example.com/m1' }],
moneyPages: [{ url: 'https://example.com/mp1' }],
},
});
const item = Config.toDynamoItem(config);
const restored = Config.fromDynamoItem(item);
expect(restored.getAuditTargetURLs()).to.deep.equal(config.getAuditTargetURLs());
});
});

describe('field validation', () => {
Expand All @@ -3200,17 +3299,20 @@ describe('Config Tests', () => {
})).to.throw();
});

it('strips unknown source keys from auditTargetURLs', () => {
it('strips unknown source keys from auditTargetURLs but keeps moneyPages', () => {
const config = Config({
auditTargetURLs: {
manual: [{ url: 'https://example.com/page1' }],
moneyPages: [{ url: 'https://example.com/mp1' }],
unknown: [{ url: 'https://example.com/page2' }],
},
});
const result = config.getAuditTargetURLs();
expect(result).to.have.lengthOf(1);
expect(result).to.have.lengthOf(2);
expect(result[0].url).to.equal('https://example.com/page1');
expect(result[0].source).to.equal('manual');
expect(result[1].url).to.equal('https://example.com/mp1');
expect(result[1].source).to.equal('moneyPages');
});
});
});
Expand Down
Loading