Process contribution data before loading models
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import EmberObject, { computed } from '@ember/object';
|
import EmberObject, { computed } from '@ember/object';
|
||||||
import { isEmpty, isPresent } from '@ember/utils';
|
import { isEmpty, isPresent } from '@ember/utils';
|
||||||
import bignumber from 'kredits-web/utils/cps/bignumber';
|
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
export default EmberObject.extend({
|
export default EmberObject.extend({
|
||||||
@@ -9,11 +8,15 @@ export default EmberObject.extend({
|
|||||||
id: null,
|
id: null,
|
||||||
contributorId: null,
|
contributorId: null,
|
||||||
amount: null,
|
amount: null,
|
||||||
confirmedAt: bignumber('confirmedAtBlock', 'toNumber'),
|
confirmedAt: null,
|
||||||
vetoed: null,
|
vetoed: null,
|
||||||
ipfsHash: null,
|
ipfsHash: null,
|
||||||
|
|
||||||
creatorAccount: null,
|
// contributor model instance
|
||||||
|
contributor: null,
|
||||||
|
|
||||||
|
// TODO contributor who submitted the contribution
|
||||||
|
// submittedBy: null,
|
||||||
|
|
||||||
// IPFS
|
// IPFS
|
||||||
kind: null,
|
kind: null,
|
||||||
@@ -22,7 +25,6 @@ export default EmberObject.extend({
|
|||||||
url: null,
|
url: null,
|
||||||
date: null,
|
date: null,
|
||||||
time: null,
|
time: null,
|
||||||
ipfsData: '',
|
|
||||||
|
|
||||||
pendingTx: null,
|
pendingTx: null,
|
||||||
|
|
||||||
@@ -41,6 +43,10 @@ export default EmberObject.extend({
|
|||||||
|
|
||||||
hasPendingChanges: computed('pendingTx', function() {
|
hasPendingChanges: computed('pendingTx', function() {
|
||||||
return isPresent(this.pendingTx);
|
return isPresent(this.pendingTx);
|
||||||
})
|
}),
|
||||||
|
|
||||||
|
serialize () {
|
||||||
|
return JSON.stringify(this);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,4 +22,5 @@ export default EmberObject.extend({
|
|||||||
serialize () {
|
serialize () {
|
||||||
return JSON.stringify(this);
|
return JSON.stringify(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
+26
-12
@@ -11,6 +11,7 @@ import { inject as service } from '@ember/service';
|
|||||||
|
|
||||||
import groupBy from 'kredits-web/utils/group-by';
|
import groupBy from 'kredits-web/utils/group-by';
|
||||||
import processContributorData from 'kredits-web/utils/process-contributor-data';
|
import processContributorData from 'kredits-web/utils/process-contributor-data';
|
||||||
|
import processContributionData from 'kredits-web/utils/process-contribution-data';
|
||||||
import formatKredits from 'kredits-web/utils/format-kredits';
|
import formatKredits from 'kredits-web/utils/format-kredits';
|
||||||
|
|
||||||
import config from 'kredits-web/config/environment';
|
import config from 'kredits-web/config/environment';
|
||||||
@@ -185,11 +186,11 @@ export default Service.extend({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const numCachedContributions = await this.browserCache.contributions.length();
|
const numCachedContributions = await this.browserCache.contributions.length();
|
||||||
if (numCachedContributions > 0) {
|
// if (numCachedContributions > 0) {
|
||||||
// TODO promises.push(this.loadContributionsFromCache);
|
// TODO promises.push(this.loadContributionsFromCache);
|
||||||
} else {
|
// } else {
|
||||||
await this.fetchContributions({ page: { size: 30 } });
|
await this.fetchContributions({ page: { size: 30 } });
|
||||||
}
|
// }
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
@@ -235,11 +236,11 @@ export default Service.extend({
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return this.cacheContributors();
|
return this.cacheLoadedContributors();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async cacheContributors () {
|
async cacheLoadedContributors () {
|
||||||
for (const c of this.contributors) {
|
for (const c of this.contributors) {
|
||||||
await this.browserCache.contributors.setItem(c.id, c.serialize());
|
await this.browserCache.contributors.setItem(c.id, c.serialize());
|
||||||
}
|
}
|
||||||
@@ -248,9 +249,9 @@ export default Service.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
async loadContributorsFromCache () {
|
async loadContributorsFromCache () {
|
||||||
return this.browserCache.contributors.iterate((value, key/* , iterationNumber */) => {
|
return this.browserCache.contributors.iterate((value/*, key , iterationNumber */) => {
|
||||||
this.contributors.pushObject(Contributor.create(JSON.parse(value)));
|
this.contributors.pushObject(Contributor.create(JSON.parse(value)));
|
||||||
}).then(result => {
|
}).then((/* result */) => {
|
||||||
console.debug(`[kredits] Loaded ${this.contributors.length} contributors from cache`);
|
console.debug(`[kredits] Loaded ${this.contributors.length} contributors from cache`);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -275,15 +276,28 @@ export default Service.extend({
|
|||||||
return this.kredits.Contribution.all(options)
|
return this.kredits.Contribution.all(options)
|
||||||
.then(contributions => {
|
.then(contributions => {
|
||||||
return contributions.map(data => {
|
return contributions.map(data => {
|
||||||
data.contributor = this.contributors.findBy('id', data.contributorId.toString());
|
const contribution = Contribution.create(processContributionData(data));
|
||||||
const contribution = Contribution.create(data);
|
contribution.set('contributor', this.contributors.findBy('id', data.contributorId.toString()));
|
||||||
this.contributions.pushObject(contribution);
|
this.contributions.pushObject(contribution);
|
||||||
return contribution;
|
return contribution;
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.then(contributions => {
|
||||||
|
const cacheWrites = contributions.map(c => {
|
||||||
|
return this.browserCache.contributions.setItem(c.id.toString(), c.serialize());
|
||||||
|
});
|
||||||
|
return Promise.all(cacheWrites).then(() => {
|
||||||
|
console.debug(`[kredits] Cached ${contributions.length} contributions in browser storage`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// TODO .then(() => {
|
},
|
||||||
// this.cacheContributions()
|
|
||||||
// });
|
async cacheLoadedContributions () {
|
||||||
|
for (const c of this.contributions) {
|
||||||
|
await this.browserCache.contributions.setItem(c.id, c.serialize());
|
||||||
|
}
|
||||||
|
console.debug(`[kredits] Cached ${this.contributions.length} contributions in browser storage`);
|
||||||
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
|
|
||||||
veto (contributionId) {
|
veto (contributionId) {
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
export default function processContributionData(data) {
|
||||||
|
const processed = {}
|
||||||
|
|
||||||
|
if (data.confirmedAtBlock && (typeof data.confirmedAtBlock.toNumber === 'function')) {
|
||||||
|
processed.confirmedAt = data.confirmedAtBlock.toNumber();
|
||||||
|
} else if (data.confirmedAt !== 'undefined') {
|
||||||
|
processed.confirmedAt = data.confirmedAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const otherProperties = [
|
||||||
|
'id', 'contributorId', 'amount', 'vetoed',
|
||||||
|
'ipfsHash', 'kind', 'description', 'details',
|
||||||
|
'url', 'date', 'time', 'pendingTx'
|
||||||
|
];
|
||||||
|
|
||||||
|
otherProperties.forEach(prop => {
|
||||||
|
processed[prop] = data[prop];
|
||||||
|
});
|
||||||
|
|
||||||
|
return processed;
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ export default function processContributorData(data) {
|
|||||||
id: data.id.toString(),
|
id: data.id.toString(),
|
||||||
balance: data.balanceInt,
|
balance: data.balanceInt,
|
||||||
totalKreditsEarned: data.totalKreditsEarned,
|
totalKreditsEarned: data.totalKreditsEarned,
|
||||||
contributionsCount: data.contributionsCount.toNumber()
|
contributionsCount: data.contributionsCount?.toNumber()
|
||||||
}
|
}
|
||||||
|
|
||||||
const otherProperties = [
|
const otherProperties = [
|
||||||
|
|||||||
Vendored
+391
File diff suppressed because one or more lines are too long
Vendored
+13
-11
@@ -1,23 +1,25 @@
|
|||||||
import Model from 'kredits-web/models/contribution';
|
import Model from 'kredits-web/models/contribution';
|
||||||
import contributors from '../../tests/fixtures/contributors';
|
import contributors from '../../tests/fixtures/contributors';
|
||||||
|
import processContributionData from 'kredits-web/utils/process-contribution-data';
|
||||||
|
|
||||||
const items = [];
|
const items = [];
|
||||||
|
|
||||||
const data = [
|
const data = [
|
||||||
{ id: 1, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 1500, kind: 'dev' },
|
{ id: 1, contributorId: 1, confirmedAt: 1000, claimed: false, vetoed: false, amount: 1500, kind: 'dev' },
|
||||||
{ id: 2, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 5000, kind: 'ops' },
|
{ id: 2, contributorId: 1, confirmedAt: 1000, claimed: false, vetoed: false, amount: 5000, kind: 'ops' },
|
||||||
{ id: 3, contributorId: 2, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 1500, kind: 'ops' },
|
{ id: 3, contributorId: 2, confirmedAt: 1000, claimed: false, vetoed: false, amount: 1500, kind: 'ops' },
|
||||||
{ id: 4, contributorId: 2, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 1500, kind: 'docs' },
|
{ id: 4, contributorId: 2, confirmedAt: 1000, claimed: false, vetoed: false, amount: 1500, kind: 'docs' },
|
||||||
{ id: 5, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 5000, kind: 'design' },
|
{ id: 5, contributorId: 1, confirmedAt: 1000, claimed: false, vetoed: false, amount: 5000, kind: 'design' },
|
||||||
{ id: 6, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: true, amount: 500, kind: 'dev' },
|
{ id: 6, contributorId: 1, confirmedAt: 1000, claimed: false, vetoed: true, amount: 500, kind: 'dev' },
|
||||||
{ id: 7, contributorId: 3, confirmedAtBlock: 2000, claimed: false, vetoed: false, amount: 5000, kind: 'dev' },
|
{ id: 7, contributorId: 3, confirmedAt: 2000, claimed: false, vetoed: false, amount: 5000, kind: 'dev' },
|
||||||
{ id: 8, contributorId: 1, confirmedAtBlock: 2000, claimed: false, vetoed: false, amount: 1500, kind: 'community' },
|
{ id: 8, contributorId: 1, confirmedAt: 2000, claimed: false, vetoed: false, amount: 1500, kind: 'community' },
|
||||||
{ id: 9, contributorId: 3, confirmedAtBlock: 2000, claimed: false, vetoed: true, amount: 1500, kind: 'docs' },
|
{ id: 9, contributorId: 3, confirmedAt: 2000, claimed: false, vetoed: true, amount: 1500, kind: 'docs' },
|
||||||
];
|
];
|
||||||
|
|
||||||
data.forEach(attrs => {
|
data.forEach(attrs => {
|
||||||
attrs.contributor = contributors.findBy('id', attrs.contributorId.toString());
|
const c = Model.create(processContributionData(attrs));
|
||||||
items.push(Model.create(attrs))
|
c.set('contributor', contributors.findBy('id', attrs.contributorId.toString()));
|
||||||
|
items.push(c);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default items;
|
export default items;
|
||||||
|
|||||||
Vendored
+5
-1
@@ -1,4 +1,5 @@
|
|||||||
import Contributor from 'kredits-web/models/contributor';
|
import Contributor from 'kredits-web/models/contributor';
|
||||||
|
import processContributorData from 'kredits-web/utils/process-contributor-data';
|
||||||
|
|
||||||
const contributors = [];
|
const contributors = [];
|
||||||
|
|
||||||
@@ -8,6 +9,9 @@ const data = [
|
|||||||
{ id: '3', name: 'Manuel', totalKreditsEarned: 0, github_uid: 54812 }
|
{ id: '3', name: 'Manuel', totalKreditsEarned: 0, github_uid: 54812 }
|
||||||
];
|
];
|
||||||
|
|
||||||
data.forEach(attrs => contributors.push(Contributor.create(attrs)));
|
data.forEach(attrs => {
|
||||||
|
const c = Contributor.create(processContributorData(attrs));
|
||||||
|
contributors.push(c);
|
||||||
|
});
|
||||||
|
|
||||||
export default contributors;
|
export default contributors;
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { module, test } from 'qunit';
|
||||||
|
import processContributionData from 'kredits-web/utils/process-contribution-data';
|
||||||
|
import testData from '../../fixtures/contribution-data';
|
||||||
|
|
||||||
|
module('Unit | Utility | process-contribution-data', function() {
|
||||||
|
|
||||||
|
let result = processContributionData(testData);
|
||||||
|
|
||||||
|
test('formats the data correctly', function(assert) {
|
||||||
|
assert.ok(typeof result.confirmedAt === 'number');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('copies other properties', function(assert) {
|
||||||
|
[
|
||||||
|
'id', 'contributorId', 'amount', 'vetoed',
|
||||||
|
'ipfsHash', 'kind', 'description', 'details',
|
||||||
|
'url', 'date', 'time', 'pendingTx'
|
||||||
|
].forEach(p => {
|
||||||
|
assert.ok(Object.prototype.hasOwnProperty.call(result, p), `copies property ${p}`);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
test('does not copy unnecessary properties', function(assert) {
|
||||||
|
['exists', '5'].forEach(p => {
|
||||||
|
assert.notOk(Object.prototype.hasOwnProperty.call(result, p));
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,10 +8,10 @@ module('Unit | Utility | process-contributor-data', function() {
|
|||||||
|
|
||||||
test('formats the data correctly', function(assert) {
|
test('formats the data correctly', function(assert) {
|
||||||
// TODO use integers everywhere for IDs
|
// TODO use integers everywhere for IDs
|
||||||
assert.ok(typeof result.id == 'string');
|
assert.ok(typeof result.id === 'string');
|
||||||
assert.ok(typeof result.balance == 'number');
|
assert.ok(typeof result.balance === 'number');
|
||||||
assert.ok(typeof result.totalKreditsEarned == 'number');
|
assert.ok(typeof result.totalKreditsEarned === 'number');
|
||||||
assert.ok(typeof result.contributionsCount == 'number');
|
assert.ok(typeof result.contributionsCount === 'number');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('copies other properties', function(assert) {
|
test('copies other properties', function(assert) {
|
||||||
|
|||||||
Reference in New Issue
Block a user