Skip to content

Commit

Permalink
allow admins to reset all memberships on front end
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev committed Nov 11, 2024
1 parent 5fdfea5 commit ec45dd6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default meta
type Story = StoryObj<typeof meta>
export const DefaultAdminMemberView: Story = {
args: {
data: mockDataArray
data: mockDataArray,
handleResetMemberships() {
alert("YOU WANT TO RESET???")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ interface IAdminMemberView {
* Used to indicate if there is currently an operation going on
*/
isUpdating?: boolean

/**
* Method which makes relevant network call to make all
* non-admin users guests, which forces everyone to pay
* for a membership again
*/
handleResetMemberships?: () => void
}

/**
Expand Down Expand Up @@ -101,7 +108,8 @@ export const AdminMemberView = ({
openAddMemberView,
exportUserDataHandler,
isUpdating,
hasNextPage
hasNextPage,
handleResetMemberships
}: IAdminMemberView) => {
/**
* For use with `AdminSearchBar`
Expand Down Expand Up @@ -194,7 +202,7 @@ export const AdminMemberView = ({
<div
className={`w-full ${isUpdating ? "brightness-75" : "brightness-100"}`}
>
<span className="mb-4 mt-6 flex w-full justify-between">
<span className="mb-4 mt-6 flex w-full flex-col justify-between sm:flex-row">
<span className="flex gap-5">
<AdminSearchBar onQueryChanged={onSeachQueryChangedHandler} />
<Button
Expand Down Expand Up @@ -227,6 +235,11 @@ export const AdminMemberView = ({
setIsLastPage(last)
}}
/>
<span className="mt-2 flex">
<Button variant="inverted-default-sm" onClick={handleResetMemberships}>
DANGER - Reset Memberships
</Button>
</span>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { AdminMemberView, MemberColumnFormat } from "./AdminMemberView"
import {
useDeleteUserMutation,
useDemoteUserMutation,
usePromoteUserMutation
usePromoteUserMutation,
useResetMembershipsMutation
} from "@/services/Admin/AdminMutations"
import { TableRowOperation } from "@/components/generic/ReusableTable/TableUtils"
import AdminUserCreationModal, {
Expand Down Expand Up @@ -37,6 +38,8 @@ const WrappedAdminMemberView = () => {
*/
const { mutateAsync: addNewUser } = useSignUpUserMutation("admin")

const { mutateAsync: resetAllMemberships } = useResetMembershipsMutation()

/**
* https://stackoverflow.com/a/68066447
*/
Expand Down Expand Up @@ -190,6 +193,15 @@ const WrappedAdminMemberView = () => {
fetchNextPage={() => {
!isFetchingNextPage && hasNextPage && fetchNextPage()
}}
handleResetMemberships={async () => {
if (
confirm(
"Are you SURE you want to reset all memberships for ALL members (they will have to pay for membership again)"
)
) {
await resetAllMemberships()
}
}}
hasNextPage={hasNextPage}
exportUserDataHandler={handleExportUsers}
isUpdating={isPending}
Expand Down
13 changes: 13 additions & 0 deletions client/src/services/Admin/AdminMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,16 @@ export function useDeleteEventMutation() {
onSuccess: invalidateEventsQuery
})
}

export function useResetMembershipsMutation() {
return useMutation({
mutationKey: ["reset-memberships"],
retry: false,
mutationFn: AdminService.resetMemberships,
onSuccess: () => {
queryClient.removeQueries({
queryKey: [ALL_USERS_QUERY]
})
}
})
}
7 changes: 7 additions & 0 deletions client/src/services/Admin/AdminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ const AdminService = {
if (!response.ok) {
throw new Error(`Failed to delete event with id ${eventId}`)
}
},
resetMemberships: async function () {
const { response } = await fetchClient.PATCH("/admin/users/demote-all")

if (!response.ok) {
throw new Error(`Failed to demote all users`)
}
}
} as const

Expand Down
2 changes: 1 addition & 1 deletion server/src/service-layer/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export class AdminController extends Controller {
const authService = new AuthService()
let allUsers: UserRecord[] = await authService.getAllUsers()
allUsers = allUsers.filter(
(user) => !user.customClaims.admin && user.customClaims.member
(user) => !user.customClaims?.admin && user.customClaims?.member
)
const demotePromises = await Promise.all(
allUsers.map((user) => {
Expand Down

0 comments on commit ec45dd6

Please sign in to comment.