Skip to content

Commit 72c6816

Browse files
committed
Added lesson 2
1 parent db453af commit 72c6816

File tree

13 files changed

+1972
-0
lines changed

13 files changed

+1972
-0
lines changed

lesson_02/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
logs

lesson_02/UserStories.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# User Stories for techNotes
2+
3+
1. [ ] Replace current sticky note system
4+
2. [ ] Add a public facing page with basic contact info
5+
3. [ ] Add an employee login to the notes app
6+
4. [ ] Provide a welcome page after login
7+
5. [ ] Provide easy navigation
8+
6. [ ] Display current user and assigned role
9+
7. [ ] Provide a logout option
10+
8. [ ] Require users to login at least once per week
11+
9. [ ] Provide a way to remove employee access asap if needed
12+
10. [ ] Notes are assigned to specific employees
13+
11. [ ] Notes have a ticket #, title, note body, created & updated dates
14+
12. [ ] Notes are either OPEN or COMPLETED
15+
13. [ ] Users can be Employees, Managers, or Admins
16+
14. [ ] Notes can only be deleted by Managers or Admins
17+
15. [ ] Anyone can create a note (when customer checks-in)
18+
16. [ ] Employees can only view and edit their assigned notes
19+
17. [ ] Managers and Admins can view, edit, and delete all notes
20+
18. [ ] Only Managers and Admins can access User Settings
21+
19. [ ] Only Managers and Admins can create new users
22+
20. [ ] Desktop mode is most important but should be available in mobile

lesson_02/config/allowedOrigins.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const allowedOrigins = [
2+
'http://localhost:3000',
3+
'https://www.dandrepairshop.com',
4+
'https://dandrepairshop.com'
5+
]
6+
7+
module.exports = allowedOrigins

lesson_02/config/corsOptions.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const allowedOrigins = require('./allowedOrigins')
2+
3+
const corsOptions = {
4+
origin: (origin, callback) => {
5+
if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
6+
callback(null, true)
7+
} else {
8+
callback(new Error('Not allowed by CORS'))
9+
}
10+
},
11+
credentials: true,
12+
optionsSuccessStatus: 200
13+
}
14+
15+
module.exports = corsOptions

lesson_02/middleware/errorHandler.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { logEvents } = require('./logger')
2+
3+
const errorHandler = (err, req, res, next) => {
4+
logEvents(`${err.name}: ${err.message}\t${req.method}\t${req.url}\t${req.headers.origin}`, 'errLog.log')
5+
console.log(err.stack)
6+
7+
const status = res.statusCode ? res.statusCode : 500 // server error
8+
9+
res.status(status)
10+
11+
res.json({ message: err.message })
12+
}
13+
14+
module.exports = errorHandler

lesson_02/middleware/logger.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { format } = require('date-fns')
2+
const { v4: uuid } = require('uuid')
3+
const fs = require('fs')
4+
const fsPromises = require('fs').promises
5+
const path = require('path')
6+
7+
const logEvents = async (message, logFileName) => {
8+
const dateTime = format(new Date(), 'yyyyMMdd\tHH:mm:ss')
9+
const logItem = `${dateTime}\t${uuid()}\t${message}\n`
10+
11+
try {
12+
if (!fs.existsSync(path.join(__dirname, '..', 'logs'))) {
13+
await fsPromises.mkdir(path.join(__dirname, '..', 'logs'))
14+
}
15+
await fsPromises.appendFile(path.join(__dirname, '..', 'logs', logFileName), logItem)
16+
} catch (err) {
17+
console.log(err)
18+
}
19+
}
20+
21+
const logger = (req, res, next) => {
22+
logEvents(`${req.method}\t${req.url}\t${req.headers.origin}`, 'reqLog.log')
23+
console.log(`${req.method} ${req.path}`)
24+
next()
25+
}
26+
27+
module.exports = { logEvents, logger }

0 commit comments

Comments
 (0)