Skip to content

Commit

Permalink
Merge pull request #96 from holabayor:dev-branch
Browse files Browse the repository at this point in the history
Dev-branch
  • Loading branch information
holabayor authored Sep 21, 2023
2 parents da36eed + 63b0c2d commit 7328c3c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 27 deletions.
77 changes: 57 additions & 20 deletions controllers/lunchControllers.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,71 @@
const { Op } = require('sequelize');
const Lunch = require('../models/lunches.model');
const User = require('../models/user.model');

//GET endpoint to retrieve all available lunches for a user
exports.getAllLunch = async (req, res) => {
const { userId } = req.params;
const getAllLunch = async (req, res) => {
const userId = req.user.id;

try {
//Query the lunch model to find available lunches for the user
const availableLunches = await Lunch.findAll({
attributes: [
'id',
'receiverId',
'senderId',
'quantity',
'redeemed',
'note',
'created_at',
],
const allLunches = await Lunch.findAll({
where: {
receiver_id: userId, //user is the receiver
redeemed: false, //lunch is not yrt redeemed
[Op.or]: [{ senderId: userId }, { receiverId: userId }], //User is either the sender or receiver
},
});
if (!availableLunches) {
return res.status(404).json({
success: false,
message: 'availableLunches not found',
data: null,

// Check if there are no lunches found
if (!allLunches || allLunches.length === 0) {
return res.status(200).json({
success: true,
message: 'No lunches found for this user',
data: [],
});
}

res.status(200).json({
success: true,
message: 'Lunches retrieved successfully',
data: availableLunches,
data: allLunches,
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Internal Server Error',
data: null,
});
}
};

const sendLunch = async (req, res) => {
const { receiverId, quantity, note } = req.body;

try {
//Create a new lunch
const lunch = await Lunch.create({
sender_id: req.user.id,
receiver_id: receiverId,
quantity,
note,
});

const sender = await User.findOne({ where: { id: req.id } });
const receiver = await User.findOne({ where: { id: receiverId } });

//Update the sender's balance
await sender.update({
balance: sender.balance - quantity,
});

//Update the receiver's balance
await receiver.update({
balance: receiver.balance + quantity,
});

res.status(201).json({
success: true,
message: 'Lunch sent successfully',
data: lunch,
});
} catch (error) {
res.status(500).json({
Expand All @@ -40,3 +75,5 @@ exports.getAllLunch = async (req, res) => {
});
}
};

module.exports = { getAllLunch, sendLunch };
38 changes: 35 additions & 3 deletions controllers/organizationController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint-disable camelcase */
const Organization = require('../models/organization.model');
const LunchWallet = require('../models/org_lunch_wallet.model');
const { createCustomError } = require('../Errors/custom-errors');
const { createCustomError } = require('../errors/custom-errors');
const orgInvites = require('../models/organisation_invite.model');
const {
generateUniqueToken,
sendInvitationEmail,
} = require('../utils/sendOTP');

// Create a new organization and user (Admin user only)
const createOrganization = async (req, res, next) => {
Expand All @@ -27,12 +32,39 @@ const createOrganization = async (req, res, next) => {

res.status(201).json({
success: true,
message: 'Organization and admin user created successfully',
message: 'Organization and Lunch Wallet created successfully',
data: { organization, lunchWallet },
});
} catch (error) {
next(error);
}
};

module.exports = { createOrganization };
async function sendInvitation(req, res, next) {
try {
const { email, organizationId } = req.body;

// Generate a unique token for the invitation (you can use a library like `uuid` for this)
const invitationToken = generateUniqueToken();

// Save the invitation details in the database
await orgInvites.create({
email,
token: invitationToken,
organization_id: organizationId,
});

// Send an email to the user with the invitation link (including the token)
await sendInvitationEmail(email, invitationToken);

res.status(200).json({
success: true,
message: 'Invitation sent successfully',
data: null,
});
} catch (error) {
next(error);
}
}

module.exports = { sendInvitation, createOrganization };
23 changes: 19 additions & 4 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable camelcase */
const bcrypt = require('bcrypt'); // import bcrypt to hash password
const User = require('../models/user.model'); //import user model
const bcrypt = require('bcrypt');
const User = require('../models/user.model');
const Invite = require('../models/organisation_invite.model');

async function getMe(req, res) {
try {
Expand Down Expand Up @@ -68,17 +69,29 @@ async function createUser(req, res) {
bank_code,
bank_name,
bank_number,
token,
} = req.body;

// Validate input data
if (!first_name || !last_name || !email || !password) {
if (!first_name || !last_name || !email || !password || !token) {
return res.status(400).json({
success: false,
message: 'Missing required fields',
data: null,
});
}

// Check if the token is valid and retrieve org_id
const invite = await Invite.findOne({ where: { token } });

if (!invite || new Date() > invite.ttl) {
return res.status(400).json({
success: false,
message: 'Invalid or expired invitation token',
data: null,
});
}

const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);

Expand Down Expand Up @@ -128,7 +141,9 @@ async function createUser(req, res) {

async function getAllUsers(req, res) {
try {
const users = await User.findAll();
const users = await User.findAll({
where: { org_id: req.user.org_id },
});

res.status(200).json({
success: true,
Expand Down

0 comments on commit 7328c3c

Please sign in to comment.