From 4bf4a6191c8e7f20ec16fc11f8d165ce3fc86b84 Mon Sep 17 00:00:00 2001 From: Duncan Carter Date: Fri, 26 Jul 2024 16:43:16 +0100 Subject: [PATCH 1/3] Add migration to create parent approval records for all parents without record in Approvals table --- ...reateParentApprovalForOldParentAccounts.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js diff --git a/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js b/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js new file mode 100644 index 0000000000..913f4aca3d --- /dev/null +++ b/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js @@ -0,0 +1,54 @@ +'use strict'; +const models = require('../server/models/index'); + +const dateAccountsCreatedInService = new Date('2019-08-30T00:00:00Z'); + +module.exports = { + up: async (queryInterface, Sequelize) => { + return queryInterface.sequelize.transaction(async (transaction) => { + const parentEstablishmentsWithoutApprovalRecord = await queryInterface.sequelize.query( + ` + SELECT e."EstablishmentID", u."RegistrationID" + FROM cqc."Establishment" e + LEFT JOIN cqc."Approvals" a ON e."EstablishmentID" = a."EstablishmentID" + JOIN cqc."User" u ON e."EstablishmentID" = u."EstablishmentID" + WHERE a."EstablishmentID" IS NULL AND e."IsParent" = true + AND e."IsRegulated" = true + AND e."Archived" = false + AND u."IsPrimary" = true; + `, + { type: Sequelize.QueryTypes.SELECT, transaction }, + ); + + for (const establishment of parentEstablishmentsWithoutApprovalRecord) { + await models.Approvals.create( + { + EstablishmentID: establishment.EstablishmentID, + ApprovalType: 'BecomeAParent', + UserID: establishment.RegistrationID, + Status: 'Approved', + createdAt: dateAccountsCreatedInService, + updatedAt: dateAccountsCreatedInService, + }, + { + transaction, + silent: true, // prevents updatedAt being overridden + }, + ); + } + }); + }, + + down: async (queryInterface) => { + return queryInterface.sequelize.transaction(async (transaction) => { + await models.Approvals.destroy( + { + where: { + createdAt: dateAccountsCreatedInService, + }, + }, + { transaction }, + ); + }); + }, +}; From 3a0a905d5ceb88863baa3cd0e1275af57b2ae184 Mon Sep 17 00:00:00 2001 From: Duncan Carter Date: Fri, 26 Jul 2024 17:19:18 +0100 Subject: [PATCH 2/3] Add check for user not being archived to ensure only one primary user will be returned --- .../20240726100029-createParentApprovalForOldParentAccounts.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js b/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js index 913f4aca3d..8285a02b7a 100644 --- a/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js +++ b/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js @@ -15,7 +15,8 @@ module.exports = { WHERE a."EstablishmentID" IS NULL AND e."IsParent" = true AND e."IsRegulated" = true AND e."Archived" = false - AND u."IsPrimary" = true; + AND u."IsPrimary" = true + AND u."Archived" = false; `, { type: Sequelize.QueryTypes.SELECT, transaction }, ); From b7fc5e83241e07831c11a4f2dedc402052d02c7e Mon Sep 17 00:00:00 2001 From: Duncan Carter Date: Mon, 29 Jul 2024 12:12:38 +0100 Subject: [PATCH 3/3] Remove check for CQC regulated so all parent workplaces are updated --- .../20240726100029-createParentApprovalForOldParentAccounts.js | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js b/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js index 8285a02b7a..f18817f127 100644 --- a/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js +++ b/backend/migrations/20240726100029-createParentApprovalForOldParentAccounts.js @@ -13,7 +13,6 @@ module.exports = { LEFT JOIN cqc."Approvals" a ON e."EstablishmentID" = a."EstablishmentID" JOIN cqc."User" u ON e."EstablishmentID" = u."EstablishmentID" WHERE a."EstablishmentID" IS NULL AND e."IsParent" = true - AND e."IsRegulated" = true AND e."Archived" = false AND u."IsPrimary" = true AND u."Archived" = false;