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

Added project creation and updating api paths

parent db3a4ff1
No related branches found
No related tags found
No related merge requests found
......@@ -41,8 +41,8 @@ export async function up(database: Knex): Promise<void> {
table.text('text').notNullable();
table.enum('status', [ 'open', 'closed', 'suspended' ]).notNullable();
table.enum('priority', [ 'low', 'medium', 'high', 'urgent' ]).notNullable();
table.dateTime('created').notNullable();
table.dateTime('edited').notNullable();
table.timestamp('created').notNullable();
table.timestamp('edited').notNullable();
})
.createTable('task_dependencies', table => {
table.uuid('task_id').notNullable().references('tasks.id');
......@@ -59,7 +59,6 @@ export async function up(database: Knex): Promise<void> {
table.uuid('user_id').notNullable().references('users.id');
table.uuid('task_id').notNullable().references('tasks.id');
table.primary(['user_id', 'task_id']);
table.integer('time').notNullable();
table.boolean('assigned').notNullable();
table.boolean('working').notNullable();
})
......@@ -68,8 +67,15 @@ export async function up(database: Knex): Promise<void> {
table.uuid('task_id').notNullable().references('tasks.id');
table.uuid('user_id').notNullable().references('users.id');
table.text('text').notNullable();
table.dateTime('created').notNullable();
table.dateTime('edited').notNullable();
table.timestamp('created').notNullable();
table.timestamp('edited').notNullable();
})
.createTable('workhours', table => {
table.uuid('id').notNullable().primary();
table.uuid('user_id').notNullable().references('users.id');
table.uuid('task_id').notNullable().references('tasks.id');
table.timestamp('started').notNullable();
table.timestamp('finished');
});
}
......@@ -84,6 +90,7 @@ export async function down(database: Knex): Promise<void> {
.dropTable('roles')
.dropTable('team_members')
.dropTable('teams')
.dropTable('users');
.dropTable('users')
.dropTable('workhours');
}
import express from 'express';
import { v4 as uuid, validate } from 'uuid';
import { requireVerification } from './auth';
import database from '../database';
import { isOfType } from '../util';
import { requireVerification, Token } from './auth';
const project = express();
project.use(requireVerification);
project.get('/', async (req, res) => {
try {
const projects = await database('users')
.innerJoin('team_members', 'users.id', 'team_members.user_id')
.innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id')
.innerJoin('projects', 'team_projects.project_id', 'projects.id')
.select({
id: 'projects.id',
name: 'projects.name',
})
.where({
'users.id': req.body.token.id,
})
.groupBy('projects.id');
res.status(200).json({
status: 'success',
projects: projects,
});
} catch (e) {
console.log(e);
res.status(400).json({
status: 'error',
message: 'failed to get project',
});
}
});
project.get('/:uuid', async (req, res) => {
try {
const id = req.params.uuid;
if (validate(id)) {
const projects = await database('users')
.innerJoin('team_members', 'users.id', 'team_members.user_id')
.innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id')
.innerJoin('projects', 'team_projects.project_id', 'projects.id')
.select({
id: 'projects.id',
name: 'projects.name',
team_id: 'team_projects.team_id',
})
.where({
'users.id': req.body.token.id,
'projects.id': id,
});
if (projects.length >= 1) {
res.status(200).json({
status: 'success',
project: {
id: projects[0].id,
name: projects[0].name,
teams: projects.map(task => task.team_id),
}
});
} else {
res.status(404).json({
status: 'error',
message: 'project not found',
});
}
} else {
res.status(400).json({
status: 'error',
message: 'malformed uuid',
});
}
} catch (e) {
console.log(e);
res.status(400).json({
status: 'error',
message: 'failed to get project',
});
}
});
interface AddProjectBody {
teams: Array<string>;
name: string;
token: Token;
}
project.post('/', async (req, res) => {
if (isOfType<AddProjectBody>(req.body, [['teams', 'object'], ['name', 'string']])) {
try {
const team_ids = req.body.teams;
for (const team_id of team_ids) {
if (!validate(team_id)) {
res.status(400).json({
status: 'error',
message: 'malformed uuid',
});
return;
}
}
const project_id = uuid();
const team = await database('users')
.innerJoin('team_members', 'users.id', 'team_members.user_id')
.select({ id: 'team_members.team_id' })
.where({
'users.id': req.body.token.id,
})
.whereIn('team_members.team_id', team_ids);
if (team.length === team_ids.length) {
await database.transaction(async transaction => {
await transaction('projects').insert({
id: project_id,
name: req.body.name,
});
await transaction('team_projects').insert(
team_ids.map(team_id => ({
project_id: project_id,
team_id: team_id,
}))
);
});
res.status(200).json({
status: 'success',
id: project_id,
});
} else {
res.status(404).json({
status: 'error',
message: 'team not found',
});
}
} catch (e) {
res.status(400).json({
status: 'error',
message: 'failed to create project',
});
}
} else {
res.status(400).json({
status: 'error',
message: 'missing request fields',
});
}
});
interface UpdateProjectBody {
add_teams?: Array<string>;
remove_teams?: Array<string>;
name?: string;
token: Token;
}
project.put('/:uuid', async (req, res) => {
if (isOfType<UpdateProjectBody>(req.body, [])) {
try {
const id = req.params.uuid;
if (validate(id)) {
const add_team_ids = req.body.add_teams ?? [];
const remove_team_ids = req.body.remove_teams ?? [];
for (const team_id of add_team_ids.concat(remove_team_ids)) {
if (!validate(team_id)) {
res.status(400).json({
status: 'error',
message: 'malformed uuid',
});
return;
}
}
const projects = await database('users')
.innerJoin('team_members', 'users.id', 'team_members.user_id')
.innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id')
.innerJoin('projects', 'team_projects.project_id', 'projects.id')
.select({
id: 'projects.id',
name: 'projects.name',
team_id: 'team_projects.team_id',
})
.where({
'users.id': req.body.token.id,
'projects.id': id,
});
if (projects.length >= 1) {
await database.transaction(async transaction => {
if (typeof req.body.name === 'string') {
await transaction('projects')
.update({
name: req.body.name,
}).where({
id: id,
});
}
if (add_team_ids.length !== 0) {
await transaction('team_projects').insert(
add_team_ids.map(team_id => ({
project_id: id,
team_id: team_id,
}))
);
}
if (remove_team_ids.length !== 0) {
await transaction('team_projects')
.delete()
.where({
'project_id': id,
})
.whereIn('team_id', remove_team_ids);
}
});
res.status(200).json({
status: 'success',
});
} else {
res.status(400).json({
status: 'error',
message: 'project not found',
});
}
} else {
res.status(400).json({
status: 'error',
message: 'malformed uuid',
});
}
} catch (e) {
res.status(400).json({
status: 'error',
message: 'failed to create project',
});
}
} else {
res.status(400).json({
status: 'error',
message: 'missing request fields',
});
}
});
export default project;
import express from 'express';
import { validate } from 'uuid';
import { v4 as uuid, validate } from 'uuid';
import database from '../database';
import { requireVerification } from './auth';
import { requireVerification, Token } from './auth';
import { isOfType } from '../util';
const task = express();
......@@ -56,5 +57,6 @@ task.get('/:uuid', async (req, res) => {
}
});
export default task;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment