-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'add-bulk-upload' into master
- Loading branch information
Showing
7 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,7 @@ typings/ | |
/db | ||
|
||
# Code Editor Configuration file | ||
.vscode | ||
.vscode | ||
|
||
# CSV test file | ||
assessmentItem.csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const questionController = require('../question/question.controller'); | ||
const config = require('../config'); | ||
const fs = require('fs'); | ||
const csv = require('fast-csv'); | ||
const _ = require('lodash'); | ||
|
||
const readAndPublish = async () => { | ||
const stream = fs.createReadStream(config.fileUploadPath); | ||
let publishedQuestion = ''; | ||
await csv | ||
.fromStream(stream, { objectMode: true, headers: true }) | ||
.validate(data => | ||
data.concept && data.content && data.expectedOutcome && data.player && data.evaluator) | ||
.on('data-invalid', () => { | ||
console.log('Error: Primary fields are missing!!!'); | ||
}) | ||
.on('data', async (data) => { | ||
let assessmentItem = data; | ||
let primaryObject = {}; | ||
primaryObject = _.pick(assessmentItem, ['concept', 'content', 'expectedOutcome', 'player', 'evaluator']); | ||
_.forOwnRight(assessmentItem, (value, key) => { | ||
if (key === 'concept' || key === 'content' || key === 'expectedOutcome' || key === 'player' || key === 'evaluator') { | ||
_.unset(assessmentItem, key); | ||
} | ||
}); | ||
const secondaryObject = assessmentItem; | ||
primaryObject.question = secondaryObject; | ||
assessmentItem = primaryObject; | ||
const initialisedQuestion = await questionController.initQuestion(); | ||
assessmentItem.id = initialisedQuestion.id; | ||
publishedQuestion = await questionController.publishQuestion(assessmentItem); | ||
return publishedQuestion; | ||
}) | ||
.on('error', (err) => { | ||
console.log(`Error is ${err}`); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
readAndPublish, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const express = require('express'); | ||
const csvController = require('./csv.controller'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/csvUpload', (req, res) => { | ||
const publishedQuestion = csvController.readAndPublish(); | ||
res.json(publishedQuestion); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./csv.router'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const config = { | ||
PORT: process.env.PORT || 4000, | ||
MONGODB_URL: process.env.MONGODB_URL || 'localhost:29017/assessment_item', | ||
fileUploadPath: process.env.FILE_UPLOAD_PATH || 'assessmentItem.csv', | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters