Skip to content

Commit

Permalink
Fix mirage transaction routes and convert balance.amount to float
Browse files Browse the repository at this point in the history
  • Loading branch information
pomm0 committed Jun 16, 2020
1 parent b26084d commit 66a22c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/mirage/factories/balance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Factory } from "miragejs"
import { Factory } from 'miragejs';
import faker from 'faker';

export default Factory.extend({
amount: () => faker.finance.amount(),
amount: () => parseFloat(faker.finance.amount())
});
17 changes: 13 additions & 4 deletions src/mirage/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const setupRoutes = (server) => {
// Transaction routes
server.get('/last-transactions', (schema) => {
let allTransactions = schema.transactions.all();
// Sort by newest first TODO: verify
// Sort by newest first
allTransactions = allTransactions.sort((a, b) => {
return a.createdAt > b.createdAt ? -1 : 1;
});
Expand All @@ -44,8 +44,10 @@ export const setupRoutes = (server) => {
server.post('/transactions', (schema, { requestBody }) => {
const model = schema.transactions.create(requestBody);

// Reconvert amount to negative value, as it is an outgoing transaction
model.update({ amount: model.amount * -1 });
model.update({
createdAt: new Date(),
isReceiving: false
});

// TODO: Change serializer to singularize rootKey
return { transaction: model.toJSON() };
Expand All @@ -63,6 +65,13 @@ export const setupRoutes = (server) => {
});

server.get('/transactions', (schema) => {
return schema.transactions.all();
let allTransactions = schema.transactions.all();

// Sort by newest first
allTransactions = allTransactions.sort((a, b) => {
return a.createdAt > b.createdAt ? -1 : 1;
});

return allTransactions;
});
};

0 comments on commit 66a22c2

Please sign in to comment.