Support multiple expenses in one reimburesement
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -9,7 +9,10 @@ class Reimbursement extends Record {
|
||||
getById (id) {
|
||||
return this.functions.get(id)
|
||||
.then(data => {
|
||||
return this.ipfs.catAndMerge(data, ExpenseSerializer.deserialize);
|
||||
return this.ipfs.catAndMerge(data, (ipfsDocument) => {
|
||||
const expenses = JSON.parse(ipfsDocument);
|
||||
return { expenses };
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -18,26 +21,32 @@ class Reimbursement extends Record {
|
||||
}
|
||||
|
||||
async add (attrs, callOptions = {}) {
|
||||
const reimbursement = new ExpenseSerializer(attrs);
|
||||
const amount = parseInt(attrs.amount);
|
||||
const token = attrs.token;
|
||||
const recipient = attrs.recipient;
|
||||
const expenses = attrs.expenses.map( e => new ExpenseSerializer(e) );
|
||||
|
||||
try { await reimbursement.validate(); }
|
||||
catch (error) { return Promise.reject(error); }
|
||||
if (!amount > 0 || !token || token === '' || !recipient || recipient === '' || !expenses.length > 0) {
|
||||
return Promise.reject(new Error('Invalid data. amount, token, expenses is required.'));
|
||||
}
|
||||
|
||||
const jsonStr = reimbursement.serialize();
|
||||
return Promise.all(expenses.map(e => e.validate()))
|
||||
.then(() => {
|
||||
const jsonStr = JSON.stringify(expenses.map(e => e.data), null, 2);
|
||||
return this.ipfs
|
||||
.add(jsonStr)
|
||||
.then(ipfsHashAttr => {
|
||||
let reimbursement = [
|
||||
amount,
|
||||
token,
|
||||
recipient,
|
||||
ipfsHashAttr.hashDigest,
|
||||
ipfsHashAttr.hashFunction,
|
||||
ipfsHashAttr.hashSize,
|
||||
];
|
||||
|
||||
return this.ipfs
|
||||
.add(jsonStr)
|
||||
.then(ipfsHashAttr => {
|
||||
let reimbursement = [
|
||||
attrs.amount,
|
||||
attrs.token,
|
||||
ipfsHashAttr.hashDigest,
|
||||
ipfsHashAttr.hashFunction,
|
||||
ipfsHashAttr.hashSize,
|
||||
];
|
||||
|
||||
console.log(reimbursement)
|
||||
return this.functions.add(...reimbursement, callOptions);
|
||||
return this.functions.add(...reimbursement, callOptions);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,11 @@ class Expense {
|
||||
* @public
|
||||
*/
|
||||
serialize () {
|
||||
// Write it pretty to ipfs
|
||||
return JSON.stringify(this.data, null, 2);
|
||||
}
|
||||
|
||||
get data () {
|
||||
let {
|
||||
title,
|
||||
description,
|
||||
@@ -47,8 +52,7 @@ class Expense {
|
||||
data['url'] = url;
|
||||
}
|
||||
|
||||
// Write it pretty to ipfs
|
||||
return JSON.stringify(data, null, 2);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +62,6 @@ class Expense {
|
||||
*/
|
||||
validate () {
|
||||
const serialized = JSON.parse(this.serialize());
|
||||
console.log(schemas['expense']);
|
||||
const valid = validator.validate(serialized, schemas['expense']);
|
||||
return valid ? Promise.resolve() : Promise.reject(validator.error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user