Skip to content

Commit

Permalink
refactor clan management
Browse files Browse the repository at this point in the history
  • Loading branch information
fcaps committed Dec 5, 2023
1 parent db94e34 commit d709955
Show file tree
Hide file tree
Showing 28 changed files with 487 additions and 543 deletions.
9 changes: 0 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ services:
build:
dockerfile: Dockerfile-dev
context: .
networks:
- faf-stack
logging:
driver: "json-file"

networks:
faf-stack:
name: faf-stack_faf
external: true
30 changes: 30 additions & 0 deletions public/styles/site/clans.sass
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,33 @@
z-index: 2
position: relative


.faf-button
appearance: button
text-decoration: none
margin: 0.2em
padding: 0.8em
color: #000000
border: 0px solid #262626
background-color: #ec9d36
border-radius: 20px
justify-self: center
font-weight: bold
letter-spacing: 0.1em
cursor: pointer
&:link
color: #000000
&:visited
color: #000000
&:hover
color: #ffffff
background-color: #731d00

.action-link
color: #ec9d36
&:link
color: #ec9d36
&:visited
color: #ec9d36
&:hover
color: #731d00
23 changes: 12 additions & 11 deletions src/backend/routes/views/clanRouter.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const create = require('../views/clans/create')
const invite = require('./clans/invite')
const leave = require('./clans/leave')
const express = require('../../ExpressApp')
const router = express.Router()
const middlewares = require('../middleware')

// This will be replaced soon, therefor I did not spend time on it
router.get('/', (req, res) => res.render('clans/clans'))
router.get('/view/:id', async (req, res) => {
const clanId = parseInt(req.params.id || null)
if (!clanId) {
return res.redirect('/clans')
}
router.get('/view/:id', require('./clans/view'))

return res.render('clans/clan', { clan: await req.appContainer.get('ClanService').getClan(clanId) })
})

router.get('/create', create)
router.post('/create', create)
router.get('/create', middlewares.isAuthenticated(), create)
router.post('/create', middlewares.isAuthenticated(), create)
router.get('/manage', middlewares.isAuthenticated(), require('./clans/get/manage'))
router.post('/update', middlewares.isAuthenticated(), require('./clans/post/update'))
router.post('/destroy', middlewares.isAuthenticated(), require('./clans/post/destroy'))

router.get('/invite', middlewares.isAuthenticated(), invite)
router.post('/invite', middlewares.isAuthenticated(), invite)
router.get('/kick/:memberId', middlewares.isAuthenticated(), require('./clans/kick'))
router.get('/leave', middlewares.isAuthenticated(), leave)
router.post('/leave', middlewares.isAuthenticated(), leave)
router.get('/join', middlewares.isAuthenticated(), require('./clans/join'))
router.get('/invite-accept', middlewares.isAuthenticated(), require('./clans/inviteAccept'))
router.get('*', (req, res) => res.status(503).render('errors/503-known-issue'))

module.exports = router
92 changes: 0 additions & 92 deletions src/backend/routes/views/clans/get/accept_invite.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/backend/routes/views/clans/get/manage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { JavaApiError } = require('../../../../services/ApiErrors')
exports = module.exports = async (req, res) => {
// if something changed in another session we should refresh the user first
await req.requestContainer.get('UserService').refreshUser()

Check warning on line 4 in src/backend/routes/views/clans/get/manage.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/get/manage.js#L4

Added line #L4 was not covered by tests

const clanMembershipId = req.requestContainer.get('UserService').getUser()?.clan?.membershipId || null
if (!clanMembershipId) {
await req.asyncFlash('error', 'You don\'t belong to a clan')

Check warning on line 8 in src/backend/routes/views/clans/get/manage.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/get/manage.js#L8

Added line #L8 was not covered by tests

return res.redirect('/clans')
}

Expand All @@ -17,6 +22,6 @@ exports = module.exports = async (req, res) => {

await req.asyncFlash('error', message)

return res.redirect('/')
return res.redirect('/clans')

Check warning on line 25 in src/backend/routes/views/clans/get/manage.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/get/manage.js#L25

Added line #L25 was not covered by tests
}
}
46 changes: 46 additions & 0 deletions src/backend/routes/views/clans/invite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { JavaApiError } = require('../../../services/ApiErrors')
const { body } = require('express-validator')
const url = require('url')

