-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
913 additions
and
1,408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class JavaApiError extends Error | ||
{ | ||
constructor(status, url, errors) { | ||
super('Failed request "' + url + '" with status "' + status + '"') | ||
|
||
this.status = status | ||
this.url = url | ||
this.errors = errors | ||
} | ||
|
||
} | ||
|
||
class GenericJavaApiError extends Error | ||
{ | ||
} | ||
|
||
module.exports.JavaApiError = JavaApiError | ||
module.exports.GenericJavaApiError = GenericJavaApiError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const {JavaApiError, GenericJavaApiError} = require("../ApiErrors"); | ||
|
||
class ClanRepository { | ||
constructor(javaApiClient, monthsInThePast = 12) { | ||
this.javaApiClient = javaApiClient | ||
this.monthsInThePast = monthsInThePast | ||
} | ||
|
||
getUpdateTimeForApiEntries() { | ||
const date = new Date(); | ||
date.setMonth(date.getMonth() - this.monthsInThePast); | ||
|
||
return date.toISOString() | ||
} | ||
|
||
async updateClan(id, name, description, tag) { | ||
const newClanObject = { | ||
"data": { | ||
"type": "clan", | ||
"id": id, | ||
"attributes": { | ||
"description": description, | ||
"name": name, | ||
"tag": tag | ||
} | ||
} | ||
}; | ||
|
||
try { | ||
const response = await this.javaApiClient.patch(`/data/clan/${id}`, newClanObject) | ||
|
||
if (response.status !== 200) { | ||
throw new Error('ClanRepository::fetchClanMembership failed with response status "' + response.status + '"') | ||
} | ||
|
||
return this.mapClanMembership(JSON.parse(response.data)) | ||
} catch (e) { | ||
if (e.response && e.response.data?.errors) { | ||
throw new JavaApiError(e.response.status, `patch /data/clan/${id}`, e.response.data.errors) | ||
} | ||
|
||
throw GenericJavaApiError('ClanRepository::fetchClanMembership failed') | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
async fetchClanMembership(clanMembershipId) { | ||
let response = await this.javaApiClient.get(`/data/clanMembership/${clanMembershipId}/clan?include=memberships.player&fields[clan]=createTime,description,name,tag,updateTime,websiteUrl,founder,leader&fields[player]=login,updateTime&fields[clanMembership]=createTime,player`,) | ||
|
||
if (response.status !== 200) { | ||
throw new Error('ClanRepository::fetchClanMembership failed with response status "' + response.status + '"') | ||
} | ||
|
||
return this.mapClanMembership(JSON.parse(response.data)) | ||
} | ||
|
||
mapClanMembership(data) { | ||
if (typeof data !== 'object' || data === null) { | ||
throw new Error('ClanRepository::mapClanMembership malformed response, not an object') | ||
} | ||
|
||
if (!data.hasOwnProperty('data')) { | ||
throw new Error('ClanRepository::mapClanMembership malformed response, expected "data"') | ||
} | ||
|
||
if (typeof data.data !== 'object' || data.data === null) { | ||
return null | ||
} | ||
|
||
if (typeof data.included !== 'object' || data.included === null) { | ||
throw new Error('ClanRepository::mapClanMembership malformed response, expected "included"') | ||
} | ||
|
||
const clanMembershipRaw = data.data.attributes | ||
|
||
|
||
const clanMembership = { | ||
clan_id: data.data.id, | ||
clan_name: clanMembershipRaw.name, | ||
clan_tag: clanMembershipRaw.tag, | ||
clan_description: clanMembershipRaw.description, | ||
clan_create_time: clanMembershipRaw.createTime, | ||
} | ||
|
||
let members = {}; | ||
|
||
for (let k in data.included) { | ||
switch (data.included[k].type) { | ||
case "player": | ||
const player = data.included[k]; | ||
if (!members[player.id]) members[player.id] = {}; | ||
members[player.id].id = player.id; | ||
members[player.id].name = player.attributes.login; | ||
|
||
break; | ||
|
||
case "clanMembership": | ||
const membership = data.included[k]; | ||
const member = membership.relationships.player.data; | ||
if (!members[member.id]) members[member.id] = {}; | ||
members[member.id].id = member.id; | ||
members[member.id].membershipId = membership.id; | ||
members[member.id].joinedAt = membership.attributes.createTime; | ||
break; | ||
} | ||
} | ||
|
||
clanMembership.members = members | ||
|
||
return clanMembership | ||
} | ||
} | ||
|
||
module.exports = ClanRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"data":{"type":"clan","id":"2354","attributes":{"createTime":"2023-11-10T23:16:32Z","description":"asdasd","name":"asd","tag":"asd","updateTime":"2023-11-10T23:16:32Z","websiteUrl":"http://localhost:8096/clan/2354"},"relationships":{"founder":{"data":{"type":"player","id":"7"}},"leader":{"data":{"type":"player","id":"7"}}}},"included":[{"type":"clanMembership","id":"15594","attributes":{"createTime":"2023-11-10T23:16:32Z"},"relationships":{"player":{"data":{"type":"player","id":"7"}}}},{"type":"player","id":"7","attributes":{"login":"steambie","updateTime":"2023-10-27T10:38:46Z"}}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const express = require('express') | ||
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.status(503).render('errors/503-known-issue')); | ||
router.get('/create', middlewares.isAuthenticated(), require('clans/get/create')) | ||
router.get('/manage', middlewares.isAuthenticated(), require('clans/get/manage')) | ||
router.get('/accept_invite', middlewares.isAuthenticated(), require('clans/get/accept_invite')) | ||
router.post('/create', middlewares.isAuthenticated(), require('clans/post/create')) | ||
router.post('/destroy', middlewares.isAuthenticated(), require('clans/post/destroy')) | ||
router.post('/invite', middlewares.isAuthenticated(), require('clans/post/invite')) | ||
router.post('/kick', middlewares.isAuthenticated(), require('clans/post/kick')) | ||
router.post('/transfer', middlewares.isAuthenticated(), require('clans/post/transfer')) | ||
router.post('/update', middlewares.isAuthenticated(), require('clans/post/update')) | ||
router.post('/leave', middlewares.isAuthenticated(), require('clans/post/leave')) | ||
router.post('/join', middlewares.isAuthenticated(), require('clans/post/join')) | ||
|
||
router.get('/', require('clans/get/clans')) | ||
router.get('/getClan', require('clans/get/getClan')) | ||
router.get('/*', (req, res) => { | ||
let id = req.path.slice(-3) | ||
res.redirect(`/clans/getClan?tag=${id}`) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
exports = module.exports = function (req, res) { | ||
let flash = {}; | ||
if (req.query.flash) { | ||
|
||
flash.class = 'alert-success'; | ||
flash.type = 'Success!'; | ||
switch (req.query.flash) { | ||
case 'leave': | ||
flash.messages = 'You left your clan.'; | ||
break; | ||
|
||
case 'destroy': | ||
flash.messages = 'You deleted your clan.'; | ||
break; | ||
|
||
case 'transfer': | ||
flash.messages = `You have transferred your clan to ${req.query.newLeader}.`; | ||
break; | ||
|
||
case 'error': | ||
flash.class = 'alert-danger'; | ||
flash.messages = 'There was an issue with your request.'; | ||
flash.type = 'Error!'; | ||
break; | ||
} | ||
} | ||
res.render('clans', {flash: flash}); | ||
|
||
}; |
Oops, something went wrong.