Added getMentionReport method to MentionsAPI

This will be used by the mentions-report-email package to generate the
data used for the report emails
This commit is contained in:
Fabien "egg" O'Carroll 2023-03-12 23:06:17 +07:00 committed by Rishabh Garg
parent c56b819748
commit 0262083bb0
3 changed files with 68 additions and 0 deletions

View File

@ -115,4 +115,15 @@ module.exports = class InMemoryMentionRepository {
}
};
}
async getAll(options) {
const page = await this.getPage({
filter: options.filter,
order: options.order,
page: null,
limit: 'all'
});
return page.data;
}
};

View File

@ -37,10 +37,16 @@ const Mention = require('./Mention');
* @typedef {PaginatedOptions | NonPaginatedOptions} GetPageOptions
*/
/**
* @typedef {object} GetAllOptions
* @prop {string} [filter] A valid NQL string
*/
/**
* @typedef {object} IMentionRepository
* @prop {(mention: Mention) => Promise<void>} save
* @prop {(options: GetPageOptions) => Promise<Page<Mention>>} getPage
* @prop {(options: GetAllOptions) => Promise<Mention[]>} getAll
* @prop {(source: URL, target: URL) => Promise<Mention>} getBySourceAndTarget
*/
@ -71,6 +77,13 @@ const Mention = require('./Mention');
* @prop {string} body
*/
/**
* @typedef {object} MentionReport
* @prop {Date} startDate
* @prop {Date} endDate
* @prop {Mention[]} mentions
*/
/**
* @typedef {object} IWebmentionMetadata
* @prop {(url: URL) => Promise<WebmentionMetadata>} fetch
@ -100,6 +113,25 @@ module.exports = class MentionsAPI {
this.#webmentionMetadata = deps.webmentionMetadata;
}
/**
* @param {Date} startDate
* @param {Date} endDate
* @returns {Promise<MentionReport>}
*/
async getMentionReport(startDate, endDate) {
const mentions = await this.#repository.getAll({
filter: `created_at:>${startDate.toISOString()}+created_at:<${endDate.toISOString()}`
});
const report = {
startDate: new Date(startDate),
endDate: new Date(endDate),
mentions
};
return report;
}
/**
* @param {object} options
* @returns {Promise<Page<Mention>>}

View File

@ -44,6 +44,31 @@ describe('MentionsAPI', function () {
sinon.restore();
});
it('Can generate a mentions report', async function () {
const repository = new InMemoryMentionRepository();
const api = new MentionsAPI({
repository,
routingService: mockRoutingService,
resourceService: mockResourceService,
webmentionMetadata: mockWebmentionMetadata
});
const mention = await api.processWebmention({
source: new URL('https://source.com'),
target: new URL('https://target.com'),
payload: {}
});
assert(mention instanceof Mention);
const now = new Date();
const report = await api.getMentionReport(new Date(0), new Date());
assert.deepEqual(report.startDate, new Date(0));
assert.deepEqual(report.endDate, now);
});
it('Can list paginated mentions', async function () {
const repository = new InMemoryMentionRepository();
const api = new MentionsAPI({