-
Notifications
You must be signed in to change notification settings - Fork 14
feat: delete user #8868
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
Closed
Closed
feat: delete user #8868
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0685b3d
feat: delete user
csiyang 92b7cb9
fix: lint issues
autofix-ci[bot] 80f444d
review comments
csiyang 51242fa
fix: lint issues
autofix-ci[bot] 744172d
update locales
csiyang b7d521f
update log text
csiyang fe02909
add interop logging
csiyang 53c5d28
fix: lint issues
autofix-ci[bot] 21d36ca
refactor: data delete
csiyang 902d922
fix: lint issues
autofix-ci[bot] b9fca43
split out delete call more
csiyang c74f642
fix: lint issues
autofix-ci[bot] 8a3faf1
refactor delete to be subscription
csiyang c4b9ae8
refactor: firebase look up
csiyang c340bac
fix: lint issues
autofix-ci[bot] 831753a
refactor use interop
csiyang f8f8273
refactor delete firebase user
csiyang c380cd3
feat: scrolling log box
csiyang a402960
update generated schema
csiyang 2c4c561
fix: lint issues
autofix-ci[bot] d598e6f
test: add user logging
csiyang 975a82c
fix: lint issues
autofix-ci[bot] 1765f22
refactor: log fields
csiyang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import './userDeleteJourneysCheck' | ||
| import './userDeleteJourneysConfirm' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export interface LogEntry { | ||
| message: string | ||
| level: string | ||
| timestamp: string | ||
| } | ||
|
|
||
| export function createLog(message: string, level = 'info'): LogEntry { | ||
| return { message, level, timestamp: new Date().toISOString() } | ||
| } |
221 changes: 221 additions & 0 deletions
221
apis/api-journeys-modern/src/schema/userDelete/userDeleteJourneysCheck.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { prisma } from '@core/prisma/journeys/client' | ||
|
|
||
| import { builder } from '../builder' | ||
|
|
||
| import { LogEntry, createLog } from './types' | ||
|
|
||
| const UserDeleteJourneysLogEntry = builder.objectRef<LogEntry>( | ||
| 'UserDeleteJourneysLogEntry' | ||
| ) | ||
|
|
||
| builder.objectType(UserDeleteJourneysLogEntry, { | ||
| fields: (t) => ({ | ||
| message: t.exposeString('message', { nullable: false }), | ||
| level: t.exposeString('level', { nullable: false }), | ||
| timestamp: t.exposeString('timestamp', { nullable: false }) | ||
| }) | ||
| }) | ||
|
|
||
| interface JourneysCheckResultShape { | ||
| journeysToDelete: number | ||
| journeysToTransfer: number | ||
| journeysToRemove: number | ||
| teamsToDelete: number | ||
| teamsToTransfer: number | ||
| teamsToRemove: number | ||
| logs: LogEntry[] | ||
| } | ||
|
|
||
| const UserDeleteJourneysCheckResult = | ||
| builder.objectRef<JourneysCheckResultShape>('UserDeleteJourneysCheckResult') | ||
|
|
||
| builder.objectType(UserDeleteJourneysCheckResult, { | ||
| fields: (t) => ({ | ||
| journeysToDelete: t.exposeInt('journeysToDelete', { nullable: false }), | ||
| journeysToTransfer: t.exposeInt('journeysToTransfer', { nullable: false }), | ||
| journeysToRemove: t.exposeInt('journeysToRemove', { nullable: false }), | ||
| teamsToDelete: t.exposeInt('teamsToDelete', { nullable: false }), | ||
| teamsToTransfer: t.exposeInt('teamsToTransfer', { nullable: false }), | ||
| teamsToRemove: t.exposeInt('teamsToRemove', { nullable: false }), | ||
| logs: t.field({ | ||
| type: [UserDeleteJourneysLogEntry], | ||
| nullable: false, | ||
| resolve: (parent) => parent.logs | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| builder.mutationField('userDeleteJourneysCheck', (t) => | ||
| t.withAuth({ isValidInterop: true }).field({ | ||
| type: UserDeleteJourneysCheckResult, | ||
| nullable: false, | ||
| args: { | ||
| userId: t.arg.string({ required: true }) | ||
| }, | ||
| resolve: async (_parent, { userId }) => { | ||
| const logs: LogEntry[] = [] | ||
|
|
||
| // Check journeys | ||
| const userJourneys = await prisma.userJourney.findMany({ | ||
| where: { userId }, | ||
| include: { | ||
| journey: { | ||
| select: { | ||
| id: true, | ||
| title: true, | ||
| userJourneys: { | ||
| select: { id: true, userId: true, role: true } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| logs.push(createLog(`${userJourneys.length} journeys found`)) | ||
|
|
||
| let journeysToDelete = 0 | ||
| let journeysToTransfer = 0 | ||
| let journeysToRemove = 0 | ||
|
|
||
| for (const uj of userJourneys) { | ||
| const others = uj.journey.userJourneys.filter( | ||
| (j) => j.userId !== userId | ||
| ) | ||
| if (others.length === 0) { | ||
| journeysToDelete++ | ||
| } else if (uj.role === 'owner') { | ||
| journeysToTransfer++ | ||
| } else { | ||
| journeysToRemove++ | ||
| } | ||
| } | ||
|
|
||
| if (journeysToDelete > 0) | ||
| logs.push( | ||
| createLog( | ||
| `${journeysToDelete} journeys will be deleted (user is sole accessor)` | ||
| ) | ||
| ) | ||
| if (journeysToTransfer > 0) | ||
| logs.push( | ||
| createLog( | ||
| `${journeysToTransfer} journey ownerships will be transferred` | ||
| ) | ||
| ) | ||
| if (journeysToRemove > 0) | ||
| logs.push( | ||
| createLog(`${journeysToRemove} journey memberships will be removed`) | ||
| ) | ||
|
|
||
| // Check teams | ||
| const userTeams = await prisma.userTeam.findMany({ | ||
| where: { userId }, | ||
| include: { | ||
| team: { | ||
| select: { | ||
| id: true, | ||
| title: true, | ||
| userTeams: { | ||
| select: { id: true, userId: true, role: true } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| logs.push(createLog(`${userTeams.length} teams found`)) | ||
|
|
||
| let teamsToDelete = 0 | ||
| let teamsToTransfer = 0 | ||
| let teamsToRemove = 0 | ||
|
|
||
| for (const ut of userTeams) { | ||
| const others = ut.team.userTeams.filter((t) => t.userId !== userId) | ||
| if (others.length === 0) { | ||
| teamsToDelete++ | ||
| } else if (ut.role === 'manager') { | ||
| teamsToTransfer++ | ||
| } else { | ||
| teamsToRemove++ | ||
| } | ||
| } | ||
|
|
||
| if (teamsToDelete > 0) | ||
| logs.push( | ||
| createLog( | ||
| `${teamsToDelete} teams will be deleted (user is sole member)` | ||
| ) | ||
| ) | ||
| if (teamsToTransfer > 0) | ||
| logs.push( | ||
| createLog(`${teamsToTransfer} team manager roles will be transferred`) | ||
| ) | ||
| if (teamsToRemove > 0) | ||
| logs.push( | ||
| createLog(`${teamsToRemove} team memberships will be removed`) | ||
| ) | ||
|
|
||
| // Check related records | ||
| const [ | ||
| userRole, | ||
| journeyProfile, | ||
| integration, | ||
| visitor, | ||
| journeyNotification, | ||
| userTeamInvite, | ||
| userInvite, | ||
| journeyEventsExportLog, | ||
| journeyTheme | ||
| ] = await Promise.all([ | ||
| prisma.userRole.count({ where: { userId } }), | ||
| prisma.journeyProfile.count({ where: { userId } }), | ||
| prisma.integration.count({ where: { userId } }), | ||
| prisma.visitor.count({ where: { userId } }), | ||
| prisma.journeyNotification.count({ where: { userId } }), | ||
| prisma.userTeamInvite.count({ | ||
| where: { OR: [{ senderId: userId }, { receipientId: userId }] } | ||
| }), | ||
| prisma.userInvite.count({ where: { senderId: userId } }), | ||
| prisma.journeyEventsExportLog.count({ where: { userId } }), | ||
| prisma.journeyTheme.count({ where: { userId } }) | ||
| ]) | ||
|
|
||
| const tablesToClean: string[] = [] | ||
| if (userRole > 0) tablesToClean.push(`UserRole(${userRole})`) | ||
| if (journeyProfile > 0) | ||
| tablesToClean.push(`JourneyProfile(${journeyProfile})`) | ||
| if (integration > 0) tablesToClean.push(`Integration(${integration})`) | ||
| if (visitor > 0) tablesToClean.push(`Visitor(${visitor})`) | ||
| if (journeyNotification > 0) | ||
| tablesToClean.push(`JourneyNotification(${journeyNotification})`) | ||
| if (userTeamInvite > 0) | ||
| tablesToClean.push(`UserTeamInvite(${userTeamInvite})`) | ||
| if (userInvite > 0) tablesToClean.push(`UserInvite(${userInvite})`) | ||
| if (journeyEventsExportLog > 0) | ||
| tablesToClean.push(`JourneyEventsExportLog(${journeyEventsExportLog})`) | ||
| if (journeyTheme > 0) tablesToClean.push(`JourneyTheme(${journeyTheme})`) | ||
|
|
||
| if (tablesToClean.length > 0) { | ||
| logs.push( | ||
| createLog(`Related records to clean up: ${tablesToClean.join(', ')}`) | ||
| ) | ||
| } else { | ||
| logs.push(createLog('No additional related records found')) | ||
| } | ||
|
|
||
| logs.push(createLog('Check complete. Ready for deletion confirmation.')) | ||
|
|
||
| return { | ||
| journeysToDelete, | ||
| journeysToTransfer, | ||
| journeysToRemove, | ||
| teamsToDelete, | ||
| teamsToTransfer, | ||
| teamsToRemove, | ||
| logs | ||
| } | ||
| } | ||
| }) | ||
| ) | ||
|
|
||
| export { UserDeleteJourneysLogEntry } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.