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

Commit

Permalink
chore: use getAllPaginated whenever we can
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKohler committed Aug 14, 2019
1 parent 36f254e commit 64a48e5
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions shared/db/collections/sentences-meta.js
Expand Up @@ -57,7 +57,7 @@ export default class SentencesMeta {
async deleteSentenceRecords(bucket) {
const languages = getAllLanguages();
for (const language of languages) {
const records = await this.getAll(language.code);
const records = await this.getAllPaginated(language.code);
const collectionName = await this.getCollectionName(language.code);
console.log(`Found ${records.length} records to delete for ${language.code}`);
await bucket.batch(b => {
Expand All @@ -83,7 +83,7 @@ export default class SentencesMeta {

async forceDeleteSentences(bucket, locale, sentences) {
const collectionName = await this.getCollectionName(locale);
const records = await this.getAll(locale);
const records = await this.getAllPaginated(locale);
const minifiedRecords = records.map((record) => ({ id: record.id, sentence: record.sentence }));
const { foundSentences, errorSentences } = sentences.reduce((acc, sentence) => {
const foundSentence = minifiedRecords.find((record) => record.sentence === sentence);
Expand Down Expand Up @@ -153,7 +153,7 @@ export default class SentencesMeta {
const adjustedResults = [];
const errors = [];
results.forEach(result => {
if (result.status === 200 || result === 201) {
if (result.status === 200) {
adjustedResults.push(result.body.data);
} else {
console.error('Approval adjustment error', result.status, result.body);
Expand All @@ -173,7 +173,7 @@ export default class SentencesMeta {

async deleteVotes(locale, username, approvalOnly) {
const collection = await this.getCollection(locale);
const records = await this.getAll(locale);
const records = await this.getAllPaginated(locale);

const foundSentences = records.filter((record) => {
return record.valid.includes(username) || record.invalid.includes(username);
Expand Down Expand Up @@ -290,10 +290,10 @@ export default class SentencesMeta {
return result.data;
}

async getAllPaginated(language) {
async getAllPaginated(language, filters = {}) {
const collection = await this.getCollection(language);

let { data, hasNextPage, next } = await collection.listRecords({});
let { data, hasNextPage, next } = await collection.listRecords(filters);
while (hasNextPage) {
const result = await next();
data = data.concat(result.data);
Expand Down Expand Up @@ -333,12 +333,11 @@ export default class SentencesMeta {
}

async getAllByUsername(language, username) {
const collection = await this.getCollection(language);
const filters = {
username,
};
const result = await collection.listRecords({ filters });
return result.data;

return this.getAllPaginated(language, filters);
}

async getLanguageAndSentenceCounts(bucket) {
Expand Down Expand Up @@ -398,17 +397,8 @@ export default class SentencesMeta {
async getAllValidatedSentences(language) {
const filters = {};
filters.approved = true;
const collection = await this.getCollection(language);

let { data, hasNextPage, next } = await collection.listRecords({ filters });
while (hasNextPage) {
const result = await next();
data = data.concat(result.data);
hasNextPage = result.hasNextPage;
next = result.next;
}

return data;
return this.getAllPaginated(language, filters);
}

prepareForSubmission(sentences) {
Expand Down

0 comments on commit 64a48e5

Please sign in to comment.