From ba174bec07c4ff0a71bfb1714c4cee74b94d2d4f Mon Sep 17 00:00:00 2001 From: nekopudding <61680928+nekopudding@users.noreply.github.com> Date: Thu, 30 Jun 2022 19:11:29 -0700 Subject: [PATCH] dev: create report endpoints --- .../main_modules/accounts/account-store.js | 4 +- .../main_modules/accounts/profile/profile.js | 6 ++- server/src/main_modules/goals/goal-store.js | 2 +- .../src/main_modules/reports/report-routes.js | 52 +++++++++++++++---- server/src/utils/cron/cron-ping.js | 6 +++ 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 server/src/utils/cron/cron-ping.js diff --git a/server/src/main_modules/accounts/account-store.js b/server/src/main_modules/accounts/account-store.js index 2201a48..a027e7d 100644 --- a/server/src/main_modules/accounts/account-store.js +++ b/server/src/main_modules/accounts/account-store.js @@ -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) => { diff --git a/server/src/main_modules/accounts/profile/profile.js b/server/src/main_modules/accounts/profile/profile.js index f54c53d..1b8b006 100644 --- a/server/src/main_modules/accounts/profile/profile.js +++ b/server/src/main_modules/accounts/profile/profile.js @@ -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, @@ -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}), diff --git a/server/src/main_modules/goals/goal-store.js b/server/src/main_modules/goals/goal-store.js index 8334e54..8b506b9 100644 --- a/server/src/main_modules/goals/goal-store.js +++ b/server/src/main_modules/goals/goal-store.js @@ -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); }) diff --git a/server/src/main_modules/reports/report-routes.js b/server/src/main_modules/reports/report-routes.js index 68dcca0..88dbb3b 100644 --- a/server/src/main_modules/reports/report-routes.js +++ b/server/src/main_modules/reports/report-routes.js @@ -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); diff --git a/server/src/utils/cron/cron-ping.js b/server/src/utils/cron/cron-ping.js new file mode 100644 index 0000000..9411036 --- /dev/null +++ b/server/src/utils/cron/cron-ping.js @@ -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'); +}); \ No newline at end of file