added bullpen session controller and route
This commit is contained in:
parent
f7f80ac690
commit
5d9f9c89fc
|
|
@ -18,7 +18,7 @@ app.use(express.urlencoded({ extended: true }));
|
|||
const db = require("./models/index");
|
||||
const Role = db.Role;
|
||||
const User = db.User;
|
||||
const PitchType = db.pitchType;
|
||||
const PitchType = db.PitchType;
|
||||
|
||||
db.sequelize.sync();
|
||||
// force: true will drop the table if it already exists
|
||||
|
|
@ -40,8 +40,7 @@ require('./routes/pitchType.routes')(app);
|
|||
function initial() {
|
||||
Role.bulkCreate([
|
||||
{ name: 'user' },
|
||||
{ name: 'moderator' },
|
||||
{ name: 'administrato' },
|
||||
{ name: 'administrator' },
|
||||
]);
|
||||
User.bulkCreate([
|
||||
{ firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: bcrypt.hashSync('nolan', 8) },
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ const Op = db.Sequelize.Op;
|
|||
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
exports.signup = (req, res) => {
|
||||
exports.register = (req, res) => {
|
||||
// Save User to Database
|
||||
User.create({
|
||||
firstName: req.body.firstName,
|
||||
|
|
@ -42,7 +42,7 @@ exports.signup = (req, res) => {
|
|||
});
|
||||
};
|
||||
|
||||
exports.signin = (req, res) => {
|
||||
exports.login = (req, res) => {
|
||||
User.findOne({
|
||||
where: {
|
||||
email: req.body.email
|
||||
|
|
@ -92,6 +92,10 @@ exports.signin = (req, res) => {
|
|||
});
|
||||
};
|
||||
|
||||
exports.logout = (req, res) => {
|
||||
|
||||
}
|
||||
|
||||
exports.refreshToken = async (req, res) => {
|
||||
const { refreshToken: requestToken } = req.body;
|
||||
|
||||
|
|
@ -113,7 +117,7 @@ exports.refreshToken = async (req, res) => {
|
|||
RefreshToken.destroy({ where: { id: refreshToken.id } });
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
const db = require("../models/index");
|
||||
const BullpenSession = db.BullpenSession;
|
||||
|
||||
exports.insert = (req, res) => {
|
||||
BullpenSession.create(req.body)
|
||||
};
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
const db = require("../models/index");
|
||||
const PitchType = db.pitchType;
|
||||
const PitchType = db.PitchType;
|
||||
const Op = db.Sequelize.Op;
|
||||
|
||||
exports.findAll = (req, res) => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
const db = require("../models/index");
|
||||
const User = db.user;
|
||||
const User = db.User;
|
||||
const Op = db.Sequelize.Op;
|
||||
|
||||
exports.findAll = (req, res) => {
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ module.exports = {
|
|||
onUpdate: 'CASCADE',
|
||||
onDelete: 'CASCADE'
|
||||
},
|
||||
pitchTime: {
|
||||
type: Sequelize.DATE,
|
||||
},
|
||||
aimedArea: {
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
'use strict';
|
||||
const {
|
||||
Model
|
||||
} = require('sequelize');
|
||||
const { Model } = require('sequelize');
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class BullpenSession extends Model {
|
||||
/**
|
||||
|
|
@ -42,4 +40,4 @@ module.exports = (sequelize, DataTypes) => {
|
|||
tableName: 'BullpenSessions'
|
||||
});
|
||||
return BullpenSession;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
'use strict';
|
||||
const {
|
||||
Model, DataTypes
|
||||
} = require('sequelize');
|
||||
const { Model } = require('sequelize');
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class Pitch extends Model {
|
||||
/**
|
||||
|
|
@ -34,6 +31,10 @@ module.exports = (sequelize, DataTypes) => {
|
|||
key: 'id'
|
||||
}
|
||||
},
|
||||
pitchTime: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false
|
||||
},
|
||||
aimedArea: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
|
|
|
|||
|
|
@ -11,14 +11,15 @@ module.exports = function(app) {
|
|||
});
|
||||
|
||||
app.post(
|
||||
"/api/auth/signup",
|
||||
"/api/auth/register",
|
||||
[
|
||||
verifySignUp.checkDuplicateUsernameOrEmail,
|
||||
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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -10,6 +10,12 @@ module.exports = function(app) {
|
|||
next();
|
||||
});
|
||||
|
||||
app.get("/api/pitch_types", controller.findAll);
|
||||
app.get("/api/pitch_types/:id", controller.findOne);
|
||||
app.get(
|
||||
"/api/pitch_types",
|
||||
[authJwt.verifyToken],
|
||||
controller.findAll);
|
||||
app.get(
|
||||
"/api/pitch_types/:id",
|
||||
[authJwt.verifyToken],
|
||||
controller.findOne);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -17,11 +17,6 @@ module.exports = {
|
|||
},
|
||||
|
||||
async down (queryInterface, Sequelize) {
|
||||
/**
|
||||
* Add commands to revert seed here.
|
||||
*
|
||||
* Example:
|
||||
* await queryInterface.bulkDelete('People', null, {});
|
||||
*/
|
||||
return queryInterface.dropTable('Roles');
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,11 +15,6 @@ module.exports = {
|
|||
},
|
||||
|
||||
async down (queryInterface, Sequelize) {
|
||||
/**
|
||||
* Add commands to revert seed here.
|
||||
*
|
||||
* Example:
|
||||
* await queryInterface.bulkDelete('People', null, {});
|
||||
*/
|
||||
return queryInterface.dropTable('Users');
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,23 +3,18 @@
|
|||
/** @type {import('sequelize-cli').Migration} */
|
||||
module.exports = {
|
||||
async up (queryInterface, Sequelize) {
|
||||
/**
|
||||
* Add seed commands here.
|
||||
*
|
||||
* Example:
|
||||
* await queryInterface.bulkInsert('People', [{
|
||||
* name: 'John Doe',
|
||||
* isBetaMember: false
|
||||
* }], {});
|
||||
*/
|
||||
return queryInterface.bulkInsert('PitchTypes',[
|
||||
{ name: 'Fastball', abbreviation: 'FB', createdAt: new Date(), updatedAt: new Date() },
|
||||
{ name: 'Curveball', abbreviation: 'CB', createdAt: new Date(), updatedAt: new Date() },
|
||||
{ name: 'Slider', abbreviation: 'SL', createdAt: new Date(), updatedAt: new Date() },
|
||||
{ name: 'Changeup', abbreviation: 'CH', createdAt: new Date(), updatedAt: new Date() },
|
||||
{ name: 'Cutter', abbreviation: 'CUT', createdAt: new Date(), updatedAt: new Date() },
|
||||
{ name: 'Sweeper', abbreviation: 'SW', createdAt: new Date(), updatedAt: new Date() },
|
||||
{ name: 'Slurve', abbreviation: 'SLV', createdAt: new Date(), updatedAt: new Date() },
|
||||
]);
|
||||
},
|
||||
|
||||
async down (queryInterface, Sequelize) {
|
||||
/**
|
||||
* Add commands to revert seed here.
|
||||
*
|
||||
* Example:
|
||||
* await queryInterface.bulkDelete('People', null, {});
|
||||
*/
|
||||
return queryInterface.dropTable('PitchTypes');
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
@ -3,8 +3,6 @@ const {
|
|||
expect,
|
||||
describe,
|
||||
test,
|
||||
beforeAll,
|
||||
afterAll,
|
||||
} = require('@jest/globals');
|
||||
|
||||
const app = require("../app")
|
||||
|
|
@ -13,38 +11,27 @@ const { signupUser } = require("./data/user.test.data")
|
|||
const res = require("express/lib/response");
|
||||
|
||||
describe("Test user authentication", () => {
|
||||
test("should signup a user", done => {
|
||||
request(app)
|
||||
.post("/api/auth/signup")
|
||||
.send(signupUser)
|
||||
.then( res => {
|
||||
expect(res.header['content-type']).toBe('application/json; charset=utf-8');
|
||||
expect(res.statusCode).toBe(200);
|
||||
done();
|
||||
});
|
||||
test("should register a user", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/register")
|
||||
.send(signupUser);
|
||||
expect(response.header['content-type']).toBe('application/json; charset=utf-8');
|
||||
expect(response.statusCode).toBe(200);
|
||||
});
|
||||
|
||||
test("Test user login", done => {
|
||||
let user = {};
|
||||
request(app)
|
||||
.post("/api/auth/signin")
|
||||
test("Test user login", async() => {
|
||||
let response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
email: 'ryan.nolan@bullpen.com',
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue