Skip to content

Commit

Permalink
Merge pull request #108 from oriechinedu/ft-api-delete-aspirants-appl…
Browse files Browse the repository at this point in the history
…ication-164032773

feat(application): implement Admin delete application
  • Loading branch information
nedssoft committed Feb 17, 2019
2 parents 0e9fe6c + 44b9989 commit c98d6ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
25 changes: 25 additions & 0 deletions server/controllers/ApplicationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,30 @@ class ApplicationController {
await client.release();
}
}

/**
*@description Deletes an application record
* @param {object} req - request
* @param {object} res - response
*/
static async deleteApplication(req, res) {
const { applicationId } = req.params;
const sqlQuery = { text: 'DELETE FROM applications WHERE id = $1 RETURNING id', values: [applicationId] };
const client = await pool.connect();
try {
const party = await client.query(sqlQuery);
if (party.rowCount) {
return res.status(200).json({
status: 200,
data: { message: 'Application deleted successfully' },
});
}
return res.status(500).json({ status: 500, message: 'Unable to delete the application' });
} catch (err) {
return res.status(500).json({ status: 500, error: 'Internal Server error' });
} finally {
await client.release();
}
}
}
export default ApplicationController;
4 changes: 3 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const { validateCandidate, checkIfOfficeExists, checkIfUserExists,
isDuplicateCandidate, hasDuplicateCandidateFlagBearer } = AdminValidator;

const { validateApplication, isDuplicateApplication } = ApplicationValidator;
const { createApplication, getAllApplications, editApplication } = ApplicationController;
const { createApplication, getAllApplications, editApplication,
deleteApplication } = ApplicationController;
router.get('/', (req, res) => {
res.send('welcome to Politico');
});
Expand Down Expand Up @@ -91,5 +92,6 @@ router.post('/api/v1/office/applications', validateApplication, checkToken,
isDuplicateApplication, createApplication);
router.get('/api/v1/office/applications', getAllApplications);
router.patch('/api/v1/office/applications/:applicationId', isAdmin, editApplication);
router.delete('/api/v1/office/applications/:applicationId', isAdmin, deleteApplication);
/** End Application */
export default router;
12 changes: 12 additions & 0 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,5 +689,17 @@ describe('Vote', () => {
console.log(err);
}
});
it('it should delete the application if it exists', async () => {
const id = 1;
try {
const res = await chai.request(app)
.delete(`${url}/${id}`)
.set('token', token)
.set('Authorization', token);
expect(res).to.have.status(200);
} catch (err) {
console.log(err);
}
});
});
});

0 comments on commit c98d6ba

Please sign in to comment.