Skip to content

Commit

Permalink
1958 seach entities by r used in their references (#1999)
Browse files Browse the repository at this point in the history
* update search request interface

* add search condition for haveReferenceTo

* add suggester for Resource

* ready version

---------

Co-authored-by: Ján Mertel <[email protected]>
  • Loading branch information
Ptrhnk and jancimertel authored Mar 26, 2024
1 parent 02bac4e commit 5de9cb1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,23 @@ export const EntitySearchBox: React.FC = () => {
enabled: api.isLoggedIn(),
});

const [referencedTo, setReferencedTo] = useState<IEntity | false>(false);

const classOptions = entitiesDict.filter(
(e) => e.value !== EntityEnums.Class.Resource
);

// apply changes to search parameters
// const handleChange = (changes: {
// [key: string]:
// | string
// | false
// | true
// | undefined
// | DropdownItem
// | Date
// | string[];
// }) => {
const handleChange = (changes: {
[key: string]:
| string
Expand Down Expand Up @@ -487,6 +499,32 @@ export const EntitySearchBox: React.FC = () => {
</div>
)}
</StyledRow>
<StyledRow>
<StyledRowHeader>referenced to</StyledRowHeader>
{referencedTo ? (
<EntityTag
entity={referencedTo}
unlinkButton={{
onClick: () => {
setReferencedTo(false);
handleChange({ haveReferenceTo: undefined });
},
}}
/>
) : (
<EntitySuggester
disableCreate
onPicked={(entity) => {
setReferencedTo(entity);
handleChange({ haveReferenceTo: entity.id });
}}
disableTemplatesAccept
categoryTypes={[EntityEnums.Class.Resource]}
inputWidth="full"
placeholder="resource"
/>
)}
</StyledRow>
<StyledRow>
<StyledRowHeader>created at</StyledRowHeader>
{debouncedValues.createdDate ? (
Expand Down
38 changes: 38 additions & 0 deletions packages/database/datasets/relationstest/entities.json
Original file line number Diff line number Diff line change
Expand Up @@ -2625,5 +2625,43 @@
"data": {
"pos": "verb"
}
},
{
"class": "C",
"data": {
"pos": ""
},
"detail": "in general - who gives advice",
"id": "2bcbd37a-34ff-4b92-bd76-152d5797cf89",
"isTemplate": null,
"label": "advisor",
"language": "eng",
"legacyId": "C1449",
"notes": ["Import batch [January2023] 2023-01-21 18:58:21.366042"],
"props": [],
"references": [
{
"id": "005bccb4-8eff-45db-b1e8-661dc1b58133",
"resource": "dissinet-resource",
"value": "2bcbd37a-34ff-4b92-bd76-152d5797cf89"
}
],
"status": "1"
},
{
"id": "dissinet-resource",
"class": "R",
"status": "1",
"data": {
"url": "https://dissinet.cz/",
"partValueLabel": "",
"partValueBaseURL": ""
},
"label": "DISSINET Database (DDB1)",
"detail": "",
"language": "eng",
"notes": [],
"props": [],
"references": []
}
]
19 changes: 18 additions & 1 deletion packages/server/src/models/entity/response-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ export class SearchQuery {
return this;
}

/**
* adds condition to search for entities which have reference to chosen resource id
* @returns
*/
whereHaveReferenceTo(refId: string): SearchQuery {
this.query = this.query.filter(function (row: RDatum) {
return row("references").contains(function(ref: RDatum) {
return ref("resource").eq(refId)
});
});

return this;
}

/**
* adds condition to filter entries with language
* @returns
Expand Down Expand Up @@ -380,7 +394,10 @@ export class SearchQuery {
this.whereEntityIds(req.entityIds);
}

//console.log(this.query.toString());
if (req.haveReferenceTo) {
this.whereHaveReferenceTo(req.haveReferenceTo);
}
console.log(this.query.toString());
}

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/server/src/modules/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,19 @@ export default Router()
*/
.get(
"/",
asyncRouteHandler<IResponseEntity[]>(async (httpRequest: IRequest) => {
const req = new RequestSearch(httpRequest.query as IRequestSearch);
if (req.label && req.label.length < 2) {
asyncRouteHandler<IResponseEntity[]>(async (req: IRequest<unknown, unknown, IRequestSearch>) => {
const search = new RequestSearch(req.query);
if (search.label && search.label.length < 2) {
return [];
}

const err = req.validate();
const err = search.validate();
if (err) {
throw err;
}

const response = new ResponseSearch(req);
return await response.prepare(httpRequest);
const response = new ResponseSearch(search);
return await response.prepare(req);
})
)
/**
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/types/request-search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Deprecated
*/

import { EntityEnums } from "../enums";
import { EnumValidators } from "../enums/validators";
import { BadParams } from "./errors";
Expand All @@ -21,6 +17,7 @@ export interface IRequestSearch {
createdDate?: Date;
updatedDate?: Date;
resourceHasDocument?: boolean;
haveReferenceTo?: string;
}

export class RequestSearch {
Expand All @@ -38,6 +35,7 @@ export class RequestSearch {
createdDate?: Date;
updatedDate?: Date;
resourceHasDocument?: boolean;
haveReferenceTo?: string;

constructor(requestData: IRequestSearch) {
this.class = requestData.class;
Expand Down Expand Up @@ -73,6 +71,7 @@ export class RequestSearch {
this.language = requestData.language || undefined;
this.subTerritorySearch = !!requestData.subTerritorySearch;
this.resourceHasDocument = !!requestData.resourceHasDocument;
this.haveReferenceTo = requestData.haveReferenceTo || undefined;
}

/**
Expand Down Expand Up @@ -140,7 +139,8 @@ export class RequestSearch {
!this.language &&
!this.createdDate &&
!this.updatedDate &&
(this.entityIds === undefined || !this.entityIds.length)
(this.entityIds === undefined || !this.entityIds.length) &&
!this.haveReferenceTo
) {
return new BadParams("one of the search field has to be set");
}
Expand Down

0 comments on commit 5de9cb1

Please sign in to comment.