From 67a4a9b9f87096a2829ed0e1e6a84abff7913c98 Mon Sep 17 00:00:00 2001 From: Duncan Carter Date: Thu, 19 Sep 2024 11:29:24 +0100 Subject: [PATCH] Move refresh of EstablishmentLastActivityView into shared util --- .../inactive-workplaces/getInactiveWorkplaces.js | 5 ----- .../getInactiveWorkplacesForDeletion.js | 5 ----- .../inactive-workplaces/setInactiveWorkplaces.js | 8 +++----- .../setInactiveWorkplacesForDeletion.js | 4 ++-- backend/server/utils/db/inactiveWorkplacesUtils.js | 5 +++++ 5 files changed, 10 insertions(+), 17 deletions(-) create mode 100644 backend/server/utils/db/inactiveWorkplacesUtils.js diff --git a/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplaces.js b/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplaces.js index 5760c1e8fb..ee6cc31edf 100644 --- a/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplaces.js +++ b/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplaces.js @@ -2,10 +2,6 @@ const moment = require('moment'); const models = require('../../index'); -const refreshInactiveWorkplaces = async () => { - return models.sequelize.query('REFRESH MATERIALIZED VIEW cqc."EstablishmentLastActivity"'); -}; - const getInactiveWorkplaces = async () => { const lastMonth = moment().subtract(1, 'months'); @@ -66,5 +62,4 @@ const getInactiveWorkplaces = async () => { module.exports = { getInactiveWorkplaces, - refreshInactiveWorkplaces, }; diff --git a/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplacesForDeletion.js b/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplacesForDeletion.js index e59964477c..9f5b465e8f 100644 --- a/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplacesForDeletion.js +++ b/backend/server/models/email-campaigns/inactive-workplaces/getInactiveWorkplacesForDeletion.js @@ -2,10 +2,6 @@ const moment = require('moment'); const models = require('../../index'); -const refreshInactiveWorkplacesForDeletion = async () => { - return models.sequelize.query('REFRESH MATERIALIZED VIEW cqc."EstablishmentLastActivity"'); -}; - const getInactiveWorkplacesForDeletion = async () => { const lastMonth = moment().subtract(1, 'months').endOf('month'); const twentyFourLastMonths = lastMonth.clone().subtract(24, 'months').format('YYYY-MM-DD'); @@ -59,5 +55,4 @@ const getInactiveWorkplacesForDeletion = async () => { module.exports = { getInactiveWorkplacesForDeletion, - refreshInactiveWorkplacesForDeletion, }; diff --git a/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplaces.js b/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplaces.js index d3eec0b63b..c7200743f8 100644 --- a/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplaces.js +++ b/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplaces.js @@ -1,8 +1,6 @@ const nextEmail = require('./nextEmail'); -const { - getInactiveWorkplaces, - refreshInactiveWorkplaces, -} = require('../../../models/email-campaigns/inactive-workplaces/getInactiveWorkplaces'); +const { getInactiveWorkplaces } = require('../../../models/email-campaigns/inactive-workplaces/getInactiveWorkplaces'); +const { refreshEstablishmentLastActivityView } = require('../../../utils/db/inactiveWorkplacesUtils'); const transformInactiveWorkplaces = (inactiveWorkplace) => { const id = inactiveWorkplace.EstablishmentID; @@ -30,7 +28,7 @@ const transformInactiveWorkplaces = (inactiveWorkplace) => { }; const findInactiveWorkplaces = async () => { - await refreshInactiveWorkplaces(); + await refreshEstablishmentLastActivityView(); return (await getInactiveWorkplaces()).filter(nextEmail.shouldReceive).map(transformInactiveWorkplaces); }; diff --git a/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplacesForDeletion.js b/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplacesForDeletion.js index 48fe8f9fd8..cb590745e9 100644 --- a/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplacesForDeletion.js +++ b/backend/server/services/email-campaigns/inactive-workplaces/setInactiveWorkplacesForDeletion.js @@ -1,7 +1,7 @@ const { getInactiveWorkplacesForDeletion, - refreshInactiveWorkplacesForDeletion, } = require('../../../models/email-campaigns/inactive-workplaces/getInactiveWorkplacesForDeletion'); +const { refreshEstablishmentLastActivityView } = require('../../../utils/db/inactiveWorkplacesUtils'); const transformInactiveWorkplacesForDeletion = (inactiveWorkplace) => { const name = inactiveWorkplace.NameValue; @@ -34,7 +34,7 @@ const formattedAddress = (inactiveWorkplace) => { }; const findInactiveWorkplacesForDeletion = async () => { - await refreshInactiveWorkplacesForDeletion(); + await refreshEstablishmentLastActivityView(); return (await getInactiveWorkplacesForDeletion()).map(transformInactiveWorkplacesForDeletion); }; diff --git a/backend/server/utils/db/inactiveWorkplacesUtils.js b/backend/server/utils/db/inactiveWorkplacesUtils.js new file mode 100644 index 0000000000..7c5d4c6829 --- /dev/null +++ b/backend/server/utils/db/inactiveWorkplacesUtils.js @@ -0,0 +1,5 @@ +const models = require('../../models'); + +module.exports.refreshEstablishmentLastActivityView = async () => { + return models.sequelize.query('REFRESH MATERIALIZED VIEW cqc."EstablishmentLastActivity"'); +};