Skip to content

Commit

Permalink
Clan Ownership refactor (#532)
Browse files Browse the repository at this point in the history
* refactor transfer backend to match others

* wip commit - backend should be done at this point regarding the actual action of transfering ownership

* add padding to allow more links on line

* add transfer btn to view

* Add route

* make a post a get - simplicity

* get the user id by querying the membership id - im assuming this returns the user

* remove relic from manage page

* lint

* finalize this feature

* add fcaps suggested changes
  • Loading branch information
beckpaul committed Jan 8, 2024
1 parent 8daa828 commit d6d1114
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 188 deletions.
1 change: 1 addition & 0 deletions public/styles/site/clans.sass
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

.action-link
color: #ec9d36
margin-right: 2em
&:link
color: #ec9d36
&:visited
Expand Down
5 changes: 5 additions & 0 deletions src/backend/routes/views/clanRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ router.get(
middlewares.isAuthenticated(),
require('./clans/kick')
)
router.get(
'/transfer/:userId',
middlewares.isAuthenticated(),
require('./clans/transfer')
)
router.get('/leave', middlewares.isAuthenticated(), leave)
router.post('/leave', middlewares.isAuthenticated(), leave)
router.get('/join', middlewares.isAuthenticated(), require('./clans/join'))
Expand Down
187 changes: 0 additions & 187 deletions src/backend/routes/views/clans/post/transfer.js

This file was deleted.

35 changes: 35 additions & 0 deletions src/backend/routes/views/clans/transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { JavaApiError } = require('../../../services/ApiErrors')

exports = module.exports = [
async (req, res) => {
if (!req.requestContainer.get('UserService').getUser()?.clan?.id) {
await req.asyncFlash('error', "You don't own a clan")

return res.redirect('/clans')
}

const newOwnerId = parseInt(req.params.userId)
try {
await req.requestContainer
.get('ClanManagementService')
.transferOwnership(newOwnerId)
await req.asyncFlash('info', 'Clan ownership transferred')

return res.redirect(
'/clans/view/' +
req.requestContainer.get('UserService').getUser().clan.id
)
} catch (e) {
let message = e.toString()
if (e instanceof JavaApiError && e.error?.errors) {
message = e.error.errors[0].detail
}

await req.asyncFlash('error', message)
return res.redirect(
'/clans/view/' +
req.requestContainer.get('UserService').getUser().clan.id
)
}
},
]
44 changes: 44 additions & 0 deletions src/backend/services/ClanManagementRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,50 @@ class ClanManagementRepository {
}
}

async transferOwnership(newOwnerId, clanId) {
try {
const transferRequestBody = {
data: {
type: 'clan',
id: clanId,
relationships: {
leader: {
data: {
id: newOwnerId,
type: 'player',
},
},
},
},
}

const response = await this.javaApiClient.patch(
'/data/clan/' + clanId,
JSON.stringify(transferRequestBody),
{
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
}
)

if (response.status !== 204) {
throw new JavaApiError(
response.status,
response.config.url,
JSON.parse(response.data) || []
)
}
} catch (e) {
if (e instanceof JavaApiError) {
throw e
}

throw new GenericJavaApiError(e.toString())
}
}

async createInvite(clanId, playerId) {
try {
const response = await this.javaApiClient.get(
Expand Down
16 changes: 16 additions & 0 deletions src/backend/services/ClanManagementService.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ class ClanManagementService {
}
}

async transferOwnership(newOwnerId) {
await this.clanManagementRepository.transferOwnership(
newOwnerId,
this.userService.getUser().clan.id
)
try {
this.clanService
.getAll(true)
.then(() => {})
.catch((e) => console.error(e.stack))
await this.userService.refreshUser()
} catch (e) {
console.error(e.stack)
}
}

async deleteClan() {
const clanId = parseInt(this.userService.getUser()?.clan.id)
if (!clanId) {
Expand Down
7 changes: 6 additions & 1 deletion src/backend/templates/views/clans/clan.pug
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ block content
td
if isLeader && member.membershipId !== userMembershipId
a.action-link(
href='/clans/kick/' + member.membershipId,
href=`/clans/kick/${member.membershipId}`,
onclick='return confirm(\'Kick?\')'
) Kick
a.action-link(
href=`/clans/transfer/${member.id}`,
onclick='return confirm(\'Transfer Clan Ownership?\')'
) Make Leader

block js
script(src=webpackAssetJS('clan'))

0 comments on commit d6d1114

Please sign in to comment.