Skip to content

Commit

Permalink
RocketChat#764 [NEW] Форма подачи документов
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarvis committed Nov 20, 2020
1 parent 0bf427e commit ffb4179
Show file tree
Hide file tree
Showing 38 changed files with 2,129 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/api/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ import './v1/working-groups';
import './v1/working-group-meetings';
import './v1/upload-files';
import './v1/protocols';
import './v1/working-groups-requests';

export { API, APIClass, defaultRateLimiterOptions } from './api';
3 changes: 2 additions & 1 deletion app/api/server/lib/protocols.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Protocols } from '../../../models/server/raw';

export async function findProtocols({ query = {}, pagination: { offset, count, sort } }) {
export async function findProtocols({ query = {}, fields = {}, pagination: { offset, count, sort } }) {
const cursor = await Protocols.find(query, {
sort: sort || { time: 1 },
skip: offset,
limit: count,
fields,
});

const total = await cursor.count();
Expand Down
29 changes: 29 additions & 0 deletions app/api/server/lib/working-groups-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { WorkingGroupsRequests } from '../../../models/server/raw';

export async function findWorkingGroupsRequests({ query = {}, pagination: { offset, count, sort } }) {
const cursor = await WorkingGroupsRequests.find(query, {
sort: sort || { time: 1 },
skip: offset,
limit: count,
});

const total = await cursor.count();

const requests = await cursor.toArray();

return {
requests,
count: requests.length,
offset,
total,
};
}

export async function findOneWorkingGroupRequestByInviteLink(inviteLink) {
const cursor = await WorkingGroupsRequests.findWorkingGroupRequestByInviteLink({ inviteLink }, {});
return cursor;
}
export async function findWorkingGroupRequest(_id) {
const cursor = await WorkingGroupsRequests.findOne({ _id });
return cursor;
}
20 changes: 20 additions & 0 deletions app/api/server/v1/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ API.v1.addRoute('protocols.list', { authRequired: true }, {
},
});

API.v1.addRoute('protocols.list.requestAnswer', { authRequired: false }, {
get() {
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();

return API.v1.success(Promise.await(findProtocols({
query,
fields: {
sections: 1,
num: 1,
},
pagination: {
offset,
count,
sort,
},
})));
},
});

API.v1.addRoute('protocols.findOne', { authRequired: true }, {
get() {
const { query } = this.parseJsonQuery();
Expand Down
102 changes: 102 additions & 0 deletions app/api/server/v1/working-groups-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Busboy from 'busboy';
import { Meteor } from 'meteor/meteor';

import { API } from '../api';
import { FileUpload } from '../../../file-upload';
import { findWorkingGroupsRequests, findOneWorkingGroupRequestByInviteLink, findWorkingGroupRequest } from '../lib/working-groups-requests';

API.v1.addRoute('working-groups-requests.list', { authRequired: true }, {
get() {
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();

return API.v1.success(Promise.await(findWorkingGroupsRequests({
query,
pagination: {
offset,
count,
sort,
},
})));
},
});

API.v1.addRoute('working-groups-requests.findOne', { authRequired: true }, {
get() {
const { query } = this.parseJsonQuery();
return API.v1.success(Promise.await(findWorkingGroupRequest(query._id)));
},
});

API.v1.addRoute('working-groups-requests.getOneByInviteLink', { authRequired: false }, {
get() {
const { query } = this.parseJsonQuery();
return API.v1.success(Promise.await(findOneWorkingGroupRequestByInviteLink(query.inviteLink)));
},
});

const getFiles = Meteor.wrapAsync(({ request }, callback) => {
const busboy = new Busboy({ headers: request.headers });
const files = [];

const fields = {};


busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
if (fieldname !== 'file') {
return callback(new Meteor.Error('invalid-field'));
}

const fileDate = [];
file.on('data', (data) => fileDate.push(data));

file.on('end', () => {
files.push({ fieldname, file, filename, encoding, mimetype, fileBuffer: Buffer.concat(fileDate) });
});
});

busboy.on('field', (fieldname, value) => { fields[fieldname] = value; });

busboy.on('finish', Meteor.bindEnvironment(() => callback(null, { files, fields })));

request.pipe(busboy);
});

API.v1.addRoute('working-groups-requests.upload/:id/:mailId/:answerId', { authRequired: false }, {
post() {
const { files, fields } = getFiles({
request: this.request,
});

if (files.length === 0) {
console.log('api.v1.route file required');
return API.v1.failure('File required');
}

if (files.length > 1) {
console.log('api.v1.route just 3 file is allowed');
return API.v1.failure('Just 3 file is allowed');
}

const file = files[0];

const details = {
name: file.filename,
size: file.fileBuffer.length,
type: file.mimetype,
workingGroupRequestId: this.urlParams.id,
workingGroupRequestMailId: this.urlParams.mailId,
workingGroupRequestAnswerId: this.urlParams.answerId,
};

// TODO: Requires user ID to upload file
const fileData = Meteor.runAsUser('rocket.cat', () => {
const fileStore = FileUpload.getStore('Uploads');
const uploadedFile = fileStore.insertSync(details, file.fileBuffer);

uploadedFile.description = fields.description;
return uploadedFile;
});
return API.v1.success({ _id: fileData._id });
},
});
5 changes: 4 additions & 1 deletion app/councils/client/views/AddCouncil.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export function AddCouncilPage() {
const data = useEndpointData('users.list', query) || { users: [] };

useEffect(() => {
setUsers(data.users);
if (data.users) {
setUsers(data.users);
}
}, [data]);

const onChange = useCallback(() => {
Expand All @@ -71,6 +73,7 @@ function AddCouncilWithNewData({ users, setUsers, onChange }) {
const [description, setDescription] = useState('');
const [invitedUsersIds, setInvitedUsersIds] = useState([]);

// TODO: maybe
const invitedUsers = useMemo(() => users.filter((user) => invitedUsersIds.findIndex((iUser) => iUser === user._id) > -1), [invitedUsersIds, users]);

const insertOrUpdateCouncil = useMethod('insertOrUpdateCouncil');
Expand Down
49 changes: 43 additions & 6 deletions app/file-upload/server/lib/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,44 @@ export const FileUpload = {
return true;
},

validateFileUpload(file) {
validateFileUploadToWorkingGroupRequestAnswer(file) {
console.log('validateFileUploadToWorkingGroupRequestAnswer');
if (!Match.test(file.workingGroupRequestId, String)) {
console.log('validateFileUploadToWorkingGroupRequestAnswer !match test');
return false;
}
const fileUploadAllowed = settings.get('FileUpload_Enabled');
// const workingGroupMeeting = WorkingGroupMeetings.findOneById(file.workingGroupMeetingId);
//
// if (!workingGroupMeeting) {
// console.log('!workingGroupMeeting');
// return false;
// }
const language = 'ru';
if (!fileUploadAllowed) {
console.log('!fileUploadAllowed');
const reason = TAPi18n.__('FileUpload_Disabled', language);
throw new Meteor.Error('error-file-upload-disabled', reason);
}
// -1 maxFileSize means there is no limit
if (maxFileSize > -1 && file.size > maxFileSize) {
console.log('maxFileSize > -1 && file.size > maxFileSize');
const reason = TAPi18n.__('File_exceeds_allowed_size_of_bytes', {
size: filesize(maxFileSize),
}, language);
throw new Meteor.Error('error-file-too-large', reason);
}

if (!fileUploadIsValidContentType(file.type)) {
console.log('fileUploadIsValidContentType');
const reason = TAPi18n.__('File_type_is_not_accepted', language);
throw new Meteor.Error('error-invalid-file-type', reason);
}

return true;
},

validateFileUploadToMessage(file) {
if (!Match.test(file.rid, String)) {
return false;
}
Expand Down Expand Up @@ -167,7 +204,7 @@ export const FileUpload = {
return {
collection: Uploads.model,
filter: new UploadFS.Filter({
onCheck: FileUpload.validateFileUpload,
onCheck: FileUpload.validateFileUploadToMessage,
}),
getPath(file) {
return `${ settings.get('uniqueID') }/uploads/${ file.rid }/${ file.userId }/${ file._id }`;
Expand Down Expand Up @@ -576,7 +613,6 @@ export class FileUploadClass {
return store.delete(file._id);
}


deleteByRoomId(rid) {
const file = this.model.findOneByRoomId(rid);

Expand Down Expand Up @@ -621,15 +657,16 @@ export class FileUploadClass {

insert(fileData, streamOrBuffer, cb) {
fileData.size = parseInt(fileData.size) || 0;

// Check if the fileData matches store filter
const filter = this.store.getFilter();
if (filter && filter.check) {
if (fileData.rid) {
filter.check(fileData);
} else {
FileUpload.validateFileUploadToWorkingGroup(fileData);
}
// else if (!FileUpload.validateFileUploadToWorkingGroupRequestAnswer(fileData)) {
// return;
// //FileUpload.validateFileUploadToWorkingGroup(fileData);
// }
}

return this._doInsert(fileData, streamOrBuffer, cb);
Expand Down
2 changes: 2 additions & 0 deletions app/models/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Councils from './models/Councils';
import WorkingGroups from './models/WorkingGroups';
import WorkingGroupMeetings from './models/WorkingGroupMeetings';
import Protocols from './models/Protocols';
import WorkingGroupsRequests from './models/WorkingGroupsRequests';

export { AppsLogsModel } from './models/apps-logs-model';
export { AppsPersistenceModel } from './models/apps-persistence-model';
Expand Down Expand Up @@ -100,4 +101,5 @@ export {
WorkingGroups,
WorkingGroupMeetings,
Protocols,
WorkingGroupsRequests,
};
Loading

0 comments on commit ffb4179

Please sign in to comment.