-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |