unlink user
This commit is contained in:
@@ -51,7 +51,7 @@ class OrgMissingError(NotFoundError): ...
|
|||||||
|
|
||||||
|
|
||||||
@router.post('/<org_id>/users')
|
@router.post('/<org_id>/users')
|
||||||
def add_user(
|
def add(
|
||||||
org_id: str,
|
org_id: str,
|
||||||
user: Annotated[User, Body(embed=True)],
|
user: Annotated[User, Body(embed=True)],
|
||||||
org: Annotated[Org, Body(embed=True)],
|
org: Annotated[Org, Body(embed=True)],
|
||||||
@@ -67,6 +67,34 @@ def add_user(
|
|||||||
return JSONResponse(HTTPStatus.NO_CONTENT)
|
return JSONResponse(HTTPStatus.NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete('/<org_id>/users/<user_id>')
|
||||||
|
def unlink(org_id: str, user_id: str):
|
||||||
|
with dyn.transact_writer() as transact:
|
||||||
|
transact.delete(
|
||||||
|
key=KeyPair(
|
||||||
|
pk=f'orgmembers#{org_id}',
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# pk=f'MEMBER#ORG#{org_id}',
|
||||||
|
sk=user_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
transact.delete(
|
||||||
|
key=KeyPair(
|
||||||
|
pk=user_id,
|
||||||
|
sk=f'orgs#{org_id}',
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# pk=f'ORG#{org_id}',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
transact.update(
|
||||||
|
key=KeyPair(user_id, '0'),
|
||||||
|
update_expr='DELETE tenant_id :org_id',
|
||||||
|
expr_attr_values={':org_id': {org_id}},
|
||||||
|
)
|
||||||
|
|
||||||
|
return JSONResponse(HTTPStatus.NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
def _create_user(user: User, org: Org) -> bool:
|
def _create_user(user: User, org: Org) -> bool:
|
||||||
now_ = now()
|
now_ = now()
|
||||||
user_id = uuid4()
|
user_id = uuid4()
|
||||||
@@ -79,7 +107,9 @@ def _create_user(user: User, org: Org) -> bool:
|
|||||||
'id': user_id,
|
'id': user_id,
|
||||||
'sk': '0',
|
'sk': '0',
|
||||||
'email_verified': False,
|
'email_verified': False,
|
||||||
'org_id': {org.id},
|
'tenant_id': {org.id},
|
||||||
|
# Post-migration: uncomment the folloing line
|
||||||
|
# 'org_id': {org.id},
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -116,8 +146,9 @@ def _create_user(user: User, org: Org) -> bool:
|
|||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': user_id,
|
'id': user_id,
|
||||||
# Post-migration: rename `orgs` to `ORG`
|
|
||||||
'sk': f'orgs#{org.id}',
|
'sk': f'orgs#{org.id}',
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# pk=f'ORG#{org.id}',
|
||||||
'name': org.name,
|
'name': org.name,
|
||||||
'cnpj': org.cnpj,
|
'cnpj': org.cnpj,
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
@@ -125,8 +156,9 @@ def _create_user(user: User, org: Org) -> bool:
|
|||||||
)
|
)
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
# Post-migration: rename `orgmembers` to `ORGMEMBER`
|
|
||||||
'id': f'orgmembers#{org.id}',
|
'id': f'orgmembers#{org.id}',
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# pk=f'MEMBER#ORG#{org_id}',
|
||||||
'sk': user_id,
|
'sk': user_id,
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
@@ -148,10 +180,14 @@ def _add_member(user_id: str, org: Org) -> None:
|
|||||||
with dyn.transact_writer() as transact:
|
with dyn.transact_writer() as transact:
|
||||||
transact.update(
|
transact.update(
|
||||||
key=KeyPair(user_id, '0'),
|
key=KeyPair(user_id, '0'),
|
||||||
update_expr='ADD org_id :org_id',
|
update_expr='ADD tenant_id :org_id',
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# update_expr='ADD tenant_id :org_id',
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':org_id': {org.id},
|
':org_id': {org.id},
|
||||||
},
|
},
|
||||||
|
cond_expr='attribute_exists(sk)',
|
||||||
|
exc_cls=UserMissingError,
|
||||||
)
|
)
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from http import HTTPMethod, HTTPStatus
|
|||||||
|
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
|
KeyPair,
|
||||||
PartitionKey,
|
PartitionKey,
|
||||||
SortKey,
|
SortKey,
|
||||||
TransactKey,
|
TransactKey,
|
||||||
@@ -50,7 +51,8 @@ def test_add_user(
|
|||||||
assert 'email' in user
|
assert 'email' in user
|
||||||
assert 'email_verified' in user
|
assert 'email_verified' in user
|
||||||
assert 'created_at' in user
|
assert 'created_at' in user
|
||||||
assert 'org_id' in user
|
# assert 'org_id' in user
|
||||||
|
assert 'tenant_id' in user
|
||||||
assert 'emails#scott@stonetemplopilots.com' in user
|
assert 'emails#scott@stonetemplopilots.com' in user
|
||||||
|
|
||||||
|
|
||||||
@@ -149,3 +151,30 @@ def test_org_not_found(
|
|||||||
)
|
)
|
||||||
body = json.loads(r['body'])
|
body = json.loads(r['body'])
|
||||||
assert body['type'] == 'OrgMissingError'
|
assert body['type'] == 'OrgMissingError'
|
||||||
|
|
||||||
|
|
||||||
|
def test_unlink(
|
||||||
|
app,
|
||||||
|
seeds,
|
||||||
|
http_api_proxy: HttpApiProxy,
|
||||||
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||||
|
lambda_context: LambdaContext,
|
||||||
|
):
|
||||||
|
r = app.lambda_handler(
|
||||||
|
http_api_proxy(
|
||||||
|
raw_path='/orgs/f6000f79-6e5c-49a0-952f-3bda330ef278/users/15bacf02-1535-4bee-9022-19d106fd7518',
|
||||||
|
method=HTTPMethod.DELETE,
|
||||||
|
),
|
||||||
|
lambda_context,
|
||||||
|
)
|
||||||
|
assert r['statusCode'] == HTTPStatus.NO_CONTENT
|
||||||
|
|
||||||
|
members = dynamodb_persistence_layer.collection.query(
|
||||||
|
PartitionKey('orgmembers#f6000f79-6e5c-49a0-952f-3bda330ef278')
|
||||||
|
)
|
||||||
|
assert len(members['items']) == 0
|
||||||
|
|
||||||
|
orgs = dynamodb_persistence_layer.collection.query(
|
||||||
|
KeyPair('15bacf02-1535-4bee-9022-19d106fd7518', 'orgs#')
|
||||||
|
)
|
||||||
|
assert len(orgs['items']) == 0
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
// Users
|
// Users
|
||||||
|
{"id": "213a6682-2c59-4404-9189-12eec0a846d4", "sk": "orgs#f6000f79-6e5c-49a0-952f-3bda330ef278", "name": "Banco do Brasil", "cnpj": "00000000000191"}
|
||||||
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "0", "name": "Sérgio R Siqueira", "email": "sergio@somosbeta.com.br", "cpf": "07879819908"}
|
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "0", "name": "Sérgio R Siqueira", "email": "sergio@somosbeta.com.br", "cpf": "07879819908"}
|
||||||
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "emails#sergio@somosbeta.com.br", "email_primary": true, "mx_record_exists": true}
|
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "emails#sergio@somosbeta.com.br", "email_primary": true, "mx_record_exists": true}
|
||||||
{"id": "213a6682-2c59-4404-9189-12eec0a846d4", "sk": "orgs#f6000f79-6e5c-49a0-952f-3bda330ef278", "name": "Banco do Brasil", "cnpj": "00000000000191"}
|
|
||||||
|
|
||||||
// User orgs
|
// User orgs
|
||||||
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "orgs#286f7729-7765-482a-880a-0b153ea799be", "name": "Banco do Brasil", "cnpj": "00000000000191"}
|
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "orgs#f6000f79-6e5c-49a0-952f-3bda330ef278", "name": "Banco do Brasil", "cnpj": "00000000000191"}
|
||||||
|
|
||||||
|
|
||||||
// Enrollments
|
// Enrollments
|
||||||
{"id": "578ec87f-94c7-4840-8780-bb4839cc7e64", "sk": "0", "course": {"id": "af3258f0-bccf-4781-aec6-d4c618d234a7", "name": "pytest", "access_period": 180}, "user": {"id": "068b4600-cc36-4b55-b832-bb620021705a", "name": "Benjamin Burnley", "email": "burnley@breakingbenjamin.com"}}
|
{"id": "578ec87f-94c7-4840-8780-bb4839cc7e64", "sk": "0", "course": {"id": "af3258f0-bccf-4781-aec6-d4c618d234a7", "name": "pytest", "access_period": 180}, "user": {"id": "068b4600-cc36-4b55-b832-bb620021705a", "name": "Benjamin Burnley", "email": "burnley@breakingbenjamin.com"}}
|
||||||
|
|||||||
@@ -44,22 +44,26 @@ interface DataTableProps<TData, TValue> {
|
|||||||
hiddenColumn?: string[]
|
hiddenColumn?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const TableContext = createContext<{ table: Table<any> } | null>(null)
|
interface TableContextProps<TData> {
|
||||||
|
table: Table<TData>
|
||||||
|
}
|
||||||
|
|
||||||
|
const TableContext = createContext<TableContextProps<any> | null>(null)
|
||||||
|
|
||||||
export function useDataTable<TData>() {
|
export function useDataTable<TData>() {
|
||||||
const ctx = useContext(TableContext) as { table: Table<TData> } | null
|
const ctx = useContext(TableContext)
|
||||||
|
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
throw new Error('TableContext is null')
|
throw new Error('TableContext is null')
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx
|
return ctx as { table: Table<TData> }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function DataTable<TData, TValue>({
|
export function DataTable<TData, TValue>({
|
||||||
|
data,
|
||||||
children,
|
children,
|
||||||
columns,
|
columns,
|
||||||
data,
|
|
||||||
sort,
|
sort,
|
||||||
pageIndex,
|
pageIndex,
|
||||||
pageSize,
|
pageSize,
|
||||||
@@ -67,10 +71,10 @@ export function DataTable<TData, TValue>({
|
|||||||
setSelectedRows,
|
setSelectedRows,
|
||||||
hiddenColumn = []
|
hiddenColumn = []
|
||||||
}: DataTableProps<TData, TValue>) {
|
}: DataTableProps<TData, TValue>) {
|
||||||
|
const [dataTable, setDataTable] = useState<TData[]>(data)
|
||||||
const columnVisibilityInit = Object.fromEntries(
|
const columnVisibilityInit = Object.fromEntries(
|
||||||
hiddenColumn.map((column) => [column, false])
|
hiddenColumn.map((column) => [column, false])
|
||||||
)
|
)
|
||||||
|
|
||||||
const [searchParams, setSearchParams] = useSearchParams()
|
const [searchParams, setSearchParams] = useSearchParams()
|
||||||
const [columnVisibility, setColumnVisibility] =
|
const [columnVisibility, setColumnVisibility] =
|
||||||
useState<VisibilityState>(columnVisibilityInit)
|
useState<VisibilityState>(columnVisibilityInit)
|
||||||
@@ -110,8 +114,12 @@ export function DataTable<TData, TValue>({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setDataTable(data)
|
||||||
|
}, [data])
|
||||||
|
|
||||||
const table = useReactTable({
|
const table = useReactTable({
|
||||||
data,
|
data: dataTable,
|
||||||
columns,
|
columns,
|
||||||
rowCount,
|
rowCount,
|
||||||
state: {
|
state: {
|
||||||
@@ -131,7 +139,12 @@ export function DataTable<TData, TValue>({
|
|||||||
onRowSelectionChange: setRowSelection,
|
onRowSelectionChange: setRowSelection,
|
||||||
onSortingChange: setSorting,
|
onSortingChange: setSorting,
|
||||||
onColumnVisibilityChange: setColumnVisibility,
|
onColumnVisibilityChange: setColumnVisibility,
|
||||||
onPaginationChange: setPagination
|
onPaginationChange: setPagination,
|
||||||
|
meta: {
|
||||||
|
removeRow: (rowId: string) => {
|
||||||
|
setDataTable((rows) => rows.filter((row: any) => row?.id !== rowId))
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -159,6 +172,7 @@ export function DataTable<TData, TValue>({
|
|||||||
key={header.id}
|
key={header.id}
|
||||||
className={cn(
|
className={cn(
|
||||||
'p-2.5',
|
'p-2.5',
|
||||||
|
// @ts-ignore
|
||||||
header.column.columnDef.meta?.className
|
header.column.columnDef.meta?.className
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
@@ -187,6 +201,7 @@ export function DataTable<TData, TValue>({
|
|||||||
key={cell.id}
|
key={cell.id}
|
||||||
className={cn(
|
className={cn(
|
||||||
'p-2.5',
|
'p-2.5',
|
||||||
|
// @ts-ignore
|
||||||
cell.column.columnDef.meta?.className
|
cell.column.columnDef.meta?.className
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -3,5 +3,17 @@ export function meta({}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function Route() {
|
export default function Route() {
|
||||||
return <>index org</>
|
return (
|
||||||
|
<>
|
||||||
|
<div className="space-y-0.5 mb-8">
|
||||||
|
<h1 className="text-2xl font-bold tracking-tight">
|
||||||
|
Gerenciar certificações
|
||||||
|
</h1>
|
||||||
|
<p className="text-muted-foreground">
|
||||||
|
Centralize o controle das certificações dos colaboradores e acompanhe
|
||||||
|
prazos e renovações com facilidade.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,17 @@
|
|||||||
|
|
||||||
import { formatCPF } from '@brazilian-utils/brazilian-utils'
|
import { formatCPF } from '@brazilian-utils/brazilian-utils'
|
||||||
import { type ColumnDef } from '@tanstack/react-table'
|
import { type ColumnDef } from '@tanstack/react-table'
|
||||||
|
import { useToggle } from 'ahooks'
|
||||||
import {
|
import {
|
||||||
EllipsisVerticalIcon,
|
EllipsisVerticalIcon,
|
||||||
PencilIcon,
|
PencilIcon,
|
||||||
UserRoundMinusIcon
|
UserRoundMinusIcon
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import { NavLink } from 'react-router'
|
import { NavLink, useParams } from 'react-router'
|
||||||
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import { Abbr } from '@/components/abbr'
|
import { Abbr } from '@/components/abbr'
|
||||||
|
import { useDataTable } from '@/components/data-table/data-table'
|
||||||
import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar'
|
import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar'
|
||||||
import { Button } from '@repo/ui/components/ui/button'
|
import { Button } from '@repo/ui/components/ui/button'
|
||||||
import {
|
import {
|
||||||
@@ -123,12 +126,39 @@ export const columns: ColumnDef<User>[] = [
|
|||||||
)}
|
)}
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
<DropdownMenuItem variant="destructive">
|
<UnlinkMenuItem userId={row.id} />
|
||||||
<UserRoundMinusIcon /> Desvincular
|
|
||||||
</DropdownMenuItem>
|
|
||||||
</DropdownMenuContent>
|
</DropdownMenuContent>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
function UnlinkMenuItem({ userId }: { userId: string }) {
|
||||||
|
const [loading, { set }] = useToggle(false)
|
||||||
|
const { orgid } = useParams()
|
||||||
|
const { table } = useDataTable<User>()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DropdownMenuItem
|
||||||
|
variant="destructive"
|
||||||
|
disabled={loading}
|
||||||
|
onClick={async (e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
set(true)
|
||||||
|
|
||||||
|
const r = await fetch(`/~/api/orgs/${orgid}/users/${userId}`, {
|
||||||
|
method: 'DELETE'
|
||||||
|
})
|
||||||
|
|
||||||
|
if (r.ok) {
|
||||||
|
toast.info('O colaborador foi desvinculado')
|
||||||
|
// @ts-ignore
|
||||||
|
table.options.meta?.removeRow?.(userId)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{loading ? <Spinner /> : <UserRoundMinusIcon />} Desvincular
|
||||||
|
</DropdownMenuItem>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -73,8 +73,6 @@ export async function action({ params, request, context }: Route.ActionArgs) {
|
|||||||
context
|
context
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(r)
|
|
||||||
|
|
||||||
if (!r.ok) {
|
if (!r.ok) {
|
||||||
const error = await r.json().catch(() => ({}))
|
const error = await r.json().catch(() => ({}))
|
||||||
return { ok: false, error }
|
return { ok: false, error }
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
{ "path": "./tsconfig.node.json" },
|
{ "path": "./tsconfig.node.json" },
|
||||||
{ "path": "./tsconfig.cloudflare.json" }
|
{ "path": "./tsconfig.cloudflare.json" }
|
||||||
],
|
],
|
||||||
|
"include": ["types/**/*.d.ts"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"checkJs": true,
|
"checkJs": true,
|
||||||
"verbatimModuleSyntax": true,
|
"verbatimModuleSyntax": true,
|
||||||
|
|||||||
11
apps/admin.saladeaula.digital/types/react-table.d.ts
vendored
Normal file
11
apps/admin.saladeaula.digital/types/react-table.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import '@tanstack/react-table'
|
||||||
|
|
||||||
|
declare module '@tanstack/react-table' {
|
||||||
|
interface ColumnMeta<TData extends unknown, TValue> {
|
||||||
|
className?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TableMeta<TData extends unknown> {
|
||||||
|
removeRow?: (rowId: string) => void
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user