Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fahrtenbuch
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWP WS21_22 - Fahrtenbuch
Team Einhorn
fahrtenbuch
Commits
74d2b7c3
Commit
74d2b7c3
authored
3 years ago
by
alrwasheda
Browse files
Options
Downloads
Patches
Plain Diff
some fixes in accounts route+controller
parent
5e44574f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/src/controllers/accounts.controllers.ts
+16
-19
16 additions, 19 deletions
server/src/controllers/accounts.controllers.ts
server/src/routes/accounts.routes.ts
+2
-2
2 additions, 2 deletions
server/src/routes/accounts.routes.ts
with
18 additions
and
21 deletions
server/src/controllers/accounts.controllers.ts
+
16
−
19
View file @
74d2b7c3
...
...
@@ -4,46 +4,49 @@ import Employee from "../db/models/Employee";
//create new account
const
createAccountController
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
//check if user is coordinator
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
const
{
first_name
,
last_name
,
email
,
password
,
role
}
=
req
.
body
;
//get input from req.body
const
input
=
req
.
body
;
//get all accounts with same given email
const
existedAccount
=
await
Employee
.
findAll
({
where
:
{
email
:
email
,
email
:
input
.
email
,
},
});
//if account with same given email was found
if
(
existedAccount
.
length
>
0
)
{
return
res
.
status
(
409
)
.
json
({
success
:
false
,
error
:
"
AccountAlreadyExists
"
});
}
const
hashedPassword
=
await
bcrypt
.
hash
(
password
,
10
);
//encrypt given password
input
.
password
=
await
bcrypt
.
hash
(
input
.
password
,
10
);
const
newAccount
=
await
Employee
.
create
({
email
,
first_name
:
first_name
,
last_name
:
last_name
,
password
:
hashedPassword
,
role
,
});
//creating new Account
const
newAccount
=
await
Employee
.
create
(
input
);
//returning result
return
res
.
status
(
201
).
send
({
success
:
true
,
account
:
{
id
:
newAccount
.
id
,
first_name
:
newAccount
.
first_name
,
last_name
:
newAccount
.
last_name
,
email
:
newAccount
.
email
,
role
:
newAccount
.
role
,
},
});
}
catch
{
}
catch
(
error
)
{
console
.
error
(
error
.
message
);
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
...
...
@@ -105,7 +108,6 @@ const showAccountById = async (req: Request, res: Response) => {
//update account by id
const
updateAccount
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
//input needs validation: handleValidationResult!?
//by trying to update email, duplicates could be found which leads to server-error: must be handled seperately?
try
{
...
...
@@ -120,14 +122,9 @@ const updateAccount = async (req: Request, res: Response) => {
const
givenId
=
req
.
params
.
id
;
//check if given ID exists in DB
const
checkIfIdExists
=
await
Employee
.
findOne
({
attributes
:
[
"
id
"
],
where
:
{
id
:
givenId
,
},
});
const
foundEmployee
=
await
Employee
.
findByPk
(
givenId
);
if
(
checkIfIdExists
===
null
)
{
if
(
foundEmployee
===
null
)
{
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
accountNotFound
"
});
}
...
...
This diff is collapsed.
Click to expand it.
server/src/routes/accounts.routes.ts
+
2
−
2
View file @
74d2b7c3
...
...
@@ -13,7 +13,7 @@ accountsRouter.post(
body
(
"
last_name
"
).
not
().
isEmpty
(),
body
(
"
email
"
).
isEmail
().
normalizeEmail
(),
body
(
"
role
"
).
isIn
([
"
coordinator
"
,
"
boatManager
"
]),
body
(
"
password
"
).
isLength
({
min
:
6
}),
body
(
"
password
"
).
isLength
({
min
:
6
})
.
isString
()
,
handleValidationResult
,
validateToken
,
accountsControllers
.
createAccountController
...
...
@@ -37,7 +37,7 @@ accountsRouter.patch(
body
(
"
last_name
"
).
if
(
body
(
"
last_name
"
).
exists
()).
not
().
isEmpty
(),
body
(
"
email
"
).
if
(
body
(
"
email
"
).
exists
()).
isEmail
().
normalizeEmail
(),
body
(
"
role
"
).
if
(
body
(
"
role
"
).
exists
()).
isIn
([
"
coordinator
"
,
"
boatManager
"
]),
body
(
"
password
"
).
if
(
body
(
"
password
"
).
exists
()).
isLength
({
min
:
6
}),
body
(
"
password
"
).
if
(
body
(
"
password
"
).
exists
()).
isLength
({
min
:
6
})
.
isString
()
,
handleValidationResult
,
validateToken
,
accountsControllers
.
updateAccount
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment