Skip to content

Commit

Permalink
feat(api): add wallet service (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Jun 6, 2024
1 parent e546860 commit e984afb
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
105 changes: 105 additions & 0 deletions apps/api/v1/services/wallet.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import prisma from '@/lib/prisma'
import type { User, UserWalletAccount } from '@prisma/client'
import type { CreateWallet, UpdateWallet } from '../validation'

export async function findUserWallet({
user,
walletId,
}: {
user: User
walletId: string
}) {
return await prisma.userWalletAccount.findUnique({
where: { id: walletId, userId: user.id },
})
}

export async function createWallet({
user,
data: { name, icon, description, lastDigits, preferredCurrency },
}: {
user: User
data: CreateWallet
}) {
return await prisma.userWalletAccount.create({
data: {
userId: user.id,
name,
icon,
description,
lastDigits,
preferredCurrency,
},
})
}

export async function updateWallet({
walletId,
data,
}: {
walletId: string
data: UpdateWallet
}) {
return await prisma.userWalletAccount.update({
where: { id: walletId },
data,
})
}

export async function deleteWallet({
walletId,
}: {
walletId: string
}) {
return await prisma.userWalletAccount.delete({
where: { id: walletId },
})
}

export async function findUserWallets({ user }: { user: User }) {
return await prisma.userWalletAccount.findMany({
where: { userId: user.id },
})
}

export async function getWalletBalance({
wallet,
}: { wallet: UserWalletAccount }) {
const balance = await prisma.transaction.aggregate({
where: { walletAccountId: wallet.id },
_sum: { amount: true },
})

return balance._sum.amount || 0
}

// biome-ignore lint/correctness/noUnusedVariables: <explanation>
export async function canUserCreateWallet({ user }: { user: User }) {
return true
}

export async function canUserUpdateWallet({
user,
walletId,
}: {
user: User
walletId: string
}) {
const wallet = await findUserWallet({ user, walletId })
return !!wallet
}

export async function canUserDeleteWallet({
user,
walletId,
}: {
user: User
walletId: string
}) {
return await canUserUpdateWallet({ user, walletId })
}

export async function walletWithBalance(wallet: UserWalletAccount) {
const balance = await getWalletBalance({ wallet })
return { ...wallet, balance }
}
1 change: 1 addition & 0 deletions apps/api/v1/validation/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './auth.zod'
export * from './user.zod'
export * from './wallet.zod'
19 changes: 19 additions & 0 deletions apps/api/v1/validation/wallet.zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { z } from 'zod'

export const zCreateWallet = z.object({
name: z.string(),
icon: z.string().optional(),
description: z.string().optional(),
lastDigits: z.string().optional(),
preferredCurrency: z.string(),
})
export type CreateWallet = z.infer<typeof zCreateWallet>

export const zUpdateWallet = z.object({
name: z.string().optional(),
icon: z.string().optional(),
description: z.string().optional(),
lastDigits: z.string().optional(),
preferredCurrency: z.string().optional(),
})
export type UpdateWallet = z.infer<typeof zUpdateWallet>

0 comments on commit e984afb

Please sign in to comment.