-
Notifications
You must be signed in to change notification settings - Fork 5
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
22 changed files
with
644 additions
and
175 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
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
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,81 @@ | ||
import models from '../models'; | ||
import { STATUS } from '../helpers/constants'; | ||
import Response from '../helpers/responseHelper'; | ||
|
||
const { Profile } = models; | ||
|
||
const { | ||
CREATED, | ||
SERVER_ERROR, | ||
UNAUTHORIZED, | ||
} = STATUS; | ||
|
||
/** profile controller class */ | ||
/** | ||
*@description Handles profile features. | ||
* | ||
* @class ProfileController | ||
*/ | ||
class ProfileController { | ||
/** | ||
* @description It creates the user's profile. | ||
* @function create | ||
* @memberof profileController | ||
* @static | ||
* @param {Object} req - The request object. | ||
* @param {Object} res - The response object. | ||
* @returns {Object} - It returns the response object. | ||
*/ | ||
static async create(req, res) { | ||
/** id should be gotten from the token */ | ||
let { | ||
firstname, lastname, username, gender, bio, phone, address, image, | ||
} = req.body; | ||
|
||
firstname = firstname ? firstname.toLowerCase().toString().replace(/\s+/g, '') : firstname; | ||
lastname = lastname ? lastname.toLowerCase().toString().replace(/\s+/g, '') : lastname; | ||
username = username ? username.toLowerCase().toString().replace(/\s+/g, '') : username; | ||
gender = gender ? gender.toUpperCase().toString().replace(/\s+/g, '') : gender; | ||
bio = bio ? bio.toLowerCase().toString().replace(/\s+/g, ' ') : bio; | ||
phone = phone ? phone.toLowerCase().toString().replace(/\s+/g, '') : phone; | ||
address = address ? address.toLowerCase().toString().replace(/\s+/g, ' ') : address; | ||
image = image ? image.toLowerCase().toString().replace(/\s+/g, '') : image; | ||
|
||
const requestForm = { | ||
firstname, lastname, username, gender, bio, phone, address, image, | ||
}; | ||
|
||
|
||
try { | ||
const [profile, success] = await Profile.findOrCreate({ | ||
where: { | ||
user_id: req.user.id, | ||
}, | ||
defaults: requestForm, | ||
}); | ||
|
||
if (success) { | ||
return Response.send(res, CREATED, profile, 'Profile created successfully', true); | ||
} | ||
|
||
await Profile.update( | ||
req.body, | ||
{ | ||
where: { | ||
user_id: req.user.id, | ||
} | ||
} | ||
); | ||
|
||
return Response.send(res, CREATED, requestForm, 'Profile updated successfully', true); | ||
} catch (error) { | ||
console.log(error, '---->'); | ||
if (error.message === 'insert or update on table "profiles" violates foreign key constraint "profiles_user_id_fkey"') { | ||
return Response.send(res, UNAUTHORIZED, [], 'You have to be signed up to create a profile', false); | ||
} | ||
return Response.send(res, SERVER_ERROR, error.message, 'Profile update failed, try again later!', false); | ||
} | ||
} | ||
} | ||
|
||
export default ProfileController; |
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,54 @@ | ||
|
||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('profiles', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstname: { | ||
type: Sequelize.STRING | ||
}, | ||
lastname: { | ||
type: Sequelize.STRING | ||
}, | ||
username: { | ||
type: Sequelize.STRING | ||
}, | ||
gender: { | ||
type: Sequelize.STRING | ||
}, | ||
bio: { | ||
type: Sequelize.STRING | ||
}, | ||
image: { | ||
type: Sequelize.STRING | ||
}, | ||
phone: { | ||
type: Sequelize.STRING | ||
}, | ||
address: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
user_id: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id', | ||
} | ||
}, | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Profiles') | ||
}; |
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,2 @@ | ||
export const urlRegex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})/; | ||
export const phoneRegex = /^\+(?:[0-9] ?){6,14}[0-9]$/; |
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
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,9 @@ | ||
import validateCreateProfile from './validateCreateProfile'; | ||
import authenticate from './authenticate'; | ||
|
||
const middlewares = { | ||
validateCreateProfile, | ||
authenticate, | ||
}; | ||
|
||
export default middlewares; |
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,52 @@ | ||
import { urlRegex, phoneRegex } from '../helpers/regex'; | ||
/** | ||
* This is a validation for creating a profile | ||
* @constant | ||
* | ||
* @param {String} req request object | ||
* @param {Object} res response object | ||
* @param {Object} err error object | ||
* | ||
* @returns {Object} | ||
* | ||
* @exports validateCreateProfile | ||
*/ | ||
|
||
const validateCreateProfile = (req, res, next) => { | ||
let { | ||
firstname, lastname, username, gender, bio, phone, image, | ||
} = req.body; | ||
|
||
firstname = firstname ? firstname.toLowerCase().toString().replace(/\s+/g, '') : firstname; | ||
lastname = lastname ? lastname.toLowerCase().toString().replace(/\s+/g, '') : lastname; | ||
username = username ? username.toLowerCase().toString().replace(/\s+/g, '') : username; | ||
gender = gender ? gender.toUpperCase().toString().replace(/\s+/g, '') : gender; | ||
bio = bio ? bio.toLowerCase().toString().replace(/\s+/g, ' ') : bio; | ||
phone = phone ? phone.toLowerCase().toString().replace(/\s+/g, '') : phone; | ||
image = image ? image.toLowerCase().toString().replace(/\s+/g, '') : image; | ||
|
||
const errors = {}; | ||
|
||
if (!firstname) errors.firstname = 'Firstname is required'; | ||
if (!lastname) errors.lastname = 'Lastname is required'; | ||
if (!username) errors.username = 'username field cannot be emnpty'; | ||
if (!gender) errors.gender = 'Gender is required'; | ||
if (gender !== 'M' && gender !== 'F') errors.genderType = 'Gender must either be M or F'; | ||
if (!bio) errors.bio = 'Please provide a brief description about yourself'; | ||
if (bio.length > 255) errors.bio = 'Bio has reached it\'s maximum limit'; | ||
if (phone && !phoneRegex.test(phone)) errors.phone = 'Phone number should have a country code and not contain alphabets e.g +234'; | ||
if (phone && !phoneRegex.test(phone)) errors.phoneLength = 'Phone number length should adhere to international standard'; | ||
if (image && !urlRegex.test(image)) errors.image = 'image URL is not valid'; | ||
|
||
if (Object.getOwnPropertyNames(errors).length) { | ||
return res.status(400).json({ | ||
status: 400, | ||
success: 'false', | ||
errors, | ||
}); | ||
} | ||
|
||
return next(); | ||
}; | ||
|
||
export default validateCreateProfile; |
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,58 @@ | ||
/** | ||
* A model class representing user resource | ||
* | ||
* @param {Sequelize} sequelize - Sequelize object | ||
* @param {Sequelize.DataTypes} DataTypes - A convinient object holding data types | ||
* @return {Sequelize.Model} - User model | ||
*/ | ||
|
||
const Profile = (sequelize, DataTypes) => { | ||
/** | ||
* @type {Sequelize.Model} | ||
*/ | ||
const ProfileSchema = sequelize.define('profile', { | ||
firstname: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
lastname: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
username: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
gender: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
bio: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
phone: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
address: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
}, | ||
image: { | ||
type: DataTypes.STRING, | ||
allowNull: true, | ||
} | ||
}, {}); | ||
|
||
ProfileSchema.associate = (models) => { | ||
ProfileSchema.belongsTo(models.User, { | ||
foreignKey: 'user_id', | ||
targetKey: 'id', | ||
onDelete: 'CASCADE' | ||
}); | ||
}; | ||
return ProfileSchema; | ||
}; | ||
|
||
export default Profile; |
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
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.