Added quick reports (at this moment only one) to campaign statistics page.
This commit is contained in:
parent
3e3c3a24fe
commit
72ffe065d2
11 changed files with 305 additions and 143 deletions
59
server/routes/quick-reports.js
Normal file
59
server/routes/quick-reports.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
'use strict';
|
||||
|
||||
const passport = require('../lib/passport');
|
||||
const shares = require('../models/shares');
|
||||
const contextHelpers = require('../lib/context-helpers');
|
||||
const {renderCsvFromStream} = require('../lib/report-helpers');
|
||||
const reports = require('../models/reports');
|
||||
const campaigns = require('../models/campaigns');
|
||||
const {castToInteger} = require('../lib/helpers');
|
||||
const {SubscriptionStatus} = require('../../shared/lists');
|
||||
const knex = require('../lib/knex');
|
||||
const {LinkId} = require('../models/links');
|
||||
|
||||
const router = require('../lib/router-async').create();
|
||||
|
||||
router.getAsync('/open-and-click-counts/:campaignId', passport.loggedIn, async (req, res) => {
|
||||
const campaignId = castToInteger(req.params.campaignId);
|
||||
|
||||
await shares.enforceEntityPermission(req.context, 'campaign', campaignId, 'viewStats');
|
||||
const campaign = await campaigns.getById(req.context, campaignId, false);
|
||||
|
||||
const listFields = await reports.getCampaignCommonListFields(campaign);
|
||||
|
||||
const results = await reports.getCampaignStatisticsStream(
|
||||
campaign,
|
||||
['subscription:email', 'open_tracker:count', 'click_tracker:count', 'open_tracker:country', ...Object.keys(listFields)],
|
||||
[
|
||||
{type: 'links', prefix: 'open_tracker', onConditions: {link: knex.raw('?', [LinkId.OPEN])} },
|
||||
{type: 'links', prefix: 'click_tracker', onConditions: {link: knex.raw('?', [LinkId.GENERAL_CLICK])} }
|
||||
],
|
||||
null,
|
||||
(qry, col) => qry
|
||||
.where(col('subscription:status'), SubscriptionStatus.SUBSCRIBED)
|
||||
);
|
||||
|
||||
res.set({
|
||||
'Content-Disposition': `attachment;filename=campaign-open-and-click-counts-${campaign.cid}.csv`,
|
||||
'Content-Type': 'text/csv'
|
||||
});
|
||||
|
||||
await renderCsvFromStream(
|
||||
results,
|
||||
res,
|
||||
{
|
||||
header: true,
|
||||
columns: [
|
||||
{ key: 'subscription:email', header: 'Email' },
|
||||
{ key: 'open_tracker:count', header: 'Open count' },
|
||||
{ key: 'click_tracker:count', header: 'Click count' },
|
||||
{ key: 'open_tracker:country', header: 'Country (first open)' },
|
||||
...Object.keys(listFields).map(key => ({key, header: listFields[key].key}))
|
||||
],
|
||||
delimiter: ','
|
||||
},
|
||||
async (row, encoding) => row
|
||||
);
|
||||
});
|
||||
|
||||
module.exports = router;
|
|
@ -5,6 +5,7 @@ const reports = require('../models/reports');
|
|||
const reportHelpers = require('../lib/report-helpers');
|
||||
const shares = require('../models/shares');
|
||||
const contextHelpers = require('../lib/context-helpers');
|
||||
const {castToInteger} = require('../lib/helpers');
|
||||
|
||||
const router = require('../lib/router-async').create();
|
||||
|
||||
|
@ -14,9 +15,10 @@ const fileSuffixes = {
|
|||
};
|
||||
|
||||
router.getAsync('/:id/download', passport.loggedIn, async (req, res) => {
|
||||
await shares.enforceEntityPermission(req.context, 'report', req.params.id, 'viewContent');
|
||||
const reportId = castToInteger(req.params.id);
|
||||
await shares.enforceEntityPermission(req.context, 'report', reportId, 'viewContent');
|
||||
|
||||
const report = await reports.getByIdWithTemplate(contextHelpers.getAdminContext(), req.params.id, false);
|
||||
const report = await reports.getByIdWithTemplate(contextHelpers.getAdminContext(), reportId, false);
|
||||
|
||||
if (report.state == reports.ReportState.FINISHED) {
|
||||
const headers = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue