-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
580 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import db from '../database/models'; | ||
import Response from '../helpers/Response'; | ||
|
||
|
||
const { | ||
Trip, Branch, User, Stop, Accomodation | ||
} = db; | ||
|
||
/** | ||
* Users' Request controller | ||
*/ | ||
class TravelRequest { | ||
/** | ||
* @description Get a user's travel requests | ||
* @param {Object} req request object | ||
* @param {Object} res response object | ||
* @returns {Object} Object | ||
*/ | ||
static async getUserTrips(req, res) { | ||
const { id } = req.payload.payload; | ||
try { | ||
const userTrips = await Trip.findAll({ | ||
where: { userId: id }, | ||
include: [ | ||
{ | ||
model: Stop, | ||
as: 'stop', | ||
attributes: ['destinationBranchId', 'accomodationId'], | ||
include: [{ model: Branch, as: 'branch', attributes: ['id', 'name', 'locationId'] }, | ||
{ model: Accomodation, as: 'accomodation', attributes: ['id', 'name', 'capacity', 'status'] }] | ||
}, | ||
{ model: User, as: 'user', attributes: ['firstName', 'lastName'] }, | ||
{ model: Branch, as: 'branch', attributes: ['name', 'locationId'] } | ||
] | ||
}); | ||
|
||
return res.status(200).send(new Response(true, 200, 'User requests successfully retrieved', userTrips)); | ||
} catch (err) { | ||
return res.status(500).send(new Response(false, 500, err.message)); | ||
} | ||
} | ||
} | ||
|
||
export default TravelRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Trips', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
}, | ||
type: { | ||
type: Sequelize.ENUM('oneway', 'return', 'multiple'), | ||
allowNull: false, | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
}, | ||
status: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('pending', 'approved', 'rejected'), | ||
defaultValue: 'pending', | ||
}, | ||
startBranchId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
}, | ||
reason: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
tripDate: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
returnDate: { | ||
type: Sequelize.DATE, | ||
allowNull: true | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: queryInterface => queryInterface.dropTable('Trips') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Locations', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
}, | ||
companyId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Companies', | ||
key: 'id' | ||
} | ||
}, | ||
country: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
city: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: queryInterface => queryInterface.dropTable('Locations') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Branches', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
locationId: { | ||
type: Sequelize.UUID, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Locations', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: queryInterface => queryInterface.dropTable('Branches') | ||
}; |
43 changes: 43 additions & 0 deletions
43
src/database/migrations/20190828150143-create-accomodation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Accomodations', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()') | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
branchId: { | ||
type: Sequelize.UUID, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Branches', | ||
key: 'id' | ||
} | ||
}, | ||
capacity: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false | ||
}, | ||
status: { | ||
type: Sequelize.ENUM('available', 'filled'), | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: queryInterface => queryInterface.dropTable('Accomodations') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Stops', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
}, | ||
destinationBranchId: { | ||
type: Sequelize.UUID, | ||
onDelete: 'RESTRICT', | ||
onUpdate: 'RESTRICT', | ||
references: { | ||
model: 'Branches', | ||
key: 'id', | ||
} | ||
}, | ||
accomodationId: { | ||
type: Sequelize.UUID, | ||
onDelete: 'RESTRICT', | ||
onUpdate: 'RESTRICT', | ||
references: { | ||
model: 'Accomodations', | ||
key: 'id' | ||
} | ||
}, | ||
tripId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
onDelete: 'RESTRICT', | ||
onUpdate: 'RESTRICT', | ||
references: { | ||
model: 'Trips', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: queryInterface => queryInterface.dropTable('Stops') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Accomodation = sequelize.define('Accomodation', { | ||
name: DataTypes.STRING, | ||
branchId: DataTypes.UUID, | ||
capacity: DataTypes.INTEGER, | ||
status: DataTypes.STRING | ||
}, {}); | ||
Accomodation.associate = (models) => { | ||
Accomodation.belongsTo(models.Branch, { | ||
foreignKey: 'branchId', | ||
as: 'branch', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Accomodation.hasMany(models.Stop, { | ||
foreignKey: 'accomodationId', | ||
as: 'stop', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
}; | ||
return Accomodation; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Branch = sequelize.define('Branch', { | ||
name: DataTypes.STRING, | ||
locationId: DataTypes.UUID | ||
}, {}); | ||
Branch.associate = (models) => { | ||
Branch.belongsTo(models.Location, { | ||
foreignKey: 'locationId', | ||
as: 'location', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Branch.hasMany(models.Trip, { | ||
foreignKey: 'startBranchId', | ||
as: 'trip', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Branch.hasMany(models.Accomodation, { | ||
foreignKey: 'branchId', | ||
as: 'accomodation', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Branch.hasMany(models.Stop, { | ||
foreignKey: 'destinationBranchId', | ||
as: 'stop', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
}; | ||
return Branch; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Location = sequelize.define('Location', { | ||
companyId: DataTypes.UUID, | ||
country: DataTypes.STRING, | ||
city: DataTypes.STRING | ||
}, {}); | ||
Location.associate = (models) => { | ||
Location.belongsTo(models.Company, { | ||
foreignKey: 'companyId', | ||
as: 'company', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Location.hasMany(models.Branch, { | ||
foreignKey: 'locationId', | ||
as: 'branch', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
}; | ||
return Location; | ||
}; |
Oops, something went wrong.