Skip to content

Commit

Permalink
Add Status parameter to GET Transactions (#56)
Browse files Browse the repository at this point in the history
* Additional query added

* Test error

* Remove second query

* Put second query back

* Update query functions

* Add another test

* Version bump

* Another test

* Add additional checks

* Use same function test

* Reuse function

* Change function

* Change db function

* Remove unused functions

* Version bump
  • Loading branch information
nitro-marky authored Apr 12, 2023
1 parent 8e8c9e2 commit 78d72b3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/dscp-matchmaker-api",
"version": "0.6.0",
"version": "0.6.1",
"description": "An OpenAPI Matchmaking API service for DSCP",
"main": "src/index.ts",
"scripts": {
Expand Down
12 changes: 7 additions & 5 deletions src/controllers/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '../../lib/logger'
import Database from '../../lib/db'
import { UUID } from '../../models/uuid'
import { BadRequest, NotFound } from '../../lib/error-handler/index'
import { TransactionApiType, TransactionResponse } from '../../models/transaction'
import { TransactionApiType, TransactionResponse, TransactionState } from '../../models/transaction'

@Route('transaction')
@Tags('transaction')
Expand All @@ -25,11 +25,13 @@ export class TransactionController extends Controller {
* @Query apiType lists all transactions by that type
*/
@Response<BadRequest>(400, 'Request was invalid')
@Response<NotFound>(404, 'Item not found')
@Get('/')
public async getAllTransactions(@Query() apiType?: TransactionApiType): Promise<TransactionResponse[]> {
if (apiType) return await this.db.getTransactionsByType(apiType)

return await this.db.getTransactions()
public async getAllTransactions(
@Query() apiType?: TransactionApiType,
@Query() status?: TransactionState
): Promise<TransactionResponse[]> {
return await this.db.getTransactions(status, apiType)
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/lib/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { pgConfig } from './knexfile'
import { DemandState, DemandSubtype } from '../../models/demand'
import { UUID } from '../../models/uuid'
import { Match2State } from '../../models/match2'
import { TransactionApiType, TransactionType } from '../../models/transaction'
import { TransactionApiType, TransactionState, TransactionType } from '../../models/transaction'

const tablesList = ['attachment', 'demand', 'transaction', 'match2', 'processed_blocks'] as const
type TABLES_TUPLE = typeof tablesList
Expand Down Expand Up @@ -120,12 +120,11 @@ export default class Database {
return this.db().transaction().select(transactionColumns).where({ id })
}

getTransactions = async () => {
return this.db().transaction().select(transactionColumns)
}

getTransactionsByType = async (apiType: TransactionApiType) => {
return this.db().transaction().where({ api_type: apiType }).select(transactionColumns)
getTransactions = async (state?: TransactionState, api_type?: TransactionApiType) => {
return this.db()
.transaction()
.where({ ...(state && { state }), ...(api_type && { api_type }) })
.select(transactionColumns)
}

getTransactionsByLocalId = async (local_id: UUID, transaction_type: TransactionType) => {
Expand Down
74 changes: 73 additions & 1 deletion test/integration/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('transaction', () => {
])
})

it('returns all transactions by type', async () => {
it('returns transactions by type', async () => {
const { status, body } = await get(app, '/transaction?apiType=capacity')

expect(status).to.equal(200)
Expand All @@ -158,4 +158,76 @@ describe('transaction', () => {
}
])
})

it('returns transactions by status', async () => {
const { status, body } = await get(app, '/transaction?status=submitted')

expect(status).to.equal(200)
expect(body).to.deep.include.members([
{
id: '1f3af974-7d4d-40b4-86a5-94a2241265cb',
state: 'submitted',
localId: '0f5af074-7d4d-40b4-86a5-17a2391303cb',
apiType: 'capacity',
transactionType: 'creation',
submittedAt: '2023-03-24T10:40:47.317Z',
updatedAt: '2023-03-24T10:40:47.317Z'
},
{
id: 'd65d8e11-150f-4ea4-b778-b920e9dbc378',
state: 'submitted',
localId: '0f5af074-7d4d-40b4-86a5-17a2391303cb',
apiType: 'capacity',
transactionType: 'creation',
submittedAt: '2023-03-24T10:40:47.317Z',
updatedAt: '2023-03-24T10:40:47.317Z'
}
])
})

it('returns transactions by status and type', async () => {
const { status, body } = await get(app, '/transaction?apiType=capacity&status=submitted')

expect(status).to.equal(200)
expect(body).to.deep.include.members([
{
id: '1f3af974-7d4d-40b4-86a5-94a2241265cb',
state: 'submitted',
localId: '0f5af074-7d4d-40b4-86a5-17a2391303cb',
apiType: 'capacity',
transactionType: 'creation',
submittedAt: '2023-03-24T10:40:47.317Z',
updatedAt: '2023-03-24T10:40:47.317Z'
},
{
id: 'd65d8e11-150f-4ea4-b778-b920e9dbc378',
state: 'submitted',
localId: '0f5af074-7d4d-40b4-86a5-17a2391303cb',
apiType: 'capacity',
transactionType: 'creation',
submittedAt: '2023-03-24T10:40:47.317Z',
updatedAt: '2023-03-24T10:40:47.317Z'
}
])
})

it('returns 422 when invalid type is passed', async () => {
const { status, body } = await get(app, '/transaction?apiType=notAType&status=submitted')

expect(status).to.equal(422)
expect(body).to.contain({
name: 'ValidateError',
message: 'Validation failed'
})
})

it('returns 422 when invalid status is passed', async () => {
const { status, body } = await get(app, '/transaction?apiType=capacity&status=notAStatus')

expect(status).to.equal(422)
expect(body).to.contain({
name: 'ValidateError',
message: 'Validation failed'
})
})
})

0 comments on commit 78d72b3

Please sign in to comment.