-
Notifications
You must be signed in to change notification settings - Fork 18
Audit approach weekly review #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9f5c840
15e999a
03542ea
52a7cf5
8d05e69
0efc654
5ce8175
54c4564
0e32d61
45d557c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 'use strict'; | ||
| module.exports = { | ||
| up: function(queryInterface, Sequelize) { | ||
| return queryInterface.createTable('Audits', { | ||
| id: { | ||
| allowNull: false, | ||
| autoIncrement: true, | ||
| primaryKey: true, | ||
| type: Sequelize.INTEGER | ||
| }, | ||
| table_name: { | ||
| type: Sequelize.STRING | ||
| }, | ||
| field_id: { | ||
| type: Sequelize.STRING | ||
| }, | ||
| field_name: { | ||
| type: Sequelize.STRING | ||
| }, | ||
| old_value: { | ||
| type: Sequelize.STRING | ||
| }, | ||
| new_value: { | ||
| type: Sequelize.STRING | ||
| }, | ||
| field_type: { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably |
||
| type: Sequelize.STRING | ||
| }, | ||
| user_id: { | ||
| type: Sequelize.INTEGER | ||
| }, | ||
| createdAt: { | ||
| allowNull: false, | ||
| type: Sequelize.DATE | ||
| }, | ||
| updatedAt: { | ||
| allowNull: false, | ||
| type: Sequelize.DATE | ||
| } | ||
| }); | ||
| }, | ||
| down: function(queryInterface, Sequelize) { | ||
| return queryInterface.dropTable('Audits'); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const returnAuditOptions = require('../src/Auditor') | ||
|
|
||
| 'use strict'; | ||
| module.exports = function(sequelize, DataTypes) { | ||
| var Audit = sequelize.define('Audit', { | ||
| table_name: DataTypes.STRING, | ||
| element_id: DataTypes.STRING, | ||
| element_name: DataTypes.STRING, | ||
| old_value: DataTypes.STRING, | ||
| new_value: DataTypes.STRING, | ||
| field_type: DataTypes.STRING, | ||
| user_id: DataTypes.INTEGER | ||
| }, { | ||
| classMethods: { | ||
| associate: function(models) { | ||
| // associations can be defined here | ||
| }, | ||
|
|
||
| } | ||
| }); | ||
| return Audit; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,51 @@ | ||
| 'use strict'; | ||
|
|
||
| const VALID_PARAMETERS = [ 'completed', 'title', 'description' ] | ||
| const returnAuditOptions = require('../src/Auditor') | ||
|
|
||
| module.exports = function(sequelize, DataTypes) { | ||
| var Item = sequelize.define('Item', { | ||
| title: DataTypes.STRING, | ||
| description: DataTypes.TEXT, | ||
| completed: DataTypes.BOOLEAN, | ||
| parent_id: DataTypes.INTEGER, | ||
| user_id: DataTypes.INTEGER | ||
| }, { | ||
| classMethods: { | ||
| associate: function(models) { | ||
| // associations can be defined here | ||
| const Audit = sequelize.models.Audit | ||
| const Item = sequelize.define('Item', | ||
| { | ||
| title: DataTypes.STRING, | ||
| description: DataTypes.TEXT, | ||
| completed: DataTypes.BOOLEAN, | ||
| parent_id: DataTypes.INTEGER, | ||
| user_id: DataTypes.INTEGER | ||
| }, | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omit unnecessary whitespace please. |
||
|
|
||
|
|
||
| { | ||
| hooks: { | ||
| afterCreate: function(item, options) { | ||
| let {updateType, data_type} = options | ||
| let auditOptions = returnAuditOptions(updateType, item, data_type) | ||
| Audit.create(auditOptions[0], {success: true}) | ||
| }, | ||
| afterUpdate: function(item, options) { | ||
|
|
||
| let {updateType, data_type} = options | ||
| let auditOptions = returnAuditOptions(updateType,item, data_type) | ||
| Audit.create(auditOptions[0], {success: true}) | ||
| } | ||
| }, | ||
| filterParameters: params => { | ||
| return VALID_PARAMETERS.reduce( (memo, key) => { | ||
| if( params[ key ] !== undefined ) { | ||
| memo[ key ] = params[ key ] | ||
| } | ||
|
|
||
| return memo | ||
| }, {} ) | ||
| classMethods: { | ||
| associate: function(models) { | ||
| // associations can be defined here | ||
| }, | ||
|
|
||
| filterParameters: params => { | ||
| return VALID_PARAMETERS.reduce( (memo, key) => { | ||
| if( params[ key ] !== undefined ) { | ||
| memo[ key ] = params[ key ] | ||
| } | ||
|
|
||
| return memo | ||
| }, {} ) | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| ); | ||
|
|
||
| return Item; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,4 +12,4 @@ module.exports = function(sequelize, DataTypes) { | |
| } | ||
| }); | ||
| return User; | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,10 @@ const params = data => | |
|
|
||
| const checkJsonForSuccessField = json => { | ||
| if( json.success ) { | ||
| console.log('checking for json success'); | ||
| Promise.resolve( json ) | ||
| } else { | ||
| console.log('rejecting for json failure'); | ||
| Promise.reject( json.message ) | ||
| } | ||
| } | ||
|
|
@@ -74,7 +76,6 @@ const completedClicked = event => { | |
| const element = $( event.target ) | ||
| const id = element.data( 'id' ) | ||
| const completed = ! element.data( 'completed' ) | ||
|
|
||
| fetch( `/items/${id}`, params({ completed: completed } ) ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please maintain whitespace between logical blocks - there should be a separation between the setup and logic here. |
||
| .then( result => result.json() ) | ||
| .then( checkJsonForSuccessField ) | ||
|
|
@@ -90,6 +91,8 @@ const completedClicked = event => { | |
| ) | ||
| } | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary whitespace |
||
|
|
||
| $(document).ready( () => { | ||
| $( '.edit-title' ).keypress( titleEdited ) | ||
| $( '.title > span' ).click( clickToUpdate( 'title' )) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ const validateEmail = require( '../src/mail/validate_email' ) | |
|
|
||
|
|
||
| const AUTH_OPTIONS = { | ||
| successRedirect: '/items', | ||
| successRedirect: '/items/weekly', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very surprising change - why does authentication change to a weekly route? |
||
| failureRedirect: '/accounts/login' | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
|
|
||
| const token = require( 'jsonwebtoken' ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove unnecessary whitespace |
||
|
|
||
| const authenticate = user => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| const returnAuditOptions = (updateType, item, data_type) => { | ||
| console.log('returning Audit Options', updateType, data_type); | ||
| const auditOptions = updateType.map(type => { | ||
| console.log('type',type, 'data_type', data_type); | ||
| if(type === 'create'){ | ||
| console.log('in create'); | ||
| let options = { | ||
| table_name: 'Items', | ||
| element_id: item.id, | ||
| element_name: 'row', | ||
| old_value: 'n/a', | ||
| new_value: JSON.stringify(item), | ||
| field_type: data_type, | ||
| user_id: item.user_id | ||
| } | ||
| console.log('>>>>>>',item); | ||
| return options | ||
| } | ||
| if(type === 'completed'){ | ||
| console.log('in completed'); | ||
| let options = { | ||
| table_name: 'Items', | ||
| element_id: item.id, | ||
| element_name: type, | ||
| old_value: ! item.completed, | ||
| new_value: item.completed, | ||
| field_type: data_type, | ||
| user_id: item.user_id | ||
| } | ||
| console.log('>>>>>>',item._previousDataValues.title); | ||
| return options | ||
| } | ||
|
|
||
| if(type === 'title' ){ | ||
| console.log('type: ', item._previousDataValues.title ); | ||
| let options = { | ||
| table_name: 'Items', | ||
| element_id: item.id, | ||
| element_name: type, | ||
| old_value: item._previousDataValues.title, | ||
| new_value: item.title, | ||
| field_type: data_type, | ||
| user_id: item.user_id | ||
| } | ||
| console.log('title: ',options); | ||
| return options | ||
| } | ||
|
|
||
| if(type === 'description'){ | ||
| let options = { | ||
| table_name: 'Items', | ||
| element_id: item.id, | ||
| element_name: 'description', | ||
| old_value: item._previousDataValues.description, | ||
| new_value: item.description, | ||
| field_type: data_type, | ||
| user_id: item.user_id | ||
| } | ||
| console.log('description', options); | ||
| return options | ||
| } | ||
| }) | ||
| console.log('auditOptions',auditOptions); | ||
| return auditOptions | ||
| } | ||
|
|
||
| module.exports = returnAuditOptions |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| const moment = require('moment') | ||
| const today = moment() | ||
| const todaysDate = moment() | ||
| const minusSevenDays = todaysDate.subtract(7, 'days') | ||
| console.log('-7: ', minusSevenDays.format("dddd, MMMM Do YYYY, h:mm:ss a"), '\n'); | ||
| console.log('todaysDate: ', today.format("dddd, MMMM Do YYYY, h:mm:ss a")); | ||
|
|
||
|
|
||
| const bulletsCreated = (auditRecords) => { | ||
| const createdThisWeek = auditRecords.filter(record => record.createdAt >= minusSevenDays) | ||
| return createdThisWeek.length | ||
| // ([all items w/ user_id && createdAt(<= today’s date - 7) ].length) | ||
| } | ||
|
|
||
| const bulletsChanged = (auditRecords) => { | ||
| const changedThisWeek = auditRecords.filter(record => record.updatedAt >= minusSevenDays) | ||
| return changedThisWeek.length | ||
| // ([all items w/ user_id && updatedAt(<= today’s date - 7) ].length) | ||
| } | ||
|
|
||
| const totalBullets = (auditRecords) => { | ||
| const totalBullets = auditRecords.filter(record => { | ||
| return record.element_name === 'row' && record.createdAt >= minusSevenDays | ||
| }) | ||
| return totalBullets.length | ||
| } | ||
|
|
||
| const bulletsCompleted = (auditRecords) => { | ||
| const completedItems = auditRecords.filter(record => { | ||
| return record.element_name === 'completed' && | ||
| record.new_value == true && | ||
| record.createdAt >= minusSevenDays | ||
| }) | ||
| console.log(); | ||
| return completedItems.length | ||
| } | ||
|
|
||
| const calculateStats = (Audit, user_id) => { | ||
| const stats =Audit.findAll({where: {user_id: user_id}}) | ||
| .then(auditRecords => { | ||
| let created = bulletsCreated(auditRecords) | ||
| let changed = bulletsChanged(auditRecords) | ||
| let completed = bulletsCompleted(auditRecords) | ||
| let total = totalBullets(auditRecords) | ||
| console.log('raw Stats: ',created, changed, completed, total); | ||
| const userStats = {created, changed, completed, total} | ||
| console.log('userStats: ', userStats); | ||
| return userStats | ||
| }) | ||
|
|
||
| return stats | ||
| } | ||
| const grabUserStats = (User, user_id, Audit, Item) => { | ||
| const where = {id: user_id} | ||
| const userStats = User.findOne({where}) | ||
| .then(user => { | ||
| return Math.abs(todaysDate.diff(user.createdAt, 'days')) | ||
| console.log('user: ', user); | ||
| }) | ||
| .then(lengthOfUse => { | ||
| console.log('lengthOfUse', lengthOfUse); | ||
| return calculateStats(Audit, user_id) | ||
| }) | ||
|
|
||
| return userStats | ||
| } | ||
|
|
||
| module.exports = grabUserStats |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we were going to rename
field_idandfield_name?