forked from balanced/balanced-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes balanced#1631: Always credit from orders
- Loading branch information
Showing
6 changed files
with
142 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 10 additions & 25 deletions
35
app/models/factories/credit-existing-funding-instrument-transaction-factory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import TransactionFactory from "./transaction-factory"; | ||
import Credit from "../credit"; | ||
|
||
var CreditOrderFactory = TransactionFactory.extend({ | ||
save: function() { | ||
var self = this; | ||
var order = this.get("order"); | ||
console.log(order); | ||
// TODO: check if order.seller == customer | ||
var seller = this.get("order.seller"); | ||
|
||
var deferred = Ember.RSVP.defer(); | ||
this.validate(); | ||
|
||
var getErrorMessage = function(error) { | ||
return Ember.isBlank(error.additional) ? | ||
error.description : | ||
error.additional; | ||
}; | ||
|
||
if (this.get("isValid")) { | ||
self.getDestination(seller) | ||
.then(function(destination) { | ||
return self.createCredit(destination, order); | ||
}) | ||
.then(function(credit) { | ||
deferred.resolve(credit); | ||
}) | ||
.catch(function(response) { | ||
response.errors.forEach(function(error) { | ||
if (error.extras) { | ||
_.each(error.extras, function(value, key) { | ||
self.get("validationErrors").add(key, "server", null, value); | ||
}); | ||
} | ||
self.get("validationErrors").add(undefined, "server", null, getErrorMessage(error)); | ||
}); | ||
deferred.reject(self); | ||
}); | ||
} else { | ||
deferred.reject(); | ||
} | ||
|
||
return deferred.promise; | ||
}, | ||
|
||
getDestination: function(/* seller */) { | ||
Ember.assert("Implement #getDestination and make it return a promise with the destination", false); | ||
}, | ||
|
||
getCreditAttributes: function() { | ||
var properties = this.getProperties("amount", "appears_on_statement_as"); | ||
properties.description = this.get("credit_description"); | ||
|
||
return properties; | ||
}, | ||
|
||
createCredit: function(destination, order) { | ||
var destinationUri = destination.get("uri"); | ||
var creditsUri = destination.get("credits_uri"); | ||
|
||
var creditAttributes = _.extend({}, this.getCreditAttributes(), { | ||
uri: creditsUri, | ||
destination_uri: destinationUri, | ||
order_uri: order.get("uri") | ||
}); | ||
|
||
return Credit.create(creditAttributes).save(); | ||
} | ||
}); | ||
|
||
export default CreditOrderFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters