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
35 changes: 30 additions & 5 deletions src/routes/projectReports/getEmbedReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ module.exports = [
permissions('projectReporting.view'),
async (req, res, next) => {
const projectId = Number(req.params.projectId);
const mockReport = config.lookerConfig.USE_MOCK === 'true';
const mockReport = config.get('lookerConfig.USE_MOCK') === 'true';
let reportName = mockReport ? 'mock' : req.query.reportName;
const authUser = req.authUser;
let REPORTS = null;
let allowedUsers = null;
try {
allowedUsers = JSON.parse(_.get(config, 'lookerConfig.ALLOWED_USERS', '[]'));
req.log.trace(allowedUsers, 'allowedUsers');
REPORTS = JSON.parse(config.lookerConfig.EMBED_REPORTS_MAPPING);
REPORTS = JSON.parse(config.get('lookerConfig.EMBED_REPORTS_MAPPING'));
} catch (error) {
req.log.error(error);
req.log.debug('Invalid reports mapping. Should be a valid JSON.');
Expand All @@ -35,14 +35,39 @@ module.exports = [
if (!mockReport) {
const project = await models.Project.findOne({
where: { id: projectId },
attributes: ['id', 'templateId'],
attributes: ['id', 'templateId', 'details'],
raw: true,
});

// we would use Project Template or Product Template category to format report name
let category = '';

// try to get project template of the project to generate the report name
const projectTemplate = project.templateId
? await models.ProjectTemplate.findByPk(project.templateId, { attributes: ['category'], raw: true })
: null;
const projectCategory = _.get(projectTemplate, 'category', '');
reportName = `${reportName}-${projectCategory}`;
if (projectTemplate) {
category = _.get(projectTemplate, 'category', '');

// if no project template found, try to find product template (for old project v2)
} else {
const productTemplate = _.get(project, 'details.products[0]')
? await models.ProductTemplate.findOne(
{
where: {
productKey: _.get(project, 'details.products[0]'),
},
},
{
attributes: ['category'],
raw: true,
},
) : null;

category = _.get(productTemplate, 'category', '');
}

reportName = `${reportName}-${category}`;
}
// check if auth user has acecss to this project
const members = req.context.currentProjectMembers;
Expand Down
Loading