Skip to content

Commit

Permalink
wip commit - backend should be done at this point regarding the actua…
Browse files Browse the repository at this point in the history
…l action of transfering ownership
  • Loading branch information
beckpaul committed Dec 15, 2023
1 parent 9cc4817 commit c67a1c0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/backend/routes/views/clans/post/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { body, validationResult, matchedData } = require('express-validator')
const { JavaApiError } = require('../../../../services/ApiErrors')

exports = module.exports = [
// validate the input
// Validations will need to change
body('transfer_to', 'Please indicate the recipient name').notEmpty(),
body(
'clan_id',
Expand All @@ -11,8 +11,6 @@ exports = module.exports = [

async (req, res) => {
const errors = validationResult(req)

// Handle client-side errors
if (!errors.isEmpty()) {
return res.render('clans/manage', {
errors: {
Expand All @@ -26,17 +24,16 @@ exports = module.exports = [
})
}

// Sanitize req
const data = matchedData(req)
const clanId = data.body.clan_id
const owner = data.user.data.attributes.userName
const newOwner = data.body.transfer_to

// This will also need changed
const newOwner = matchedData(req).body.transfer_to
try {
await req.requestContainer
.get('ClanManagementService')
.transferOwnership(owner, newOwner, clanId)
await req.asyncFlash('info', 'Clan ownership transferred')
.transferOwnership(newOwner)
await req.asyncFlash(
'info',
`Clan ownership transferred to ${newOwner}`
)

return res.redirect(
'/clans/view/' +
Expand Down
1 change: 1 addition & 0 deletions src/backend/routes/views/clans/post/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ exports = module.exports = [

return res.render('clans/manage', {
clan_tag: req.body.clan_tag,
clan_id: req.body.clan_id,
clan_name: req.body.clan_name,
clan_description: req.body.clan_description,
})
Expand Down
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
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 @@ class ClanManagementService {
}
}

async transferOwnership(newOwnerId) {
const clanId = this.userService.getUser().clan.id

await this.clanManagementRepository.transferOwnership(
newOwnerId,
clanId
)
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

0 comments on commit c67a1c0

Please sign in to comment.