Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exclude-own-transactions #410

Merged
merged 1 commit into from
Dec 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/controller/transaction-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export default class TransactionController extends BaseController {
* @param {integer} createdById.query - User that created selected transaction
* @param {integer} toId.query - To-user for selected transactions
* transactions. Requires ContainerId
* @param {integer} excludeById.query - Your own ID to not include in transactions
* @param {integer} pointOfSaleId.query - Point of sale ID for selected transactions
* @param {integer} productId.query - Product ID for selected transactions
* @param {integer} productRevision.query - Product Revision for selected
Expand Down
3 changes: 3 additions & 0 deletions src/service/transaction-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface TransactionFilterParameters {
fromDate?: Date,
tillDate?: Date,
invoiceId?: number,
excludeById?: number,
}

export function parseGetTransactionsFilters(req: RequestWithToken): TransactionFilterParameters {
Expand All @@ -121,6 +122,7 @@ export function parseGetTransactionsFilters(req: RequestWithToken): TransactionF
fromDate: asDate(req.query.fromDate),
tillDate: asDate(req.query.tillDate),
invoiceId: asNumber(req.query.invoiceId),
excludeById: asNumber(req.query.excludeById),
};

return filters;
Expand Down Expand Up @@ -571,6 +573,7 @@ export default class TransactionService extends WithManager {

query.orderBy({ 'transaction.createdAt': 'DESC' });

if (p.excludeById) query.andWhere('createdById != :excludeById', { excludeById: p.excludeById });
if (fromDate) query.andWhere('transaction.createdAt >= :fromDate', { fromDate: toMySQLString(fromDate) });
if (tillDate) query.andWhere('transaction.createdAt < :tillDate', { tillDate: toMySQLString(tillDate) });

Expand Down
31 changes: 30 additions & 1 deletion test/unit/controller/transaction-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { BaseTransactionResponse } from '../../../src/controller/response/transa
import { verifyBaseTransactionEntity } from '../validators';
import RoleManager from '../../../src/rbac/role-manager';
import { TransactionRequest } from '../../../src/controller/request/transaction-request';
import { defaultPagination, PAGINATION_DEFAULT, PaginationResult } from '../../../src/helpers/pagination';
import { defaultPagination, maxPagination, PAGINATION_DEFAULT, PaginationResult } from '../../../src/helpers/pagination';
import { inUserContext, UserFactory } from '../../helpers/user-factory';
import MemberAuthenticator from '../../../src/entity/authenticator/member-authenticator';
import { truncateAllTables } from '../../setup';
Expand Down Expand Up @@ -530,6 +530,35 @@ describe('TransactionController', (): void => {
.query({ skip: 'Wie dit leest trekt een bak' });
expect(res.status).to.equal(400);
});

it('should return all transactions except for the ones createdby a user themself', async () => {
const excludeById = 7;
const take = maxPagination();

const res = await request(ctx.app)
.get('/transactions')
.set('Authorization', `Bearer ${ctx.adminToken}`)
.query({ take, excludeById });
expect(res.status).to.equal(200);

const actualTransactions = ctx.transactions
.filter((transactions) => transactions.createdBy.id != excludeById);

const transactions = res.body.records as BaseTransactionResponse[];
const spec = await Swagger.importSpecification();
expect(transactions.length).to.equal(actualTransactions.length);
transactions.forEach((transaction: BaseTransactionResponse) => {
verifyBaseTransactionEntity(spec, transaction);
});
});

it('should return 400 when excludeById is not a number', async () => {
const res = await request(ctx.app)
.get('/transactions')
.set('Authorization', `Bearer ${ctx.adminToken}`)
.query({ skip: '42 is erg Vo' });
expect(res.status).to.equal(400);
});
});

describe('GET /transactions/{id}', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/service/transaction-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,15 @@ describe('TransactionService', (): void => {

expect(records.length).to.equal(0);
});

it('should not return transactions createdBy given user', async () => {
const transaction = ctx.transactions[0];

const records = (await new TransactionService().getTransactions({ excludeById: transaction.createdBy.id })).records;
records.forEach((r) => {
expect(r.createdBy).to.not.eq(transaction.createdBy.id);
});
});
});

describe('Get all transactions involving a user', () => {
Expand Down
Loading