1+ const formidable = require ( "formidable" ) ;
2+ const _ = require ( "lodash" ) ;
3+ const fs = require ( "fs" ) ;
4+ const StudentPayment = require ( '../../Modules/Class/module.class' ) ;
5+
6+ /**
7+ * Get a specific Student payment controller
8+ * @param req
9+ * @param res
10+ * @param next
11+ * @param id
12+ * @returns {Promise<any> }
13+ */
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"
19+ } ) ;
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+ } ;
31+
32+ /**
33+ * Create Student Payment controller
34+ * @param req
35+ * @param res
36+ * @returns {Promise<any> }
37+ */
38+ const createTeacherTask = ( 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 ) ;
76+ } ) ;
77+ } ) ;
78+ }
79+
80+ /**
81+ * Get all Student Payments controller
82+ * @param req
83+ * @param res
84+ * @returns {Promise<any> }
85+ */
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+ } ) ;
101+ }
102+
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+
112+ /**
113+ * Update only the status of student payment controller
114+ * @param req
115+ * @param res
116+ * @returns {Promise<any> }
117+ */
118+ const updateStatus = async ( req , res ) => {
119+ const id = req . params . id ;
120+ const { status} = req . body ;
121+ const updateStudentPayment = {
122+ status
123+ }
124+ const update = await StudentPayment . findByIdAndUpdate ( id , updateStudentPayment )
125+ . then ( ( ) => {
126+ res . status ( 200 ) . send ( { status : "Status Updated" } )
127+ } ) . catch ( ( err ) => {
128+ console . log ( err ) ;
129+ res . status ( 500 ) . send ( { status : " Error" , error :err . message } ) ;
130+ } )
131+ }
132+
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+
175+ /**
176+ * Delete student Payment controller
177+ * @param req
178+ * @param res
179+ * @returns {Promise<any> }
180+ */
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+ } ;
195+
196+ /**
197+ * Calculate total Fee controller
198+ * @param req
199+ * @param res
200+ * @returns {Promise<any> }
201+ */
202+ const calculateAmount = async ( req , res ) => {
203+
204+ }
205+
206+ /**
207+ * export controllers
208+ * @type {{createStudentPayment: createStudentPayment,
209+ * getAllStudentPayment: getAllStudentPayment,
210+ * viewStudentPaymentById: viewStudentPaymentById,
211+ * updateById: updateById,
212+ * deleteById: deleteById,
213+ * calculateAmount: calculateAmount}}
214+ */
215+ module . exports = {
216+ studentPaymentById,
217+ read,
218+ createStudentPayment,
219+ getAllStudentPayment,
220+ photo,
221+ updateStatus,
222+ updateById,
223+ deleteById,
224+ calculateAmount
225+ }
0 commit comments