Skip to content

Commit

Permalink
refactor: test with rollback and fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Branje <[email protected]>
  • Loading branch information
jeroenbranje committed Jan 17, 2024
1 parent 58c7e3e commit 5c72c31
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
6 changes: 4 additions & 2 deletions apps/envited.ascs.digital/app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { db } from '../../../common/database/queries'
import { ok } from '../../../common/utils'
import { internalServerError, ok } from '../../../common/utils'

export async function POST(request: Request) {
try {
const credential = await request.json()

const connection = await db()
const newUser = await connection.insertUserTx(credential)

return ok(newUser)
} catch (error) {
console.log('error', error)
return Response.json(error)

return internalServerError()
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/envited.ascs.digital/common/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const initDb =
config = {
host,
port,
database: dbname as string,
database: dbname,
username,
password,
max: 1,
Expand Down
32 changes: 32 additions & 0 deletions apps/envited.ascs.digital/common/database/queries/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,38 @@ describe('common/database/users', () => {
})
})

it('should rollback a user with all the relational', async () => {
// when ... we want to insert and get an rejected user
// then ... we should get the rollback as expected
const dependencies = {
insertAddressTypeTx: () => jest.fn().mockResolvedValue([{ id: 'ADDRESS_TYPE_ID' }]),
insertIssuerTx: () => jest.fn().mockResolvedValue([{ id: 'ISSUER_ID' }]),
insertUsersToRolesTx: () => jest.fn().mockResolvedValue([{ id: 'ISSUER_ID' }]),
insertCredentialTypeTx: () => jest.fn().mockResolvedValue([{ id: 'CREDENTIAL_TYPE_ID' }]),
} as any

const tx = {
insert: jest.fn().mockReturnValue({
values: jest.fn().mockReturnValue({
onConflictDoNothing: jest.fn().mockReturnValue({
returning: jest.fn().mockResolvedValue({}),
}),
returning: jest.fn().mockRejectedValue('ERROR'),
}),
}),
select: jest.fn().mockReturnValue({
from: jest.fn().mockReturnValue({
where: jest.fn().mockReturnValue([{ id: 'ROLE_ID' }]),
}),
}),
rollback: jest.fn().mockResolvedValue({}),
}

await _txn(dependencies)(USER_CREDENTIAL)(tx as any)

expect(tx.rollback).toHaveBeenCalled()
})

describe('_insertUserTx', () => {
it('should insert a user with all the relational connections', async () => {
// when ... we want to connect a user to a role
Expand Down
1 change: 0 additions & 1 deletion apps/envited.ascs.digital/common/database/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export const _txn =
} catch (error) {
console.log(error)
tx.rollback()
throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/envited.ascs.digital/common/database/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StringChange } from '@nx/devkit'
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js'

import * as schema from './schema'
import { StringChange } from '@nx/devkit'

export type DatabaseConnection = PostgresJsDatabase<typeof schema>

Expand Down

0 comments on commit 5c72c31

Please sign in to comment.