UNPKG

5.61 kBJavaScriptView Raw
1var APIClient = require( '@bipsync/apiclient' );
2
3
4APIClient.generateReportTool( {
5
6 'formats' : [ 'csv' ],
7
8 schema : {
9 'type' : 'object',
10 'properties' : {
11 'since' : {
12 'title' : 'Get notes since this date',
13 'type' : 'string'
14 }
15 },
16 "required" : [ "since" ]
17 },
18
19 fetch : function() {
20
21 APIClient( 'https://devenvone-api.bipsync.dev' )
22 .then( function( client ) {
23
24 // fetch all notes after since date
25 client.requestAll( 'v1/research', { body : {
26 select : 'title,user,created,sharedGroups,fields,sharedUsers',
27 limit : 500,
28 sort: { 'created': 1 },
29 dateFormat : 'm/j/Y',
30 since: client.options.since
31 } } )
32 .then( function( notes ) {
33
34 // get all unique users, entities and project ids in notes
35 var userIds = new Set(),
36 entityIds = new Set(),
37 projectIds = new Set();
38 notes.forEach( function( note ) {
39 userIds.add( note.user );
40 ( ( note.fields ? note.fields.entities : [] ) || [] )
41 .map( entityIds.add.bind( entityIds ) );
42 ( ( note.fields ? note.fields.projects : [] ) || [] )
43 .forEach( projectIds.add.bind( projectIds ) );
44 } );
45
46 var output = {
47 notes : notes,
48 classifications : {}
49 };
50
51 // fetch all classifications
52 var chain = Promise.resolve().then( function() {
53 return client.request( 'v1/classification' );
54 } )
55 .catch( function() {} )
56 .then( function( documents ) {
57 if ( documents && documents.results ) {
58 documents.results.forEach( function( document ) {
59 output.classifications[ document.id ] = document;
60 } );
61 }
62 } );
63
64 // resolve all references
65 var collections = [
66 { ids : userIds, url : 'v1/user/', name : 'users' },
67 { ids : entityIds, url : 'v2/category/', name : 'entities' },
68 { ids : projectIds, url : 'v1/project/', name : 'projects' }
69 ];
70 collections.forEach( function( collection ) {
71 collection.ids.forEach( function( id ) {
72 chain = chain.then( function() {
73 return client.request( collection.url + id );
74 } )
75 .catch( function() {} )
76 .then( function( document ) {
77 output[ collection.name ] = output[ collection.name ] || {};
78 output[ collection.name ][ id ] = document;
79 } );
80 } );
81 } );
82
83 chain.then( function() {
84 client.output( output );
85 } );
86
87 } );
88
89 } );
90
91 },
92
93 render : function( helpers, view ) {
94
95 var notes = view.notes,
96 users = view.users,
97 entities = view.entities,
98 projects = view.projects,
99 classifications = view.classifications;
100
101 helpers.outputCSVLine( [
102 'Subject',
103 'Author',
104 'Created Date',
105 'User Group',
106 'Entity/Project',
107 'Content Type',
108 'Sub Type'
109 ] );
110
111 notes.forEach( function( note ) {
112 var shared = ( note.sharedGroups || [] ).length || ( note.sharedUsers || [] ).length;
113 var line = [
114 ( note.title || 'Untitled' ).substring( 0, 70 ),
115 ( ( users[ note.user ] || {} ).name || '' ),
116 note.created,
117 !shared ? 'Private' : ( note.sharedGroups || [] ).join( ', ' )
118 ];
119 if ( note.fields && note.fields.entities ) {
120 note.fields.entities.forEach( function( entityId ) {
121 var entity = ( entities[ entityId ] || {} );
122 helpers.outputCSVLine( line.concat( [
123 entity.name || '',
124 'Entity',
125 ( classifications[ entity.classification ] || {} ).name || ''
126 ] ) );
127 } );
128 }
129 if ( note.fields && note.fields.projects ) {
130 note.fields.projects.forEach( function( projectId ) {
131 var project = ( projects[ projectId ] || {} );
132 helpers.outputCSVLine( line.concat( [
133 project.name || '',
134 'Project',
135 project.pipeline || ''
136 ] ) );
137 } );
138 }
139 } );
140
141 }
142
143} );