Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: Routes + Schemas + Controllers #4

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PII_DB_URL_KZ=
CLINICAL_DB_URL_KZ=
MASTER_DB_URL=

NODE_ENV=
PORT=

Expand Down
72 changes: 30 additions & 42 deletions service/controllers/clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@ const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
const AWS_REGION = process.env.AWS_REGION;
const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;

export const getClientById = async ({ country, language, clientId }) => {
const handleClientQueryResponse = async ({
res,
language,
country,
user_id,
}) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
};

export const getClientById = async ({ country, language, client_id }) => {
return await getClientByIdQuery({
poolCountry: country,
clientId,
client_id,
})
.then((res) => {
if (res.rowCount === 0) {
Expand Down Expand Up @@ -81,16 +97,9 @@ export const updateClientData = async ({
yearOfBirth,
urbanRural,
})
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
.then(async (res) =>
handleClientQueryResponse({ res, language, country, user_id })
)
.catch((err) => {
throw err;
});
Expand Down Expand Up @@ -175,16 +184,9 @@ export const updateClientImage = async ({
client_id,
image,
})
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
.then(async (res) =>
handleClientQueryResponse({ res, language, country, user_id })
)
.catch((err) => {
throw err;
});
Expand All @@ -200,16 +202,9 @@ export const deleteClientImage = async ({
poolCountry: country,
client_id,
})
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
.then(async (res) =>
handleClientQueryResponse({ res, language, country, user_id })
)
.catch((err) => {
throw err;
});
Expand All @@ -227,16 +222,9 @@ export const updateClientDataProcessing = async ({
client_id,
dataProcessing,
})
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
.then(async (res) =>
handleClientQueryResponse({ res, language, country, user_id })
)
.catch((err) => {
throw err;
});
Expand Down
70 changes: 10 additions & 60 deletions service/controllers/consultation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ export const getAllConsultations = async ({ country, language, client_id }) => {
poolCountry: country,
client_id,
})
.then((res) => {
if (res.rowCount === 0) {
return [];
} else {
return res.rows;
}
})
.then((res) => res.rows)
.catch((err) => {
throw err;
});
Expand Down Expand Up @@ -87,75 +81,31 @@ export const getSecurityCheckAnswersByConsultationId = async ({
poolCountry: country,
consultation_id,
})
.then((res) => {
if (res.rowCount === 0) {
return {};
} else {
return res.rows[0];
}
})
.then((res) => (res.rowCount === 0 ? {} : res.rows[0]))
.catch((err) => {
throw err;
});

return answers;
};

export const addSecurityCheckAnswers = async ({
country,
consultationId,
contactsDisclosure,
suggestOutsideMeeting,
identityCoercion,
unsafeFeeling,
moreDetails,
}) => {
export const addSecurityCheckAnswers = async (props) => {
return await addSecurityCheckAnswersQuery({
poolCountry: country,
consultationId,
contactsDisclosure,
suggestOutsideMeeting,
identityCoercion,
unsafeFeeling,
moreDetails,
...props,
poolCountry: props.country,
})
.then((res) => {
if (res.rowCount === 0) {
return {};
} else {
return res.rows[0];
}
})
.then((res) => (res.rowCount === 0 ? {} : res.rows[0]))
.catch((err) => {
throw err;
});
};

export const updateSecurityCheckAnswers = async ({
country,
consultationId,
contactsDisclosure,
suggestOutsideMeeting,
identityCoercion,
unsafeFeeling,
moreDetails,
}) => {
export const updateSecurityCheckAnswers = async (props) => {
return await updateSecurityCheckAnswersQuery({
poolCountry: country,
consultationId,
contactsDisclosure,
suggestOutsideMeeting,
identityCoercion,
unsafeFeeling,
moreDetails,
...props,
poolCountry: props.country,
})
.then((res) => {
if (res.rowCount === 0) {
return {};
} else {
return res.rows[0];
}
})
.then((res) => (res.rowCount === 0 ? {} : res.rows[0]))
.catch((err) => {
throw err;
});
Expand Down
50 changes: 18 additions & 32 deletions service/controllers/moodTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,37 @@ export const getMoodTrackForToday = async ({ country, client_id }) => {
const moodTracks = await getMoodTrackForTodayQuery({
poolCountry: country,
client_id,
}).then((res) => {
if (res.rowCount === 0) {
return [];
} else {
return res.rows;
}
});
})
.then((res) => res.rows)
.catch((err) => {
throw err;
});

return moodTracks;
};

export const addMoodTrackForToday = async ({
country,
client_id,
mood,
comment,
}) => {
export const addMoodTrackForToday = async (props) => {
await addMoodTrackForTodayQuery({
poolCountry: country,
client_id,
mood,
comment,
...props,
poolCountry: props.country,
}).catch((err) => {
throw err;
});

return { success: true };
};

export const getMoodTrackForWeek = async ({
country,
client_id,
startDate,
}) => {
const date = new Date(Number(startDate) * 1000);
export const getMoodTrackForWeek = async (props) => {
const date = new Date(Number(props.startDate) * 1000);
const moodTracks = await getMoodTrackForWeekQuery({
poolCountry: country,
client_id,
poolCountry: props.country,
client_id: props.client_id,
startDate: date,
}).then((res) => {
if (res.rowCount === 0) {
return [];
} else {
return res.rows;
}
});
})
.then((res) => res.rows)
.catch((err) => {
throw err;
});

return moodTracks;
};
4 changes: 2 additions & 2 deletions service/queries/clients.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getDBPool } from "#utils/dbConfig";

export const getClientByIdQuery = async ({ poolCountry, clientId }) =>
export const getClientByIdQuery = async ({ poolCountry, client_id }) =>
await getDBPool("piiDb", poolCountry).query(
`

Expand All @@ -10,7 +10,7 @@ export const getClientByIdQuery = async ({ poolCountry, clientId }) =>
LIMIT 1;

`,
[clientId]
[client_id]
);

export const getClientByUserID = async (poolCountry, user_id) =>
Expand Down
Loading