Skip to content

Commit

Permalink
RocketChat#821 [EDIT] Исправление формы отправки документов
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarvis committed Feb 11, 2021
1 parent 0f5befe commit 8deebec
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 181 deletions.
12 changes: 12 additions & 0 deletions app/api/server/lib/working-groups-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ export async function findWorkingGroupRequestMailAnswerByAnswerId(requestId, mai
}
return {};
}

export async function findWorkingGroupRequestAnswerByAnswerId(requestId, answerId) {
const cursor = await WorkingGroupsRequests.findOne({ _id: requestId });
const answers = cursor.answers ?? [];

for (let i = 0; i < answers.length; i++) {
if (answers[i]._id === answerId) {
return answers[i];
}
}
return {};
}
2 changes: 1 addition & 1 deletion app/api/server/v1/persons.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ API.v1.addRoute('persons.listToAutoComplete', { authRequired: true }, {

return API.v1.success(Promise.await(findPersons({
query,
fields: { surname: 1, name: 1, patronymic: 1 },
fields: { surname: 1, name: 1, patronymic: 1, userId: 1 },
pagination: {
offset,
count,
Expand Down
13 changes: 10 additions & 3 deletions app/api/server/v1/working-groups-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Meteor } from 'meteor/meteor';

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

API.v1.addRoute('working-groups-requests.list', { authRequired: true }, {
get() {
Expand Down Expand Up @@ -42,6 +42,13 @@ API.v1.addRoute('working-groups-requests.findAnswerOne', { authRequired: true },
},
});

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

API.v1.addRoute('working-groups-requests.getOneByInviteLink', { authRequired: false }, {
get() {
const { query } = this.parseJsonQuery();
Expand Down Expand Up @@ -76,7 +83,7 @@ const getFiles = Meteor.wrapAsync(({ request }, callback) => {
request.pipe(busboy);
});

API.v1.addRoute('working-groups-requests.upload/:id/:mailId/:answerId', { authRequired: false }, {
API.v1.addRoute('working-groups-requests.upload/:id/:answerId', { authRequired: false }, {
post() {
const { files, fields } = getFiles({
request: this.request,
Expand Down Expand Up @@ -110,7 +117,7 @@ API.v1.addRoute('working-groups-requests.upload/:id/:mailId/:answerId', { authRe

uploadedFile.description = fields.description;

Meteor.call('sendFileWorkingGroupRequestAnswer', this.urlParams.id, this.urlParams.mailId, this.urlParams.answerId, uploadedFile);
Meteor.call('sendFileWorkingGroupRequestAnswer', this.urlParams.id, this.urlParams.answerId, uploadedFile);

return uploadedFile;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Uploads, WorkingGroupsRequests } from '../../../models';
import { FileUpload } from '../lib/FileUpload';

Meteor.methods({
async sendFileWorkingGroupRequestAnswer(workingGroupRequestId, workingGroupRequestMailId, workingGroupRequestAnswerId, file) {
async sendFileWorkingGroupRequestAnswer(workingGroupRequestId, workingGroupRequestAnswerId, file) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'sendFileMessage' });
}
Expand Down Expand Up @@ -48,7 +48,7 @@ Meteor.methods({
attachment.video_size = file.size;
}

WorkingGroupsRequests.addWorkingGroupRequestAnswerFile(workingGroupRequestId, workingGroupRequestMailId, workingGroupRequestAnswerId, attachment);
WorkingGroupsRequests.addWorkingGroupRequestAnswerFile(workingGroupRequestId, workingGroupRequestAnswerId, attachment);
return attachment;
},
});
90 changes: 48 additions & 42 deletions app/models/server/models/WorkingGroupsRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,55 +57,61 @@ class WorkingGroupsRequests extends Base {
answers: [answerData],
};

if (data.mails) {
let isAdded = false;
data.mails.forEach((mail) => {
if (mail._id === mailId) {
isAdded = true;
if (mail.answers) {
let internalNum = 0;
mail.answers.forEach((answer) => {
if (answer.inum > internalNum) {
internalNum = answer.inum;
}
});
internalNum++;
answerData.inum = internalNum;
mail.answers = [...mail.answers, answerData];
} else {
answerData.inum = 1;
mail.answers = [answerData];
}
}
});
if (!isAdded) {
data.mails = [...data.mails, newMailData];
}
// if (data.mails) {
// let isAdded = false;
// data.mails.forEach((mail) => {
// if (mail._id === mailId) {
// isAdded = true;
// if (mail.answers) {
// let internalNum = 0;
// mail.answers.forEach((answer) => {
// if (answer.inum > internalNum) {
// internalNum = answer.inum;
// }
// });
// internalNum++;
// answerData.inum = internalNum;
// mail.answers = [...mail.answers, answerData];
// } else {
// answerData.inum = 1;
// mail.answers = [answerData];
// }
// }
// });
// if (!isAdded) {
// data.mails = [...data.mails, newMailData];
// }
// } else {
// data.mails = [newMailData];
// }
if (data.answers) {
data.answers = [...data.answers, answerData];
} else {
data.mails = [newMailData];
data.answers = [answerData];
}

this.update({ _id: workingGroupRequestId }, { $set: { ...data } });
return { answerId: _id, mailId: newMailId };
// return { answerId: _id, mailId: newMailId };
return { answerId: _id, mailId: _id };
}

addWorkingGroupRequestAnswerFile(workingGroupRequestId, mailId, answerId, fileData) {
addWorkingGroupRequestAnswerFile(workingGroupRequestId, answerId, fileData) {
const data = this.findOne({ _id: workingGroupRequestId });
const indexMail = data.mails ? data.mails.findIndex((mail) => mail._id === mailId) : -1;
if (indexMail < 0) {
return;
}
// const indexMail = data.mails ? data.mails.findIndex((mail) => mail._id === mailId) : -1;
// if (indexMail < 0) {
// return;
// }

const indexAnswer = data.mails[indexMail].answers ? data.mails[indexMail].answers.findIndex((answer) => answer._id === answerId) : -1;
const indexAnswer = data.answers ? data.answers.findIndex((answer) => answer._id === answerId) : -1;
if (indexAnswer < 0) {
return;
}

data._updatedAt = new Date();
if (data.mails[indexMail].answers[indexAnswer].documents) {
data.mails[indexMail].answers[indexAnswer].documents.push(fileData);
if (data.answers[indexAnswer].documents) {
data.answers[indexAnswer].documents.push(fileData);
} else {
data.mails[indexMail].answers[indexAnswer].documents = [fileData];
data.answers[indexAnswer].documents = [fileData];
}
return this.update({ _id: workingGroupRequestId }, { $set: { ...data } });
}
Expand All @@ -129,20 +135,20 @@ class WorkingGroupsRequests extends Base {
}
}

readAnswer(_id, mailId, answerId) {
readAnswer(_id, answerId) {
const data = this.findOne({ _id });
const indexMail = data.mails ? data.mails.findIndex((mail) => mail._id === mailId) : -1;
if (indexMail < 0) {
return;
}
// const indexMail = data.mails ? data.mails.findIndex((mail) => mail._id === mailId) : -1;
// if (indexMail < 0) {
// return;
// }

const indexAnswer = data.mails[indexMail].answers ? data.mails[indexMail].answers.findIndex((answer) => answer._id === answerId) : -1;
const indexAnswer = data.answers ? data.answers.findIndex((answer) => answer._id === answerId) : -1;
if (indexAnswer < 0) {
return;
}

data._updatedAt = new Date();
data.mails[indexMail].answers[indexAnswer].unread = false;
data.answers[indexAnswer].unread = false;
return this.update({ _id }, { $set: { ...data } });
}

Expand Down
2 changes: 1 addition & 1 deletion app/ui/client/lib/fileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export const uploadFileWithWorkingGroupRequestAnswer = async (workingGroupReques
uploads.push(upload);
Session.set('uploading', uploads);

const { xhr, promise } = await APIClient.upload(`v1/working-groups-requests.upload/${ workingGroupRequestId }/${ mailId }/${ answerId }`, {}, data, {
const { xhr, promise } = await APIClient.upload(`v1/working-groups-requests.upload/${ workingGroupRequestId }/${ answerId }`, {}, data, {
progress(progress) {
const uploads = Session.get('uploading') || [];

Expand Down
33 changes: 28 additions & 5 deletions app/working-group-requests/client/views/AddRequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useCallback, useMemo, useState } from 'react';
import { Button, ButtonGroup, Field, TextInput, TextAreaInput } from '@rocket.chat/fuselage';
import DatePicker, { registerLocale } from 'react-datepicker';
import ru from 'date-fns/locale/ru';

import { useTranslation } from '../../../../client/contexts/TranslationContext';
import { useMethod } from '../../../../client/contexts/ServerContext';
Expand All @@ -8,11 +10,15 @@ import { createWorkingGroupRequestData, validateWorkingGroupRequestData } from '
import VerticalBar from '../../../../client/components/basic/VerticalBar';
import { checkNumberWithDot } from '../../../utils/client/methods/checkNumber';

registerLocale('ru', ru);
require('react-datepicker/dist/react-datepicker.css');

export function AddRequest({ editData, onChange }) {
const data = {
_id: null,
number: '',
desc: '',
date: new Date(),
mails: [],
};
console.log(editData);
Expand All @@ -24,11 +30,12 @@ function AddRequestWithData({ request, onChange, ...props }) {
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();

const { _id, number: previousNumber, desc: previousDescription } = request || {};
const { _id, number: previousNumber, desc: previousDescription, date: previousDate } = request || {};
const previousRequest = request || {};

const [number, setNumber] = useState(previousNumber);
const [description, setDescription] = useState(previousDescription);
const [date, setDate] = useState(previousDate ? new Date(previousDate) : new Date());

const insertOrUpdateWorkingGroupRequest = useMethod('insertOrUpdateWorkingGroupRequest');
const goBack = () => {
Expand All @@ -50,10 +57,10 @@ function AddRequestWithData({ request, onChange, ...props }) {
}
};

const saveAction = useCallback(async (number, description) => {
const saveAction = useCallback(async (number, description, date) => {
console.log(number);
console.log(description);
const requestData = createWorkingGroupRequestData(number, description, { previousNumber, previousDescription, _id });
const requestData = createWorkingGroupRequestData(number, description, date, { previousNumber, previousDescription, _id });
const validation = validateWorkingGroupRequestData(requestData);
if (validation.length === 0) {
await insertOrUpdateWorkingGroupRequest(requestData);
Expand All @@ -62,7 +69,7 @@ function AddRequestWithData({ request, onChange, ...props }) {
}, [_id, dispatchToastMessage, insertOrUpdateWorkingGroupRequest, number, description, previousNumber, previousDescription, previousRequest, t]);

const handleSaveRequest = useCallback(async () => {
await saveAction(number, description);
await saveAction(number, description, date);
if (!request._id) {
dispatchToastMessage({
type: 'success',
Expand All @@ -76,7 +83,7 @@ function AddRequestWithData({ request, onChange, ...props }) {
}
onChange();
goBack();
}, [saveAction, onChange]);
}, [saveAction, onChange, number, description, date]);

return <VerticalBar.ScrollableContent {...props}>
<Field>
Expand All @@ -91,6 +98,22 @@ function AddRequestWithData({ request, onChange, ...props }) {
<TextAreaInput style={ { whiteSpace: 'normal' } } row='10' border='1px solid #4fb0fc' value={description} onChange={(e) => setDescription(e.currentTarget.value)} placeholder={t('Description')} />
</Field.Row>
</Field>
<Field>
<Field.Label>{t('Date')}</Field.Label>
<Field.Row>
<DatePicker
dateFormat='dd.MM.yyyy HH:mm'
selected={date}
onChange={(newDate) => setDate(newDate)}
showTimeSelect
timeFormat='HH:mm'
timeIntervals={5}
timeCaption='Время'
customInput={<TextInput />}
locale='ru'
popperClassName='date-picker'/>
</Field.Row>
</Field>
<Field>
<Field.Row>
<ButtonGroup stretch w='full'>
Expand Down
2 changes: 1 addition & 1 deletion app/working-group-requests/client/views/Answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function AnswerPage() {
query: JSON.stringify({ _id: requestId, mailId, answerId }),
}), [mailId]);

const data = useEndpointData('working-groups-requests.findAnswerOne', query) || { documents: [] };
const data = useEndpointData('working-groups-requests.findAnswerOneById', query) || { documents: [] };

const filesQuery = useMemo(() => ({ query: JSON.stringify({ _id: data ? { $in: data.documents?.map((doc) => doc._id ?? '') } : {} }) }), [data]);

Expand Down
53 changes: 28 additions & 25 deletions app/working-group-requests/client/views/Answers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function AnswersWithData({ answers, mail, onClick, onChange }) {

const [params, setParams] = useState({ current: 0, itemsPerPage: 25 });
const numberLabel = mail.number ? [t('Working_group_mail'), formatDate(mail.ts ?? ''), '№', mail.number ?? ''].join(' ') : t('Working_group_request_mail_not_chosen_answer');
const descriptionLabel = mail.number ? mail.description : t('Working_group_request_mail_not_chosen_answer');
const descriptionLabel = mail.number ? mail.desc : t('Working_group_request_mail_not_chosen_answer');

const readAnswer = useMethod('readAnswer');

Expand All @@ -34,7 +34,8 @@ function AnswersWithData({ answers, mail, onClick, onChange }) {
const index = answers.findIndex((answer) => answer._id === answerEnter._id);
if (index > -1) {
answers[index].unread = false;
await readAnswer(requestId, mail._id, answerEnter._id);
// await readAnswer(requestId, mail._id, answerEnter._id);
await readAnswer(requestId, answerEnter._id);
onChange();
}
}
Expand Down Expand Up @@ -64,27 +65,29 @@ function AnswersWithData({ answers, mail, onClick, onChange }) {
</Table.Row>;
};

return <Page>
<Page.Header>
<Field width={'100%'} display={'block'} marginBlock={'15px'}>
<GoBackButton/>
<Label fontScale='h1'>{t('Working_group_received_mails')}</Label>
</Field>
</Page.Header>
<Page.Content>
<Field mbe='x8'>
<Field.Label>{t('Number')}</Field.Label>
<Field.Row>
<TextInput readOnly placeholder={t('Number')} is='span' fontScale='p1'>{numberLabel}</TextInput>
</Field.Row>
</Field>
<Field mbe='x8'>
<Field.Label>{t('Description')}</Field.Label>
<Field.Row>
<TextAreaInput rows='3' value={descriptionLabel} readOnly placeholder={t('Description')} fontScale='p1'/>
</Field.Row>
</Field>
<GenericTable header={header} renderRow={renderRow} results={answers} onChange={onChange} total={answers.length} setParams={setParams} params={params} />
</Page.Content>
</Page>;
return <GenericTable header={header} renderRow={renderRow} results={answers} onChange={onChange} total={answers.length} setParams={setParams} params={params} />;

// return <Page>
// <Page.Header>
// <Field width={'100%'} display={'block'} marginBlock={'15px'}>
// <GoBackButton/>
// <Label fontScale='h1'>{t('Working_group_received_mails')}</Label>
// </Field>
// </Page.Header>
// <Page.Content>
// <Field mbe='x8'>
// <Field.Label>{t('Number')}</Field.Label>
// <Field.Row>
// <TextInput readOnly placeholder={t('Number')} is='span' fontScale='p1'>{numberLabel}</TextInput>
// </Field.Row>
// </Field>
// <Field mbe='x8'>
// <Field.Label>{t('Description')}</Field.Label>
// <Field.Row>
// <TextAreaInput rows='3' value={descriptionLabel} readOnly placeholder={t('Description')} fontScale='p1'/>
// </Field.Row>
// </Field>
// <GenericTable header={header} renderRow={renderRow} results={answers} onChange={onChange} total={answers.length} setParams={setParams} params={params} />
// </Page.Content>
// </Page>;
}
Loading

0 comments on commit 8deebec

Please sign in to comment.