From 6ffcf812f5f5dc41518faca6c9c5e2f4482680bb Mon Sep 17 00:00:00 2001 From: Anna Hughes Date: Tue, 9 Jan 2024 11:20:44 +0000 Subject: [PATCH 1/2] Add function to delete cypress crisp profiles (#379) --- src/api/crisp/crisp-api.ts | 22 +++++++++++++++ src/partner-access/partner-access.service.ts | 2 +- src/user/user.controller.ts | 7 +++++ src/user/user.service.ts | 28 +++++++++++++++----- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/api/crisp/crisp-api.ts b/src/api/crisp/crisp-api.ts index 0bce330d..b51924af 100644 --- a/src/api/crisp/crisp-api.ts +++ b/src/api/crisp/crisp-api.ts @@ -210,3 +210,25 @@ export const deleteCrispProfile = async (email: string) => { throw error; } }; + +export const deleteCypressCrispProfiles = async () => { + try { + const profiles = await apiCall({ + url: `${baseUrl}/people/profiles/1?search_text=cypresstestemail+`, + type: 'get', + headers, + }); + + profiles.data.data.forEach(async (profile) => { + await apiCall({ + url: `${baseUrl}/people/profile/${profile.email}`, + type: 'delete', + headers, + }); + }); + + return 'ok'; + } catch (error) { + throw error; + } +}; diff --git a/src/partner-access/partner-access.service.ts b/src/partner-access/partner-access.service.ts index 8f048e0d..b03721ef 100644 --- a/src/partner-access/partner-access.service.ts +++ b/src/partner-access/partner-access.service.ts @@ -206,7 +206,7 @@ export class PartnerAccessService { const partnerAccessRecords = await this.partnerAccessRepository //get partner access instances where user is a cypress user .createQueryBuilder('partnerAccess') .leftJoinAndSelect('partnerAccess.user', 'user') - .where('user.name LIKE :searchTerm', { searchTerm: `%Cypress test user%` }) + .where('user.name LIKE :searchTerm', { searchTerm: `%Cypress test%` }) .getMany(); await Promise.all( partnerAccessRecords.map(async (access) => { diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 4234a7a2..13d188f6 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -69,6 +69,13 @@ export class UserController { return await this.userService.deleteCypressTestUsers(); } + @ApiBearerAuth('access-token') + @Delete('/cypress-clean') + @UseGuards(SuperAdminAuthGuard) + async cleanCypressUsers(): Promise { + return await this.userService.deleteCypressTestUsers(true); + } + @ApiBearerAuth() @Delete(':id') @ApiParam({ name: 'id', description: 'User id to delete' }) diff --git a/src/user/user.service.ts b/src/user/user.service.ts index cbff29fb..55e270cc 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -14,6 +14,7 @@ import { import { addCrispProfile, deleteCrispProfile, + deleteCypressCrispProfiles, updateCrispProfileData, } from '../api/crisp/crisp-api'; import { AuthService } from '../auth/auth.service'; @@ -353,7 +354,8 @@ export class UserService { return user; } - public async deleteCypressTestUsers(): Promise { + + public async deleteCypressTestUsers(clean = false): Promise { try { const queryResult = await this.userRepository .createQueryBuilder('user') @@ -364,6 +366,7 @@ export class UserService { const deletedUsers = await Promise.all( queryResult.map(async (user) => { try { + await deleteCrispProfile(user.email); await this.authService.deleteFirebaseUser(user.firebaseUid); await this.userRepository.delete(user); return user; @@ -373,15 +376,28 @@ export class UserService { }), ); - // Delete all remaining cypress firebase users (e.g. from failed user creations) - this.authService.deleteCypressFirebaseUsers(); - return deletedUsers; } catch (error) { - // If this fails we don't want to break cypress tests - this.logger.error(`deleteCypressTestAccessCodes - Unable to delete all cypress users`, error); + // If this fails we don't want to break cypress tests but we want to be alerted + this.logger.error(`deleteCypressTestUsers - Unable to delete all cypress users`, error); + } + + try { + // Clean remaining user accounts in firebase and crisp that do not have a user record in the db + // These rogue accounts may be left over from incomplete signups or errors + if (clean) { + // Delete all remaining cypress firebase users (e.g. from failed user creations) + await this.authService.deleteCypressFirebaseUsers(); + + // Delete all remaining crisp accounts + await deleteCypressCrispProfiles(); + } + } catch (error) { + // If this fails we don't want to break cypress tests but we want to be alerted + this.logger.error(`deleteCypressTestUsers - Unable to clean all cypress users`, error); } } + public async getUsers( filters: { email?: string; From d9ec88d68d252bcb16a479da7064440bea9ee50b Mon Sep 17 00:00:00 2001 From: Anna Hughes Date: Tue, 9 Jan 2024 11:24:45 +0000 Subject: [PATCH 2/2] Fix workflow triggers (#381) --- .github/workflows/.ci.yml | 2 +- .github/workflows/codeql-analysis.yml | 2 +- .github/workflows/create-release-pr.yml | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/.ci.yml b/.github/workflows/.ci.yml index 34edb78d..a342c29d 100644 --- a/.github/workflows/.ci.yml +++ b/.github/workflows/.ci.yml @@ -2,7 +2,7 @@ name: Bloom Backend CI Pipeline on: pull_request: - branches: [develop, main] + branches: [develop] push: branches: [develop] diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 0c7fbe17..9d626f37 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -15,7 +15,7 @@ on: push: branches: [develop, main] pull_request: - branches: [develop, main] + branches: [develop] schedule: - cron: '25 5 * * 5' diff --git a/.github/workflows/create-release-pr.yml b/.github/workflows/create-release-pr.yml index fad725fe..b8dd0498 100644 --- a/.github/workflows/create-release-pr.yml +++ b/.github/workflows/create-release-pr.yml @@ -2,8 +2,7 @@ name: Create release PR on: push: - branches: - - develop + branches: [develop] jobs: create-pr-to-main: