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
93 changes: 92 additions & 1 deletion server/src/CondoUnit/controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
const pool = require('../../db')
const queries = require('./queries')
const nodemailer = require('nodemailer')

const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.BUTLER_EMAIL,
pass: process.env.BUTLER_EMAIL_PASSWORD
}
})

const sendEmail = ({ to, subject, text }) => {
const mailOptions = {
from: process.env.BUTLER_EMAIL,
to,
subject,
text
}

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending email:', error)
} else {
console.log('Email sent:', info.response)
}
})
}

const getCondoUnits = (req, res) => {
console.log('Get All Condo Units')
Expand Down Expand Up @@ -142,11 +168,76 @@ const calculateTotalCondoFee = (req, res) => {
})
}

const sendCondoFeesToOwners = (req, res) => {
const propertyId = req.body.propertyId

pool.query(queries.getCondoOwnersEmailsByProperty, [propertyId], (error, ownersResults) => {
if (error) {
console.error('Error fetching condo owners:', error)
return res.status(500).send('Error fetching condo owners')
}

ownersResults.rows.forEach(owner => {
const condoId = owner.condoid
const ownerEmail = owner.user_email

calculateTotalCondoFeeForEmail(condoId, (condoFeeInfo) => {
if (condoFeeInfo) {
sendEmail({
to: ownerEmail,
subject: 'Your Condo Fees',
text: `Hello, here are your condo fees details:\n\n${JSON.stringify(condoFeeInfo, null, 2)}`
})
}
})
})

res.send('Condo fees sent to owners')
})
}

const calculateTotalCondoFeeForEmail = (condoId, callback) => {
pool.query(queries.getCondoFeePerSqrft, [condoId], (error, feeResults) => {
if (error || feeResults.rowCount === 0) {
console.error('Error fetching condo fee or condo unit not found:', error)
callback(null)
} else {
const condoInformation = feeResults.rows[0]
const condoSize = condoInformation.size
const feePerSquareFoot = condoInformation.condo_fee_per_sqrft
const CondoFee = condoSize * feePerSquareFoot

pool.query(queries.getCondoParkingFee, [condoId], (parkingError, parkingResults) => {
const parkingFee = parkingResults.rows.length > 0 ? parkingResults.rows[0].parking_fee : 0

pool.query(queries.getCondoLockerFee, [condoId], (lockerError, lockerResults) => {
const lockerFee = lockerResults.rows.length > 0 ? lockerResults.rows[0].locker_fee : 0

const condoFeeInfo = {
'Condo Size': condoSize,
'Fee Per Square Foot': feePerSquareFoot,
'Condo Fee': CondoFee,
'Additional Fees': {
'Parking Fee': parkingFee,
'Locker Fee': lockerFee
},
'Total Additional Fees': parkingFee + lockerFee,
'Total Condo Fee': CondoFee + parkingFee + lockerFee
}

callback(condoFeeInfo)
})
})
}
})
}

module.exports = {
getCondoUnits,
getCondoUnitById,
addCondoUnit,
removeCondoUnit,
updateCondoUnit,
calculateTotalCondoFee
calculateTotalCondoFee,
sendCondoFeesToOwners
}
4 changes: 3 additions & 1 deletion server/src/CondoUnit/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const updateCondoUnit = 'UPDATE condo_unit SET companyid = $1, property_id = $2,
const getCondoFeePerSqrft = 'SELECT pp.condo_fee_per_sqrft, cu.size FROM property pp, condo_unit cu WHERE pp.property_id = cu.property_id AND cu.condoid = $1'
const getCondoParkingFee = 'SELECT p.parking_fee FROM property p, condo_unit c, active_registration_key ark, assigned_parking_spot asp WHERE ark.condoid = $1 AND asp.userid = ark.userid AND c.condoid = $1 AND c.property_id = p.property_id'
const getCondoLockerFee = 'SELECT p.locker_fee FROM property p, condo_unit c, active_registration_key ark, assigned_locker al WHERE ark.condoid = $1 AND al.userid = ark.userid AND c.condoid = $1 AND c.property_id = p.property_id'
const getCondoOwnersEmailsByProperty = 'SELECT cu.condoid, pu.email AS user_email FROM condo_unit cu INNER JOIN active_registration_key ark ON cu.condoid = ark.condoid INNER JOIN public_user pu ON ark.userid = pu.userid WHERE cu.property_id = $1'

module.exports = {
getCondoUnits,
Expand All @@ -19,5 +20,6 @@ module.exports = {
updateCondoUnit,
getCondoFeePerSqrft,
getCondoParkingFee,
getCondoLockerFee
getCondoLockerFee,
getCondoOwnersEmailsByProperty
}
1 change: 1 addition & 0 deletions server/src/CondoUnit/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ router.post('/', authenticateToken, controller.addCondoUnit)
router.post('/:condoid', authenticateToken, controller.calculateTotalCondoFee)
router.delete('/:condoid', authenticateToken, controller.removeCondoUnit)
router.patch('/:condoid', authenticateToken, controller.updateCondoUnit)
router.post('/send-fees', authenticateToken, controller.sendCondoFeesToOwners)

module.exports = router