Skip to content

Commit

Permalink
🚀 Improve code resilience (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
n3a9 committed May 22, 2021
1 parent 2456aec commit 64833cd
Show file tree
Hide file tree
Showing 47 changed files with 2,050 additions and 1,788 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ jobs:
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
Expand Down
14 changes: 12 additions & 2 deletions api/src/api/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ router.post(
const [long, lat] = await extractLongLat(
`${address},${city},${state},${zip}`,
);
if (long != null && lat != null) {
if (
long !== null &&
long !== undefined &&
lat !== null &&
lat !== undefined
) {
req.body.geoLocation = { type: 'Point', coordinates: [long, lat] };
}
}
Expand Down Expand Up @@ -200,7 +205,12 @@ router.put(
const [long, lat] = await extractLongLat(
`${address},${city},${state},${zip}`,
);
if (long != null && lat != null) {
if (
long !== null &&
long !== undefined &&
lat !== null &&
lat !== undefined
) {
req.body.geoLocation = { type: 'Point', coordinates: [long, lat] };
}
}
Expand Down
31 changes: 23 additions & 8 deletions api/src/api/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ router.get(
} = req.query;

let query = {};
if (category != null && category !== '' && category !== 'All Resources') {
if (category && category !== 'All Resources') {
query = { category: category };
if (subcategory != null && subcategory !== '') {
if (subcategory) {
query = { ...query, subcategory: subcategory };
}
}
if (cost != null) {
if (cost !== null && cost !== undefined) {
// get last part, if length is 1 then all resources
const parts = cost.split(' ');
if (parts.length > 0) {
Expand All @@ -90,16 +90,21 @@ router.get(
// If $$$, then it's all prices, so no need to query extra
}
}
if (language != null && language !== 'All') {
if (language !== null && language !== undefined && language !== 'All') {
query = { ...query, availableLanguages: language };
}
if (city != null && city !== 'All' && city !== 'All / Champaign County') {
if (
city !== null &&
city !== undefined &&
city !== 'All' &&
city !== 'All / Champaign County'
) {
const ignoreCase = new RegExp('^' + city, 'i');
query = { ...query, city: ignoreCase };
}

let orderBy = null;
if (sort != null) {
if (sort !== null && sort !== undefined) {
if (
sort === 'Cost' ||
sort === 'Costo' ||
Expand All @@ -118,7 +123,12 @@ router.get(
}

let aggregation = [];
if (long == null || lat == null) {
if (
long === null ||
long === undefined ||
lat === null ||
lat === undefined
) {
if (orderBy === null) {
aggregation = [
{
Expand All @@ -128,7 +138,12 @@ router.get(
},
},
];
} else if (page == null || size == null) {
} else if (
page === null ||
page === undefined ||
size === null ||
size === undefined
) {
aggregation = [
{
$facet: {
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ router.get(
errorWrap(async (req, res) => {
const { language } = req.query;
let translations;
if (language != null) {
if (language !== null && language !== undefined) {
translations = await Translation.findOne({ language: { $eq: language } });
res.json({
code: 200,
Expand Down
22 changes: 14 additions & 8 deletions api/src/scripts/addVerifiedTranslations.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ async function main() {
});
await verifiedDesc.save();

if (phoneNumbers != null && phoneNumbers.length !== 0) {
if (
phoneNumbers !== null &&
phoneNumbers !== undefined &&
phoneNumbers.length !== 0
) {
for (const phone of phoneNumbers) {
const verifiedPhone = new VerifiedTranslation({
resourceID: _id,
Expand All @@ -100,7 +104,7 @@ async function main() {
await verifiedPhone.save();
}
}
if (eligibilityRequirements != null && eligibilityRequirements !== '') {
if (eligibilityRequirements) {
const verifiedElig = new VerifiedTranslation({
resourceID: _id,
translationID: `resource-eligibilityRequirements-${_id}`,
Expand All @@ -110,7 +114,7 @@ async function main() {
});
await verifiedElig.save();
}
if (financialAidDetails != null) {
if (financialAidDetails !== null && financialAidDetails !== undefined) {
for (const financialKey of Object.keys(financialAidDetails.toJSON())) {
if (financialKey !== '_id') {
const verifiedFin = new VerifiedTranslation({
Expand All @@ -124,18 +128,20 @@ async function main() {
}
}
}
if (requiredDocuments != null && requiredDocuments.length !== 0) {
let idx = 0;
for (const requiredDoc of requiredDocuments) {
if (
requiredDocuments !== null &&
requiredDocuments !== undefined &&
requiredDocuments.length > 0
) {
for (let i = 0; i < requiredDocuments.length; i++) {
const verifiedDoc = new VerifiedTranslation({
resourceID: _id,
translationID: `resource-requiredDoc-${_id}-${idx}`,
translationID: `resource-requiredDoc-${_id}-${i}`,
verified: false,
numReports: 0,
language: translation.language,
});
await verifiedDoc.save();
idx++;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/extractLongLat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Address,city,state,zip
const extractLongLat = async (location) => {
if (location != null) {
if (location !== null && location !== undefined) {
const apiLatLong =
`https://www.mapquestapi.com/geocoding/v1/address?key=` +
`${process.env.MAPBOX_KEY}&maxResults=5&location=${location}`;
Expand Down
32 changes: 24 additions & 8 deletions api/src/utils/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ async function deleteTranslatedText(
id,
) {
await deleteString(description, `resource-description-${id}`);
if (phoneNumbers != null && phoneNumbers.length !== 0) {
if (
phoneNumbers !== null &&
phoneNumbers !== undefined &&
phoneNumbers.length > 0
) {
await deletePhoneTypes(phoneNumbers);
}
if (financialAidDetails != null) {
if (financialAidDetails !== null && financialAidDetails !== undefined) {
await deleteFinancialAidDetails(financialAidDetails);
}
if (eligibilityRequirements != null && eligibilityRequirements !== '') {
if (eligibilityRequirements) {
await deleteString(
eligibilityRequirements,
`resource-eligibilityRequirements-${id}`,
);
}
if (requiredDocuments != null && requiredDocuments.length !== 0) {
if (
requiredDocuments !== null &&
requiredDocuments !== undefined &&
requiredDocuments.length > 0
) {
await deleteRequiredDocuments(requiredDocuments, id);
}
await VerifiedTranslation.deleteMany({ resourceID: id });
Expand Down Expand Up @@ -94,20 +102,28 @@ async function translateAndSaveText(
id,
) {
await translateString(description, `resource-description-${id}`, id);
if (phoneNumbers != null && phoneNumbers.length !== 0) {
if (
phoneNumbers !== null &&
phoneNumbers !== undefined &&
phoneNumbers.length > 0
) {
await translatePhoneTypes(phoneNumbers, id);
}
if (financialAidDetails != null) {
if (financialAidDetails !== null && financialAidDetails !== undefined) {
await translateFinancialAidDetails(financialAidDetails, id);
}
if (eligibilityRequirements != null && eligibilityRequirements !== '') {
if (eligibilityRequirements) {
await translateString(
eligibilityRequirements,
`resource-eligibilityRequirements-${id}`,
id,
);
}
if (requiredDocuments != null && requiredDocuments.length !== 0) {
if (
requiredDocuments !== null &&
requiredDocuments !== undefined &&
requiredDocuments.length > 0
) {
await translateRequiredDocuments(requiredDocuments, id);
}
}
Expand Down
16 changes: 12 additions & 4 deletions api/src/utils/verification.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ const getVerificationDetails = async (type, language, id) => {
verificationDetails.push(detail);
}

if (phoneNumbers != null && phoneNumbers.length !== 0) {
if (
phoneNumbers !== null &&
phoneNumbers !== undefined &&
phoneNumbers.length > 0
) {
for (const phone of phoneNumbers) {
const detail = await getTranslationDetail(
`resource-phoneType-${phone._id}`,
Expand All @@ -171,7 +175,7 @@ const getVerificationDetails = async (type, language, id) => {
}
}
}
if (eligibilityRequirements != null && eligibilityRequirements !== '') {
if (eligibilityRequirements) {
const detail = await getTranslationDetail(
`resource-eligibilityRequirements-${id}`,
language,
Expand All @@ -181,7 +185,7 @@ const getVerificationDetails = async (type, language, id) => {
verificationDetails.push(detail);
}
}
if (financialAidDetails != null) {
if (financialAidDetails !== null && financialAidDetails !== undefined) {
for (const financialKey of Object.keys(financialAidDetails.toJSON())) {
if (financialKey !== '_id') {
const detail = await getTranslationDetail(
Expand All @@ -195,7 +199,11 @@ const getVerificationDetails = async (type, language, id) => {
}
}
}
if (requiredDocuments != null && requiredDocuments.length !== 0) {
if (
requiredDocuments !== null &&
requiredDocuments !== undefined &&
requiredDocuments.length > 0
) {
let idx = 0;
for (const requiredDoc of requiredDocuments) {
const detail = await getTranslationDetail(
Expand Down
Loading

0 comments on commit 64833cd

Please sign in to comment.