Skip to content

Commit bb271c8

Browse files
Fixes for challenge report UI.
1 parent d2aac78 commit bb271c8

File tree

5 files changed

+34
-40
lines changed

5 files changed

+34
-40
lines changed

backend/src/components/assessments/assessments.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ async function postAssessmentStudent(req, res) {
153153
};
154154
payload.studentStatusCode = 'ACTIVE';
155155
const token = getAccessToken(req);
156-
console.log(JSON.stringify(payload));
157156
const result = await postData(token, payload, `${config.get('assessments:assessmentStudentsURL')}`, req.session?.correlationID);
158157
return res.status(HttpStatus.OK).json(result);
159158
} catch (e) {

backend/src/components/challengeReports.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const HttpStatus = require('http-status-codes');
44
const log = require('./logger');
55
const config = require('../config');
66
const {getSchoolBySchoolID, getDistrictByDistrictID} = require('./cache-service');
7+
const cacheService = require('./cache-service');
78

89
async function getActiveChallengeReportsPeriod(req, res) {
910
try {
@@ -31,10 +32,19 @@ async function getActiveChallengeReportsPeriod(req, res) {
3132
async function downloadDistrictChallengeReport(req, res) {
3233
try {
3334
const token = getAccessToken(req);
35+
36+
let district = cacheService.getDistrictByDistrictID(req.params.districtID);
37+
if(!district || district.districtID !== req.session.activeInstituteIdentifier){
38+
return res.status(HttpStatus.BAD_REQUEST).json({ message: 'Invalid district provided' });
39+
}
40+
3441
const url = `${config.get('challengeReports:rootURL')}/district/${req.params.districtID}/download`;
3542
const data = await getData(token, url);
3643

37-
const fileDetails = { contentType: 'text/csv' };
44+
console.log('District: ' + JSON.stringify(district));
45+
let reportType = req.query.isPrelim === true ? 'PRELIM_DISTRICT_REPORT' : 'FINAL_DISTRICT_REPORT';
46+
47+
const fileDetails = getFileDetails(reportType, district.districtNumber, req.query.period);
3848
setResponseHeaders(res, fileDetails);
3949
const buffer = Buffer.from(data.documentData, 'base64');
4050

@@ -83,7 +93,8 @@ async function createSchoolCountList(schoolsWithCounts){
8393
}));
8494
}
8595

86-
function setResponseHeaders(res, {contentType }) {
96+
function setResponseHeaders(res, { filename, contentType }) {
97+
res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
8798
res.setHeader('Content-Type', contentType);
8899
}
89100

@@ -97,6 +108,15 @@ function formatCompletionDate(rawDate) {
97108
return `${year}/${month}/${day}`;
98109
}
99110

111+
function getFileDetails(reportType, district, session) {
112+
const mappings = {
113+
'PRELIM_DISTRICT_REPORT': { filename: `SD ${district} Preliminary District Level Funding Report For Course Challenges ${session}.csv`, contentType: 'text/csv' },
114+
'FINAL_DISTRICT_REPORT': { filename: `SD ${district} District Level Funding Report For Course Challenges ${session}.csv`, contentType: 'text/csv' },
115+
'DEFAULT': { filename: 'download.pdf', contentType: 'application/pdf' }
116+
};
117+
return mappings[reportType] || mappings['DEFAULT'];
118+
}
119+
100120
module.exports = {
101121
getActiveChallengeReportsPeriod,
102122
downloadDistrictChallengeReport,

backend/src/routes/challengeReports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ router.get('/active-period', passport.authenticate('jwt', {session: false}, unde
1515
router.get('/:districtID', passport.authenticate('jwt', {session: false}, undefined), isValidBackendToken, validateAccessToken, checkEdxUserPermission(PERMISSION.CHALLENGE_REPORTS),
1616
validate(getChallengeReportDownloadSchema), getDistrictChallengeReportsCounts);
1717

18-
router.get('/:districtID/download', passport.authenticate('jwt', {session: false}, undefined), isValidBackendToken, validateAccessToken, checkEdxUserPermission(PERMISSION.CHALLENGE_REPORTS),
18+
router.get('/:districtID/download', isValidBackendToken, validateAccessToken, checkEdxUserPermission(PERMISSION.CHALLENGE_REPORTS),
1919
validate(getChallengeReportDownloadSchema), downloadDistrictChallengeReport);
2020

2121
module.exports = router;

backend/src/validations/challengeReports.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { object, string} = require('yup');
1+
const { object, string, boolean} = require('yup');
22

33
const getChallengeReportPeriodSchema = object({
44
body: object().noUnknown(),
@@ -11,7 +11,10 @@ const getChallengeReportDownloadSchema = object({
1111
params: object({
1212
districtID: string()
1313
}).noUnknown(),
14-
query: object().noUnknown()
14+
query: object({
15+
isPrelim: boolean(),
16+
period: string()
17+
}).noUnknown()
1518
}).noUnknown();
1619

1720

frontend/src/components/challengeReports/ChallengeReports.vue

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ const REPORT_STATUS_FINAL = 'Final';
131131
132132
import ApiService from '../../common/apiService';
133133
import { ApiRoutes } from '../../utils/constants';
134-
import { setSuccessAlert } from '../composable/alertComposable';
135134
import DownloadLink from '../common/DownloadLink.vue';
136135
import alertMixin from '../../mixins/alertMixin';
137136
import {sortBy} from 'lodash';
@@ -281,39 +280,12 @@ export default {
281280
});
282281
},
283282
downloadDistrictReport() {
284-
ApiService.apiAxios
285-
.get(`${ApiRoutes.challengeReports.BASE_URL}/${this.districtID}/download`)
286-
.then((response) => {
287-
if (response.data && response.data.type === 'Buffer' && Array.isArray(response.data.data)) {
288-
const byteArray = new Uint8Array(response.data.data);
289-
const blob = new Blob([byteArray], { type: 'text/csv' });
290-
const link = document.createElement('a');
291-
link.href = URL.createObjectURL(blob);
292-
293-
if (this.activePeriod.challengeReportsSessionStatus === REPORT_STATUS_PRELIMINARY) {
294-
link.download = `Preliminary District Level Funding Report for Course Challenges ${this.startingYear}-${this.endingYear}.csv`;
295-
} else {
296-
link.download = `District Level Funding Report for Course Challenges ${this.startingYear}-${this.endingYear}.csv`;
297-
}
298-
299-
document.body.appendChild(link);
300-
link.click();
301-
document.body.removeChild(link);
302-
URL.revokeObjectURL(link.href);
303-
setSuccessAlert('Challenge Report downloaded successfully.');
304-
} else {
305-
console.error('Unexpected response format for file download:', response.data);
306-
this.setFailureAlert('Failed to process the report data. Unexpected format.');
307-
}
308-
})
309-
.catch((error) => {
310-
console.error(error);
311-
this.setFailureAlert(
312-
error?.response?.data?.message
313-
? error?.response?.data?.message
314-
: 'An error occurred while trying to retrieve your district\'s report.'
315-
);
316-
});
283+
let isPrelim = false;
284+
if(this.activePeriod.challengeReportsSessionStatus === REPORT_STATUS_PRELIMINARY){
285+
isPrelim = true;
286+
}
287+
let url = `${ApiRoutes.challengeReports.BASE_URL}/${this.districtID}/download?isPrelim=${isPrelim}&period=${this.startingYear}/${this.endingYear}`;
288+
window.open(url);
317289
},
318290
showTooltip() {
319291
this.tooltipWidth = this.$refs.chipRef.$el.offsetWidth + 'px';

0 commit comments

Comments
 (0)