1
+ import { Meteor } from "meteor/meteor" ;
2
+
1
3
import { API } from '../api' ;
2
4
import { findCouncils , findOneCouncil , findCouncil , findOneCouncilByInviteLink } from '../lib/councils' ;
3
5
import { hasPermission } from '../../../authorization' ;
6
+ import { FileUpload } from '../../../file-upload' ;
4
7
import { Users } from '../../../models' ;
8
+ import { Persons } from '../../../models' ;
9
+
10
+ import Busboy from 'busboy' ;
5
11
6
12
API . v1 . addRoute ( 'councils.list' , { authRequired : true } , {
7
13
get ( ) {
@@ -44,7 +50,7 @@ API.v1.addRoute('councils.findOne', { authRequired: true }, {
44
50
} ,
45
51
} ) ;
46
52
47
- API . v1 . addRoute ( 'councils.invitedUsers ' , { authRequired : true } , {
53
+ API . v1 . addRoute ( 'councils.invitedPersons ' , { authRequired : true } , {
48
54
get ( ) {
49
55
if ( ! hasPermission ( this . userId , 'view-c-room' ) ) {
50
56
return API . v1 . unauthorized ( ) ;
@@ -55,12 +61,58 @@ API.v1.addRoute('councils.invitedUsers', { authRequired: true }, {
55
61
56
62
const council = Promise . await ( findCouncil ( query . _id ) ) ;
57
63
58
- const users = Users . find ( { _id : { $in : council . invitedUsers } } , {
64
+ const persons = Persons . find ( { _id : { $in : council . invitedPersons . map ( ( user ) => user . _id ) } } , {
65
+ sort : sort || { surname : 1 , name : 1 , patronymic : 1 } ,
66
+ skip : offset ,
67
+ limit : count ,
68
+ fields : { _id : 1 , surname : 1 , name : 1 , patronymic : 1 , phone : 1 , email : 1 } ,
69
+ } ) . fetch ( ) . map ( ( user ) => {
70
+ const iUser = council . invitedPersons . find ( ( iUser ) => iUser . _id === user . _id ) ;
71
+ if ( ! iUser ) { return ; }
72
+ if ( ! iUser . ts ) {
73
+ user . ts = new Date ( 'January 1, 2021 00:00:00' ) ;
74
+ } else {
75
+ user . ts = iUser . ts ;
76
+ }
77
+ return user ;
78
+ } ) ;
79
+
80
+ return API . v1 . success ( {
81
+ persons,
82
+ count : persons . length ,
83
+ offset,
84
+ total : Persons . find ( { _id : { $in : council . invitedPersons . map ( ( iPerson ) => iPerson . _id ) } } ) . count ( ) ,
85
+ } ) ;
86
+ } ,
87
+ } ) ;
88
+
89
+ API . v1 . addRoute ( 'councils.invitedUsers' , { authRequired : true } , {
90
+ get ( ) {
91
+ if ( ! hasPermission ( this . userId , 'view-c-room' ) ) {
92
+ return API . v1 . unauthorized ( ) ;
93
+ }
94
+
95
+ const { offset, count } = this . getPaginationItems ( ) ;
96
+ const { sort, fields, query } = this . parseJsonQuery ( ) ;
97
+
98
+ const council = Promise . await ( findCouncil ( query . $and [ 1 ] . _id ) ) ;
99
+
100
+ const users = Users . find ( { _id : { $in : council . invitedUsers . map ( ( user ) => typeof user === typeof { } ? user . _id : user ) } } , {
101
+ // const users = Users.find({ _id: { $in: council.invitedUsers } }, {
59
102
sort : sort || { username : 1 } ,
60
103
skip : offset ,
61
104
limit : count ,
62
105
fields,
63
- } ) . fetch ( ) ;
106
+ } ) . fetch ( ) . map ( ( user ) => {
107
+ const iUser = council . invitedUsers . find ( ( iUser ) => ( typeof iUser === typeof { } && iUser . _id === user . _id ) || ( typeof iUser === typeof '' && iUser === user . _id ) ) ;
108
+ if ( ! iUser ) { return ; }
109
+ if ( ! iUser . ts ) {
110
+ user . ts = new Date ( 'January 1, 2021 00:00:00' ) ;
111
+ } else {
112
+ user . ts = iUser . ts ;
113
+ }
114
+ return user ;
115
+ } ) ;
64
116
65
117
return API . v1 . success ( {
66
118
invitedUsers : users ,
@@ -70,3 +122,70 @@ API.v1.addRoute('councils.invitedUsers', { authRequired: true }, {
70
122
} ) ;
71
123
} ,
72
124
} ) ;
125
+
126
+ const getFiles = Meteor . wrapAsync ( ( { request } , callback ) => {
127
+ const busboy = new Busboy ( { headers : request . headers } ) ;
128
+ const files = [ ] ;
129
+
130
+ const fields = { } ;
131
+
132
+
133
+ busboy . on ( 'file' , ( fieldname , file , filename , encoding , mimetype ) => {
134
+ if ( fieldname !== 'file' ) {
135
+ return callback ( new Meteor . Error ( 'invalid-field' ) ) ;
136
+ }
137
+
138
+ const fileDate = [ ] ;
139
+ file . on ( 'data' , ( data ) => fileDate . push ( data ) ) ;
140
+
141
+ file . on ( 'end' , ( ) => {
142
+ files . push ( { fieldname, file, filename, encoding, mimetype, fileBuffer : Buffer . concat ( fileDate ) } ) ;
143
+ } ) ;
144
+ } ) ;
145
+
146
+ busboy . on ( 'field' , ( fieldname , value ) => { fields [ fieldname ] = value ; } ) ;
147
+
148
+ busboy . on ( 'finish' , Meteor . bindEnvironment ( ( ) => callback ( null , { files, fields } ) ) ) ;
149
+
150
+ request . pipe ( busboy ) ;
151
+ } ) ;
152
+
153
+ API . v1 . addRoute ( 'councils.upload/:id' , { authRequired : true } , {
154
+ post ( ) {
155
+ const { files, fields } = getFiles ( {
156
+ request : this . request ,
157
+ } ) ;
158
+
159
+ if ( files . length === 0 ) {
160
+ console . log ( 'api.v1.route file required' ) ;
161
+ return API . v1 . failure ( 'File required' ) ;
162
+ }
163
+
164
+ if ( files . length > 1 ) {
165
+ console . log ( 'api.v1.route just 1 file is allowed' ) ;
166
+ return API . v1 . failure ( 'Just 1 file is allowed' ) ;
167
+ }
168
+
169
+ const file = files [ 0 ] ;
170
+
171
+ const details = {
172
+ name : file . filename ,
173
+ size : file . fileBuffer . length ,
174
+ type : file . mimetype ,
175
+ workingGroupMeetingId : this . urlParams . id ,
176
+ userId : this . userId ,
177
+ } ;
178
+
179
+ const fileData = Meteor . runAsUser ( this . userId , ( ) => {
180
+ const fileStore = FileUpload . getStore ( 'Uploads' ) ;
181
+ const uploadedFile = fileStore . insertSync ( details , file . fileBuffer ) ;
182
+
183
+ uploadedFile . description = fields . description ;
184
+
185
+ Meteor . call ( 'sendFileCouncil' , this . urlParams . id , uploadedFile ) ;
186
+
187
+ return uploadedFile ;
188
+ } ) ;
189
+ return API . v1 . success ( { _id : fileData . _id } ) ;
190
+ } ,
191
+ } ) ;
0 commit comments