-
Notifications
You must be signed in to change notification settings - Fork 206
feat: bulk delete members ✌️ #879
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e87f18
allow max rows for textarea
ramiAbdou 7abe2fe
remove students
ramiAbdou b116935
entrypoint in the students page
ramiAbdou abd2d8b
update default bull options
ramiAbdou d5b5348
update language
ramiAbdou 4f8f10a
ensure that airtable delete throws
ramiAbdou 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
140 changes: 140 additions & 0 deletions
140
apps/admin-dashboard/app/routes/_dashboard.students.remove.tsx
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,140 @@ | ||
| import { | ||
| type ActionFunctionArgs, | ||
| data, | ||
| Form, | ||
| type LoaderFunctionArgs, | ||
| redirect, | ||
| useActionData, | ||
| } from 'react-router'; | ||
| import z from 'zod'; | ||
|
|
||
| import { job } from '@oyster/core/bull'; | ||
| import { db } from '@oyster/db'; | ||
| import { | ||
| Button, | ||
| ErrorMessage, | ||
| Field, | ||
| getErrors, | ||
| Modal, | ||
| Textarea, | ||
| validateForm, | ||
| } from '@oyster/ui'; | ||
| import { Callout } from '@oyster/ui/callout'; | ||
|
|
||
| import { Route } from '@/shared/constants'; | ||
| import { | ||
| commitSession, | ||
| ensureUserAuthenticated, | ||
| toast, | ||
| } from '@/shared/session.server'; | ||
|
|
||
| export async function loader({ request }: LoaderFunctionArgs) { | ||
| await ensureUserAuthenticated(request); | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| const RemoveMembersFormData = z.object({ | ||
| memberIds: z | ||
| .string() | ||
| .min(1) | ||
| .transform((value) => value.split('\n').filter(Boolean)), | ||
| }); | ||
|
|
||
| export async function action({ request }: ActionFunctionArgs) { | ||
| const session = await ensureUserAuthenticated(request); | ||
|
|
||
| const form = await request.formData(); | ||
|
|
||
| const result = await validateForm(form, RemoveMembersFormData); | ||
|
|
||
| if (!result.ok) { | ||
| return data(result, { status: 400 }); | ||
| } | ||
|
|
||
| const count = await removeMembers(result.data.memberIds); | ||
|
|
||
| toast(session, { | ||
| message: `Removed ${count} members.`, | ||
| }); | ||
|
|
||
| return redirect(Route['/students'], { | ||
| headers: { | ||
| 'Set-Cookie': await commitSession(session), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| async function removeMembers(ids: string[]): Promise<number> { | ||
| const students = await db | ||
| .deleteFrom('students') | ||
| .where('id', 'in', ids) | ||
| .returning(['airtableId', 'email', 'firstName', 'slackId']) | ||
| .execute(); | ||
|
|
||
| for (const student of students) { | ||
| job('student.removed', { | ||
| airtableId: student.airtableId as string, | ||
| email: student.email, | ||
| firstName: student.firstName, | ||
| sendViolationEmail: false, | ||
| slackId: student.slackId, | ||
| }); | ||
| } | ||
|
|
||
| return students.length; | ||
| } | ||
|
|
||
| export default function RemoveMembersPage() { | ||
| const { error, errors } = getErrors(useActionData<typeof action>()); | ||
|
|
||
| return ( | ||
| <Modal onCloseTo={Route['/students']}> | ||
| <Modal.Header> | ||
| <Modal.Title>Bulk Remove Members</Modal.Title> | ||
| <Modal.CloseButton /> | ||
| </Modal.Header> | ||
|
|
||
| <Modal.Description> | ||
| This action is not reversible. All of their engagement records will be | ||
| deleted and they will be removed from Slack, Mailchimp and Airtable. | ||
| </Modal.Description> | ||
|
|
||
| <Callout color="blue"> | ||
| Note: These members will immediately be removed from our database, but | ||
| it may take some time for them to be removed from Slack, Mailchimp and | ||
| Airtable. | ||
| </Callout> | ||
|
|
||
| <Form className="form" method="post"> | ||
| <ErrorMessage>{error}</ErrorMessage> | ||
|
|
||
| <Field | ||
| description="Please list the IDs of the members to remove separated by a newline." | ||
| error={errors.memberIds} | ||
| label="Member IDs" | ||
| labelFor="memberIds" | ||
| required | ||
| > | ||
| <Textarea | ||
| id="memberIds" | ||
| maxRows={10} | ||
| minRows={10} | ||
| name="memberIds" | ||
| required | ||
| /> | ||
| </Field> | ||
|
|
||
| <Button.Group> | ||
| <Button color="error" type="submit"> | ||
| Remove | ||
| </Button> | ||
| </Button.Group> | ||
| </Form> | ||
| </Modal> | ||
| ); | ||
| } | ||
|
|
||
| export function ErrorBoundary() { | ||
| return <></>; | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Unsafe Type Assertion Causes Null Handling Issues
The
student.airtableId as stringtype assertion is unsafe. IfairtableIdis null from the database, it passes null to thestudent.removedjob, bypassing TypeScript's null checks. This can cause runtime errors or unexpected behavior in the job processor.