Skip to content

Commit 748be2b

Browse files
Half way sprint two
2 parents cc68ab8 + f78f92b commit 748be2b

42 files changed

Lines changed: 2765 additions & 233 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 149 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,121 @@
1+
const formidable = require("formidable");
2+
const _ = require("lodash");
3+
const fs = require("fs");
14
const StudentPayment = require('../../Modules/Accountant/module.student.payment');
25

36
/**
4-
* Create Student Payment controller
7+
* Get a specific Student payment controller
58
* @param req
69
* @param res
10+
* @param next
11+
* @param id
712
* @returns {Promise<any>}
813
*/
9-
const createStudentPayment = async (req, res) => {
10-
if(req.body) {
11-
const studentPayment = new StudentPayment(req.body);
12-
await studentPayment.save()
13-
.then(data=>{
14-
res.status(200).send({data: data});
15-
})
16-
.catch(error =>{
17-
res.status(500).send({error: error.message});
14+
const studentPaymentById = (req, res, next, id) => {
15+
StudentPayment.findById(id).exec((err,studentPayment) => {
16+
if (err || !studentPayment) {
17+
return res.status(400).json({
18+
error: "Payment not found"
1819
});
19-
}
20-
}
20+
}
21+
req.studentPayment = studentPayment;
22+
next();
23+
});
24+
};
25+
26+
// get the payment details except photo
27+
const read = (req, res) => {
28+
req.studentPayment.paymentSlip = undefined;
29+
return res.json(req.studentPayment);
30+
};
2131

2232
/**
23-
* Get all Student Payments controller
33+
* Create Student Payment controller
2434
* @param req
2535
* @param res
2636
* @returns {Promise<any>}
2737
*/
28-
const getAllStudentPayment = async (req, res) => {
29-
await StudentPayment.find({})
30-
.then(data=>{
31-
res.status(200).send({data: data});
32-
})
33-
.catch(error =>{
34-
res.status(500).send({error: error.message});
38+
const createStudentPayment = (req, res) => {
39+
let form = new formidable.IncomingForm()
40+
form.keepExtensions = true
41+
form.parse(req, (err, fields, files) => {
42+
if(err) {
43+
return res.status(400).json({
44+
error: "Image could not be uploaded"
45+
});
46+
}
47+
let studentPayment = new StudentPayment(fields)
48+
49+
const {name,contactNo, studentID,depositedAmount,depositedDate,bank,branch,type,classes,teacher} = fields
50+
51+
// validate fields
52+
if(!name || !contactNo|| !studentID|| !depositedAmount|| !depositedDate || !bank || !branch || !type || !classes || !teacher) {
53+
return res.status(400).json({
54+
error: "All fields are required"
55+
});
56+
}
57+
58+
// validate photo size
59+
if(files.paymentSlip) {
60+
if(files.paymentSlip.size > 10000000) {
61+
return res.status(400).json({
62+
error: "Image should be less than 10mb in size"
63+
});
64+
}
65+
studentPayment.paymentSlip.data = fs.readFileSync(files.paymentSlip.path)
66+
studentPayment.paymentSlip.contentType = files.paymentSlip.type
67+
}
68+
69+
studentPayment.save((err, result) => {
70+
if(err) {
71+
return res.status(400).json({
72+
error: 'Error Found'
73+
});
74+
}
75+
res.json(result);
3576
});
77+
});
3678
}
3779

3880
/**
39-
* Get a specific Student payment controller
81+
* Get all Student Payments controller
4082
* @param req
4183
* @param res
4284
* @returns {Promise<any>}
4385
*/
44-
const viewStudentPaymentById = async (req, res) => {
45-
if (req.params && req.params.id) {
46-
await StudentPayment.findById(req.params.id)
47-
.populate('studentpayments', '_id name contactNo studentID depositedAmount depositedDate bank branch paymentSlip status type class teacher')
48-
.then(response => {
49-
res.status(200).send({ data: response });
50-
})
51-
.catch(error => {
52-
res.status(500).send({ error: error.message });
53-
});
54-
}
86+
const getAllStudentPayment = (req, res) => {
87+
let order = req.query.order ? req.query.order : 'asc'
88+
let sortBy = req.query.sortBy ? req.query.sortBy : '_id'
89+
90+
StudentPayment.find()
91+
.select("-paymentSlip")
92+
.sort([[sortBy, order]])
93+
.exec((err, studentPayments) => {
94+
if(err) {
95+
return res.status(400).json ({
96+
error: 'No Student Payment Found'
97+
});
98+
}
99+
res.json(studentPayments);
100+
});
55101
}
56102

103+
// get the photo of conference
104+
const photo = (req, res, next) => {
105+
if(req.studentPayment.paymentSlip.data) {
106+
res.set('Content-Type', req.studentPayment.paymentSlip.contentType)
107+
return res.send(req.studentPayment.paymentSlip.data);
108+
}
109+
next();
110+
};
111+
57112
/**
58-
* Update the status of student payment controller
113+
* Update only the status of student payment controller
59114
* @param req
60115
* @param res
61116
* @returns {Promise<any>}
62117
*/
63-
const updateById = async (req, res) => {
118+
const updateStatus = async (req, res) => {
64119
const id = req.params.id;
65120
const {status} = req.body;
66121
const updateStudentPayment = {
@@ -75,17 +130,68 @@ const updateById = async (req, res) => {
75130
})
76131
}
77132

133+
/**
134+
* Update the student payment controller
135+
* @param req
136+
* @param res
137+
* @returns {Promise<any>}
138+
*/
139+
const updateById = async(req, res) => {
140+
let form = new formidable.IncomingForm()
141+
form.keepExtensions = true
142+
form.parse(req, (err, fields, files) => {
143+
if(err) {
144+
return res.status(400).json({
145+
error: "Image could not be uploaded"
146+
});
147+
}
148+
149+
const {name,contactNo, studentID,depositedAmount,depositedDate,bank,branch,type,classes,teacher} = fields
150+
151+
let studentPayment = req.studentPayment;
152+
studentPayment = _.extend(studentPayment,fields)
153+
154+
if(files.paymentSlip) {
155+
if(files.paymentSlip.size > 10000000) {
156+
return res.status(400).json({
157+
error: "Image should be less than 10mb in size"
158+
});
159+
}
160+
studentPayment.paymentSlip.data = fs.readFileSync(files.paymentSlip.path)
161+
studentPayment.paymentSlip.contentType = files.paymentSlip.type
162+
}
163+
164+
studentPayment.save((err, result) => {
165+
if(err) {
166+
return res.status(400).json({
167+
error: "Update Failed"
168+
});
169+
}
170+
res.json(result);
171+
});
172+
});
173+
};
174+
78175
/**
79176
* Delete student Payment controller
80177
* @param req
81178
* @param res
82179
* @returns {Promise<any>}
83180
*/
84-
const deleteById = async (req, res) => {
85-
const id = req.params.id
86-
await StudentPayment.findByIdAndRemove(id).exec()
87-
res.send('itemDeleted');
88-
}
181+
const deleteById = (req, res) => {
182+
let studentPayment = req.studentPayment
183+
studentPayment.remove((err, deletedPayment) => {
184+
if(err) {
185+
return res.status(400).json({
186+
error: "Error On Delete"
187+
});
188+
}
189+
res.json({
190+
deletedPayment,
191+
message: "Payment Deleted successfully"
192+
});
193+
});
194+
};
89195

90196
/**
91197
* export controllers
@@ -96,9 +202,12 @@ const deleteById = async (req, res) => {
96202
* deleteById: deleteById}}
97203
*/
98204
module.exports = {
205+
studentPaymentById,
206+
read,
99207
createStudentPayment,
100208
getAllStudentPayment,
101-
viewStudentPaymentById,
209+
photo,
210+
updateStatus,
102211
updateById,
103212
deleteById
104213
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const Material = require('../../Modules/Teacher/module.material');
2+
3+
const createMaterial = async (req, res) => {
4+
if(req.body) {
5+
const material = new Material(req.body);
6+
await material.save()
7+
.then(data=>{
8+
res.status(200).send({data: data});
9+
})
10+
.catch(error =>{
11+
res.status(500).send({error: error.message});
12+
});
13+
}
14+
}
15+
16+
const getAllMaterials = async (req, res) => {
17+
await Material.find({})
18+
.then(data=>{
19+
res.status(200).send({data: data});
20+
})
21+
.catch(error =>{
22+
res.status(500).send({error: error.message});
23+
});
24+
}
25+
26+
const viewMaterialById = async (req, res) => {
27+
if (req.params && req.params.id) {
28+
await Material.findById(req.params.id)
29+
.populate('material', '_id subjectName subjectCode lesson description')
30+
.then(response => {
31+
res.status(200).send({ data: response });
32+
})
33+
.catch(error => {
34+
res.status(500).send({ error: error.message });
35+
});
36+
}
37+
}
38+
39+
40+
const updateById = async (req, res) => {
41+
const id = req.params.id;
42+
const {status} = req.body;
43+
const updateMaterial = {
44+
status
45+
}
46+
const update = await Material.findByIdAndUpdate(id, updateMaterial)
47+
.then(() => {
48+
res.status(200).send({status: "Teacher Registration Updated"})
49+
}).catch((err) => {
50+
console.log(err);
51+
res.status(500).send({status: " Error", error:err.message});
52+
})
53+
}
54+
55+
const deleteById = async (req, res) => {
56+
const id = req.params.id
57+
await Material.findByIdAndRemove(id).exec()
58+
res.send('Teacher Registration Declined');
59+
}
60+
61+
module.exports = {
62+
createMaterial,
63+
getAllMaterials,
64+
viewMaterialById,
65+
updateById,
66+
deleteById
67+
}

0 commit comments

Comments
 (0)