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

Handle login at Coinify level #274

Merged
merged 3 commits into from Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 45 additions & 27 deletions src/coinify/coinify.js
Expand Up @@ -120,6 +120,12 @@ Object.defineProperties(Coinify.prototype, {
return this._kycs;
}
},
'hasAccount': {
configurable: false,
get: function () {
return Boolean(this._offline_token);
}
},
'isLoggedIn': {
configurable: false,
get: function () {
Expand Down Expand Up @@ -228,15 +234,7 @@ Coinify.prototype.login = function () {
};

Coinify.prototype.fetchProfile = function () {
var parentThis = this;

if (this.isLoggedIn) {
return this._profile.fetch();
} else {
return this.login().then(function () {
return parentThis._profile.fetch();
});
}
return this._profile.fetch();
};

Coinify.prototype.getBuyQuote = function (amount, baseCurrency, quoteCurrency) {
Expand All @@ -263,15 +261,7 @@ Coinify.prototype.buy = function (amount, baseCurrency, medium) {

var self = this;

var doBuy = function () {
return CoinifyTrade.buy(self._lastQuote, medium, self);
};

if (!this.isLoggedIn) {
return this.login().then(doBuy);
} else {
return doBuy();
}
return CoinifyTrade.buy(self._lastQuote, medium, self);
};

Coinify.prototype.getTrades = function () {
Expand All @@ -281,15 +271,7 @@ Coinify.prototype.getTrades = function () {
Coinify.prototype.triggerKYC = function () {
var self = this;

var doKYC = function () {
return CoinifyKYC.trigger(self);
};

if (!this.isLoggedIn) {
return this.login().then(doKYC);
} else {
return doKYC();
}
return CoinifyKYC.trigger(self);
};

Coinify.prototype.getKYCs = function () {
Expand Down Expand Up @@ -352,14 +334,50 @@ Coinify.prototype.GET = function (endpoint, data) {
return this.request('GET', endpoint, data);
};

Coinify.prototype.authGET = function (endpoint, data) {
var doGET = function () {
return this.GET(endpoint, data);
};

if (this.isLoggedIn) {
return doGET.bind(this)();
} else {
return this.login().then(doGET.bind(this));
}
};

Coinify.prototype.POST = function (endpoint, data) {
return this.request('POST', endpoint, data);
};

Coinify.prototype.authPOST = function (endpoint, data) {
var doPOST = function () {
return this.POST(endpoint, data);
};

if (this.isLoggedIn) {
return doPOST.bind(this)();
} else {
return this.login().then(doPOST.bind(this));
}
};

Coinify.prototype.PATCH = function (endpoint, data) {
return this.request('PATCH', endpoint, data);
};

Coinify.prototype.authPATCH = function (endpoint, data) {
var doPATCH = function () {
return this.PATCH(endpoint, data);
};

if (this.isLoggedIn) {
return doPATCH.bind(this)();
} else {
return this.login().then(doPATCH.bind(this));
}
};

Coinify.prototype.request = function (method, endpoint, data) {
var url = this._rootURL + endpoint;

Expand Down
35 changes: 10 additions & 25 deletions src/coinify/kyc.js
Expand Up @@ -63,14 +63,7 @@ Object.defineProperties(CoinifyKYC.prototype, {
});

CoinifyKYC.prototype.refresh = function () {
var updateTrade = function () {
return this._coinify.GET('kyc/' + this._id).then(this.set.bind(this));
};
if (this._coinify.isLoggedIn) {
return updateTrade.bind(this)();
} else {
return this._coinify.login().then(updateTrade.bind(this));
}
return this._coinify.authGET('kyc/' + this._id).then(this.set.bind(this));
};

CoinifyKYC.trigger = function (coinify) {
Expand All @@ -80,26 +73,18 @@ CoinifyKYC.trigger = function (coinify) {
return kyc;
};

return coinify.POST('traders/me/kyc').then(processKYC);
return coinify.authPOST('traders/me/kyc').then(processKYC);
};

// Fetches the latest trades and updates coinify._trades
CoinifyKYC.fetchAll = function (coinify) {
var getKycs = function () {
return coinify.GET('kyc').then(function (res) {
coinify._kycs.length = 0; // empty array without losing reference
for (var i = 0; i < res.length; i++) {
var kyc = new CoinifyKYC(res[i], coinify);
coinify._kycs.push(kyc);
}

return coinify._kycs;
});
};
return coinify.authGET('kyc').then(function (res) {
coinify._kycs.length = 0; // empty array without losing reference
for (var i = 0; i < res.length; i++) {
var kyc = new CoinifyKYC(res[i], coinify);
coinify._kycs.push(kyc);
}

if (coinify.isLoggedIn) {
return getKycs();
} else {
return coinify.login().then(getKycs);
}
return coinify._kycs;
});
};
30 changes: 11 additions & 19 deletions src/coinify/payment-method.js
Expand Up @@ -107,26 +107,18 @@ Object.defineProperties(PaymentMethod.prototype, {
});

PaymentMethod.fetchAll = function (inCurrency, outCurrency, coinify) {
var getPaymentMethods = function () {
var params = {};
if (inCurrency) { params.inCurrency = inCurrency; }
if (outCurrency) { params.outCurrency = outCurrency; }
var params = {};
if (inCurrency) { params.inCurrency = inCurrency; }
if (outCurrency) { params.outCurrency = outCurrency; }

var output = [];
return coinify.GET('trades/payment-methods', params).then(function (res) {
output.length = 0;
for (var i = 0; i < res.length; i++) {
output.push(new PaymentMethod(res[i], coinify));
}
return Promise.resolve(output);
});
};

if (coinify.isLoggedIn) {
return getPaymentMethods();
} else {
return coinify.login().then(getPaymentMethods);
}
var output = [];
return coinify.authGET('trades/payment-methods', params).then(function (res) {
output.length = 0;
for (var i = 0; i < res.length; i++) {
output.push(new PaymentMethod(res[i], coinify));
}
return Promise.resolve(output);
});
};

PaymentMethod.prototype.calculateFee = function (quote) {
Expand Down
4 changes: 2 additions & 2 deletions src/coinify/profile.js
Expand Up @@ -94,7 +94,7 @@ Object.defineProperties(CoinifyProfile.prototype, {

CoinifyProfile.prototype.fetch = function () {
var parentThis = this;
return this._coinify.GET('traders/me').then(function (res) {
return this._coinify.authGET('traders/me').then(function (res) {
parentThis._full_name = res.profile.name;
parentThis._gender = res.profile.gender;

Expand Down Expand Up @@ -180,5 +180,5 @@ CoinifyProfile.prototype.setZipcode = function (value) {
};

CoinifyProfile.prototype.update = function (values) {
return this._coinify.PATCH('traders/me', values);
return this._coinify.authPATCH('traders/me', values);
};
17 changes: 11 additions & 6 deletions src/coinify/quote.js
Expand Up @@ -98,19 +98,24 @@ Quote.getQuote = function (coinify, amount, baseCurrency, quoteCurrency) {
return quote;
};

var getQuote = function () {
var getAnonymousQuote = function () {
return coinify.POST('trades/quote', {
baseCurrency: baseCurrency,
quoteCurrency: quoteCurrency,
baseAmount: parseFloat(baseAmount)
});
};

if (coinify._offline_token == null) {
return getQuote().then(processQuote);
}
if (!coinify.isLoggedIn) {
return coinify.login().then(getQuote).then(processQuote);
var getQuote = function () {
return coinify.authPOST('trades/quote', {
baseCurrency: baseCurrency,
quoteCurrency: quoteCurrency,
baseAmount: parseFloat(baseAmount)
});
};

if (!coinify.hasAccount) {
return getAnonymousQuote().then(processQuote);
} else {
return getQuote().then(processQuote);
}
Expand Down
81 changes: 25 additions & 56 deletions src/coinify/trade.js
Expand Up @@ -200,15 +200,7 @@ CoinifyTrade.prototype.cancel = function () {
return self._coinify.save();
};

var cancelOrder = function () {
return self._coinify.PATCH('trades/' + self._id + '/cancel').then(processCancel);
};

if (this._coinify.isLoggedIn) {
return cancelOrder();
} else {
return this._coinify.login().then(cancelOrder);
}
return self._coinify.authPATCH('trades/' + self._id + '/cancel').then(processCancel);
};

// Checks the balance for the receive address and monitors the websocket if needed:
Expand Down Expand Up @@ -263,18 +255,10 @@ CoinifyTrade.prototype.btcExpected = function () {
CoinifyTrade.prototype.fakeBankTransfer = function () {
var self = this;

var fakeBankTransfer = function () {
return self._coinify.POST('trades/' + self._id + '/test/bank-transfer', {
sendAmount: parseFloat((self.inAmount / 100).toFixed(2)),
currency: self.inCurrency
});
};

if (this._coinify.isLoggedIn) {
return fakeBankTransfer();
} else {
return this._coinify.login().then(fakeBankTransfer);
}
return self._coinify.authPOST('trades/' + self._id + '/test/bank-transfer', {
sendAmount: parseFloat((self.inAmount / 100).toFixed(2)),
currency: self.inCurrency
});
};

// QA tool:
Expand All @@ -299,7 +283,7 @@ CoinifyTrade.buy = function (quote, medium, coinify) {
return coinify.save().then(function () { return trade; });
};

return coinify.POST('trades', {
return coinify.authPOST('trades', {
priceQuoteId: quote.id,
transferIn: {
medium: medium
Expand All @@ -315,47 +299,32 @@ CoinifyTrade.buy = function (quote, medium, coinify) {

// Fetches the latest trades and updates coinify._trades
CoinifyTrade.fetchAll = function (coinify) {
var getTrades = function () {
return coinify.GET('trades').then(function (res) {
var trade;
for (var i = 0; i < res.length; i++) {
trade = undefined;
for (var k = 0; k < coinify._trades.length; k++) {
if (coinify._trades[k]._id === res[i].id) {
trade = coinify._trades[k];
trade.set.bind(trade)(res[i]);
}
}
if (trade === undefined) {
trade = new CoinifyTrade(res[i], coinify);
coinify._trades.push(trade);
}

if (['rejected', 'cancelled', 'expired'].indexOf(trade.state) > -1) {
coinify.delegate.releaseReceiveAddress(trade, CoinifyTrade.filteredTrades(coinify.trades));
return coinify.authGET('trades').then(function (res) {
var trade;
for (var i = 0; i < res.length; i++) {
trade = undefined;
for (var k = 0; k < coinify._trades.length; k++) {
if (coinify._trades[k]._id === res[i].id) {
trade = coinify._trades[k];
trade.set.bind(trade)(res[i]);
}
}
if (trade === undefined) {
trade = new CoinifyTrade(res[i], coinify);
coinify._trades.push(trade);
}

return coinify.save().then(function () { return coinify._trades; });
});
};
if (['rejected', 'cancelled', 'expired'].indexOf(trade.state) > -1) {
coinify.delegate.releaseReceiveAddress(trade, CoinifyTrade.filteredTrades(coinify.trades));
}
}

if (coinify.isLoggedIn) {
return getTrades();
} else {
return coinify.login().then(getTrades);
}
return coinify.save().then(function () { return coinify._trades; });
});
};

CoinifyTrade.prototype.refresh = function () {
var updateTrade = function () {
return this._coinify.GET('trades/' + this._id).then(this.set.bind(this));
};
if (this._coinify.isLoggedIn) {
return updateTrade.bind(this)();
} else {
return this._coinify.login().then(updateTrade.bind(this));
}
return this._coinify.authGET('trades/' + this._id).then(this.set.bind(this));
};

CoinifyTrade.prototype._monitorAddress = function () {
Expand Down