Skip to content

Commit

Permalink
fix: grant related fixes (#1960)
Browse files Browse the repository at this point in the history
* fix: fix grant continuation fetch

* fix: move finalizationReason addition to separate migration

* chore: lint

* feat: check for column existence before migrating

* feat: update migration to use enum update
  • Loading branch information
mkurapov authored Sep 26, 2023
1 parent 6a2ae15 commit 7eedb1e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const grantsTable = 'grants'
const finalizationReasonColumn = 'finalizationReason'
const finalzationReasonType = 'finalization_reason_type'
const finalizationReasonList = ['ISSUED', 'REVOKED', 'REJECTED']

const toPsqlList = (list) => `(${list.map((el) => `'${el}'`).join(', ')})`

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema
.hasColumn(grantsTable, finalizationReasonColumn)
.then((exists) => {
if (!exists) {
return knex.schema.alterTable(grantsTable, (table) => {
table.enum(finalizationReasonColumn, finalizationReasonList, {
useNative: true,
enumName: finalzationReasonType
})
})
}

return knex.schema.raw(
`CREATE TYPE ${finalzationReasonType} AS ENUM ${toPsqlList(
finalizationReasonList
)};
ALTER TABLE "${grantsTable}" ALTER COLUMN "${finalizationReasonColumn}" TYPE ${finalzationReasonType} USING "${finalizationReasonColumn}"::${finalzationReasonType}`
)
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema
.hasColumn(grantsTable, finalizationReasonColumn)
.then((exists) => {
if (exists) {
return knex.schema
.alterTable(grantsTable, (table) => {
table.dropColumn(finalizationReasonColumn)
})
.then(() => {
return knex.schema.raw(
`DROP TYPE IF EXISTS ${finalzationReasonType}`
)
})
}
})
}
48 changes: 48 additions & 0 deletions packages/auth/src/grant/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,54 @@ describe('Grant Service', (): void => {
expect(fetchedGrant?.continueId).toEqual(continueId)
expect(fetchedGrant?.continueToken).toEqual(continueToken)
})

test('properly fetches grant by continuation information with multiple existing grants', async (): Promise<void> => {
const grantRequest: GrantRequest = {
...BASE_GRANT_REQUEST,
access_token: {
access: [
{
...BASE_GRANT_ACCESS,
type: AccessType.IncomingPayment
}
]
},
interact: undefined
}

const grant1 = await grantService.create(grantRequest)
await grant1
.$query()
.patch({ finalizationReason: GrantFinalization.Issued })

const grant2 = await grantService.create(grantRequest)
const grant3 = await grantService.create(grantRequest)
await grant3
.$query()
.patch({ finalizationReason: GrantFinalization.Revoked })

const fetchedGrant1 = await grantService.getByContinue(
grant1.continueId,
grant1.continueToken
)

expect(fetchedGrant1?.id).toEqual(grant1.id)
expect(fetchedGrant1?.continueId).toEqual(grant1.continueId)
expect(fetchedGrant1?.continueToken).toEqual(grant1.continueToken)

const fetchedGrant2 = await grantService.getByContinue(
grant2.continueId,
grant2.continueToken
)

expect(fetchedGrant2?.id).toEqual(grant2.id)
expect(fetchedGrant2?.continueId).toEqual(grant2.continueId)
expect(fetchedGrant2?.continueToken).toEqual(grant2.continueToken)

await expect(
grantService.getByContinue(grant3.continueId, grant3.continueToken)
).resolves.toBeUndefined()
})
})

describe('getByIdWithAccess', (): void => {
Expand Down
7 changes: 4 additions & 3 deletions packages/auth/src/grant/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ async function getByContinue(
.withGraphFetched('interaction')

if (!includeRevoked) {
queryBuilder
.whereNull('finalizationReason')
.orWhereNot('finalizationReason', GrantFinalization.Revoked)
queryBuilder.andWhere((queryBuilder) => {
queryBuilder.whereNull('finalizationReason')
queryBuilder.orWhereNot('finalizationReason', GrantFinalization.Revoked)
})
}

const grant = await queryBuilder
Expand Down

0 comments on commit 7eedb1e

Please sign in to comment.