Skip to content

Commit

Permalink
First Beta Release. (WithOut Authentication)
Browse files Browse the repository at this point in the history
Added Pluckable URL for users and projects
Added Relationships among entities.
Deletions Handled Gracefully.
Completed Middlewares.
Projects sharing among members.
Added Roles
.... and many bug fixes
  • Loading branch information
Sithira committed Jun 14, 2018
1 parent 65f29be commit 38bc376
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 97 deletions.
9 changes: 9 additions & 0 deletions app/Controllers/Http/ProjectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class ProjectController
// create the new project in the database.
const project = await Project.create(request.all());

// get the team
let team = request.post().team;

// add the project to the team
team.merge({_project_id: project._id});

// save the team
await team.save();

// return the response with the newly created data
return response.status(201).json({
status: "OK",
Expand Down
64 changes: 41 additions & 23 deletions app/Controllers/Http/TeamController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict';

const Team = use("App/Models/Team");

Expand All @@ -7,15 +7,14 @@ const User = use("App/Models/User");
class TeamController {

// Todo: Only Admins can access this
// Todo: Add members for the team.

/**
* Get all the teams that are in the system.
*
* @param response
* @returns {Promise<*|{limit, strict, types}|Promise<any>>}
*/
static async index({response})
async index({response})
{
return response.status(200).json({
status: "OK",
Expand Down Expand Up @@ -128,7 +127,7 @@ class TeamController {

return response.status(200).json({
status: "OK",
message: `The ticket with the id: ${teamId} has been successfully soft deleted`
message: `The team with the id: ${teamId} has been successfully soft deleted`
})
}

Expand All @@ -150,33 +149,41 @@ class TeamController {
// get the team with users in it
let team = await Team.with(['users']).find(teamId);

console.log(team);

// count the number of team members
let teamUserCount = team.$relations.users.rows.length;

// limit the number of heads per team
if (teamUserCount >= 5)
if (teamUserCount <= 5)
{

// return the error message
return response.status(400).json({
status: "ERROR",
message: `Maximum user count for team ${team.name} of ${teamUserCount} / 05 has reached`
});
}
// get the user the request body
let user = request.post().user;

// get the user the request body
let user = request.post().user;
if (!user.hasOwnProperty("type") && !(user.type === "admin" || user.type === "manager"))
{
// add the user to the team instance (many-to-many)
user.merge({_team_id: team._id, type: "user"});
}

// add the user to the team instance (many-to-many)
await team.users().save(user);
await user.save();

// return the response
return response.status(200).json({
status: "OK",
data: {
team: team,
user: user
}
// return the response
return response.status(200).json({
status: "OK",
data: {
team: team,
user: user
}
});

}

// return the error message
return response.status(400).json({
status: "ERROR",
message: `Maximum user count for team ${team.name} of ${teamUserCount} / 05 has reached`
});

}
Expand All @@ -194,8 +201,19 @@ class TeamController {
// find the team with users
let team = await Team.with(['users']).find(request.post().team._id);

let user = request.post().user;

if (!user.hasOwnProperty("type") && !(user.type === "admin" || user.type === "manager"))
{
// add the user to the team instance (many-to-many)
user.merge({type: "client"});
}

// detach the user from the team
await team.users().detach([request.post().user._id]);
user._team_id = null;

// save the user.
await user.save();

// return the response
return response.status(200).json({
Expand Down
4 changes: 3 additions & 1 deletion app/Controllers/Http/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class UserController {
{

// get the force delete parameter.
const { forceDestroy = "false" } = request.all();
const {
forceDestroy = "false",
} = request.all();

// get the user form the database
let user = request.post().user;
Expand Down
17 changes: 13 additions & 4 deletions app/Middleware/ProjectFindFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ class ProjectFindFail {
async handle({request, response, params}, next) {

const {
forceDestroy = "false"
forceDestroy = "false",
relations = "false",
} = request.all();

// get the project from the database.
const project = await Project.find(params.projectId);
let project = null;

if (relations === "true")
{
project = await Project.with(['client', 'team']).find(params.projectId);
}
else
{
project = await Project.find(params.projectId)
}

// check for the project existence
if (project === null || (forceDestroy === "false" && project.$attributes.hasOwnProperty("deleted_at"))) {
if (project === null || project === undefined || (forceDestroy === "false" && project.$attributes.hasOwnProperty("deleted_at"))) {
// return the error response.
return response.status(400).json({
status: "ERROR",
Expand Down
40 changes: 36 additions & 4 deletions app/Middleware/SoftDeleted/ProjectFindFailSoftDeleted.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,45 @@ class ProjectFindFailSoftDeleted

async handle ({ request, response, params }, next) {

const projects = await Project.all();
let projects = null;

const {
forceDestroy="false",
showAll = "false"
let {
pluck = "false",
showAll = "false",
plucks
} = request.all();

if (pluck === "true")
{

if (plucks === undefined || plucks == null)
{

projects = await Project.query().setVisible(['_id', 'name']).fetch();

}
else
{

plucks = JSON.parse(plucks);

if (plucks.length >= 1)
{
projects = await Project.query().setVisible(plucks).fetch();
}
else
{
projects = await Project.query().setVisible(['_id', 'name']).fetch();
}

}

}
else
{
projects = await Project.all();
}

if (showAll === "false")
{
for (let i = 0; i < projects.rows.length; i++)
Expand Down
46 changes: 43 additions & 3 deletions app/Middleware/SoftDeleted/UserFindFailSoftDeleted.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,52 @@ class UserFindFailSoftDeleted
async handle ({ request }, next)
{

const users = await User.all();
let users = null;

const {
showAll = "false"
let {
showAll = "false",
pluck = "false",
plucks
} = request.all();

// if we have request to pluck from the users
if (pluck === "true")
{
// check if we have any defined plucks
if (plucks === undefined || plucks === null)
{

// since we dont have plucks defined, but still pluck is true
// show the ID and name
users = await User.query().setVisible(['_id', 'name']).fetch();
}
else
{

// if we have plucks
plucks = JSON.parse(plucks);

// check for the length
if (plucks.length >= 1)
{

// pluck using plucks
users = await User.query().setVisible(plucks).fetch();
}
else
{

// or else return the default pluck.
users = await User.query().setVisible(['_id', 'name']).fetch();
}
}
}
else
{
users = await User.all();
}

// if we need to see soft deleted users
if (showAll === "false")
{
for (let i = 0; i < users.rows.length; i++)
Expand Down
5 changes: 3 additions & 2 deletions app/Middleware/SprintFindFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class SprintFindFail {
let sprint = await Sprint.find(params.sprintId);

// check for the users
if (sprint === null || (forceDestroy === "false" && sprint.$attributes.hasOwnProperty("deleted_at")))
if (sprint === null || sprint === undefined || (forceDestroy === "false" && sprint.$attributes.hasOwnProperty("deleted_at")))
{

// return 400 response with the message, if the user does not exists.
return response.status(400).json({
status: "ERROR",
Expand All @@ -28,6 +27,8 @@ class SprintFindFail {
// add the user object to the request body.
request.body.sprint = sprint;

//console.log("Passing from middleware...");

// carry on with the request.
await next()

Expand Down
22 changes: 20 additions & 2 deletions app/Middleware/TeamFindFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@ class TeamFindFail
async handle({request, response, params}, next)
{

let team = await Team.find(params.teamId);
// get the flags
const {
relations = "false"
} = request.all();

if (team === null)
// define the variable for the team
let team = null;

// check for request for the members
if (relations === "true")
{
// load the relationship with users.
team = await Team.with(['users', 'projects']).find(params.teamId);
}
else
{
team = await Team.find(params.teamId);
}

// check if the team exists or team has been soft deleted
if (team === null || team.$attributes.hasOwnProperty("deleted_at"))
{
return response.status(400).json({
status: "ERROR",
Expand Down
38 changes: 38 additions & 0 deletions app/Middleware/TeamFindFromBody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const Team = use('App/Models/Team');

class TeamFindFromBody {
async handle({request, response}, next) {

const {
_team_id
} = request.all();

if (_team_id === undefined || _team_id === null)
{
return response.status(400).json({
status: 'ERROR',
message: `No TeamId found.`
});
}

let team = await Team.find(_team_id);

if (team.$attributes.hasOwnProperty("deleted_at"))
{
return response.status(400).json({
status: 'ERROR',
message: `No with team id: ${_team_id} has been removed or does not exists`
});
}

// add the request to the body.
request.body.team = team;

// call next to advance the request
await next()
}
}

module.exports = TeamFindFromBody;
Loading

0 comments on commit 38bc376

Please sign in to comment.