added bullpen session controller and route

This commit is contained in:
Sascha Kühl 2025-03-21 17:58:39 +01:00
parent f7f80ac690
commit 5d9f9c89fc
16 changed files with 115 additions and 77 deletions

View File

@ -18,7 +18,7 @@ app.use(express.urlencoded({ extended: true }));
const db = require("./models/index"); const db = require("./models/index");
const Role = db.Role; const Role = db.Role;
const User = db.User; const User = db.User;
const PitchType = db.pitchType; const PitchType = db.PitchType;
db.sequelize.sync(); db.sequelize.sync();
// force: true will drop the table if it already exists // force: true will drop the table if it already exists
@ -40,8 +40,7 @@ require('./routes/pitchType.routes')(app);
function initial() { function initial() {
Role.bulkCreate([ Role.bulkCreate([
{ name: 'user' }, { name: 'user' },
{ name: 'moderator' }, { name: 'administrator' },
{ name: 'administrato' },
]); ]);
User.bulkCreate([ User.bulkCreate([
{ firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: bcrypt.hashSync('nolan', 8) }, { firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: bcrypt.hashSync('nolan', 8) },

View File

@ -6,7 +6,7 @@ const Op = db.Sequelize.Op;
const jwt = require("jsonwebtoken"); const jwt = require("jsonwebtoken");
exports.signup = (req, res) => { exports.register = (req, res) => {
// Save User to Database // Save User to Database
User.create({ User.create({
firstName: req.body.firstName, firstName: req.body.firstName,
@ -42,7 +42,7 @@ exports.signup = (req, res) => {
}); });
}; };
exports.signin = (req, res) => { exports.login = (req, res) => {
User.findOne({ User.findOne({
where: { where: {
email: req.body.email email: req.body.email
@ -92,6 +92,10 @@ exports.signin = (req, res) => {
}); });
}; };
exports.logout = (req, res) => {
}
exports.refreshToken = async (req, res) => { exports.refreshToken = async (req, res) => {
const { refreshToken: requestToken } = req.body; const { refreshToken: requestToken } = req.body;
@ -113,7 +117,7 @@ exports.refreshToken = async (req, res) => {
RefreshToken.destroy({ where: { id: refreshToken.id } }); RefreshToken.destroy({ where: { id: refreshToken.id } });
res.status(403).json({ res.status(403).json({
message: "Refresh token was expired. Please make a new signin request", message: "Refresh token was expired. Please make a new login request",
}); });
return; return;
} }

View File

@ -0,0 +1,6 @@
const db = require("../models/index");
const BullpenSession = db.BullpenSession;
exports.insert = (req, res) => {
BullpenSession.create(req.body)
};

View File

@ -1,5 +1,5 @@
const db = require("../models/index"); const db = require("../models/index");
const PitchType = db.pitchType; const PitchType = db.PitchType;
const Op = db.Sequelize.Op; const Op = db.Sequelize.Op;
exports.findAll = (req, res) => { exports.findAll = (req, res) => {

View File

@ -1,5 +1,5 @@
const db = require("../models/index"); const db = require("../models/index");
const User = db.user; const User = db.User;
const Op = db.Sequelize.Op; const Op = db.Sequelize.Op;
exports.findAll = (req, res) => { exports.findAll = (req, res) => {

View File

@ -27,6 +27,9 @@ module.exports = {
onUpdate: 'CASCADE', onUpdate: 'CASCADE',
onDelete: 'CASCADE' onDelete: 'CASCADE'
}, },
pitchTime: {
type: Sequelize.DATE,
},
aimedArea: { aimedArea: {
type: Sequelize.INTEGER type: Sequelize.INTEGER
}, },

View File

@ -1,7 +1,5 @@
'use strict'; 'use strict';
const { const { Model } = require('sequelize');
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
class BullpenSession extends Model { class BullpenSession extends Model {
/** /**
@ -42,4 +40,4 @@ module.exports = (sequelize, DataTypes) => {
tableName: 'BullpenSessions' tableName: 'BullpenSessions'
}); });
return BullpenSession; return BullpenSession;
}; };

View File

@ -1,7 +1,4 @@
'use strict'; const { Model } = require('sequelize');
const {
Model, DataTypes
} = require('sequelize');
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
class Pitch extends Model { class Pitch extends Model {
/** /**
@ -34,6 +31,10 @@ module.exports = (sequelize, DataTypes) => {
key: 'id' key: 'id'
} }
}, },
pitchTime: {
type: DataTypes.DATE,
allowNull: false
},
aimedArea: { aimedArea: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false allowNull: false

View File

@ -11,14 +11,15 @@ module.exports = function(app) {
}); });
app.post( app.post(
"/api/auth/signup", "/api/auth/register",
[ [
verifySignUp.checkDuplicateUsernameOrEmail, verifySignUp.checkDuplicateUsernameOrEmail,
verifySignUp.checkRolesExisted verifySignUp.checkRolesExisted
], ],
controller.signup controller.register
); );
app.post("/api/auth/signin", controller.signin); app.post("/api/auth/login", controller.login);
app.post("/api/auth/logout", controller.logout);
app.post("/api/auth/refreshtoken", controller.refreshToken); app.post("/api/auth/refreshtoken", controller.refreshToken);
}; };

View File

@ -10,6 +10,12 @@ module.exports = function(app) {
next(); next();
}); });
app.get("/api/pitch_types", controller.findAll); app.get(
app.get("/api/pitch_types/:id", controller.findOne); "/api/pitch_types",
[authJwt.verifyToken],
controller.findAll);
app.get(
"/api/pitch_types/:id",
[authJwt.verifyToken],
controller.findOne);
}; };

View File

@ -17,11 +17,6 @@ module.exports = {
}, },
async down (queryInterface, Sequelize) { async down (queryInterface, Sequelize) {
/** return queryInterface.dropTable('Roles');
* Add commands to revert seed here.
*
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
} }
}; };

View File

@ -15,11 +15,6 @@ module.exports = {
}, },
async down (queryInterface, Sequelize) { async down (queryInterface, Sequelize) {
/** return queryInterface.dropTable('Users');
* Add commands to revert seed here.
*
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
} }
}; };

View File

@ -3,23 +3,18 @@
/** @type {import('sequelize-cli').Migration} */ /** @type {import('sequelize-cli').Migration} */
module.exports = { module.exports = {
async up (queryInterface, Sequelize) { async up (queryInterface, Sequelize) {
/** return queryInterface.bulkInsert('PitchTypes',[
* Add seed commands here. { name: 'Fastball', abbreviation: 'FB', createdAt: new Date(), updatedAt: new Date() },
* { name: 'Curveball', abbreviation: 'CB', createdAt: new Date(), updatedAt: new Date() },
* Example: { name: 'Slider', abbreviation: 'SL', createdAt: new Date(), updatedAt: new Date() },
* await queryInterface.bulkInsert('People', [{ { name: 'Changeup', abbreviation: 'CH', createdAt: new Date(), updatedAt: new Date() },
* name: 'John Doe', { name: 'Cutter', abbreviation: 'CUT', createdAt: new Date(), updatedAt: new Date() },
* isBetaMember: false { name: 'Sweeper', abbreviation: 'SW', createdAt: new Date(), updatedAt: new Date() },
* }], {}); { name: 'Slurve', abbreviation: 'SLV', createdAt: new Date(), updatedAt: new Date() },
*/ ]);
}, },
async down (queryInterface, Sequelize) { async down (queryInterface, Sequelize) {
/** return queryInterface.dropTable('PitchTypes');
* Add commands to revert seed here.
*
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
} }
}; };

View File

@ -0,0 +1,20 @@
const bullpenData = {
pitcherId: 1,
startedAt: new Date(2025, 3, 22, 16, 5, 13),
finishedAt: new Date(2025, 3, 22, 16, 23, 45),
pitches: [{
pitchTypeId: 1,
pitchTime: new Date(2025, 3, 22, 16, 7, 21),
aimedArea: 11,
hitArea: 12
}, {
pitchTypeId: 4,
pitchTime: new Date(2025, 3, 22, 16, 9, 57),
aimedArea: 6,
hitArea: 14
}]
};
module.exports = {
bullpenData
}

View File

@ -0,0 +1,28 @@
const request = require("supertest")
const {
expect,
describe,
test,
afterAll,
} = require('@jest/globals');
const app = require("../app")
describe("Test retrieving pitch types", () => {
test("should retrieve all pitch types", async () => {
let user = {};
let response = await request(app)
.post("/api/auth/login")
.send({
email: 'ryan.nolan@bullpen.com',
password: 'nolan'
});
user = response.body;
response = await request(app)
.get('/api/pitch_types')
.set('x-access-token', user.accessToken);
expect(response.statusCode).toBe(200);
expect(response.body.length).toEqual(7);
});
});

View File

@ -3,8 +3,6 @@ const {
expect, expect,
describe, describe,
test, test,
beforeAll,
afterAll,
} = require('@jest/globals'); } = require('@jest/globals');
const app = require("../app") const app = require("../app")
@ -13,38 +11,27 @@ const { signupUser } = require("./data/user.test.data")
const res = require("express/lib/response"); const res = require("express/lib/response");
describe("Test user authentication", () => { describe("Test user authentication", () => {
test("should signup a user", done => { test("should register a user", async () => {
request(app) const response = await request(app)
.post("/api/auth/signup") .post("/api/auth/register")
.send(signupUser) .send(signupUser);
.then( res => { expect(response.header['content-type']).toBe('application/json; charset=utf-8');
expect(res.header['content-type']).toBe('application/json; charset=utf-8'); expect(response.statusCode).toBe(200);
expect(res.statusCode).toBe(200);
done();
});
}); });
test("Test user login", done => { test("Test user login", async() => {
let user = {}; let response = await request(app)
request(app) .post("/api/auth/login")
.post("/api/auth/signin")
.send({ .send({
email: 'ryan.nolan@bullpen.com', email: 'ryan.nolan@bullpen.com',
password: 'nolan' password: 'nolan'
})
.then( res => {
expect(res.statusCode).toBe(200);
expect(res.body.accessToken).not.toBeNull();
console.log(res.body);
user = res.body;
done();
// }).then(() => {
// request(app)
// .get(`/api/users/${user.id}`)
// .then( res2 => {
// expect(res2.statusCode).toBe(200);
// })
//
}); });
expect(response.statusCode).toBe(200);
expect(response.body.accessToken).not.toBeNull();
const user = response.body;
response = await request(app)
.get(`/api/users/${user.id}`)
.set('x-access-token', user.accessToken);
expect(response.statusCode).toBe(200);
}); });
}); });