Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
planner-team-one
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
21
Issues
21
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Евгений Положенцев
planner-team-one
Commits
3c477cbb
Commit
3c477cbb
authored
Nov 25, 2022
by
Евгений Положенцев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#63
added router/projects/remove-user & add-user & fix projects/my
parent
57423f71
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
5 deletions
+85
-5
members.ts
planner-api/src/routers/members.ts
+8
-1
projects.ts
planner-api/src/routers/projects.ts
+77
-4
No files found.
planner-api/src/routers/members.ts
View file @
3c477cbb
import
{
Member
}
from
"../models/Member"
;
\ No newline at end of file
import
express
,{
Router
,
Request
,
Response
}
from
'express'
;
import
{
myDataSource
}
from
'../app-data-source'
;
import
{
Member
}
from
"../models/Member"
;
const
router
:
Router
=
express
.
Router
();
const
dataSource
=
myDataSource
;
planner-api/src/routers/projects.ts
View file @
3c477cbb
...
...
@@ -26,20 +26,35 @@ router.get('/my',async (req:Request, res:Response): Promise<Response>=> {
if
(
!
user
)
return
res
.
status
(
404
).
send
({
Message
:
'user not found'
})
const
p
rojects
=
await
dataSource
const
rawP
rojects
=
await
dataSource
.
createQueryBuilder
()
.
from
(
Project
,
"project"
)
.
select
(
"project"
)
.
select
(
[
"project.id"
]
)
.
leftJoinAndSelect
(
'project.members'
,
'member'
)
.
loadRelationCountAndMap
(
'project.tasks'
,
'project.tasks'
)
.
leftJoinAndSelect
(
'member.user'
,
'user'
)
.
addSelect
(
'member'
)
.
where
(
'member.userId = :userId'
,{
userId
:
user
.
id
})
// .where("project.id = :id", { id: req.params.id })
// .select(["*","members"])
// .select("*")
.
getMany
()
const
projectIds
=
[]
if
(
rawProjects
.
length
>
0
){
for
(
let
project
of
rawProjects
){
projectIds
.
push
(
project
.
id
)
}
}
console
.
log
(
'projectIds'
,
projectIds
)
const
projects
=
await
dataSource
.
createQueryBuilder
()
.
from
(
Project
,
"project"
)
.
select
([
"project"
])
.
leftJoinAndSelect
(
'project.members'
,
'member'
)
.
loadRelationCountAndMap
(
'project.tasks'
,
'project.tasks'
)
.
leftJoinAndSelect
(
'member.user'
,
'user'
)
.
where
(
'project.id IN(:...projectIds)'
,
{
projectIds
})
.
getMany
()
// const projects:Project[] = await dataSource.manager.find(Project)
return
res
.
send
({
projects
})
})
...
...
@@ -120,4 +135,62 @@ router.get('/user/:userId', async (req : Request, res : Response): Promise<Respo
return
res
.
send
({
userProjects
})
})
/** Add user to specific project */
router
.
post
(
'/add-user/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
>=>
{
const
{
userId
,
projectId
,
roleProject
}
=
req
.
body
;
const
newMember
:
Member
=
new
Member
();
try
{
newMember
.
user
=
userId
;
newMember
.
project
=
projectId
newMember
.
roleProject
=
roleProject
await
newMember
.
save
()
return
res
.
send
({
newMember
})
}
catch
(
e
){
return
res
.
send
({
message
:
"add user to project failed"
})
}
})
/** Remove user from specific project */
router
.
post
(
'/remove-user'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
>=>
{
console
.
log
(
'in delete user'
)
const
token
=
req
.
get
(
'Authorization'
);
console
.
log
(
'token '
,
token
)
const
{
userId
,
projectId
}
=
req
.
body
;
console
.
log
(
'req.body'
,
req
.
body
)
const
adminOfProject
=
await
dataSource
.
createQueryBuilder
()
.
select
(
"user"
)
.
from
(
User
,
"user"
)
.
leftJoinAndSelect
(
"user.membership"
,
"member"
)
.
leftJoinAndSelect
(
'member.project'
,
'project'
)
.
where
(
"user.token = :token"
,
{
token
})
.
andWhere
(
'project.id=:projectId'
,{
projectId
})
.
andWhere
(
'member.roleProject=:roleProject'
,{
roleProject
:
'admin'
})
.
getOne
()
if
(
!
adminOfProject
){
return
res
.
send
({
message
:
'User is not authorized'
})
}
// return res.send({adminOfProject})
console
.
log
(
'adminOfProject'
,
adminOfProject
)
try
{
await
dataSource
.
createQueryBuilder
()
.
delete
()
.
from
(
Member
)
.
where
(
"user = :userId"
,
{
userId
})
.
andWhere
(
"project=:projectId"
,{
projectId
})
.
execute
()
return
res
.
send
({
message
:
"User removed from project successfully"
})
}
catch
(
e
){
return
res
.
send
({
message
:
'Failed to remove user from project'
})
}
})
export
default
router
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment