(userId, chartIds, filters, projectId)
| 1055 | } |
| 1056 | |
| 1057 | exportChartData(userId, chartIds, filters, projectId) { |
| 1058 | const parsedChartIds = Array.isArray(chartIds) |
| 1059 | ? [...new Set(chartIds |
| 1060 | .map((id) => Number(id)) |
| 1061 | .filter((id) => Number.isInteger(id) && id > 0))] |
| 1062 | : []; |
| 1063 | |
| 1064 | if (parsedChartIds.length === 0) { |
| 1065 | return Promise.reject(new Error("Invalid chart IDs")); |
| 1066 | } |
| 1067 | |
| 1068 | const whereCondition = { id: parsedChartIds }; |
| 1069 | if (projectId) { |
| 1070 | whereCondition.project_id = projectId; |
| 1071 | } |
| 1072 | |
| 1073 | return db.Chart.findAll({ |
| 1074 | where: whereCondition, |
| 1075 | include: [{ model: db.ChartDatasetConfig, include: [{ model: db.Dataset }] }], |
| 1076 | order: [[db.ChartDatasetConfig, "order", "ASC"]], |
| 1077 | }) |
| 1078 | .then((charts) => { |
| 1079 | if (charts.length !== parsedChartIds.length) { |
| 1080 | return Promise.reject(new Error("One or more charts are not accessible")); |
| 1081 | } |
| 1082 | |
| 1083 | const dataPromises = []; |
| 1084 | charts.forEach((chart) => { |
| 1085 | dataPromises.push( |
| 1086 | this.updateChartData( |
| 1087 | chart.id, |
| 1088 | { id: userId }, |
| 1089 | { |
| 1090 | noSource: false, |
| 1091 | skipParsing: false, |
| 1092 | filters, |
| 1093 | isExport: true |
| 1094 | }, |
| 1095 | ) |
| 1096 | .then((data) => { |
| 1097 | // first, make sure the sheet name is no longer than 31 characters |
| 1098 | let sheetName = `${chart.name} - ${nanoid(5)}`; |
| 1099 | if (chart.name.length > 26) { |
| 1100 | let newChartName = chart.name.substring(0, 23); |
| 1101 | if (newChartName.lastIndexOf(" ") > 10) { |
| 1102 | newChartName = newChartName.substring(0, newChartName.lastIndexOf(" ")); |
| 1103 | } |
| 1104 | sheetName = `${newChartName} - ${nanoid(5)}`; |
| 1105 | } |
| 1106 | |
| 1107 | // remove any special characters |
| 1108 | sheetName = sheetName.replace(/[^a-zA-Z ]/g, ""); |
| 1109 | |
| 1110 | return { |
| 1111 | name: sheetName, |
| 1112 | datasets: chart.ChartDatasetConfigs, |
| 1113 | data, |
| 1114 | }; |
no test coverage detected