Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Revert "chore: instantly return when submitting sentences"
Browse files Browse the repository at this point in the history
This reverts commit 05450e4.
  • Loading branch information
MichaelKohler committed May 29, 2020
1 parent 05450e4 commit e7ff5b5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
17 changes: 12 additions & 5 deletions server/lib/sentences.js
Expand Up @@ -164,7 +164,7 @@ async function getUserAddedSentencesPerLocale(user) {
return sentencesStats;
}

function addSentences(data) {
async function addSentences(data) {
const {
sentences,
user,
Expand All @@ -177,6 +177,7 @@ function addSentences(data) {
const { valid, validValidated, filtered } = validateSentences(locale, sentences);

debug('Creating database entries');
let duplicateCounter = 0;

const addSentenceToDatabase = (sentence, isValidated) => {
const params = {
Expand All @@ -187,7 +188,7 @@ function addSentences(data) {
localeId: locale,
};

Sentence.create(params)
return Sentence.create(params)
.then((sentence) => {
if (!isValidated) {
return sentence;
Expand All @@ -203,14 +204,20 @@ function addSentences(data) {
.catch((error) => {
if (error.parent && error.parent.errno === DUPLICATE_ERROR) {
debug('Ignoring duplicate sentence', sentence);
duplicateCounter++;
}
});
};

valid.map((sentence) => addSentenceToDatabase(sentence, false));
validValidated.map((sentence) => addSentenceToDatabase(sentence, true));
const validPromises = valid.map((sentence) => addSentenceToDatabase(sentence, false));
const validatedPromises = validValidated.map((sentence) => addSentenceToDatabase(sentence, true));

return { errors: filtered };
await Promise.all([
...validPromises,
...validatedPromises,
]);

return { errors: filtered, duplicates: duplicateCounter };
}

function getReviewQuery({ locale, user }) {
Expand Down
13 changes: 10 additions & 3 deletions server/routes/sentences.js
Expand Up @@ -38,9 +38,16 @@ router.get('/rejected', async (req, res) => {
router.put('/', async (req, res) => {
debug('CREATE_SENTENCES', req.body);

const result = sentences.addSentences(req.body);
res.status(STATUS_CREATED);
res.json(result);
sentences.addSentences(req.body)
.then((result) => {
res.status(STATUS_CREATED);
res.json(result);
})
.catch((error) => {
debug('CREATE_SENTENCES_ERROR', error);
res.status(STATUS_ERROR);
res.json({ message: error.message });
});
});

router.get('/:locale', async (req, res) => {
Expand Down
26 changes: 26 additions & 0 deletions server/tests/lib/sentences.test.js
Expand Up @@ -81,6 +81,7 @@ test.serial('addedSentences: should add all unreviewed sentences', async (t) =>

t.false(votes.addVoteForSentence.called);
t.is(result.errors.length, 0);
t.is(result.duplicates, 0);
});

test.serial('addedSentences: should fall back to en if no locale provided', async (t) => {
Expand Down Expand Up @@ -120,6 +121,31 @@ test.serial('addedSentences: should add all sentences - mixed validated and unre

t.is(Sentence.create.callCount, 3);
t.is(result.errors.length, 0);
t.is(result.duplicates, 0);
});

test.serial('addedSentences: should return duplicate counter', async (t) => {
const error = new Error('duplicate');
error.parent = {
errno: 1062,
};
Sentence.create.onCall(2).rejects(error);

const sentenceParams = {
sentences: {
unreviewed: ['Hi!'],
validated: ['I am a test', 'I am a test'],
},
user: 'foo',
source: 'source',
locale: 'en',
};

const result = await sentences.addSentences(sentenceParams);

t.is(Sentence.create.callCount, 3);
t.is(result.errors.length, 0);
t.is(result.duplicates, 1);
});

test.serial('getStats: should fetch all stats correctly', async (t) => {
Expand Down
12 changes: 12 additions & 0 deletions server/tests/routes/sentences.test.js
Expand Up @@ -132,3 +132,15 @@ test.serial('should add sentences', async (t) => {
t.is(response.status, 201);
t.true(sentences.addSentences.calledWith(sentenceParams));
});

test.serial('adding sentences should pass on error message', async (t) => {
sentences.addSentences.rejects(new Error('nope'));

const response = await request(app)
.put('/sentences');

t.is(response.status, 500);
t.deepEqual(response.body, {
message: 'nope',
});
});
4 changes: 2 additions & 2 deletions web/src/components/pages/add.jsx
Expand Up @@ -156,7 +156,7 @@ class Add extends React.Component {
const sentences = this.getReadySentences();
const locale = this.state.language;
const source = this.state.source;
const { errors } = await this.props.uploadSentences({
const { errors, duplicates } = await this.props.uploadSentences({
locale,
sentences,
source,
Expand All @@ -170,7 +170,7 @@ class Add extends React.Component {
this.historyUnblock();
this.resetState();
this.setState({
message: `Submitted sentences.`,
message: `Submitted sentences. ${duplicates} sentences were rejected as duplicates.`,
error: errors && errors.length > 0 ? `${errors.length} sentences failed` : '',
});
} catch (err) {
Expand Down

0 comments on commit e7ff5b5

Please sign in to comment.