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

Commit

Permalink
chore: add correct approval script
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKohler committed Aug 4, 2019
1 parent 210e481 commit 36f254e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
16 changes: 16 additions & 0 deletions scripts/db-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ACTION_DELETE_SPECIFIC = 'delete-specific';
const ACTION_FORCE_DELETE_SPECIFIC = 'force-delete-specific';
const ACTION_FORCE_DELETE_FILE = 'force-delete-file';
const ACTION_DELETE_VOTES = 'delete-votes';
const ACTION_APPROVAL_CHECK = 'correct-approvals';

const system = process.env.SC_SYSTEM;
const remote = process.env.KINTO_URL_LOCAL;
Expand All @@ -31,6 +32,7 @@ const deleteFile = process.env.DELETE_SPECIFIC_SENTENCES_FILE;
const approvalOnly = /true/.test(process.env.DELETE_APPROVAL_ONLY);

const action = process.argv[2];
const locale = process.argv[3];

async function flushKinto() {
const server = new KintoTestServer(remote);
Expand Down Expand Up @@ -113,6 +115,12 @@ async function forceDeleteSpecificSentences() {
await db.forceDeleteSpecificSentenceRecords(deleteLocale, deleteUsername);
}

async function correctApprovals(locale) {
const remoteHost = system === 'production' ? prodRemote : remote;
const db = new DB(remoteHost, username, password);
await db.correctApprovals(locale);
}

async function deleteVotes() {
const remoteHost = system === 'production' ? prodRemote : remote;
const db = new DB(remoteHost, username, password);
Expand Down Expand Up @@ -185,6 +193,14 @@ async function run() {
await deleteVotes();
break;

case ACTION_APPROVAL_CHECK:
if (!locale) {
throw new Error('NO_LOCALE_SPECIFIED');
}

await correctApprovals(locale);
break;

default:
fail(`Unrecognized action: ${action}`);
break;
Expand Down
4 changes: 4 additions & 0 deletions shared/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export default class DB {
return this.sentences.getAllValidatedSentences(language);
}

async correctApprovals(language) {
return this.sentences.correctApprovals(language);
}

async submitSentences(language, sentences, source) {
return this.sentences.submitSentences(language, sentences, source);
}
Expand Down
64 changes: 64 additions & 0 deletions shared/db/collections/sentences-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,56 @@ export default class SentencesMeta {
});
}

async correctApprovals(locale) {
const collection = await this.getCollection(locale);
const records = await this.getAllPaginated(locale);
console.log(`Found ${records.length} records to analyze for ${locale}`);

const adjustedSentences = records.map((record) => {
const isApproved = this.checkIfApproved(record);
if (typeof isApproved === 'undefined' || isApproved === record.approved) {
// nothing to do here
return;
}

record.approved = isApproved;
record.approvalDate = Date.now();

return record;
}).filter(Boolean);

console.log(`Found ${adjustedSentences.length} to adjust`);

const results = await collection.batch(c => {
adjustedSentences.forEach(record => {
c.updateRecord(record, {
safe: true,
last_modified: record.last_modified,
});
});
});

const adjustedResults = [];
const errors = [];
results.forEach(result => {
if (result.status === 200 || result === 201) {
adjustedResults.push(result.body.data);
} else {
console.error('Approval adjustment error', result.status, result.body);
errors.push(result);
}
});

if (adjustedResults.length > 0) {
console.log('All approvals adjusted:');
console.log(`Updated ${adjustedResults.length} records`);
}

if (errors.length > 0) {
console.log(`Errors updating ${errors.length} records!`);
}
}

async deleteVotes(locale, username, approvalOnly) {
const collection = await this.getCollection(locale);
const records = await this.getAll(locale);
Expand Down Expand Up @@ -240,6 +290,20 @@ export default class SentencesMeta {
return result.data;
}

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

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

return data;
}

getAllRejectedByUsername(languages, username) {
const promises = languages.map((language) => this.getAllUnapprovedByUsername(language, username));

Expand Down

0 comments on commit 36f254e

Please sign in to comment.