Skip to content

Commit

Permalink
feat: default ledger endpoint for front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hub committed Aug 10, 2020
1 parent 6b32ba3 commit 7a7fa10
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/services/ledger/defaultLedger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* update this for default ledger init
*/
export default {
ledger: {
name: 'Default',
description: 'a default ledger for getting startted',
},
accounts: [
{
name: 'cash account',
description: '',
amount: 0,
},
{
name: 'debit account',
description: '',
amount: 0,
}, {
name: 'credit account',
description: '',
amount: 0,
}
],
categories: [
{
name: 'Food',
description: 'vegetables, fruit, etc',
ledgerID: null,
},
{
name: 'Accommodation',
description: 'renting or housing',
},
{
name: 'Petrol/Gas',
description: '',
},
{
name: 'Investment',
description: '',
},
{
name: 'Salary',
description: 'stable income',
},
{
name: 'Part time wage',
description: 'unstable income',
},
],
payees: [
{
name: 'Employer',
description: 'the Boss',
},
{
name: 'MM-Cloud',
description: 'the best accounting and money tracker SaaS provider',
},
]
}
11 changes: 11 additions & 0 deletions src/services/ledger/ledgerRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getOrmManager } from '../../db/ormManager';
import { User } from '../../entity/User';
import { signAccessToken } from '../auth/provider';
import logger from '../../logger';
import defaultLedger from './defaultLedger';

const app = express();
app.use('/', theRouter);
Expand Down Expand Up @@ -134,4 +135,14 @@ describe("ledger routes", () => {
expect(resp.status).toEqual(204);
});

it('get default one', async () => {
const resp = await supertest(app).post('/default')
.send({})
.set({'Authorization': token}
);
expect(resp.status).toEqual(200);
// todo better test asserting
expect(resp.body.accounts.length).toEqual(defaultLedger.accounts.length)
});

});
1 change: 1 addition & 0 deletions src/services/ledger/ledgerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ router.use(authenticateJWT);

router.post('/', services.createOne);
router.get('/', services.getAll);
router.post('/default/', services.createDefault);
router.put('/:ledgerID', services.updateOne);
router.get('/:ledgerID', services.getOne);
router.delete('/:ledgerID', services.deleteOne);
Expand Down
74 changes: 73 additions & 1 deletion src/services/ledger/ledgerService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Response } from 'express';
import { Ledger } from '../../entity/Ledger';
import { Account } from '../../entity/Account';
import { Payee } from '../../entity/Payee';
import { Category } from '../../entity/Category';
import { getFindOption } from './ledgerUtils';
import defaultLedgerData from './defaultLedger';

export const getAll = async (req: any, res: Response) => {
const ledgerRepo = Ledger.getRepo();
Expand Down Expand Up @@ -74,7 +78,7 @@ export const deleteOne = async (req, res: Response) => {
const ledgerID = req.params?.ledgerID;
const ledgerRepo = Ledger.getRepo();
try {
const ledger = await ledgerRepo.findOne(ledgerID, getFindOption(req.user.id));
const ledger = await ledgerRepo.findOne(ledgerID, getFindOption(req.user.id));
if (ledger && ledger.userId === req.user.id) {
await ledgerRepo.softDelete(ledger.id);
res.status(200).json({});
Expand All @@ -86,3 +90,71 @@ export const deleteOne = async (req, res: Response) => {
res.status(500).end();
}
};

export const createDefault = async (req, res: Response) => {
const ledgerRepo = Ledger.getRepo();
try {
let ledger = await ledgerRepo.create(defaultLedgerData.ledger) as unknown as Ledger;
ledger.user = req.user;
await ledgerRepo.save(ledger);


const calls = [];

const createAccount = async (data) => {
const repo = Account.getRepo();
const item = (await repo.create(data)) as unknown as Account;
item.ledgerId = ledger.id;
await repo.save(item)
}
defaultLedgerData.accounts.forEach(
data => {
calls.push(
createAccount(data)
)
}
)

const createCategory = async (data) => {
const repo = Category.getRepo();
const item = (await repo.create(data)) as unknown as Category;
item.ledgerId = ledger.id;
await repo.save(item)
}
defaultLedgerData.categories.forEach(
data => {
calls.push(
createCategory(data)
)
}
)

const createPayee = async (data) => {
const repo = Payee.getRepo();
const item = (await repo.create(data)) as unknown as Payee;
item.ledgerId = ledger.id;
await repo.save(item)
}
defaultLedgerData.payees.forEach(
data => {
calls.push(
createPayee(data)
)
}
)

await Promise.all(calls)

// todo save this one db hit
ledger = await ledgerRepo.findOne(ledger.id,
Object.assign({}, getFindOption(req.user.id), {
relations: ["accounts", "categories", "payees"],
})
);

res.status(200).send(ledger);
} catch (e) {
console.error(e);
res.status(500).end();
}
};

0 comments on commit 7a7fa10

Please sign in to comment.