exports = module.exports = [
body('invited_player', 'Please select a player').notEmpty().isLength({ max: 20 }),
async (req, res) => {

Check warning on line 7 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L7

Added line #L7 was not covered by tests
if (req.method === 'POST') {
const user = await req.appContainer.get('DataRepository').fetchUserByName(req.body.invited_player)

Check warning on line 9 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L9

Added line #L9 was not covered by tests

if (!user) {
await req.asyncFlash('error', 'User not found')
return res.render('clans/invite', {

Check warning on line 13 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L12-L13

Added lines #L12 - L13 were not covered by tests
invited_player: req.body.invited_player
})
}

try {
const invitation = await req.requestContainer.get('ClanManagementService').createInvite(user.id)

Check warning on line 19 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L18-L19

Added lines #L18 - L19 were not covered by tests

return res.render('clans/invite', {

Check warning on line 21 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L21

Added line #L21 was not covered by tests
invited_player: req.body.invited_player,
link: url.format({
pathname: '/clans/invite-accept',
query: {
token: encodeURIComponent(invitation)
}
})
})
} catch (e) {
let message = e.toString()

Check warning on line 31 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L31

Added line #L31 was not covered by tests
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail

Check warning on line 33 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L33

Added line #L33 was not covered by tests
}

await req.asyncFlash('error', message)

Check warning on line 36 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L36

Added line #L36 was not covered by tests

return res.redirect('/clans/manage')

Check warning on line 38 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L38

Added line #L38 was not covered by tests
}
}

return res.render('clans/invite', {

Check warning on line 42 in src/backend/routes/views/clans/invite.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/invite.js#L42

Added line #L42 was not covered by tests
invited_player: ''
})
}
]
36 changes: 36 additions & 0 deletions src/backend/routes/views/clans/inviteAccept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const decodingJWT = (token) => {
if (token !== null) {
const base64String = token.split('.')[1]
return JSON.parse(Buffer.from(base64String,

Check warning on line 4 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L3-L4

Added lines #L3 - L4 were not covered by tests
'base64').toString('ascii'))
}
return null

Check warning on line 7 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L7

Added line #L7 was not covered by tests
}

exports = module.exports = async function (req, res) {
if (!req.query.token) {
await req.asyncFlash('error', 'The invitation link is invalid!')

Check warning on line 12 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L12

Added line #L12 was not covered by tests

return res.redirect('/clans')

Check warning on line 14 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L14

Added line #L14 was not covered by tests
}
const token = req.query.token

Check warning on line 16 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L16

Added line #L16 was not covered by tests

const decodedToken = decodingJWT(req.query.token)

Check warning on line 18 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L18

Added line #L18 was not covered by tests

if (decodedToken === null || decodedToken.clan?.name === undefined) {
await req.asyncFlash('error', 'The invitation link is invalid!')

Check warning on line 21 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L21

Added line #L21 was not covered by tests

return res.redirect('/clans')

Check warning on line 23 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L23

Added line #L23 was not covered by tests
}

if (req.requestContainer.get('UserService').getUser()?.clan) {
await req.asyncFlash('error', 'You are already in a clan')

Check warning on line 27 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L27

Added line #L27 was not covered by tests

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)

Check warning on line 29 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L29

Added line #L29 was not covered by tests
}

