Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recent assignment deliveries #403

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
26 changes: 18 additions & 8 deletions api/controller/assignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,19 @@ exports.findAll = function (req, res) {
**/
//find all deliveries
exports.findAllDeliveries = function (req, res) {
var assignmentId = req.params.assignmentId;
var assignmentId = req.query.assignmentId;
//validate
if (!mongo.ObjectID.isValid(assignmentId)) {
if (assignmentId && !mongo.ObjectID.isValid(assignmentId)) {
return res.status(401).send({
'error': "Invalid assignment id",
'code': 35
});
}
//find all deliveries with filters and pagination
var query = {"metadata.assignmentId": assignmentId};
var query = {};
if (assignmentId) {
query = {"metadata.assignmentId": assignmentId};
}
query = addQuery("buddy_name", req.query, query);
query = addQuery("Delivered", req.query, query);
db.collection(journalCollection, function (err, collection) {
Expand All @@ -427,6 +430,17 @@ exports.findAllDeliveries = function (req, res) {
var route = req.route.path;
var options = getOptions(req, count, "+buddy_name");
//find all entries which matches with assignment id using aggregation
var filters = {
input: "$content",
as: "item",
cond:{}
};
if (query["metadata.isSubmitted"]) {
filters["cond"] = {$eq:["$$item.metadata.isSubmitted", query["metadata.isSubmitted"]['$eq']]};
}
if (assignmentId) {
filters["cond"] = {$eq:["$$item.metadata.assignmentId", assignmentId]};
}
collection.aggregate([
{
$match: {content: {$elemMatch: query}},
Expand All @@ -435,11 +449,7 @@ exports.findAllDeliveries = function (req, res) {
$project: {
_id: 1,
content: {
$filter: {
input: "$content",
as: "item",
cond: { $eq: ["$$item.metadata.assignmentId", assignmentId] },
}
$filter: filters
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion api/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = function (app, ini, db) {

//Register Assignments APIs
app.get("/api/v1/assignments", auth.allowedRoles([Admin, Teacher]), assignments.findAll);
app.get("/api/v1/assignments/deliveries/:assignmentId", auth.allowedRoles([Admin, Teacher]), assignments.findAllDeliveries);
app.get("/api/v1/assignments/deliveries", auth.allowedRoles([Admin, Teacher]), assignments.findAllDeliveries);
app.get("/api/v1/assignments/:assignmentId", auth.allowedRoles([Admin, Teacher]), assignments.findById);
app.post("/api/v1/assignments/", auth.allowedRoles([Admin, Teacher]), assignments.addAssignment);
app.get("/api/v1/assignments/launch/:assignmentId", auth.allowedRoles([Admin, Teacher]), assignments.launchAssignment);
Expand Down
229 changes: 229 additions & 0 deletions api/test/assignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,16 @@ describe('Assignments', () => {

// submit assignment ---PUT/:id/submit---
describe('/PUT/:id/submit assignments', () => {
before((done) => {
chai.request(server)
.get('/api/v1/journal/'+fake.student1.private_journal)
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.end((err,res) => {
fake.delivery1=res.body.entries[0];
done();
});
});
it('it should do nothing on invalid assignment', (done) => {
chai.request(server)
.put('/api/v1/assignments/deliveries/submit/' + 'invalid?oid=' + fake.delivery1.objectId)
Expand Down Expand Up @@ -531,6 +541,211 @@ describe('Assignments', () => {
});
});

// get Assignment Deliveries ---GET---

describe('/GET/deliveries assignments', () => {
it('it should do nothing on invalid assignment', (done) => {
chai.request(server)
.get('/api/v1/assignments/deliveries')
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
assignmentId: 'invalid'
})
.end((err, res) => {
res.should.have.status(401);
res.body.code.should.be.eql(35);
done();
});
});

it('it should return the deliveries of valid assignment', (done) => {
chai.request(server)
.get('/api/v1/assignments/deliveries')
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
assignmentId: fake.assignment1._id
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('deliveries');
res.body.should.have.property('offset');
res.body.should.have.property('limit');
res.body.should.have.property('sort');
res.body.should.have.property('links');
res.body.deliveries.should.be.a("array");
res.body.offset.should.be.a("number");
res.body.limit.should.be.a("number");
res.body.total.should.be.a("number");
res.body.sort.should.be.eql("buddy_name(asc)");
res.body.links.should.be.a("object");
res.body.deliveries.should.be.a("array");
res.body.deliveries.should.have.length.above(0);
for(var i=0;i<res.body.deliveries.length;i++){
var delivery = res.body.deliveries[i];
delivery.should.not.be.null;
delivery.should.have.property("_id");
delivery.content.should.not.be.null;
}
done();
});
});

it('it should return the deliveries of valid assignment which are not delivered', (done) => {
chai.request(server)
.get('/api/v1/assignments/deliveries')
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
assignmentId: fake.assignment1._id,
Delivered: 'false'
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('deliveries');
res.body.should.have.property('offset');
res.body.should.have.property('limit');
res.body.should.have.property('sort');
res.body.should.have.property('links');
res.body.deliveries.should.be.a("array");
res.body.offset.should.be.a("number");
res.body.limit.should.be.a("number");
res.body.total.should.be.a("number");
res.body.sort.should.be.eql("buddy_name(asc)");
res.body.links.should.be.a("object");
res.body.deliveries.should.be.a("array");
res.body.deliveries.should.have.length.above(0);
for(var i=0;i<res.body.deliveries.length;i++){
var delivery = res.body.deliveries[i];
delivery.should.not.be.null;
delivery.should.have.property("_id");
delivery.content.should.not.be.null;
for(var j=0;j<delivery.content.length;j++){
var content = delivery.content[j];
content.should.have.property("metadata");
content.metadata.isSubmitted.should.be.eql(false);
}
}
done();
});
});

it('it should return the deliveries of valid assignment which are delivered', (done) => {
chai.request(server)
.get('/api/v1/assignments/deliveries')
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
assignmentId: fake.assignment1._id,
Delivered: 'true'
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('deliveries');
res.body.should.have.property('offset');
res.body.should.have.property('limit');
res.body.should.have.property('sort');
res.body.should.have.property('links');
res.body.deliveries.should.be.a("array");
res.body.offset.should.be.a("number");
res.body.limit.should.be.a("number");
res.body.total.should.be.a("number");
res.body.sort.should.be.eql("buddy_name(asc)");
res.body.links.should.be.a("object");
res.body.deliveries.should.be.a("array");
res.body.deliveries.should.have.length.above(0);
for(var i=0;i<res.body.deliveries.length;i++){
var delivery = res.body.deliveries[i];
delivery.should.not.be.null;
delivery.should.have.property("_id");
delivery.content.should.not.be.null;
for(var j=0;j<delivery.content.length;j++){
var content = delivery.content[j];
content.should.have.property("metadata");
content.metadata.status.should.be.eql("Delivered");
content.metadata.isSubmitted.should.be.eql(true);
}
}
done();
});
});

it('it should return the deliveries of all assignments which are delivered', (done) => {
chai.request(server)
.get('/api/v1/assignments/deliveries')
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
Delivered: "true"
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('deliveries');
res.body.should.have.property('offset');
res.body.should.have.property('limit');
res.body.should.have.property('sort');
res.body.should.have.property('links');
res.body.deliveries.should.be.a("array");
res.body.offset.should.be.a("number");
res.body.limit.should.be.a("number");
res.body.total.should.be.a("number");
res.body.sort.should.be.eql("buddy_name(asc)");
res.body.links.should.be.a("object");
res.body.deliveries.should.be.a("array");
res.body.deliveries.should.have.length.above(0);
for(var i=0;i<res.body.deliveries.length;i++){
var delivery = res.body.deliveries[i];
delivery.should.not.be.null;
delivery.should.have.property("_id");
delivery.content.should.not.be.null;
for(var j=0;j<delivery.content.length;j++){
var content = delivery.content[j];
content.should.have.property("metadata");
content.metadata.status.should.be.eql("Delivered");
content.metadata.isSubmitted.should.be.eql(true);
}
}
done();
});
});

it('it should return one deliveries after skipping first', (done) => {
chai.request(server)
.get('/api/v1/assignments/deliveries')
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
assignmentId: fake.assignment1._id,
offset: 1
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('deliveries');
res.body.should.have.property('offset');
res.body.should.have.property('limit');
res.body.should.have.property('sort');
res.body.should.have.property('links');
res.body.deliveries.should.be.a("array");
res.body.offset.should.be.a("number");
res.body.limit.should.be.a("number");
res.body.total.should.be.a("number");
res.body.sort.should.be.eql("buddy_name(asc)");
res.body.links.should.be.a("object");
res.body.deliveries.should.be.a("array");
res.body.deliveries.should.have.length(1);

for (var i = 0; i < res.body.deliveries.length; i++) {
var delivery = res.body.deliveries[i];
delivery.should.not.be.null;
delivery.should.have.property("_id");
delivery.should.have.property("content");
}
done();
});
});
});

// return assignment ---GET/:id/return---
describe('/GET/:id/return assignments', () => {
it('it should do nothing on invalid assignment', (done) => {
Expand Down Expand Up @@ -642,4 +857,18 @@ describe('Assignments', () => {
}
});
}

after((done) =>{
chai.request(server)
.delete('/api/v1/journal/' + fake.teacher1.user.private_journal)
.set('x-access-token', fake.teacher1.token)
.set('x-key', fake.teacher1.user._id)
.query({
oid:"ffffffff-ffff-ffff-ffff-fffffffffff1"
})
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});
2 changes: 1 addition & 1 deletion dashboard/controller/assignments/getAllDeliveries.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = function getAllDeliveries(req, res) {
iconMap[activities[i].id] = '/' + activities[i].directory + '/' + activities[i].icon;
}
superagent
.get(common.getAPIUrl(req) + 'api/v1/assignments/deliveries/' + req.params.assignmentId)
.get(common.getAPIUrl(req) + 'api/v1/assignments/deliveries?assignmentId=' + req.params.assignmentId)
.set(common.getHeaders(req))
.query(query)
.end(function (error, response) {
Expand Down
19 changes: 19 additions & 0 deletions dashboard/controller/dashboard/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,22 @@ exports.getAllClassrooms = function(req, res, callback) {
}
});
};

exports.getAllAssignments = function(req,res,callback){
//call
var query = req.query || {};
superagent
.get(common.getAPIUrl(req)+'api/v1/assignments/deliveries')
.set(common.getHeaders(req))
.query(query)
.end(function (error,response){
if(response.statusCode == 200) {
//callback
callback(response.body);
} else {
req.flash('errors',{
msg: common.l10n.get('ErrorCode'+response.body.code)
});
}
});
};
6 changes: 5 additions & 1 deletion dashboard/controller/graph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var _util = require('./util'),
getRecentTeachers = _util.getRecentTeachers,
getRecentAdmins = _util.getRecentAdmins,
getRecentActivities = _util.getRecentActivities,
averageEntries = _util.averageEntries;
averageEntries = _util.averageEntries,
getRecentAssignmentsDeliveries = _util.getRecentAssignmentsDeliveries;

exports.getGraph = function(req, res) {

Expand All @@ -28,6 +29,9 @@ exports.getGraph = function(req, res) {
if (req.query.type == 'recent-activities') {
getRecentActivities(req, res);
}
if (req.query.type == 'recent-deliveries') {
getRecentAssignmentsDeliveries(req, res);
}
};


Expand Down
Loading