Fix deprecation warninsg

This commit is contained in:
2021-09-25 15:55:11 +02:00
parent cc9cc53d0b
commit 623cf6d6da
2 changed files with 73 additions and 52 deletions

View File

@@ -1,64 +1,66 @@
const Record = require('./record');
const ExpenseSerializer = require('../serializers/expense');
const Record = require("./record");
const ExpenseSerializer = require("../serializers/expense");
class Reimbursement extends Record {
get count () {
return this.functions.reimbursementsCount();
get count() {
return this.contract.reimbursementsCount();
}
getById (id) {
return this.functions.get(id)
.then(data => {
return this.ipfs.catAndMerge(data, (ipfsDocument) => {
const expenses = JSON.parse(ipfsDocument);
return { expenses };
});
getById(id) {
return this.contract.get(id).then((data) => {
return this.ipfs.catAndMerge(data, (ipfsDocument) => {
const expenses = JSON.parse(ipfsDocument);
return { expenses };
});
});
}
getData (id) {
return this.functions.getReimbursement(id);
getData(id) {
return this.contract.getReimbursement(id);
}
async add (attrs, callOptions = {}) {
async add(attrs, callOptions = {}) {
const amount = parseInt(attrs.amount);
const token = attrs.token;
const recipientId = attrs.recipientId;
const expenses = attrs.expenses.map( e => new ExpenseSerializer(e) );
const expenses = attrs.expenses.map((e) => new ExpenseSerializer(e));
let errorMessage;
if (typeof amount !== 'number' || amount <= 0) {
errorMessage = 'Invalid data: amount must be a positive number.';
if (typeof amount !== "number" || amount <= 0) {
errorMessage = "Invalid data: amount must be a positive number.";
}
if (!token || token === '') {
errorMessage = 'Invalid data: token must be a token address.';
if (!token || token === "") {
errorMessage = "Invalid data: token must be a token address.";
}
if (!recipientId || recipientId === '') {
errorMessage = 'Invalid data: recipientId is required.';
if (!recipientId || recipientId === "") {
errorMessage = "Invalid data: recipientId is required.";
}
if (expenses.length === 0) {
errorMessage = 'Invalid data: at least one expense item is required.';
errorMessage = "Invalid data: at least one expense item is required.";
}
if (errorMessage) {
return Promise.reject(new Error(errorMessage));
}
if (errorMessage) { return Promise.reject(new Error(errorMessage)); }
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 => {
const reimbursement = [
amount,
token,
parseInt(recipientId),
ipfsHashAttr.hashDigest,
ipfsHashAttr.hashFunction,
ipfsHashAttr.hashSize,
];
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) => {
const reimbursement = [
amount,
token,
parseInt(recipientId),
ipfsHashAttr.hashDigest,
ipfsHashAttr.hashFunction,
ipfsHashAttr.hashSize,
];
return this.functions.add(...reimbursement, callOptions);
});
return this.contract.add(...reimbursement, callOptions);
});
});
}
}