11const sequelize = require ( 'sequelize' ) ;
22const User = require ( '../models/user' ) ;
3+ const Expense = require ( "../models/expense" ) ;
4+ const Premium = require ( '../models/premium' ) ;
35const path = require ( 'path' ) ;
46const jwt = require ( "jsonwebtoken" ) ;
7+ const Razorpay = require ( "razorpay" ) ;
58
69module . exports . signUp = async ( req , res ) => {
710 try {
@@ -50,7 +53,7 @@ module.exports.signIn = async function (req, res) {
5053
5154 else if ( user && user . password == password ) {
5255 const token = generateAccessToken ( user . id , user . email ) ;
53- res . cookie ( "jwt_token" , token ) ;
56+ res . cookie ( "jwt_token" , token ) ; // , { maxAge: 1000, httpOnly: true } = for set the cookie expire time
5457 res . status ( 200 ) . send ( `<script> window.location.href='/user_dashboard'; </script>` ) ;
5558
5659 }
@@ -67,4 +70,165 @@ module.exports.userDashboard = function (req, res) {
6770 res . sendFile ( path . join ( __dirname , '../public/views/user_dashboard.html' ) ) ;
6871} ;
6972
73+ module . exports . buyPremium = async ( req , res ) => {
74+ try {
75+ var rzp = new Razorpay ( {
76+ key_id : process . env . RAZORPAY_KEY_ID ,
77+ key_secret : process . env . RAZORPAY_KEY_SECRET
78+ } ) ;
79+
80+ //amount should be written in paise
81+ rzp . orders . create ( { amount : 49900 , currency : "INR" } , ( err , order ) => {
82+ if ( err ) {
83+ throw new Error ( JSON . stringify ( err ) ) ;
84+ }
85+ Premium . create ( {
86+ orderid : order . id ,
87+ status : "PENDING" ,
88+ userId : req . user . id
89+ } ) . then ( ( ) => {
90+ return res . status ( 201 ) . json ( { order, key_id : rzp . key_id } ) ;
91+ } ) . catch ( ( err ) => {
92+ throw new Error ( err ) ;
93+ } ) ;
94+
95+ // this code is same as upper for create in order table here req.user belong to a row create is a function of sequlize and premium is a table.
96+ // this below code is the easier way to write the same code as above
97+
98+ // req.user
99+ // .createPremium({ orderid: order.id, status: "PENDING" })
100+ // .then(() => {
101+ // return res.status(201).json({ order, key_id: rzp.key_id });
102+ // })
103+ // .catch((err) => {
104+ // throw new Error(err);
105+ // });
106+ } ) ;
107+ } catch ( err ) {
108+ res . status ( 403 ) . json ( { message : "Something went wrong" , error : err } ) ;
109+ }
110+ } ;
111+
112+ module . exports . updateTransactionStatus = async ( req , res ) => {
113+ try {
114+ const { payment_id, order_id } = req . body ;
115+ const premium = await Premium . findOne ( { where : { orderid : order_id } } ) ;
116+ premium . update ( {
117+ paymentid : payment_id ,
118+ status : "SUCCESSFUL" ,
119+ } ) . then ( ( ) => {
120+ return res . status ( 202 ) . json ( {
121+ sucess : true ,
122+ message : "Transaction Successful" ,
123+ } ) ;
124+ } ) . catch ( ( error ) => {
125+ throw new Error ( error ) ;
126+ } ) ;
127+
128+ req . user . update ( { isPremiumUser : true } ) ;
129+
130+ // Promise.all([promise1, promise2])
131+ // .then(() => {
132+ // return res.status(202).json({
133+ // sucess: true,
134+ // message: "Transaction Successful",
135+ // // token: userController.generateAccessToken(userId, undefined, true),
136+ // });
137+ // })
138+ // .catch((error) => {
139+ // throw new Error(error);
140+ // });
141+ } catch ( err ) {
142+ console . log ( err ) ;
143+ res . status ( 403 ) . json ( { error : err , message : "Sometghing went wrong" } ) ;
144+ }
145+ } ;
146+
147+ module . exports . isPremiumUser = async ( req , res ) => {
148+ try {
149+ if ( req . user . isPremiumUser ) {
150+ return res . json ( { isPremiumUser : true } ) ;
151+ }
152+ } catch ( err ) {
153+ console . log ( err ) ;
154+ }
155+ } ;
156+
157+ module . exports . getLeaderboardPage = async ( req , res ) => {
158+ try {
159+ res . sendFile ( path . join ( __dirname , '../public/views/leaderboard.html' ) ) ;
160+ } catch {
161+ ( err ) => console . log ( err ) ;
162+ }
163+ } ;
164+
165+ module . exports . getLeaderboardUser = async ( req , res ) => {
166+ try {
167+ const users = await User . findAll ( ) ;
168+ res . json ( users ) ; // send the date where api call (This is is API or controller for get expense data)
169+ } catch ( err ) {
170+ console . log ( err ) ;
171+ }
172+ } ;
173+
174+ //send sorted data from backend - pending wqtch sir video
175+
176+ // exports.getLeaderboardUser = (req, res, next) => {
177+ // Expense.findAll({
178+ // attributes: [
179+ // [sequelize.fn("sum", sequelize.col("amount")), "totalExpense"],
180+ // [sequelize.col("user.name"), "name"],
181+ // ],
182+ // group: ["userId"],
183+ // include: [
184+ // {
185+ // model: User,
186+ // attributes: [],
187+ // },
188+ // ],
189+ // order: [[sequelize.fn("sum", sequelize.col("amount")), "DESC"]],
190+ // })
191+ // .then((expenses) => {
192+ // const result = expenses.map((expense) => ({
193+ // name: expense.getDataValue("name"),
194+ // amount: expense.getDataValue("totalExpense"),
195+ // }));
196+ // res.send(JSON.stringify(result));
197+ // })
198+ // .catch((err) => console.log(err));
199+ // };
200+
201+ module . exports . getReportsPage = ( req , res ) => {
202+ res . sendFile ( path . join ( __dirname , '../public/views/reports.html' ) ) ;
203+ } ;
70204
205+ module . exports . dailyReports = async ( req , res ) => {
206+ try {
207+ const date = req . body . date ;
208+ const expenses = await Expense . findAll ( {
209+ where : { date : date , userId : req . user . id } ,
210+ } ) ;
211+ return res . send ( expenses ) ;
212+ } catch ( error ) {
213+ console . log ( error ) ;
214+ }
215+ } ;
216+
217+ module . exports . monthlyReports = async ( req , res ) => {
218+ try {
219+ const month = req . body . month ;
220+ const expenses = await Expense . findAll ( {
221+ where : {
222+ date : {
223+ [ sequelize . like ] : `%-${ month } -%` ,
224+ } ,
225+ userId : req . user . id ,
226+ } ,
227+ raw : true ,
228+ } ) ;
229+
230+ return res . send ( expenses ) ;
231+ } catch ( error ) {
232+ console . log ( error ) ;
233+ }
234+ } ;
0 commit comments