Skip to content

Commit

Permalink
Merged in enrollByBatchCode (pull request coding-blocks#43)
Browse files Browse the repository at this point in the history
EnrollByBatchCode
  • Loading branch information
sd5869 authored and Varun Hasija committed Feb 20, 2018
2 parents 166196c + 809cb93 commit 3a27989
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ router.get('/:user_id', function (req, res, next) {
router.patch('/:id', UserPolicy.PATCHPolicy, (req,res)=>{
const user = jsonapi.deserialize('user',req.body);
UserService.updateUser(user).then(result=>{
UserService.addToContest(user.b_code,res.locals.user.id)
res.sendStatus(204);
}).catch(function (err) {
jsonapi.error(res, 500, err);
Expand Down
20 changes: 20 additions & 0 deletions services/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const User = require('../models/user');
const UserRole = require('../models/user_role');
const Problem = require('../models/problem');
const Submission = require('../models/submission');
const CStudent = require('../models/student_contest');
const Contest = require('../models/contest');
const Raven = require ('raven');

const getUser = function (userId, isMe, isChatModule) {
if (isMe) {
Expand Down Expand Up @@ -84,6 +87,23 @@ const updateUser = function (user) {
});
};

const addToContest = function (b_code,userId) {
return Contest.findAll({
where: {batch_code:b_code},
}).then(result => {
let cstudents = []
result.forEach((contest)=>{
cstudents.push({created_by:userId,user_id:userId,contest_id:contest.id});
});
return cstudents;
}).then(cstudents => {
return CStudent.bulkCreate(cstudents, {returning: true});
}).catch(err=>{
Raven.captureException(err);
});
};

exports.addToContest = addToContest;
exports.getUser = getUser;
exports.getUsers = getUsers;
exports.updateUser = updateUser;
Expand Down

0 comments on commit 3a27989

Please sign in to comment.