Skip to content
Open
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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Assignment Completed

**Author:** Christopher Kaliszewski

**Code Coverage (_using `nyc`_):** 100%

**Test Command:** `npm test .\test\consumer\merge-school-enrollment-info.test.js`

# UnitTestingNode

Here's the assignment task

Create a new function named mergeSchoolInfoToEnrollments
This function should fetch the schools and the enrollment data and run the enrollment data through a stream
in one the streams ,we need to add the school information each enrollment.
in one the streams ,we need to add the school information each enrollment.
and then the final stream should write the data to the a file named enrollments_with_schools.json

Also write the unit test for this function.
Also write the unit test for this function.

8 changes: 4 additions & 4 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ const express = require('express');
const app = express();
const fs = require('fs');
app.get("/students", (req, res) => {
var fileContent = fs.readFileSync("students.json");
var fileContent = fs.readFileSync('api/students.json');
res.json(JSON.parse(fileContent.toString()));
});

app.get("/schools", (req, res) => {
var fileContent = fs.readFileSync("school.json");
var fileContent = fs.readFileSync('api/schools.json');
res.json(JSON.parse(fileContent.toString()));
});

app.get("/enrollments", (req, res) => {
var fileContent = fs.readFileSync("enrollments.json");
var fileContent = fs.readFileSync('api/enrollments.json');
res.json(JSON.parse(fileContent.toString()));
});

app.listen(3000);
app.listen(3000);
10 changes: 5 additions & 5 deletions consumer/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const produceStudentOutput = require('./produce-student-output');
const mergeSchoolEnrollmentInfo = require('./merge-school-enrollment-info');

(async function() {
await produceStudentOutput();
}
)();
(async () => {
// await produceStudentOutput();
await mergeSchoolEnrollmentInfo();
})();
68 changes: 68 additions & 0 deletions consumer/merge-school-enrollment-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const StudentApi = require('./student-api');
const JSONStream = require('jsonstream');
const { Writable, Transform } = require('stream');
const pump = require('pump');
const fs = require('fs');

const buildSchoolLookup = async (schoolStream) => {
const schoolLookup = {};

await new Promise((resolve) => {
schoolStream
.pipe(JSONStream.parse('*'))
.pipe(new Writable({
objectMode: true,
write(chunk, encoding, callback) {
schoolLookup[chunk.id] = chunk;
callback();
}
}));

schoolStream.on('end', () => {
resolve();
});
});

return schoolLookup;
};

module.exports = async function() {
const api = new StudentApi();
const schoolLookup = await buildSchoolLookup(await api.getSchools());
const enrollmentStream = await api.getEnrollments();

const processEnrollmentStream = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
const school = schoolLookup[chunk.school_id] || null;
this.push({
id: chunk.id,
student_id: chunk.student_id,
school
});
callback();
}
});

const resultFilePath = './merged-enrollments.json';

try {
fs.unlinkSync(resultFilePath);
} catch(error) { }

const fileStream = fs.createWriteStream(resultFilePath);

return new Promise((resolve, reject) => {
pump(
enrollmentStream,
JSONStream.parse('*'),
processEnrollmentStream,
JSONStream.stringify(),
fileStream,
(error) => {
if (error) { reject(error); }
resolve();
}
);
});
};
Loading