Skip to content

Commit

Permalink
Merge 1005c5c into 3df78fb
Browse files Browse the repository at this point in the history
  • Loading branch information
tvpeter committed Aug 31, 2019
2 parents 3df78fb + 1005c5c commit e6181a7
Show file tree
Hide file tree
Showing 23 changed files with 580 additions and 9 deletions.
44 changes: 44 additions & 0 deletions src/controllers/TravelRequest.js
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;
10 changes: 10 additions & 0 deletions src/database/migrations/20190821105503-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ module.exports = {
type: Sequelize.ENUM('active', 'inactive', 'unverified'),
defaultValue: 'unverified'
},
favorites: {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false,
set: (value) => {
if (value === 'true') value = true;
if (value === 'false') value = false;
this.setDataValue('favorites', value);
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
57 changes: 57 additions & 0 deletions src/database/migrations/20190828140409-create-trip.js
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')
};
40 changes: 40 additions & 0 deletions src/database/migrations/20190828145910-create-location.js
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')
};
35 changes: 35 additions & 0 deletions src/database/migrations/20190828145911-create-branch.js
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 src/database/migrations/20190828150143-create-accomodation.js
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')
};
49 changes: 49 additions & 0 deletions src/database/migrations/20190828150144-create-stop.js
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')
};
23 changes: 23 additions & 0 deletions src/database/models/accomodation.js
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;
};
33 changes: 33 additions & 0 deletions src/database/models/branch.js
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;
};
13 changes: 12 additions & 1 deletion src/database/models/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ module.exports = (sequelize, DataTypes) => {
owner: DataTypes.STRING
}, {});
Company.associate = (models) => {
// associations can be defined here
Company.hasMany(models.Location, {
foreignKey: 'companyId',
as: 'location',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Company.hasMany(models.User, {
foreignKey: 'companyId',
as: 'user',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Company;
};
22 changes: 22 additions & 0 deletions src/database/models/location.js
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;
};
Loading

0 comments on commit e6181a7

Please sign in to comment.