Skip to content

Commit

Permalink
Made creating new donation functionality (note: if the user wants to …
Browse files Browse the repository at this point in the history
…create a new user on the new donation form there are some input fields missing so currently those missing fields - like contact_phone - are just initialized with default values)
  • Loading branch information
charlesjin123 committed Mar 24, 2024
1 parent 914682d commit 3c4bcfa
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
78 changes: 73 additions & 5 deletions client/src/NewDonation/NewDonationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Dayjs } from 'dayjs';
import FormControl from '@mui/material/FormControl';
import ArrowForwardIcon from '@mui/icons-material/ArrowForward';
import { Box } from '@mui/system';
import { useData } from '../util/api';
import { postData, useData } from '../util/api';

/**
The New Donation Page
Expand Down Expand Up @@ -57,7 +57,7 @@ function NewDonationPage() {

useEffect(() => {
const data = purposes?.data || [];
console.log('purposes', data);
// console.log('purposes', data);
setPurposesData(data);
}, [purposes]);

Expand Down Expand Up @@ -98,6 +98,76 @@ function NewDonationPage() {
setPaymentType(event.target.value);
};

const handleSubmit = () => {
if (isNewDonator) {
const newDonator = {
contact_name: donator?.title,
contact_email: newDonatorEmail,
contact_address: newDonatorAddress,
contact_phone: '0', // no input field for this yet
donor_group: newDonatorGroup,
registered_date: new Date(), // no input field for this yet
last_donation_date: new Date(), // no input field for this yet
type: '0', // no input field for this yet
// comments: null,
// _id: null,
};

postData('donor/create', newDonator)
.then((response) => {
setDonator(response.data);

const newDonation = {
// eslint-disable-next-line no-underscore-dangle
donor_id: response.data._id,
date: donationDate?.format('YYYY-MM-DD'),
amount: donationAmount,
// eslint-disable-next-line no-underscore-dangle
purpose_id: campaignPurpose?._id,
payment_type: paymentType,
type: donationType,
comments: notes,
};

postData('donation/new', newDonation)
.then((response1) => {
// Handle the response here
console.log(response1);
})
.catch((error) => {
// Handle the error here
console.log(error);
});
})
.catch((error) => {
// Handle the error here
console.log(error);
});
} else {
const newDonation = {
// eslint-disable-next-line no-underscore-dangle
donor_id: donator?._id,
date: donationDate?.format('YYYY-MM-DD'),
amount: donationAmount,
// eslint-disable-next-line no-underscore-dangle
purpose_id: campaignPurpose?._id,
payment_type: paymentType,
type: donationType,
comments: notes,
};

postData('donation/new', newDonation)
.then((response) => {
// Handle the response here
console.log(response);
})
.catch((error) => {
// Handle the error here
console.log(error);
});
}
};

return (
<Grid container sx={{ m: 3 }} spacing={2}>
<Grid item xs={12}>
Expand Down Expand Up @@ -387,9 +457,7 @@ function NewDonationPage() {
variant="contained"
color="primary"
endIcon={<ArrowForwardIcon />}
onClick={() => {
alert('clicked');
}}
onClick={handleSubmit}
sx={{ width: '40%' }}
size="large"
style={{ justifyContent: 'flex-start' }}
Expand Down
15 changes: 8 additions & 7 deletions server/src/controllers/donation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,27 @@ const createNewDonation = async (
res: express.Response,
next: express.NextFunction,
) => {
const { donorId, date, amount, purposeId, paymentType, type } = req.body;
if (!donorId || !date || !amount || !purposeId || !paymentType || !type) {
const { donor_id, date, amount, purpose_id, payment_type, type } = req.body;
if (!donor_id || !date || !amount || !purpose_id || !payment_type || !type) {
next(
ApiError.missingFields([
'donorId',
'donor_id',
'date',
'amount',
'purposeId',
'paymentType',
'purpose_id',
'payment_type',
'type',
]),
);
return;
}
const newDonation: IDonation | null = req.body.donation as IDonation;
// const newDonation: IDonation | null = req.body.donation as IDonation;
const newDonation: IDonation | null = req.body as IDonation;
return createDonation(newDonation)
.then((donation: any) => {
res.status(StatusCode.CREATED).send(donation);
})
.catch(() => {
.catch((e) => {
next(ApiError.internal('Unable to create donation'));
});
};
Expand Down
5 changes: 3 additions & 2 deletions server/src/controllers/donor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const createDonorController = async (
res: express.Response,
next: express.NextFunction,
) => {
const donor: IDonor | null = req.body.donation as IDonor;
const donor: IDonor | null = req.body as IDonor;
if (!donor) {
next(ApiError.missingFields(['donor']));
return;
Expand All @@ -83,7 +83,8 @@ const createDonorController = async (
.then((donor2: unknown) => {
res.status(StatusCode.OK).send(donor2);
})
.catch(() => {
.catch((e) => {
console.log('unable to create donor error', e.message);
next(ApiError.internal('Unable to create donor'));
});
};
Expand Down
2 changes: 2 additions & 0 deletions server/src/routes/donation.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ router.get('/:id', getDonation);

router.get('/donor/:donorId', isAuthenticated, getDonationsByDonorId);

// router.post('/new', isAuthenticated, createNewDonation);
// For testing:
router.post('/new', createNewDonation);

router.put('/acknowledge/:id', isAuthenticated, acknowledgeDonationById);
Expand Down
3 changes: 2 additions & 1 deletion server/src/routes/donor.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ router.get('/:type', isAuthenticated, getAllDonorsOfType);

router.get('/:id', isAuthenticated, getDonorByIdController);

router.post('/create', isAuthenticated, createDonorController);
// router.post('/create', isAuthenticated, createDonorController);
router.post('/create', createDonorController);

export default router;

0 comments on commit 3c4bcfa

Please sign in to comment.