Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 48 additions & 31 deletions controllers/airtimedataCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@ const { sendSms } = require('../services/sms')
const { v4: uuidv4 } = require('uuid');
const { phoneValidation } = require('../utils/helpers')
const {AirtimeModel, DataModel} = require('../models/airtimedatamodel')

const {validateAirtime, validateData}= require('../validations/airtimedataValidations')


// Function to credit a buyer airtime
// airtime operationId for Nigerian Telecomms brands
// order : MTN=341, 9mobile=340, Airtel=342, Glo=344
const airtime = async (req, res) => {

const { error } = validateAirtime(req.body)
if (error !== undefined) {
res.status(400).json({
status: true,
message: error.details[0].message || "Bad request"
})
return
}
try {
let { newAmount, phoneNumber, operatorID } = req.body
let { newAmount, phoneNumber, operatorName } = req.body
const { user_id } = req.params
phoneNumber = phoneValidation(phoneNumber)
if (phoneNumber === false) throw new Error('Invalid phone number', 400)

if (!user_id || !newAmount || !operatorID) {
if (!user_id || !newAmount || !operatorName) {
throw new Error('All fields are required', 400)

}
Expand All @@ -30,7 +39,7 @@ const airtime = async (req, res) => {
const airtimeId = uuidv4()
await AirtimeModel.create({
airtime_id: airtimeId,
airtime_type: operatorID,
airtime_type: operatorName,
airtime_phoneNumber: phoneNumber,
user_id:userID,
amount: 0,
Expand All @@ -40,7 +49,7 @@ const airtime = async (req, res) => {

})
// in order : MTN=341, 9mobile=340, Airtel=342, Glo=344
rechargeFunc(newAmount, phoneNumber, operatorID)
rechargeFunc(newAmount, phoneNumber, operatorName)
res.status(200).json({
status: true,
message: "Recharge card successfully topped",
Expand All @@ -61,53 +70,61 @@ const airtime = async (req, res) => {
//data operatorId for Nigerian Telecomms brand network in order
//: MTN=345, 9mobile=645, Airtel=646, Glo=931
const data =async(req, res)=>{
const { newAmount, phoneNumber, operatorID } = req.body

if (!newAmount || !phoneNumber || !operatorID) {
res.status(400).json({
status: false,
message: "All fields are required"
})
return
}

const checkWallet = await walletBalance.balance
if(checkWallet < newAmount){
const { error } = validateData(req.body)
if (error !== undefined) {
res.status(400).json({
status: false,
message:"Insufficient balance"
})
}

if(phoneNumber.length<11){
res.status(400).json({
status: false,
message:"Incorrect number",

status: true,
message: error.details[0].message || "Bad request"
})
return
}
const userID= await transaction(userID)
const dataId = uuidv4()
try {
let { newAmount, phoneNumber, operatorName } = req.body
const { user_id } = req.params
phoneNumber = phoneValidation(phoneNumber)
if (phoneNumber === false) throw new Error('Invalid phone number', 400)

if (!user_id || !newAmount || !operatorName) {
throw new Error('All fields are required', 400)

}

const checkWallet = await walletBalance.balance
if (checkWallet < newAmount)throw new Error('Insufficient Funds', 400)
const userID= await transaction(user_id);
const newWalletBalance = checkWallet - newAmount;
const dataId = uuidv4()

await DataModel.create({
data_id: dataId,
data_type: operatorID,
data_type: operatorName,
data_phoneNumber: phoneNumber,
user_id:userID,
newWalletBalance,
data_amount: newAmount,
data_date: Date.now(),

})
//: MTN=345, 9mobile=645, Airtel=646, Glo=931
rechargeFunc(newAmount, phoneNumber, operatorID)

//success message
res.status(200).json({
status: true,
message: "The phone number has been topped with data",
})

sendSms(phoneNumber, `${phoneNumber} has ${newAmount} worth of data`)

}catch(err){
res.status(500).json({
status: false,
message: err.message
})
}

}


module.exports ={
airtime,
data
Expand Down
29 changes: 24 additions & 5 deletions controllers/billControllers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@

const {walletBalance, transaction} = require('./walletController')
const { v4: uuidv4 } = require('uuid');
const {validateUtility, validateBillHistory}= require('../validations/billsValidation')

const {BillModel, BillHistory} = require('../models/billModel')
const BillModel = require('../models/billModel');
const { validateUtility } = require('../validations/billsValidation');

// Function to purchase a utility bill
// utility billerId for some Nigerian Electricity Disco brands
// order : Eko id=3, Ikeja id= 5, Abuja=6, Ibadan=8
const utilityFunc =async(request, result)=>{
const { amount, billerId, subscriberAccountNumber } = await request.body
if (!amount || !billerId || !subscriberAccountNumber || !phoneNumber) {

const { error } = validateUtility(req.body)
if (error !== undefined) {
res.status(400).json({
status: true,
message: error.details[0].message || "Bad request"
})
return
}
const { amount, billerName, subscriberAccountNumber } = await request.body
if (!amount || !billerName || !subscriberAccountNumber || !phoneNumber) {
res.status(400).json({
status: false,
message: "All fields are required"
Expand Down Expand Up @@ -64,7 +75,15 @@ const utilityFunc =async(request, result)=>{
}


const BillLog =async ()=>{
const BillLog =async()=>{
const { error } = validateUtility(req.body)
if (error !== undefined) {
res.status(400).json({
status: true,
message: error.details[0].message || "Bad request"
})
return
}
await BillHistory.create({
biller_id: billerId,
bill_amount:amount,
Expand All @@ -73,7 +92,7 @@ const BillLog =async ()=>{
})

}



module.exports ={
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const displayRoutes = require('express-routemap');
const userRoutes = require('./routes/userRoutes')
const airtimedataRoutes = require('./routes/airtimedataRoutes')
const walletRoutes = require('./routes/walletRoutes')
const billRoutes = require('./routes/billRoutes')


const CardRoutes = require('./routes/cardRoutes')

Expand All @@ -17,7 +19,8 @@ const faqRoute = require('./routes/faqRoute');

const bankRoutes = require('./routes/bankRoutes')

const {billRoutes, BillHistoryRoute} = require('./routes/billRoutes')
const billRoutes = require('./routes/billRoutes')


const { notFoundMessage } = require('./constants/messages')
const sequelize = require('./config/db')
Expand All @@ -33,11 +36,13 @@ app.use('/api/v1/transaction', transactionRoutes)
app.use('/api/v1/bank', bankRoutes)
app.use('/api/v1/airtimedata', airtimedataRoutes)

app.use('/api/v1/bill', billRoutes)


app.use('/api/v1/card', CardRoutes)

// app.use('/api/v1/bill', billRoutes)
// app.use('/api/v1/billhistory', BillHistoryRoute)

//>>>>>>> 3cd795137ca928dd7b12bbaba3bbbb9d187d526c



Expand Down
2 changes: 1 addition & 1 deletion models/airtimedatamodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ wallet_balance:{

Airtime.removeAttribute(['id'])


//Model for data
const Data = sequelize.define('datas',
{

Expand Down
11 changes: 8 additions & 3 deletions models/billModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ bill_id: {
allowNull: false,
primaryKey: true
},
biller_id:{
biller_type:{
type: DataTypes.ENUM,
values: [3, 5, 6, 9],
values: ['EKO ELECTRIC','IKEJA ELECTRIC', "IBADAN ELECTRIC", "ABUJA ELECTRIC"],
allowNull: false,
},
user_id:{
Expand Down Expand Up @@ -56,14 +56,19 @@ wallet_balance:{

Bill.removeAttribute(['id'])

const BillHistory = sequelize.define('bills',
const BillHistory = sequelize.define('billhistory',
{

bill_id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true
},
biller_type:{
type: DataTypes.ENUM,
values: ['EKO ELECTRIC','IKEJA ELECTRIC', "IBADAN ELECTRIC", "ABUJA ELECTRIC"],
allowNull: false
},
user_id:{
type: DataTypes.UUID,
allowNull: false
Expand Down
2 changes: 1 addition & 1 deletion services/bills.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const result= dataToken.access_token;
}


// accessing utility bearer token
// accessing utility bearer token

const utilityBillToken=()=>{
const url = 'https://auth.reloadly.com/oauth/token';
Expand Down
33 changes: 33 additions & 0 deletions validations/airtimedataValidations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Joi = require('joi');

const validateAirtime = (data) => {

const airtimeSchema = Joi.object({
airtime_type: Joi.string ().required(),
new_amount: Joi.ENUM.required(),
value:[],
airtime_phoneNumber: Joi.INTEGER().required()

})

return airtimeSchema.validate(data);
}


const validateData = (data) => {

const dataSchema = Joi.object({
data_type: Joi.string().required(),
data_amount: Joi.ENUM.required(),
value:[],
data_phoneNumber: Joi.INTEGER().required()

})

return dataSchema.validate(data);
}

module.exports = {
validateAirtime,
validateData
};
31 changes: 31 additions & 0 deletions validations/billsValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Joi = require('joi');

const validateUtility = (data) => {

const utilitySchema = Joi.object({
biller_type: Joi.string ().required(),
bill_amount: Joi.integer().required(),
subscriber_account_number: Joi.INTEGER().required()

})

return utilitySchema.validate(data);
}


const validateBillHistory = (data) => {

const billHistorySchema = Joi.object({
biller_type: Joi.string().required(),
bill_amount: Joi.integer().required(),
bill_type: Joi.dateonly()

})

return billHistorySchema.validate(data);
}

module.exports = {
validateUtility,
validateBillHistory
};