Skip to content
Snippets Groups Projects
Commit 9e071180 authored by Bernard Roland (Student Com20)'s avatar Bernard Roland (Student Com20)
Browse files

Added an API route to update a team

parent 982b88eb
No related branches found
No related tags found
No related merge requests found
......@@ -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({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment