Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clan Ownership refactor #532

Merged
merged 11 commits into from
Jan 8, 2024
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')

beckpaul marked this conversation as resolved.
Show resolved Hide resolved
exports = module.exports = [
async (req, res) => {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L4 was not covered by tests
if (!req.requestContainer.get('UserService').getUser()?.clan?.id) {
await req.asyncFlash('error', "You don't own a clan")

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

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/transfer.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/transfer.js

View check run for this annotation

Codecov / codecov/patch

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

Added line #L8 was not covered by tests
}

const newOwnerId = parseInt(req.params.userId)
try {
await req.requestContainer

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L11 - L13 were not covered by tests
.get('ClanManagementService')
.transferOwnership(newOwnerId)
await req.asyncFlash('info', 'Clan ownership transferred')

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L16 was not covered by tests

return res.redirect(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L18 was not covered by tests
'/clans/view/' +
req.requestContainer.get('UserService').getUser().clan.id
)
} catch (e) {
let message = e.toString()

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L25 was not covered by tests
}

await req.asyncFlash('error', message)
return res.redirect(

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

View check run for this annotation

Codecov / codecov/patch

src/backend/routes/views/clans/transfer.js#L28-L29

Added lines #L28 - L29 were not covered by tests
'/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 @@
}
}

async transferOwnership(newOwnerId, clanId) {
try {
const transferRequestBody = {

Check warning on line 91 in src/backend/services/ClanManagementRepository.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementRepository.js#L89-L91

Added lines #L89 - L91 were not covered by tests
data: {
type: 'clan',
id: clanId,
relationships: {
leader: {
data: {
id: newOwnerId,
type: 'player',
},
},
},
},
}

const response = await this.javaApiClient.patch(

Check warning on line 106 in src/backend/services/ClanManagementRepository.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementRepository.js#L106

Added line #L106 was not covered by tests
'/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(

Check warning on line 118 in src/backend/services/ClanManagementRepository.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementRepository.js#L118

Added line #L118 was not covered by tests
response.status,
response.config.url,
JSON.parse(response.data) || []
)
}
} catch (e) {
if (e instanceof JavaApiError) {
throw e

Check warning on line 126 in src/backend/services/ClanManagementRepository.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementRepository.js#L126

Added line #L126 was not covered by tests
}

throw new GenericJavaApiError(e.toString())

Check warning on line 129 in src/backend/services/ClanManagementRepository.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementRepository.js#L129

Added line #L129 was not covered by tests
}
}

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 @@
}
}

async transferOwnership(newOwnerId) {
await this.clanManagementRepository.transferOwnership(

Check warning on line 40 in src/backend/services/ClanManagementService.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L39-L40

Added lines #L39 - L40 were not covered by tests
newOwnerId,
this.userService.getUser().clan.id
)
try {
this.clanService

Check warning on line 45 in src/backend/services/ClanManagementService.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L44-L45

Added lines #L44 - L45 were not covered by tests
.getAll(true)
.then(() => {})
.catch((e) => console.error(e.stack))
await this.userService.refreshUser()

Check warning on line 49 in src/backend/services/ClanManagementService.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L47-L49

Added lines #L47 - L49 were not covered by tests
} catch (e) {
console.error(e.stack)

Check warning on line 51 in src/backend/services/ClanManagementService.js

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L51

Added line #L51 was not covered by tests
}
}

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'))