-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from andela/ft-create-and-get-office-location-…
…168432541 #168432541 create and get office location
- Loading branch information
Showing
11 changed files
with
572 additions
and
0 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,65 @@ | ||
import models from '../models'; | ||
import response from '../utils/response'; | ||
import DbServices from '../services/dbServices'; | ||
import messages from '../utils/messages'; | ||
|
||
const { OfficeLocation } = models; | ||
const { serverError, noLocation } = messages; | ||
const { getAllRecord, create, getById } = DbServices; | ||
|
||
/** | ||
* get all office locations in controller | ||
* @param {Object} req - server request | ||
* @param {Object} res - server response | ||
* @returns {Object} - custom response | ||
*/ | ||
const getLocation = async (req, res) => { | ||
try { | ||
const allLoactions = await getAllRecord(OfficeLocation, {}); | ||
if (allLoactions.length === 0) return response(res, 200, 'success', { message: noLocation }); | ||
return response(res, 200, 'success', allLoactions); | ||
} catch (error) { | ||
return response(res, 500, 'error', serverError); | ||
} | ||
}; | ||
|
||
/** | ||
* get office location by id in controller | ||
* @param {Object} req - server request | ||
* @param {Object} res - server response | ||
* @returns {Object} - custom response | ||
*/ | ||
const getLocationById = async (req, res) => { | ||
const { locationId } = req.params; | ||
try { | ||
const location = await getById(OfficeLocation, locationId, {}); | ||
if (!location) return response(res, 200, 'success', { message: noLocation }); | ||
return response(res, 200, 'success', location); | ||
} catch (error) { | ||
return response(res, 500, 'error', serverError); | ||
} | ||
}; | ||
|
||
/** | ||
* insert office location in controller | ||
* @param {Object} req - server request | ||
* @param {Object} res - server response | ||
* @returns {Object} - custom response | ||
*/ | ||
const postLocation = async (req, res) => { | ||
const { | ||
name, description, address, phoneNo, latitude, longitude | ||
} = req.body; | ||
try { | ||
const createdLocation = await create(OfficeLocation, { | ||
name, description, address, phoneNo, latitude, longitude | ||
}); | ||
return response(res, 200, 'success', { message: createdLocation }); | ||
} catch (error) { | ||
return response(res, 500, 'error', serverError); | ||
} | ||
}; | ||
|
||
export default { | ||
getLocation, postLocation, getLocationById | ||
}; |
49 changes: 49 additions & 0 deletions
49
src/database/migrations/20190913121008-create-office-locations.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,49 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('OfficeLocations', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
description: { | ||
type: Sequelize.TEXT, | ||
allowNull: true | ||
}, | ||
address: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
phoneNo: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
latitude: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
longitude: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: true, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: true, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('OfficeLocations'); | ||
} | ||
}; |
56 changes: 56 additions & 0 deletions
56
src/database/seeders/20190914074114-demo-office-location.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,56 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkInsert('OfficeLocations', [ | ||
{ | ||
id: '122a0d86-8b78-4bb8-b28f-8e5f7811c477', | ||
name: 'Lagos Mainland Branch', | ||
description: 'GPRINTS NIGERIA, Osho Street, Lagos', | ||
address: 'GPRINTS NIGERIA, Osho Street, Lagos', | ||
phoneNo: '08022835496', | ||
latitude: '6.524990', | ||
longitude: '3.381760', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
id: '122a0d86-8b78-4bb8-b28f-8e5f7811c487', | ||
name: 'Abuja Branch', | ||
description: 'Atiku Abubakar Plaza, Trade Fair Complex,, Badagry Expressway, Trade Fair Complex 102109, Ojo', | ||
address: 'Atiku Abubakar Plaza, Trade Fair Complex,, Badagry Expressway, Trade Fair Complex 102109, Ojo', | ||
phoneNo: '08022835496', | ||
latitude: '-29.500030', | ||
longitude: '-63.693770', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
id: '122a0d86-8b78-4bb8-b28f-8e5f7811c489', | ||
name: 'Rivers Branch', | ||
description: 'Port Harcourt Rivers NG, 62 Aggrey Rd, Port Harcourt 500272, River State', | ||
address: 'Port Harcourt Rivers NG, 62 Aggrey Rd, Port Harcourt 500272, River State', | ||
phoneNo: '08022836496', | ||
latitude: '4.761790', | ||
longitude: '7.023810', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
id: '122a0d86-8b78-4bb8-b78f-8e5f7811c487', | ||
name: 'Kano Branch', | ||
description: '110 Murtala Mohammed Rd, Kofar Mazugal, Kano', | ||
address: '110 Murtala Mohammed Rd, Kofar Mazugal, Kano', | ||
phoneNo: '09023835496', | ||
latitude: '12.007560', | ||
longitude: '8.556870', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
], {}); | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkDelete('OfficeLocations', null, {}); | ||
} | ||
}; |
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,36 @@ | ||
export default (sequelize, DataTypes) => { | ||
const OfficeLocation = sequelize.define('OfficeLocation', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
description: { | ||
type: DataTypes.TEXT, | ||
allowNull: true, | ||
}, | ||
address: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
phoneNo: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
latitude: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
longitude: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}); | ||
|
||
return OfficeLocation; | ||
}; |
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,164 @@ | ||
import officeLocationController from '../../controllers/officeLocationController'; | ||
import checkBlacklist from '../../middlewares/blacklistMiddleware'; | ||
import { checkToken } from '../../middlewares/userMiddlewares'; | ||
import validate from '../../middlewares/validator'; | ||
import authorize from '../../middlewares/authorizer'; | ||
import roles from '../../utils/roles'; | ||
import officeLocationSchema from '../../validation/officeLocationSchema'; | ||
|
||
const { SUPER_ADMIN } = roles; | ||
|
||
const { getLocation, postLocation, getLocationById } = officeLocationController; | ||
const { createLocationSchema, getLocationByIdSchema } = officeLocationSchema; | ||
|
||
const officeLocationRoutes = (router) => { | ||
router.route('/officelocation') | ||
/** | ||
* @swagger | ||
* components: | ||
* schemas: | ||
* OfficeLocationResponse: | ||
* properties: | ||
* name: | ||
* type: string | ||
* description: | ||
* type: string | ||
* address: | ||
* type: string | ||
* phoneNo: | ||
* type: string | ||
* latitude: | ||
* type: string | ||
* longitude: | ||
* type: string | ||
* ErrorResponse: | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: error | ||
* data: | ||
* type: object | ||
*/ | ||
|
||
/** | ||
* @swagger | ||
* /api/v1/officelocation: | ||
* get: | ||
* tags: | ||
* - Office Locations | ||
* description: Get all office locations | ||
* produces: | ||
* - application/json | ||
* responses: | ||
* 200: | ||
* description: Get all office locations was successful | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: success | ||
* data: | ||
* type: array | ||
* description: array of office locations | ||
* items: | ||
* $ref: '#/components/schemas/OfficeLocationResponse' | ||
* 403: | ||
* description: Unauthorized | ||
* 500: | ||
* description: Internal Server error | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/ErrorResponse' | ||
* security: | ||
* - bearerAuth: [] | ||
*/ | ||
.get(checkToken, checkBlacklist, getLocation) | ||
|
||
/** | ||
* @swagger | ||
* components: | ||
* schemas: | ||
* Chat: | ||
* properties: | ||
* message: | ||
* type: string | ||
*/ | ||
|
||
/** | ||
* @swagger | ||
* /api/v1/officelocation: | ||
* post: | ||
* tags: | ||
* - Office Locations | ||
* description: Create new office location | ||
* produces: | ||
* - application/json | ||
* requestBody: | ||
* description: Office location data object | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/OfficeLocationResponse' | ||
* responses: | ||
* 200: | ||
* description: office location created successfully | ||
* 500: | ||
* description: Internal Server error | ||
* security: | ||
* - bearerAuth: [] | ||
*/ | ||
.post(checkToken, checkBlacklist, | ||
authorize([SUPER_ADMIN]), validate(createLocationSchema), postLocation); | ||
|
||
router.route('/officelocation/:locationId') | ||
/** | ||
* @swagger | ||
* /api/v1/officelocation/{locationId}: | ||
* get: | ||
* tags: | ||
* - Office Locations | ||
* description: Get office location by id | ||
* produces: | ||
* - application/json | ||
* parameters: | ||
* - in: path | ||
* name: locationId | ||
* schema: | ||
* type: string | ||
* required: true | ||
* responses: | ||
* 200: | ||
* description: Get office location was successful | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* status: | ||
* type: string | ||
* example: success | ||
* data: | ||
* type: object | ||
* description: object of office location | ||
* $ref: '#/components/schemas/OfficeLocationResponse' | ||
* | ||
* 403: | ||
* description: Unauthorized | ||
* 500: | ||
* description: Internal Server error | ||
* content: | ||
* application/json: | ||
* schema: | ||
* $ref: '#/components/schemas/ErrorResponse' | ||
* security: | ||
* - bearerAuth: [] | ||
*/ | ||
.get(checkToken, checkBlacklist, validate(getLocationByIdSchema), getLocationById); | ||
}; | ||
|
||
export default officeLocationRoutes; |
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
Oops, something went wrong.