Skip to content

Commit

Permalink
No parent id when displ docs by category (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur authored Oct 16, 2024
1 parent 8b9ae52 commit d83b409
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 68 deletions.
6 changes: 2 additions & 4 deletions papermerge/core/cli/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ def document_types():


@app.command(name="list-by-type")
def list_documents_by_type(type_id: uuid.UUID, parent_id: uuid.UUID):
def list_documents_by_type(type_id: uuid.UUID):
"""List all documents by specific document type"""
docs = get_docs_by_type(
session, type_id=type_id, user_id=uuid.uuid4(), ancestor_id=parent_id
)
docs = get_docs_by_type(session, type_id=type_id, user_id=uuid.uuid4())
print_docs(docs)


Expand Down
9 changes: 2 additions & 7 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,10 @@ def update_doc_cfv(
def get_docs_by_type(
session: Session,
type_id: UUID,
ancestor_id: UUID,
user_id: UUID,
) -> list[schemas.DocumentCFV]:
"""
Returns list of documents + doc CFv for all documents with of given type
All fetched documents are descendants of `ancestor_id` node.
"""
stmt = """
SELECT node.title,
Expand Down Expand Up @@ -284,12 +281,10 @@ def get_docs_by_type(
) AS cf ON cf.cf_id = dtcf.custom_field_id
LEFT OUTER JOIN custom_field_values AS cfv
ON cfv.field_id = cf.cf_id AND cfv.document_id = doc_id
WHERE node.parent_id = :parent_id
AND doc.document_type_id = :document_type_id
WHERE doc.document_type_id = :document_type_id
"""
str_parent_id = str(ancestor_id).replace("-", "")
str_type_id = str(type_id).replace("-", "")
params = {"parent_id": str_parent_id, "document_type_id": str_type_id}
params = {"document_type_id": str_type_id}
results = []
rows = session.execute(text(stmt), params)
for document_id, group in itertools.groupby(rows, lambda r: r.doc_id):
Expand Down
5 changes: 1 addition & 4 deletions papermerge/core/routers/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def get_documents_by_type(
user: Annotated[
schemas.User, Security(get_current_user, scopes=[scopes.NODE_VIEW])
],
ancestor_id: uuid.UUID,
db_session: db.Session = Depends(db.get_session),
) -> list[schemas.DocumentCFV]:
"""
Expand All @@ -39,9 +38,7 @@ def get_documents_by_type(
Required scope: `{scope}`
"""

docs = db.get_docs_by_type(
db_session, type_id=document_type_id, ancestor_id=ancestor_id, user_id=user.id
)
docs = db.get_docs_by_type(db_session, type_id=document_type_id, user_id=user.id)

return docs

Expand Down
10 changes: 3 additions & 7 deletions tests/core/models/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,10 @@ def test_get_docs_by_type_basic(db_session: Session, make_document_receipt):
doc_1: Document = make_document_receipt(title="receipt_1.pdf")
make_document_receipt(title="receipt_2.pdf")
user_id = doc_1.user.id
parent_id = doc_1.parent.id
type_id = doc_1.document_type.id

items: list[schemas.DocumentCFV] = db.get_docs_by_type(
db_session, type_id=type_id, user_id=user_id, ancestor_id=parent_id
db_session, type_id=type_id, user_id=user_id
)

assert len(items) == 2
Expand All @@ -527,7 +526,6 @@ def test_get_docs_by_type_one_doc_with_nonempty_cfv(
doc_1: Document = make_document_receipt(title="receipt_1.pdf")
make_document_receipt(title="receipt_2.pdf")
user_id = doc_1.user.id
parent_id = doc_1.parent.id
type_id = doc_1.document_type.id

# update all CFV of receipt_1.pdf to non-empty values
Expand All @@ -538,7 +536,7 @@ def test_get_docs_by_type_one_doc_with_nonempty_cfv(
)

items: list[schemas.DocumentCFV] = db.get_docs_by_type(
db_session, type_id=type_id, user_id=user_id, ancestor_id=parent_id
db_session, type_id=type_id, user_id=user_id
)

assert len(items) == 2
Expand Down Expand Up @@ -580,7 +578,6 @@ def test_get_docs_by_type_missmatching_type(db_session: Session, make_document_r
doc_1: Document = make_document_receipt(title="receipt_1.pdf")
make_document_receipt(title="receipt_2.pdf")
user_id = doc_1.user.id
parent_id = doc_1.parent.id
groceries_type_id = doc_1.document_type.id

# to reproduce the bug bill document type should share at least one
Expand All @@ -600,10 +597,9 @@ def test_get_docs_by_type_missmatching_type(db_session: Session, make_document_r
db_session,
type_id=billType.id,
user_id=user_id,
ancestor_id=parent_id,
)
groceriesDocs: list[schemas.DocumentCFV] = db.get_docs_by_type(
db_session, type_id=groceries_type_id, user_id=user_id, ancestor_id=parent_id
db_session, type_id=groceries_type_id, user_id=user_id
)

# because there are no documents of type "Bill"
Expand Down
15 changes: 5 additions & 10 deletions ui2/src/features/document/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ type UpdateDocumentCustomFields = {
}>
}

type GetDocsByTypeArgs = {
document_type_id: string
ancestor_id: string
}

type UpdateDocumentTypeArgs = {
document_id?: string
invalidatesTags: {
Expand Down Expand Up @@ -199,12 +194,12 @@ export const apiSliceWithDocuments = apiSlice.injectEndpoints({
{type: "DocumentCustomField", id: arg}
]
}),
getDocsByType: builder.query<DocumentCFV[], GetDocsByTypeArgs>({
query: args => ({
url: `/documents/type/${args.document_type_id}?ancestor_id=${args.ancestor_id}`
getDocsByType: builder.query<DocumentCFV[], string>({
query: document_type_id => ({
url: `/documents/type/${document_type_id}`
}),
providesTags: (_result, _error, arg) => [
{type: "DocumentCFV", id: arg.document_type_id}
providesTags: (_result, _error, document_type_id) => [
{type: "DocumentCFV", id: document_type_id}
]
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {useAppDispatch} from "@/app/hooks"
import {currentNodeChanged} from "@/features/ui/uiSlice"
import {Checkbox, Table} from "@mantine/core"
import {useSelector} from "react-redux"
import {Link} from "react-router-dom"

import {selectSelectedIds} from "@/features/users/usersSlice"
import {useNavigate} from "react-router-dom"

import type {DocumentCFV} from "@/types"

Expand All @@ -11,20 +10,33 @@ type Args = {
}

export default function DocumentRow({doc}: Args) {
const selectedIds = useSelector(selectSelectedIds)
const dispatch = useAppDispatch()
const navigate = useNavigate()

const customFieldsDataColumns = doc.custom_fields.map(cf => (
<Table.Td key={cf[0]}>{cf[1]}</Table.Td>
))

const onChange = () => {}
const onClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
if (e.ctrlKey) {
dispatch(
currentNodeChanged({id: doc.id, ctype: "document", panel: "secondary"})
)
} else {
navigate(`/document/${doc.id}`)
}
}

return (
<Table.Tr>
<Table.Td>
<Checkbox checked={selectedIds.includes(doc.id)} onChange={onChange} />
<Checkbox />
</Table.Td>
<Table.Td>
<Link to={`/document/${doc.id}`}>{doc.title}</Link>
<a href="#" onClick={onClick}>
{doc.title}
</a>
</Table.Td>
{customFieldsDataColumns}
</Table.Tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import {useAppDispatch} from "@/app/hooks"
import {useAppDispatch, useAppSelector} from "@/app/hooks"
import PanelContext from "@/contexts/PanelContext"
import {useGetDocumentTypesQuery} from "@/features/document-types/apiSlice"
import {commanderDocumentTypeIDUpdated} from "@/features/ui/uiSlice"
import {
commanderDocumentTypeIDUpdated,
selectCommanderDocumentTypeID
} from "@/features/ui/uiSlice"
import {Select} from "@mantine/core"
import {useContext, useState} from "react"

import type {PanelMode} from "@/types"

export default function DocumentTypeFilter() {
const mode: PanelMode = useContext(PanelContext)
const lastDocumentTypeID = useAppSelector(s =>
selectCommanderDocumentTypeID(s, mode)
)
const dispatch = useAppDispatch()
const {data: allDocumentTypes = []} = useGetDocumentTypesQuery()
const [currentDocumentTypeID, setCurrentDocumentTypeID] = useState<
string | undefined
>("")
>(lastDocumentTypeID)
const onDocumentTypeChanged = (value: string | null) => {
let newValue

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import {useAppSelector} from "@/app/hooks"
import Breadcrumbs from "@/components/Breadcrumbs"
import PanelContext from "@/contexts/PanelContext"
import {useGetDocsByTypeQuery} from "@/features/document/apiSlice"
import {useGetFolderQuery} from "@/features/nodes/apiSlice"
import {
selectCommanderDocumentTypeID,
selectCurrentNodeID
} from "@/features/ui/uiSlice"
import type {NType, PanelMode} from "@/types"
import {selectCommanderDocumentTypeID} from "@/features/ui/uiSlice"
import type {PanelMode} from "@/types"
import {Box, Checkbox, Stack, Table} from "@mantine/core"
import {skipToken} from "@reduxjs/toolkit/query"
import {useContext} from "react"
Expand All @@ -16,29 +11,18 @@ import DocumentRow from "./DocumentRow"

export default function DocumentsByCategoryCommander() {
const mode: PanelMode = useContext(PanelContext)
const currentNodeID = useAppSelector(s => selectCurrentNodeID(s, mode))
const currentDocumentTypeID = useAppSelector(s =>
selectCommanderDocumentTypeID(s, mode)
)
const {data: currentFolder} = useGetFolderQuery(currentNodeID ?? skipToken)
const {data: nodes} = useGetDocsByTypeQuery(
currentNodeID && currentDocumentTypeID
? {document_type_id: currentDocumentTypeID, ancestor_id: currentNodeID}
: skipToken
currentDocumentTypeID ?? skipToken
)

const onClick = (_node: NType) => {}

if (!nodes || (nodes && nodes.length == 0)) {
return (
<Box>
<Stack>
<ActionButtons />
<Breadcrumbs
breadcrumb={currentFolder?.breadcrumb}
onClick={onClick}
isFetching={false}
/>
</Stack>
<Stack>Empty</Stack>
</Box>
Expand All @@ -54,14 +38,9 @@ export default function DocumentsByCategoryCommander() {
<Box>
<Stack>
<ActionButtons />
<Breadcrumbs
breadcrumb={currentFolder?.breadcrumb}
onClick={onClick}
isFetching={false}
/>
</Stack>
<Stack>
<Table>
<Table mt={"lg"}>
<Table.Thead>
<Table.Tr>
<Table.Th>
Expand Down

0 comments on commit d83b409

Please sign in to comment.