contracts/lib/contracts/contributor.js
Michael Bumann 766463b57b Dynamic function to test for core contributor flag
This removes the isCore flag and allows us to dynamically calculate the
core flag for contributors.

For now this is just us (the first 6 contirbutors)

Also we do not need the default contributor anymore because the deploy
user has the role to manage contributors and can create the first real
contributors.
2019-04-05 18:55:21 +02:00

78 lines
2.0 KiB
JavaScript

const ethers = require('ethers');
const RSVP = require('rsvp');
const ContributorSerializer = require('../serializers/contributor');
const Base = require('./base');
class Contributor extends Base {
all() {
return this.functions.contributorsCount()
.then(count => {
let contributors = [];
for (let id = 1; id <= count; id++) {
contributors.push(this.getById(id));
}
return RSVP.all(contributors);
});
}
getById(id) {
return this.functions.getContributorById(id)
.then((data) => {
// TODO: remove when naming updated on the contract
data.hashDigest = data.ipfsHash;
return data;
})
// Fetch IPFS data if available
.then((data) => {
return this.ipfs.catAndMerge(data, ContributorSerializer.deserialize);
});
}
filterByAccount(search) {
return this._byAccount(search, 'filter');
}
findByAccount(search) {
return this._byAccount(search, 'find');
}
_byAccount(search, method = 'filter') {
return this.all().then((contributors) => {
const searchEntries = Object.entries(search);
return contributors[method]((contributor) => {
if (!contributor.accounts) { return false; }
return contributor.accounts.find((account) => {
return searchEntries.every((item) => {
let [ key, value ] = item;
return account[key] === value;
});
});
});
});
}
add(contributorAttr, callOptions = {}) {
let json = ContributorSerializer.serialize(contributorAttr);
// TODO: validate against schema
return this.ipfs
.add(json)
.then((ipfsHashAttr) => {
let contributor = [
contributorAttr.account,
ipfsHashAttr.hashDigest,
ipfsHashAttr.hashFunction,
ipfsHashAttr.hashSize,
];
return this.functions.addContributor(...contributor, callOptions);
});
}
}
module.exports = Contributor;