Skip to content

Commit

Permalink
dev: create report endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nekopudding committed Jul 3, 2022
1 parent 0224b45 commit ba174be
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
4 changes: 2 additions & 2 deletions server/src/main_modules/accounts/account-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ module.exports = {
* @param {*} callback
*/
updateProfile:(id,data,callback) => {
const {firstname,lastname,email,age,profession,hasAccountant} = data;
const {avatar,firstname,lastname,email,age,profession,hasAccountant} = data;

const fieldsToUpdate = parseProfileData({firstname,lastname,email,age,profession,hasAccountant})
const fieldsToUpdate = parseProfileData({avatar,firstname,lastname,email,age,profession,hasAccountant})
Account.findByIdAndUpdate(id,{$set: fieldsToUpdate},
{returnDocument: 'after'},
(err,foundAccount) => {
Expand Down
6 changes: 4 additions & 2 deletions server/src/main_modules/accounts/profile/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { getDefinedFields } = require('../../../utils/get-defined-fields');
const {r_string,r_bool,r_num, r_date} = require.main.require('./utils/types/mongo-required')

const profileSchema = new mongoose.Schema({
avatar: String,
firstname: r_string,
lastname: r_string,
email: r_string,
Expand All @@ -18,10 +19,11 @@ const profileSchema = new mongoose.Schema({
* @param {object} fields - some or all of the fields in the profile schema
*/
function parseProfileData(fields) {
const {firstname,lastname,email,age,profession,hasAccountant} = fields;
const df = getDefinedFields({firstname,lastname,email,age,profession,hasAccountant});
const {avatar,firstname,lastname,email,age,profession,hasAccountant} = fields;
const df = getDefinedFields({avatar,firstname,lastname,email,age,profession,hasAccountant});

const fieldsToUpdate = {
...(df.avatar && {"profile.avatar": df.avatar}),
...(df.firstname && {"profile.firstname": df.firstname}),
...(df.lastname && {"profile.lastname": df.lastname}),
...(df.email && {"profile.email": df.email}),
Expand Down
2 changes: 1 addition & 1 deletion server/src/main_modules/goals/goal-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
)
},
deleteGoals: (accountId,callback) => {
UserGoal.deleteOne({userId: accountId}, (err) => {
UserGoal.findOneAndUpdate({userId: accountId}, {goals: []},(err) => {
if (err) console.log(err);
callback(err);
})
Expand Down
52 changes: 42 additions & 10 deletions server/src/main_modules/reports/report-routes.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
const { authenticate } = require("../accounts/account-auth");
const { UserReport } = require("./models");

module.exports = function(app) {
app.route('/reports/:userId')
.get((req,res) => {
res.send(req.params);
})
.post((req,res) => {
res.send(req.params);
.get((req,res,next) => {
authenticate(req.query.token, req.params.userId, (err,foundUser) => {
if (err || !foundUser) {
authenticate(req.query.token, req.query.accountantId, (err,foundUser) => {
if (err || !foundUser)
return next(err);
})
}
UserReport.findOne({userId: req.params.userId}, (err,userReport) => {
if (err || !userReport) return next(err);
return res.send(userReport);
})
})
})
.delete((req,res) => {
res.send(req.params);
.delete((req,res,next) => {
authenticate(req.query.token, userId, (err,foundUser) => {
if (err || !foundUser) return next(err);
UserReport.findOneAndUpdate({userId: req.params.userId}, {reports: []}, (err,userReport) => {
if (err || !userReport) return next(err);
res.send(userReport);
})
})

})

app.route('/reports/:userId/reportId')
app.route('/reports/:userId/:reportId')
.get((req,res) => {
res.send(req.params);
authenticate(req.query.token, req.query.accountantId, (err,foundUser) => {
if (err || !foundUser) return next(err);
UserReport.findOne({userId: req.params.userId}, (err,userReport) => {
if (err || !userReport) return next(err);
const report = userReport.reports.filter(r => r.id === req.params.reportId)[0]
return res.send(report);
})
})
})
.put((req,res) => {
res.send(req.params);
authenticate(req.query.token, req.query.accountantId, (err,foundUser) => {
if (err || !foundUser) return next(err);
UserReport.findOne({userId: req.params.userId}, (err,userReport) => {
if (err || !userReport) return next(err);
const report = userReport.reports.filter(r => r.id === req.params.reportId)[0]
return res.send(report);
})
})
})
.delete((req,res) => {
res.send(req.params);
Expand Down
6 changes: 6 additions & 0 deletions server/src/utils/cron/cron-ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//uses node-cron to schedule creation of monthly reports
const cron = require('node-cron');

cron.schedule('0 0 1 * *', function() {
console.log('running a task every minute');
});

0 comments on commit ba174be

Please sign in to comment.