Skip to content

Commit

Permalink
Merge pull request #1 from MisterCxmpy/develop
Browse files Browse the repository at this point in the history
see comment
  • Loading branch information
MisterCxmpy authored Aug 7, 2023
2 parents 97c2f55 + 841264f commit dbc205d
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 46 deletions.
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"pg": "^8.11.2",
Expand Down
24 changes: 16 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
const express = require('express');
const app = express()
const cookieParser = require('cookie-parser')
const cors = require('cors')

const AuthRouter = require('./routes/AuthRouter')
const TeamRouter = require('./routes/TeamRouter')
const { validateToken } = require('./utils/validateToken');

app.use(express.json())
const AuthRouter = require('./routes/AuthRouter');
const TeamRouter = require('./routes/TeamRouter');
const UserRouter = require('./routes/UserRouter');

app.use(express.json());
app.use(cookieParser());
app.use(cors({ credentials: true })) // add your client domain in here like { origin: 'http://localhost:5172' }

app.use('/auth', AuthRouter)
app.use('/teams', TeamRouter)
app.use('/teams', validateToken, TeamRouter)
app.use('/me', validateToken, UserRouter)

// currently can create teams and query teams for their associated members

// need to:

// add members to team
// remove members from team

// add tickets to backlog and assign them to members
// add first, last name and email to the user model, concattinate both to create username

// assign tickets to members
// edit state of tickets and reassign them to new members

// extract route logic to controllers
// restrict access to services

app.get('/', (req, res, next) => {
res.json({ message: 'ok. api is working' })
})

module.exports = app;
module.exports = app;
7 changes: 1 addition & 6 deletions src/models/TeamModel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../database/sequelize'); // Import your Sequelize instance
const User = require('./UserModel');

const { v4: uuidv4 } = require('uuid');

const Team = sequelize.define('Team', {
Expand All @@ -17,10 +15,7 @@ const Team = sequelize.define('Team', {
backlog: {
type: DataTypes.ARRAY(DataTypes.INTEGER), // Assuming backlog contains ticket IDs
defaultValue: [] // Initialize with an empty array
}
},
});

// Define associations
Team.hasMany(User, { foreignKey: 'team_id', as: 'members' });

module.exports = Team;
9 changes: 4 additions & 5 deletions src/models/TicketModel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../database/sequelize');
const User = require('./UserModel')
const Team = require('./TeamModel'); // Import the Team model
const User = require('./UserModel'); // Import the Team model

const Ticket = sequelize.define('Ticket', {
id: {
Expand All @@ -17,14 +18,12 @@ const Ticket = sequelize.define('Ticket', {
},
state: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.TEXT
defaultValue: 'new' // new, todo, closed, removed
}
});

// Define associations
Ticket.belongsTo(Team, { foreignKey: 'team_id', as: 'team' });
Ticket.belongsTo(User, { foreignKey: 'owner_id', as: 'owner' });

module.exports = Ticket;
4 changes: 4 additions & 0 deletions src/models/UserModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../database/sequelize');
const Team = require('./TeamModel');

const User = sequelize.define('User', {
id: {
Expand All @@ -18,4 +19,7 @@ const User = sequelize.define('User', {
}
});

User.belongsToMany(Team, { through: 'UserTeam' });
Team.belongsToMany(User, { through: 'UserTeam' });

module.exports = User;
42 changes: 39 additions & 3 deletions src/routes/TeamRouter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const router = require('express').Router();
const TeamService = require('../services/TeamService');
const TicketService = require('../services/TicketService');

// DEV OPERATIONS

Expand All @@ -9,11 +10,11 @@ router.get('/', async (req, res) => {

res.json(teams)
} catch (error) {
console.log(error)
res.status(500).json({ error: error.message });
}
})


// Team CRUD Operations

router.post('/new', async (req, res) => {
Expand All @@ -35,6 +36,18 @@ router.get('/:teamId', async (req, res) => {
}
});

router.get('/:teamId/join', async (req, res) => {
try {
const teamId = req.params.teamId;
const teamDetails = await TeamService.addMemberToTeam(teamId, req.userId);

res.json(teamDetails);
} catch (error) {
console.log(error.message)
res.status(500).json({ error: 'Failed to fetch team details.' });
}
});

router.patch('/:teamId', async (req, res) => {
try {
const teamId = req.params.teamId;
Expand All @@ -55,18 +68,28 @@ router.delete('/:teamId', async (req, res) => {
}
});

// Team Tickets CRUD Operations
// Team Tickets Operations

router.get('/:teamId/tasks', async (req, res) => {
try {
const teamId = req.params.teamId;
const teamTasks = await TeamService.getTeamTasks(teamId);
const teamTasks = await TeamService.getBacklog(teamId);
res.json(teamTasks);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch team tasks.' });
}
});

router.post('/:teamId/tasks/new', async (req, res) => {
try {
const teamId = req.params.teamId;
const team = await TicketService.create(teamId, req.body);
res.json(team);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch team tasks.' });
}
});

router.get('/:teamId/:memberId/tasks', async (req, res) => {
try {
const teamId = req.params.teamId;
Expand All @@ -78,4 +101,17 @@ router.get('/:teamId/:memberId/tasks', async (req, res) => {
}
});

// Team Memebrs Operations

router.get('/:teamId/members', async (req, res) => {
try {
const teamId = req.params.teamId;
const teamTasks = await TeamService.getMembers(teamId);
res.json(teamTasks);
} catch (error) {
console.log(error)
res.status(500).json({ error: 'Failed to fetch team members.' });
}
});

module.exports = router;
16 changes: 16 additions & 0 deletions src/routes/UserRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const router = require('express').Router();
const UserService = require('../services/UserService');

router.get('/teams', async (req, res) => {
try {
const teams = await UserService.getUserTeams(req.userId)

res.json(teams.map(t => t.team_name))
} catch (error) {
console.log(error)
res.json({ message: error.message })
}
})


module.exports = router;
Loading

0 comments on commit dbc205d

Please sign in to comment.