UNPKG

7.07 kBJavaScriptView Raw
1var APIClient = require( '@bipsync/apiclient' );
2
3
4APIClient.generateReportTool( {
5
6 'formats' : [ 'xlsx' ],
7
8 fetch : function() {
9
10 APIClient( 'https://devenvone-api.bipsync.dev' )
11 .then( function( client ) {
12
13 client.request( 'v1/contenttype/file' )
14 .then( function( contentType ) {
15 return client.requestAll( 'v1/field', { body : { limit : 50 } } )
16 .then( function( fields ) {
17 return fields.filter( function( field ) {
18 return field.contentType == contentType.id &&
19 field.options !== undefined &&
20 field.options.behaviours !== undefined &&
21 field.options.behaviours.includes( 'primaryFileType' );
22 } );
23 } );
24 } )
25 .then( function( fileTypeFields ) {
26 var fileTypeField = fileTypeFields.shift(),
27 fileTypes = {};
28
29 Object.keys( fileTypeField.options.values ).forEach( function ( id ) {
30 var fileType = fileTypeField.options.values[ id ];
31 if ( fileType.disabled !== true ) {
32 fileTypes[ id ] = fileType;
33 }
34 } );
35
36 // fetch all companies (will only include user companies or those with attachments)
37 client.request( 'v1/category', { body : {
38 classification : 'company',
39 select : 'name,ticker,attachments',
40 limit : 500,
41 sort: { 'name': 1 }
42 } } )
43 .then( function( response ) {
44
45 var attachmentIds = new Set();
46 // filter down to categories with attachments that have a file type
47 return response.results.filter( function( category ) {
48
49 if ( category.attachments === undefined ) {
50 return false;
51 }
52
53 var attachments = category.attachments.filter( function( attachment ) {
54 return attachment.fields !== undefined &&
55 attachment.fields[ fileTypeField.id ] !== undefined;
56 } );
57
58 if ( !attachments.length ) {
59 return false;
60 }
61
62 category.attachments = attachments;
63
64 return category;
65 } );
66
67 } )
68 .then( function( categories ) {
69
70 var output = {
71 fileTypes : fileTypes,
72 categories : []
73 };
74
75 categories = categories.sort( function( a, b ) {
76 if (a.name < b.name) {
77 return -1;
78 } else if (a.name > b.name) {
79 return 1;
80 }
81 return 0;
82 } );
83
84 var chain = Promise.resolve();
85
86 categories.forEach( function( category ) {
87 var attachmentArray = category.attachments;
88 delete( category.attachments );
89
90 attachmentArray.forEach( function( attachment ) {
91 var type = attachment.fields[ fileTypeField.id ];
92
93 chain = chain.then( function() {
94 return client.request( 'v2/attachment/' + attachment.id + '/document', { body : {
95 dateFormat : 'c'
96 } } );
97 } )
98 .catch( function() {} )
99 .then( function( file ) {
100 if ( file !== undefined && file.uploadDate !== undefined ) {
101 category[ type ] = new Date( file.uploadDate );
102 }
103 } );
104
105 } );
106
107 } );
108
109 chain.then( function() {
110 output.categories = categories;
111 client.output( output );
112 } );
113
114 } );
115
116 } );
117 } );
118
119 },
120
121 render : function( helpers, view ) {
122
123 var categories = view.categories,
124 fileTypes = view.fileTypes;
125
126 var worksheet = helpers.getExcelWorksheet( 'Worksheet' ),
127 headers = [
128 { header: 'Company Name', key: 'name', width: 30 },
129 { header: 'Ticker', key: 'ticker', width: 10 }
130 ];
131
132 Object.keys( fileTypes ).forEach( function ( id ) {
133 headers.push( {
134 header: fileTypes[ id ].name,
135 key: id,
136 width: 15
137 } );
138 } );
139 worksheet.columns = headers;
140
141 categories.forEach( function( category ) {
142 var row = worksheet.addRow( category ),
143 dateNow = new Date(),
144 fiveDaysAgo = new Date();
145
146 fiveDaysAgo.setDate( fiveDaysAgo.getDate() - 5 );
147
148 Object.keys( fileTypes ).forEach( function ( id ) {
149
150 var cell = row.getCell( id );
151 if ( !cell.value ) {
152 return;
153 }
154
155 // Convert date string to a Date
156 var dateValue = new Date( cell.value );
157 cell.value = dateValue;
158
159 // Conditional Styling
160 if ( dateValue.getTime() < fiveDaysAgo.getTime() ) {
161 // Red if it's older than 5 days ago
162 cell.font = {
163 color: { argb: '00FF0000' }
164 };
165 } else if ( dateValue.getTime() < dateNow.getTime() ) {
166 // Green if it's within the last 5 days
167 cell.font = {
168 color: { argb: 'CC00FF00' }
169 };
170 }
171
172 } );
173
174 row.commit();
175 } );
176
177 helpers.commitWorkbook();
178
179 }
180
181} );