Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions apps/admin-dashboard/app/routes/_dashboard.admins.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type LoaderFunctionArgs, Outlet, useLoaderData } from 'react-router';
import { Plus } from 'react-feather';
import {
Link,
type LoaderFunctionArgs,
Outlet,
useLoaderData,
} from 'react-router';

import {
doesAdminHavePermission,
Expand All @@ -7,8 +13,9 @@ import {
} from '@oyster/core/admins';
import { type AdminRole } from '@oyster/core/admins/types';
import { AdminTable } from '@oyster/core/admins/ui';
import { Dashboard } from '@oyster/ui';
import { Button, Dashboard } from '@oyster/ui';

import { Route } from '@/shared/constants';
import { ensureUserAuthenticated, user } from '@/shared/session.server';

export async function loader({ request }: LoaderFunctionArgs) {
Expand Down Expand Up @@ -65,10 +72,21 @@ export default function Admins() {
<>
<Dashboard.Header>
<Dashboard.Title>Admins</Dashboard.Title>
<AddAdminButton />
</Dashboard.Header>

<AdminTable admins={admins} />
<Outlet />
</>
);
}

function AddAdminButton() {
return (
<Button.Slot variant="primary">
<Link to={Route['/admins/add']}>
<Plus size={16} /> Add Admin
</Link>
</Button.Slot>
);
}
20 changes: 8 additions & 12 deletions packages/core/src/modules/admins/admins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ export async function addAdmin({
});
}

const existingAdmin = await getAdmin({
select: [],
where: { email },
});

if (existingAdmin) {
return fail({
code: 409,
error: 'An admin already exists with this email.',
});
}

const adminId = id();

await db.transaction().execute(async (trx) => {
Expand All @@ -153,6 +141,14 @@ export async function addAdmin({
role,
};
})
.onConflict((eb) => {
return eb.column('email').doUpdateSet({
deletedAt: null,
firstName,
lastName,
role,
});
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Admin Update Bug Causes Incorrect ID Handling

The addAdmin function returns a newly generated ID when an existing admin is updated via the onConflict clause, leading to an incorrect ID reference. This onConflict logic also overwrites firstName, lastName, and role for active admins, and it doesn't re-evaluate or update the memberId.

Fix in Cursor Fix in Web

.execute();
});

Expand Down