forked from cerbos/demo-node-cerbfinance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.hardcoded.js
155 lines (135 loc) · 4.16 KB
/
app.hardcoded.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const express = require("express");
const { users, expenses } = require("./db");
const app = express();
app.use(express.json());
app.use((req, res, next) => {
const user = users.find((u) => u.id === req.headers["authorization"]);
if (!user) {
return res.status(401).json({ error: "Unauthorized" });
} else {
req.user = user;
next();
}
});
app.get("/", (req, res) => {
res.json({
message: "CerbFinance is running",
});
});
app.get("/expenses/:id", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
// Admins can do everything
if (req.user.roles.includes("ADMIN")) {
return res.json(expense);
} else if (req.user.roles.includes("USER")) {
// Users can see their own expense and who approved it if it has been approved
if (expense.attributes.ownerId === req.user.id) {
if (expense.attributes.status != "APPROVED") {
// delete expense.attributes.approvedBy;
}
return res.json(expense);
}
// Region managers can see expenses from their region
if (
req.user.roles.includes("MANAGER") &&
expense.attributes.region === req.user.attributes.region
) {
return res.json(expense);
}
// Anyone in FINANCE can see expenses
if (req.user.department === "FINANCE") {
return res.json(expense);
}
}
return res.status(401).json({ error: "Unauthorized" });
});
app.patch("/expenses/:id", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
let canPatch = false;
// Admins can do everything
if (req.user.roles.includes("ADMIN")) {
canPatch = true;
} else if (req.user.roles.includes("USER")) {
// Users can only patch their own expense if OPEN
if (
expense.attributes.ownerId === req.user.id &&
expense.attributes.status === "OPEN"
) {
canPatch = true;
}
}
if (canPatch) {
// do the patch here
return res.json(expense);
} else {
return res.status(401).json({ error: "Unauthorized" });
}
});
app.post("/expenses/:id/approve", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
let canApprove = false;
// Admins can do everything
if (req.user.roles.includes("ADMIN")) {
canApprove = true;
} else if (
req.user.roles.includes("USER") &&
req.user.attributes.department === "FINANCE" &&
expense.attributes.ownerId != req.user.id
) {
// Finance user <$1000
if (expense.attributes.amount < 1000) {
canApprove = true;
} else if (req.user.roles.includes("MANAGER")) {
canApprove = true;
}
}
if (canApprove) {
// do the approve here
return res.json(expense);
} else {
return res.status(401).json({ error: "Unauthorized" });
}
});
app.delete("/expenses/:id", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
let canDelete = false;
// Admins can do everything
if (req.user.roles.includes("ADMIN")) {
canDelete = true;
} else if (req.user.roles.includes("USER")) {
// Finance Managers can delete
if (
req.user.roles.includes("MANAGER") &&
req.user.attributes.department === "FINANCE"
) {
canDelete = true;
}
// Owner can delete IF status is OPEN AND it was created in the last hour
if (
expense.attributes.ownerId === req.user.id &&
expense.attributes.status === "OPEN"
) {
const createdAt = new Date(expense.attributes.createdAt);
const now = new Date();
const diff = now.getTime() - createdAt.getTime();
if (diff < 3600000) {
canDelete = true;
}
}
}
if (canDelete) {
// do the deletion here
return res.json({
message: "expense deleted",
});
} else {
return res.status(401).json({ error: "Unauthorized" });
}
});
app.listen(8000, () => {
console.log("Server is running on port 8000");
});