1- import { Dataset , Organization } from "@portaljs/ckan" ;
2- import {
3- privateToPublicDatasetName ,
4- privateToPublicOrgName ,
5- publicToPrivateOrgName ,
6- } from "./utils" ;
1+ import { Organization } from "@portaljs/ckan" ;
72import CkanRequest , { CkanResponse } from "@portaljs/ckan-api-client-js" ;
83
94const DMS = process . env . NEXT_PUBLIC_DMS ;
10- const mainOrg = process . env . NEXT_PUBLIC_ORG ;
115
126export const getOrganization = async ( {
137 name,
@@ -16,87 +10,21 @@ export const getOrganization = async ({
1610 name : string ;
1711 include_datasets ?: boolean ;
1812} ) => {
19- const privateName = publicToPrivateOrgName ( name ) ;
20-
2113 const organization = await CkanRequest . get < CkanResponse < Organization > > (
22- `organization_show?id=${ privateName } &include_datasets=${ include_datasets } ` ,
14+ `organization_show?id=${ name } &include_datasets=${ include_datasets } ` ,
2315 { ckanUrl : DMS }
2416 ) ;
2517
26- if ( include_datasets ) {
27- organization . result . packages . forEach ( ( dataset : Dataset ) => {
28- dataset . organization . name = name ;
29- dataset . name = privateToPublicDatasetName ( dataset . name ) ;
30- } ) ;
31- }
32-
33- const publicName = privateToPublicOrgName ( organization . result . name ) ;
34-
35- return {
36- ...organization . result ,
37- name : publicName ,
38- _name : organization . result . name ,
39- } ;
18+ return organization . result
4019} ;
4120
42- export const getAllOrganizations = async ( {
43- detailed = true , // Whether to add organization_show or not
44- } : {
45- detailed ?: boolean ;
46- } ) => {
47- if ( ! mainOrg ) {
48- const organizations = await CkanRequest . get < CkanResponse < Organization [ ] > > (
49- `organization_list?all_fields=True` ,
50- {
51- ckanUrl : DMS ,
52- }
53- ) ;
54-
55- return organizations . result . map ( ( o ) => {
56- return { ...o , _name : o . name } ;
57- } ) ;
58- }
59-
60- /*
61- * Get hierarchy from root org
62- *
63- */
64-
65- const organizationsTree = await CkanRequest . get <
66- CkanResponse < Organization & { children : Organization [ ] ; _name : string } >
67- > ( `group_tree_section?type=organization&id=${ mainOrg } ` , {
68- ckanUrl : DMS ,
69- } ) ;
70-
71- /*
72- * Flatten orgs hierarchy, fix name and preserve
73- * internal name as `_name`
74- *
75- */
76- const { children, ...parent } = organizationsTree . result ;
77-
78- let organizations = children . map ( ( c ) => {
79- const publicName = privateToPublicOrgName ( c . name ) ;
80- return { ...c , name : publicName , _name : c . name } ;
81- } ) ;
82-
83- organizations . unshift ( { ...parent , _name : parent . name } ) ;
84-
85- /*
86- * Get details for each org
87- *
88- */
89- if ( organizations && detailed ) {
90- organizations = await Promise . all (
91- organizations . map ( async ( o ) => {
92- const orgDetails = await getOrganization ( {
93- name : o . name ,
94- } ) ;
95-
96- return { ...o , ...orgDetails , name : o . name , _name : o . _name } ;
97- } )
98- ) ;
99- }
21+ export const getAllOrganizations = async ( ) => {
22+ const organizations = await CkanRequest . get < CkanResponse < Organization [ ] > > (
23+ `organization_list?all_fields=True` ,
24+ {
25+ ckanUrl : DMS ,
26+ }
27+ ) ;
10028
101- return organizations ;
29+ return organizations . result
10230} ;
0 commit comments