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/:memberId',
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.

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

beckpaul marked this conversation as resolved.
Show resolved Hide resolved
exports = module.exports = [
async (req, res) => {
const newOwnerMemberId = req.params.memberId
const clanId = req.requestContainer.get('UserService').getUser().clan.id
try {
await req.requestContainer

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#L4-L8

Added lines #L4 - L8 were not covered by tests
.get('ClanManagementService')
.transferOwnership(newOwnerMemberId, clanId)
await req.asyncFlash('info', 'Clan ownership transferred')

Check warning on line 11 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

Added line #L11 was not covered by tests

return res.redirect(`/clans/view/${clanId}`, {

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#L13

Added line #L13 was not covered by tests
clan_tag: req.body.clan_tag,
clan_name: req.body.clan_name,
clan_description: req.body.clan_description,
})
} catch (e) {
let message = e.toString()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L19 was 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/transfer.js

View check run for this annotation

Codecov / codecov/patch

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

Added line #L21 was not covered by tests
}

await req.asyncFlash('error', message)
return res.redirect(`/clans/view/${clanId}`, {

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#L24-L25

Added lines #L24 - L25 were not covered by tests
clan_tag: req.body.clan_tag,
clan_name: req.body.clan_name,
clan_description: req.body.clan_description,
})
}
},
]
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
18 changes: 18 additions & 0 deletions src/backend/services/ClanManagementService.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
}
}

async transferOwnership(newOwnerMemberId, clanId) {

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

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L39

Added line #L39 was not covered by tests
const newOwnerUser =
await this.clanService.getClanMembership(newOwnerMemberId)
beckpaul marked this conversation as resolved.
Show resolved Hide resolved
await this.clanManagementRepository.transferOwnership(

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

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L41-L42

Added lines #L41 - L42 were not covered by tests
newOwnerUser.id,
clanId
)
try {
this.clanService

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

View check run for this annotation

Codecov / codecov/patch

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

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

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#L49-L51

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

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

View check run for this annotation

Codecov / codecov/patch

src/backend/services/ClanManagementService.js#L53

Added line #L53 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#transfer-clan-ownership.action-link(
href=`/clans/transfer/${member.membershipId}`,
onclick='return confirm(\'Transfer Clan Ownership?\')'
) Make Leader

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