Skip to content

Commit e325aee

Browse files
maxceemxxcxy
andauthored
Report unit tests and support for old projects with product templates (#487)
* Add reports tests * Fix reports tests * feat: support reports for old projects v2 with product template Co-authored-by: xxcxy <bert7914@gmail.com>
1 parent 2fd32ce commit e325aee

File tree

4 files changed

+698
-6
lines changed

4 files changed

+698
-6
lines changed

src/routes/projectReports/getEmbedReport.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ module.exports = [
1414
permissions('projectReporting.view'),
1515
async (req, res, next) => {
1616
const projectId = Number(req.params.projectId);
17-
const mockReport = config.lookerConfig.USE_MOCK === 'true';
17+
const mockReport = config.get('lookerConfig.USE_MOCK') === 'true';
1818
let reportName = mockReport ? 'mock' : req.query.reportName;
1919
const authUser = req.authUser;
2020
let REPORTS = null;
2121
let allowedUsers = null;
2222
try {
2323
allowedUsers = JSON.parse(_.get(config, 'lookerConfig.ALLOWED_USERS', '[]'));
2424
req.log.trace(allowedUsers, 'allowedUsers');
25-
REPORTS = JSON.parse(config.lookerConfig.EMBED_REPORTS_MAPPING);
25+
REPORTS = JSON.parse(config.get('lookerConfig.EMBED_REPORTS_MAPPING'));
2626
} catch (error) {
2727
req.log.error(error);
2828
req.log.debug('Invalid reports mapping. Should be a valid JSON.');
@@ -35,14 +35,39 @@ module.exports = [
3535
if (!mockReport) {
3636
const project = await models.Project.findOne({
3737
where: { id: projectId },
38-
attributes: ['id', 'templateId'],
38+
attributes: ['id', 'templateId', 'details'],
3939
raw: true,
4040
});
41+
42+
// we would use Project Template or Product Template category to format report name
43+
let category = '';
44+
45+
// try to get project template of the project to generate the report name
4146
const projectTemplate = project.templateId
4247
? await models.ProjectTemplate.findByPk(project.templateId, { attributes: ['category'], raw: true })
4348
: null;
44-
const projectCategory = _.get(projectTemplate, 'category', '');
45-
reportName = `${reportName}-${projectCategory}`;
49+
if (projectTemplate) {
50+
category = _.get(projectTemplate, 'category', '');
51+
52+
// if no project template found, try to find product template (for old project v2)
53+
} else {
54+
const productTemplate = _.get(project, 'details.products[0]')
55+
? await models.ProductTemplate.findOne(
56+
{
57+
where: {
58+
productKey: _.get(project, 'details.products[0]'),
59+
},
60+
},
61+
{
62+
attributes: ['category'],
63+
raw: true,
64+
},
65+
) : null;
66+
67+
category = _.get(productTemplate, 'category', '');
68+
}
69+
70+
reportName = `${reportName}-${category}`;
4671
}
4772
// check if auth user has acecss to this project
4873
const members = req.context.currentProjectMembers;

0 commit comments

Comments
 (0)