res.render('clans/accept_invite', {

Check warning on line 32 in src/backend/routes/views/clans/inviteAccept.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/inviteAccept.js#L32

Added line #L32 was not covered by tests
acceptURL: `/clans/join?token=${token}`,
clanName: decodedToken.clan.name
})
}
33 changes: 33 additions & 0 deletions src/backend/routes/views/clans/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = async function (req, res) {
if (!req.query.token) {
await req.asyncFlash('error', 'The invitation link is invalid!')

Check warning on line 5 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L5

Added line #L5 was not covered by tests

return res.redirect('/clans')

Check warning on line 7 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L7

Added line #L7 was not covered by tests
}
const token = req.query.token

Check warning on line 9 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L9

Added line #L9 was not covered by tests

try {
await req.requestContainer.get('ClanManagementService').acceptInvitation(token)
await req.asyncFlash('info', 'Clan joined!')

Check warning on line 13 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L11-L13

Added lines #L11 - L13 were not covered by tests

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)

Check warning on line 15 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L15

Added line #L15 was not covered by tests
} catch (e) {
console.log(e.stack)
let message = e.toString()

Check warning on line 18 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L17-L18

Added lines #L17 - L18 were not covered by tests

if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail

Check warning on line 21 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L21

Added line #L21 was not covered by tests

if (e.error.errors[0].code === '152') {
await req.asyncFlash('error', message)
return res.redirect('/clans')

Check warning on line 25 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L24-L25

Added lines #L24 - L25 were not covered by tests
}
}

await req.asyncFlash('error', message)

Check warning on line 29 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L29

Added line #L29 was not covered by tests

return res.redirect('/clans')

Check warning on line 31 in src/backend/routes/views/clans/join.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/join.js#L31

Added line #L31 was not covered by tests
}
}
33 changes: 33 additions & 0 deletions src/backend/routes/views/clans/kick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = async function (req, res) {
const memberId = parseInt(req.params.memberId || null)
if (!memberId) {
await req.asyncFlash('error', 'memberId is required')

Check warning on line 6 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L6

Added line #L6 was not covered by tests

return res.redirect('/clans')

Check warning on line 8 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L8

Added line #L8 was not covered by tests
}

const user = req.requestContainer.get('UserService').getUser()

Check warning on line 11 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L11

Added line #L11 was not covered by tests

if (!user.clan) {
await req.asyncFlash('info', 'You are not in a clan')

Check warning on line 14 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L14

Added line #L14 was not covered by tests

return res.redirect('/clans')

Check warning on line 16 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L16

Added line #L16 was not covered by tests
}

try {
await req.requestContainer.get('ClanManagementService').kickMember(memberId)

Check warning on line 20 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L19-L20

Added lines #L19 - L20 were not covered by tests

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)

Check warning on line 22 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L22

Added line #L22 was not covered by tests
} catch (e) {
let message = e.toString()

Check warning on line 24 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L24

Added line #L24 was not covered by tests
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail

Check warning on line 26 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L26

Added line #L26 was not covered by tests
}

await req.asyncFlash('error', message)

Check warning on line 29 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L29

Added line #L29 was not covered by tests

return res.redirect('/clans/view/' + req.requestContainer.get('UserService').getUser().clan.id)

Check warning on line 31 in src/backend/routes/views/clans/kick.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/kick.js#L31

Added line #L31 was not covered by tests
}
}
32 changes: 32 additions & 0 deletions src/backend/routes/views/clans/leave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = async function (req, res) {
const user = req.requestContainer.get('UserService').getUser()

Check warning on line 4 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L4

Added line #L4 was not covered by tests

if (!user.clan.membershipId) {
await req.asyncFlash('info', 'You are not in a clan')

Check warning on line 7 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L7

Added line #L7 was not covered by tests

return res.redirect('/clans')

Check warning on line 9 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L9

Added line #L9 was not covered by tests
}

if (req.method === 'POST') {
try {
await req.requestContainer.get('ClanManagementService').leaveClan()
await req.asyncFlash('info', 'Clan left')

Check warning on line 15 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L13-L15

Added lines #L13 - L15 were not covered by tests

return res.redirect('/clans')

Check warning on line 17 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L17

Added line #L17 was not covered by tests
} catch (e) {
console.error(e.stack)
let message = e.toString()

Check warning on line 20 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L19-L20

Added lines #L19 - L20 were not covered by tests
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail

Check warning on line 22 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L22

Added line #L22 was not covered by tests
}

await req.asyncFlash('error', message)

Check warning on line 25 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L25

Added line #L25 was not covered by tests

return res.redirect('/clans')

Check warning on line 27 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L27

Added line #L27 was not covered by tests
}
}

return res.render('clans/leave')

Check warning on line 31 in src/backend/routes/views/clans/leave.js

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/leave.js#L31

Added line #L31 was not covered by tests
}
Loading

0 comments on commit d709955

Please sign in to comment.