Skip to content

Commit

Permalink
Merge pull request #93 from Money-Manager-SaaS/typeorm
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hub authored Jul 13, 2020
2 parents 09b3e6c + e22a870 commit 3a985c7
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 31 deletions.
5 changes: 0 additions & 5 deletions src/db/faker.ts

This file was deleted.

77 changes: 77 additions & 0 deletions src/services/index.crud.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import theRouter from './index';
import * as supertest from 'supertest';
import * as express from "express";
import { User } from '../entity/User';
import { signAccessToken } from './auth/provider';
import { Ledger } from '../entity/Ledger';
import logger from '../logger';
import { Account } from '../entity/Account';
import * as faker from 'faker';

const app = express();
app.use('/', theRouter);

const entityData = {
description: 'haha optional',
amount: 10,
toAmount: null,
accountID: undefined,
toAccountID: undefined,
categoryID: undefined,
payeeID: undefined,
};

const ledger2Data = {
name: 'ledger2transaction2',
description: 'desp',
userId: null,
};

const accountData = {
name: 'account3',
description: 'haha',
amount: 11,
}

const ledgers = [];
const accounts = [];
const categories = [];
const payees = [];
const transactions = [];


describe("transactions routes basic query parameters", () => {
let token;
let user;
let ledger;

beforeAll(async () => {
// sign user
const UserRepo = User.getRepo();
const users = await UserRepo.find();
user = users[0];
token = signAccessToken(users[0].id, user.email);

// create 1 ledgers
const ledgerRepo = Ledger.getRepo();
ledger = await ledgerRepo.create(ledger2Data);
ledger.user = user;
await ledgerRepo.save(ledger);
});

it('create 2 ledgers ', async () => {
for (let i =0; i< 2; i++) {
const data = {
name: faker.name.title(),
description: faker.random.words(5),
};
const resp = await supertest(app).post('/ledgers')
.send(data)
.set({'Authorization': token}
);
expect(resp.status).toEqual(200);
ledgers.push(resp.body);
logger.debug('create a ledger ' + resp.body.name);
}
});
});
2 changes: 1 addition & 1 deletion src/services/transaction/transRouter.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const accountData = {
}


describe("transactions routes", () => {
describe("transactions routes basic query parameters", () => {
let token;
let user;
let transactions = [];
Expand Down
9 changes: 8 additions & 1 deletion src/services/transaction/transService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Response } from 'express';
import { Transaction as Entity } from '../../entity/Transaction';
import logger from '../../logger';
import { DEFAULT_LIMIT, getFindOption, getQueryOptions } from './utils';
import { getFindOption, getQueryOptions } from './utils';

/**
* there is some limitation here
* lt gt
* dateStart dateEnd cannot be used together as query parameter
* @param req
* @param res
*/
export const getAll = async (req: any, res: Response) => {
const entityRepo = Entity.getRepo();
try {
Expand Down
47 changes: 23 additions & 24 deletions src/services/transaction/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const getFindOption = (body: any, ledgerAccounts: Account[]) => {
logger.debug('the ledger accounts in req');
logger.debug(ledgerAccounts);

const conditions = {
const conditions = [{
accountId: In(ledgerAccounts.map(acc=>acc.id))
};
}];

return {
where: conditions,
Expand All @@ -37,58 +37,58 @@ export const getQueryOptions = (query:any, option:any) => {
}
);
}

const whereConditions = option.where;
const theFirstWhereCondition = whereConditions[0];

if (query.accountID) {
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
accountId: query.accountID
}
)
}
if (query.toAccountID) {
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
toAccountId: query.toAccountID
}
)
}
if (query.categoryID) {
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
categoryId: query.categoryID
}
)
}
if (query.payeeID) {
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
payeeId: query.payeeID
}
)
}
if (query.gt) {
logger.debug('found query gt ')
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
amount: MoreThanOrEqual(query.gt)
}
)
logger.debug(option);
logger.debug(option.where);
logger.debug(theFirstWhereCondition);
}
if (query.lt) {
const whereCondition = {
amount: LessThanOrEqual(query.lt)
};
if ('amount' in option.where) {
option.where.push(whereCondition);
} else {
Object.assign(option.where,
whereCondition
)
}
Object.assign(theFirstWhereCondition,
whereCondition
)
}
if (query.dateStart) {
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
date: MoreThanOrEqual(query.dateStart)
}
Expand All @@ -98,20 +98,19 @@ export const getQueryOptions = (query:any, option:any) => {
const whereCondition = {
date: LessThanOrEqual(query.dateEnd)
};
if ('date' in option.where) {
option.where.push(whereCondition);
} else {
Object.assign(option.where,
whereCondition
)
}
Object.assign(theFirstWhereCondition,
whereCondition
)
}
if (query.description) {
Object.assign(option.where,
Object.assign(theFirstWhereCondition,
{
description: Like(`%${query.description}%`)
}
)
}

whereConditions[0]=theFirstWhereCondition;
option.where=whereConditions;
return option;
}

0 comments on commit 3a985c7

Please sign in to comment.