diff --git a/server/src/v1/team.ts b/server/src/v1/team.ts index 48930618aa9e95a6b1eea22c9743920c94b60875..a12609eb7adbbcddafef31b9ec3a00a43630a9cb 100644 --- a/server/src/v1/team.ts +++ b/server/src/v1/team.ts @@ -10,48 +10,96 @@ const team = express(); team.use(requireVerification); -interface TeamCreateBody { +interface CreateTeamBody { name: string; token: Token; } team.post('/', async (req, res) => { - if (isOfType<TeamCreateBody>(req.body, [['name', 'string']])) { + if (isOfType<CreateTeamBody>(req.body, [['name', 'string']])) { const team_id = uuid(); - const team_name = req.body.name.trim(); const role_id = uuid(); - if (team_name.length >= 4) { + const team_name = req.body.name; + try { + await database.transaction(async transaction => { + await transaction('teams').insert({ + id: team_id, + name: team_name, + }); + await transaction('roles').insert({ + id: role_id, + team_id: team_id, + name: 'Default', + }); + await transaction('team_members').insert({ + user_id: req.body.token.id, + team_id: team_id, + role_id: role_id, + }); + }); + res.status(200).json({ + status: 'success', + id: team_id, + }); + } catch (e) { + res.status(400).json({ + status: 'error', + message: 'failed create team', + }); + } + } else { + res.status(400).json({ + status: 'error', + message: 'missing request fields', + }); + } +}); + +interface UpdateTeamBody { + name: string; + token: Token; +} + +team.put('/:uuid', async (req, res) => { + if (isOfType<UpdateTeamBody>(req.body, [['name', 'string']])) { + const team_id = req.params.uuid; + if (validate(team_id)) { + const team_name = req.body.name; try { - await database.transaction(async transaction => { - await transaction('teams').insert({ - id: team_id, - name: team_name, + const team = await database('teams') + .leftJoin('team_members', 'teams.id', 'team_members.team_id') + .select({ id: 'teams.id' }) + .where({ + 'team_members.user_id': req.body.token.id, + 'teams.id': team_id, }); - await transaction('roles').insert({ - id: role_id, - team_id: team_id, - name: 'Default', + if (team.length >= 1) { + await database('teams') + .update({ + name: team_name, + }) + .where({ + 'teams.id': team_id, + }) + res.status(200).json({ + status: 'success', }); - await transaction('team_members').insert({ - user_id: req.body.token.id, - team_id: team_id, - role_id: role_id, + } else { + res.status(404).json({ + status: 'error', + message: 'team not found', }); - }); - res.status(200).json({ - status: 'success', - id: team_id, - }); + } } catch (e) { res.status(400).json({ status: 'error', - message: 'failed create team', + message: 'failed update team', }); } } else { res.status(400).json({ status: 'error', - message: 'team names must be four letters or longer', + message: 'malformed uuid', }); } } else { @@ -105,7 +153,7 @@ team.get('/:uuid/', async (req, res) => { 'team_members.user_id': null, 'teams.id': id, }) - if (team.length === 1) { + if (team.length >= 1) { res.status(200).json({ status: 'success', team: team[0], @@ -196,7 +244,7 @@ team.delete('/:teamid/members/:userid', async (req, res) => { 'team_members.user_id': req.body.token.id, 'team_members.team_id': team_id, }); - if (team.length === 1) { + if (team.length >= 1) { const deleted = await database('team_members') .delete() .where({ @@ -233,7 +281,6 @@ team.delete('/:teamid/members/:userid', async (req, res) => { } }); - team.get('/:uuid/roles', async (req, res) => { try { const id = req.params.uuid; @@ -366,7 +413,7 @@ team.post('/:uuid/roles', async (req, res) => { 'team_members.user_id': req.body.token.id, 'team_members.team_id': team_id, }); - if (team.length === 1) { + if (team.length >= 1) { const role_id = uuid(); const role_name = req.body.name; await database('roles').insert({ @@ -418,7 +465,7 @@ team.delete('/:teamid/roles/:roleid', async (req, res) => { 'team_members.user_id': req.body.token.id, 'team_members.team_id': team_id, }); - if (team.length === 1) { + if (team.length >= 1) { const deleted = await database('roles') .delete() .where({ @@ -476,7 +523,7 @@ team.post('/:uuid/members', async (req, res) => { 'team_members.team_id': team_id, 'roles.id': role_id, }); - if (role.length === 1) { + if (role.length >= 1) { await database('team_members').insert({ user_id: user_id, team_id: team_id, @@ -532,7 +579,7 @@ team.put('/:uuid/members', async (req, res) => { 'team_members.team_id': team_id, 'roles.id': role_id, }); - if (role.length === 1) { + if (role.length >= 1) { await database('team_members') .update({ role_id: role_id }) .where({