-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomerRoutes.js
More file actions
39 lines (33 loc) · 1.02 KB
/
customerRoutes.js
File metadata and controls
39 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const express = require('express');
const db = require('./database/dbHelpers');
const router = express.Router();
// root is '/api/customer'
router.get('/', async (req, res) => {
try {
const workers = await db.getWorkers();
res.json(workers);
} catch (err) {
res.status(500).json({ error: 'an error has occured' });
}
});
// endpoint for a customer to send a tip to a specific worker
router.post('/worker/:id', async (req, res, next) => {
// need worker id to query the DB
const { id } = req.params;
// need tip amount
const { tip } = req.body;
if (!tip) {
return res.send('Please enter a tip amount');
}
if (tip < 0) {
return res.send('Valid tips need to be at least $0.01');
}
// query DB to get specific workors totalTips, then sum up the current total with the amount that the customer is sending
try {
const sendTip = await db.sendTipToWorker(id, tip);
res.send(sendTip);
} catch (err) {
res.status(500).json({ error: 'an error has occured' });
}
});
module.exports = router;