Skip to content

Commit

Permalink
delete cache data for client on update/delete requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-ivanovv committed Dec 5, 2022
1 parent 3fd8c5c commit 997598c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
34 changes: 29 additions & 5 deletions service/controllers/clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "#queries/clients";

import { clientNotFound, incorrectPassword, emailUsed } from "#utils/errors";
import { deleteCacheItem } from "#utils/cache";

const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
Expand All @@ -22,6 +23,7 @@ export const updateClientData = async ({
country,
language,
client_id,
user_id,
name,
surname,
nickname,
Expand Down Expand Up @@ -59,10 +61,13 @@ export const updateClientData = async ({
yearOfBirth,
urbanRural,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand Down Expand Up @@ -127,6 +132,9 @@ export const deleteClientData = async ({
}
}

const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand All @@ -139,17 +147,21 @@ export const updateClientImage = async ({
country,
language,
client_id,
user_id,
image,
}) => {
return await updateClientImageQuery({
poolCountry: country,
client_id,
image,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand All @@ -158,15 +170,23 @@ export const updateClientImage = async ({
});
};

export const deleteClientImage = async ({ country, language, client_id }) => {
export const deleteClientImage = async ({
country,
language,
client_id,
user_id,
}) => {
return await deleteClientImageQuery({
poolCountry: country,
client_id,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand All @@ -179,17 +199,21 @@ export const updateClientDataProcessing = async ({
country,
language,
client_id,
user_id,
dataProcessing,
}) => {
return await updateClientDataProcessingQuery({
poolCountry: country,
client_id,
dataProcessing,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand Down
4 changes: 2 additions & 2 deletions service/middlewares/populateMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const populateClient = async (req, res, next) => {
return next(clientNotFound(country));
}

await setCacheItem(cacheKey, client, 3600);
await setCacheItem(cacheKey, client, 60 * 60 * 2); // cache data for 2 hours
req.client = client;
}

Expand All @@ -45,7 +45,7 @@ export const populateUser = async (req, res, next) => {
throw err;
});

await setCacheItem(cacheKey, user, 3600);
await setCacheItem(cacheKey, user, 60 * 60 * 2); // cache data for 2 hours
req.user = user;
}

Expand Down
19 changes: 15 additions & 4 deletions service/routes/v1/ClientRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ router.put("/", populateClient, async (req, res, next) => {
*/
const country = req.header("x-country-alpha-2");
const language = req.header("x-language-alpha-2");
const user_id = req.header("x-user-id");

const client_id = req.client.client_detail_id;
const currentEmail = req.client.email;
Expand All @@ -46,7 +47,14 @@ router.put("/", populateClient, async (req, res, next) => {
return await updateClientDataSchema(language)
.noUnknown(true)
.strict()
.validate({ country, language, client_id, currentEmail, ...payload })
.validate({
country,
language,
user_id,
client_id,
currentEmail,
...payload,
})
.then(updateClientData)
.then((result) => res.status(200).send(result))
.catch(next);
Expand Down Expand Up @@ -92,6 +100,7 @@ router.put("/image", populateClient, populateUser, async (req, res, next) => {
*/
const country = req.header("x-country-alpha-2");
const language = req.header("x-language-alpha-2");
const user_id = req.header("x-user-id");

const client_id = req.client.client_detail_id;

Expand All @@ -100,7 +109,7 @@ router.put("/image", populateClient, populateUser, async (req, res, next) => {
return await updateClientImageSchema
.noUnknown(true)
.strict()
.validate({ country, language, client_id, image })
.validate({ country, language, client_id, user_id, image })
.then(updateClientImage)
.then((result) => res.status(200).send(result))
.catch(next);
Expand All @@ -113,13 +122,14 @@ router.delete("/image", populateClient, async (req, res, next) => {
*/
const country = req.header("x-country-alpha-2");
const language = req.header("x-language-alpha-2");
const user_id = req.header("x-user-id");

const client_id = req.client.client_detail_id;

return await deleteClientImageSchema
.noUnknown(true)
.strict()
.validate({ country, language, client_id })
.validate({ country, language, client_id, user_id })
.then(deleteClientImage)
.then((result) => res.status(200).send(result))
.catch(next);
Expand All @@ -135,14 +145,15 @@ router.put(
*/
const country = req.header("x-country-alpha-2");
const language = req.header("x-language-alpha-2");
const user_id = req.header("x-user-id");

const client_id = req.client.client_detail_id;
const payload = req.body;

return await updateClientDataProcessingSchema
.noUnknown(true)
.strict()
.validate({ country, language, client_id, ...payload })
.validate({ country, language, user_id, client_id, ...payload })
.then(updateClientDataProcessing)
.then((result) => res.status(200).send(result))
.catch(next);
Expand Down
4 changes: 4 additions & 0 deletions service/schemas/clientSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const updateClientDataSchema = (language) =>
yup.object().shape(
{
client_id: yup.string().uuid().required(),
user_id: yup.string().uuid().required(),
country: yup.string().required(),
language: yup.string().required(),
name: yup.string().notRequired(),
Expand Down Expand Up @@ -54,19 +55,22 @@ export const deleteClientDataSchema = yup.object().shape({

export const updateClientImageSchema = yup.object().shape({
client_id: yup.string().uuid().required(),
user_id: yup.string().uuid().required(),
country: yup.string().required(),
language: yup.string().required(),
image: yup.string().required(),
});

export const deleteClientImageSchema = yup.object().shape({
client_id: yup.string().uuid().required(),
user_id: yup.string().uuid().required(),
country: yup.string().required(),
language: yup.string().required(),
});

export const updateClientDataProcessingSchema = yup.object().shape({
client_id: yup.string().uuid().required(),
user_id: yup.string().uuid().required(),
country: yup.string().required(),
language: yup.string().required(),
dataProcessing: yup.boolean().required(),
Expand Down

0 comments on commit 997598c

Please sign in to comment.