Skip to content

Commit

Permalink
add the gift card functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Emengkeng committed Oct 4, 2023
1 parent e4e5ea2 commit 9386930
Show file tree
Hide file tree
Showing 17 changed files with 579 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
5 changes: 2 additions & 3 deletions Ideas/New features.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
4. Manage Family card request (first need to add the feature of username)
- Simplify process by allowing Family members and friends to request cards in app
- Include notes and attachments for approver to review
- Review and approve requests on the go from the extend app
- Review and approve requests on the go from the app
- Whitelist the people who are allowed to request for cards from you
1. Allocate Budgets and delegate spending power
- Create budget for different family members friends, projects, clients or expense categories
1. Allocate Budgets and delegate spending power
14 changes: 14 additions & 0 deletions ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# with a discount

# TODO
-- Make sure user deposit one time with creation of card
For now, when the user creates the card we only charge the user card fee. We need to add the option for the user to deposit and charge him from his account

--- Add option to terminate a card

Expand All @@ -20,3 +22,15 @@ for each card creation and transaction, users earn points. For card creation, th
--- Give the user the ability accept a gift card
1.When a user gifts a card, the reciever should choose wether to accept the card or reject it. For now the card is forcely added to the reciever
2.The gifter should also choose who is going to pay the monthly fee for the card

# Mailing
- add signup welcome mail
- add card, deposit, withdrawal and account to account transaction mail
- add Create card mail
- add gift card mail




# Logic Check
- Make sure the hasEnoughBalance function in user.service.js is working properly when called in creatcard.service.js and other files
62 changes: 54 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"randomstring": "^1.3.0",
"sequelize": "^6.33.0",
"url-parse": "^1.5.10",
"uuid": "^9.0.1",
"validator": "^13.11.0",
"xss-clean": "^0.1.4"
},
Expand Down
16 changes: 16 additions & 0 deletions server/config/cardfee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('dotenv/config');

const fee = {
BASICC: process.env.BASICC ,
UNLIMITEDC: process.env.UNLIMITEDC ,
FAMILYC: process.env.FAMILYC ,
TRAVELC: process.env.TRAVELC ,
BUSINESSC: process.env.BUSINESSC ,
STUDENTC: process.env.STUDENTC ,
CHARITYC: process.env.CHARITYC ,
EXCLUSIVEC: process.env.EXCLUSIVEC ,
REWARDC: process.env.REWARDC ,
BUDGETC: process.env.BUDGETC ,
};

module.exports = fee
98 changes: 83 additions & 15 deletions server/controllers/card.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { json } from 'express';
import rave from '../config/flutterwave_init';
import fee from '../config/cardfee';
import model from '../models';
import axios from 'axios';
const { validationResult } = require("express-validator");
Expand All @@ -9,6 +10,7 @@ import { fundCard } from '../services/fundcard.service';
import { giftCard } from '../services/giftcard.service';
import { findUserByEmail, findUserById, getUserBalance } from '../services/user.service';
const catchAsync = require("../utils/catchasync");
const BadRequestError = require("../utils/errors/badrequest.error");

require('dotenv').config();

Expand Down Expand Up @@ -72,28 +74,22 @@ const create_Vcard = catchAsync(async (req, res) => {
})
}

/* //Get User card in order to update card id
const cardDetails = await model.Card.findOne({
where: { userId: req.user.user.id },
}) */

// Get User balance
const accountDetails = await getUserBalance(userId);

//console.log(accountDetails);


const { balance } = accountDetails.dataValues;
console.log(balance);

// Logic check before creating card
const amt = parseFloat(balance);
console.log('amount', amt);
if (amt < amount) {
if (amt < fee.cardType) {
return res.status(403).json({
message: 'Not Enough Balance to Create Card',
message: 'Not Enough Balance to Gift Card',
});
}

//Create Card
const response = await createCard(data, cardtype, userId);

//Update user Balance
const newBalance = amt - parseFloat(amount);
Expand All @@ -108,10 +104,7 @@ const create_Vcard = catchAsync(async (req, res) => {
}
);
console.log('update', update);


//Create Card
const response = await createCard(data, cardtype, userId);

return res.status(httpStatus.CREATED).json({
success: true,
Expand Down Expand Up @@ -424,6 +417,81 @@ const freeze_card = catchAsync(async (req, res) => {
});

const gift_card = catchAsync(async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(httpStatus.BAD_REQUEST).json({ success: false, errors: errors.array() });
}
try {

const { id: userId } = req.user;
const { cardType } = req.body;

// Find the sender by id
const sender = await findUserById(userId)
if(!sender){
console.log('No User Found')
return res.json({
message: 'No User Found',
})
}

// Find Reciever By Email
const reciever = await findUserByEmail(wallet_email)
if(!reciever){
console.log('The Reciever is not Found')
return res.json({
message: 'The Reciever is not Found',
})
}

// Get User balance
const accountDetails = await getUserBalance(userId);
const { balance } = accountDetails.dataValues;
console.log(balance);

// Logic check before creating card
const amt = parseFloat(balance);
console.log('amount', amt);
if (amt < fee.cardType) { //
return res.status(403).json({
message: 'Not Enough Balance to Gift Card',
});
}

//Gift Card
const response = await giftCard(data, cardType, reciever.id, reciever.first_name, sender.id, req.body.date);

//Update user Balance
const newBalance = amt - parseFloat(amount);
const update = await model.Accounts.update(
{
balance: newBalance,
},
{
where: {
userId,
},
}
);
console.log('update', update);
return res.status(httpStatus.CREATED).json({
success: true,
message: "Card Gifted successfully!",
data: {
...response
},
});

} catch (error) {
// handle error here
return res.status(httpStatus.BAD_REQUEST).json({
success: false,
error: error,
})
}
})

const accept_gift_card = catchAsync(async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(httpStatus.BAD_REQUEST).json({ success: false, errors: errors.array() });
Expand Down Expand Up @@ -537,7 +605,6 @@ const gift_card = catchAsync(async (req, res) => {
error: error,
})
}
return ;
})

module.exports = {
Expand All @@ -550,4 +617,5 @@ module.exports = {
pay_Vcard,
freeze_card,
create_Vcard,
accept_gift_card,
}
34 changes: 34 additions & 0 deletions server/controllers/forgetPassword.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jwt from 'jsonwebtoken';
import transporter from '../middlewares/transporter';
import model from '../models';
const sendMail = require('../services');

const forgetPassword = async (req, res) => {
try {
const { email } = req.body;
console.log('email', email);
const result = await model.Users.findOne({
where: {
email,
},
});
if (result.length < 1) {
res.status(400).send({
message: `Sorry an Account with Email: ${email} doesn't exist`,
});
} else {
const secret = result.password;
const token = jwt.sign({ result }, secret, {
expiresIn: 3600, // 1 hour
});
const sendEmail = await sendMail.forgetPassword(email, `${process.env.APP_URL}/reset/${resetLink}`, recieverName);
// const url = `http://localhost:8080/resetPassword/${result.id}-${token}`;

return sendEmail;
}
} catch (error) {
res.status(500).json(error);
}
};

export default forgetPassword;
Loading

0 comments on commit 9386930

Please sign in to comment.