Skip to content
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
10 changes: 8 additions & 2 deletions packages/db/src/wallet/wallet-delete.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { tryCatch } from '@workspace/core/try-catch'

import { accountFindMany } from '../account/account-find-many.ts'
import type { Database } from '../database.ts'

export async function walletDelete(db: Database, id: string): Promise<void> {
return db.transaction('rw', db.wallets, async () => {
return db.transaction('rw', db.accounts, db.wallets, async () => {
const accounts = await accountFindMany(db, { walletId: id })
const { error: errorAccounts } = await tryCatch(db.accounts.bulkDelete(accounts.map((account) => account.id)))
if (errorAccounts) {
console.log(errorAccounts)
throw new Error(`Error deleting accounts for wallet with id ${id}`)
}
const { data, error } = await tryCatch(db.wallets.delete(id))
if (error) {
console.log(error)
Expand Down
22 changes: 20 additions & 2 deletions packages/db/test/wallet-delete.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { PromiseExtended } from 'dexie'

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { accountCreate } from '../src/account/account-create.ts'
import { accountFindUnique } from '../src/account/account-find-unique.ts'
import { walletCreate } from '../src/wallet/wallet-create.ts'
import { walletDelete } from '../src/wallet/wallet-delete.ts'
import { walletFindUnique } from '../src/wallet/wallet-find-unique.ts'
import { createDbTest, testWalletCreateInput } from './test-helpers.ts'
import { createDbTest, testAccountCreateInput, testWalletCreateInput } from './test-helpers.ts'

const db = createDbTest()

Expand All @@ -28,6 +29,23 @@ describe('wallet-delete', () => {
const deletedItem = await walletFindUnique(db, id)
expect(deletedItem).toBeNull()
})

it('should delete the accounts in a wallet', async () => {
// ARRANGE
expect.assertions(2)
const input = testWalletCreateInput()
const id = await walletCreate(db, input)
const accountId = await accountCreate(db, testAccountCreateInput({ walletId: id }))

// ACT
await walletDelete(db, id)

// ASSERT
const deletedWallet = await walletFindUnique(db, id)
const deletedAccount = await accountFindUnique(db, accountId)
expect(deletedWallet).toBeNull()
expect(deletedAccount).toBeNull()
})
})

describe('unexpected behavior', () => {
Expand Down
Loading