Skip to content

Commit

Permalink
feat(api): includes wallet and budget to transaction queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdev98 committed Sep 21, 2024
1 parent dc1d49e commit e1c6a39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 12 additions & 4 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CreateTransaction, UpdateTransaction } from '@6pm/validation'
import type {
Budget,
Prisma,
Transaction,
User,
UserWalletAccount,
Expand All @@ -16,6 +17,13 @@ import { getExchangeRate } from './exchange-rates.service'

const VND = 'VND'

const TRANSACTION_INCLUDE: Prisma.TransactionInclude = {
category: true,
blobAttachments: true,
walletAccount: true,
budget: true,
}

export async function canUserCreateTransaction({
user,
budget,
Expand Down Expand Up @@ -110,7 +118,7 @@ export async function findTransaction({
}) {
return prisma.transaction.findUnique({
where: { id: transactionId },
include: { category: true, blobAttachments: true },
include: TRANSACTION_INCLUDE,
})
}

Expand Down Expand Up @@ -149,7 +157,7 @@ export async function createTransaction({
connect: blobAttachmentIds?.map((id) => ({ id })),
},
},
include: { category: true, blobAttachments: true },
include: TRANSACTION_INCLUDE,
})

return transaction
Expand Down Expand Up @@ -206,7 +214,7 @@ export async function updateTransaction({
amountInVnd,
blobAttachments: { set: blobAttachmentIds?.map((id) => ({ id })) },
},
include: { category: true, blobAttachments: true },
include: TRANSACTION_INCLUDE,
})

return transaction
Expand Down Expand Up @@ -252,7 +260,7 @@ export async function listTransactions({
},
orderBy: { date: 'desc' },
take: pagination.first || pagination.last,
include: { category: true, blobAttachments: true },
include: TRANSACTION_INCLUDE,
})

const totalCount = await prisma.transaction.count({
Expand Down
11 changes: 10 additions & 1 deletion packages/validation/src/transaction.zod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { z } from 'zod'
import { CategorySchema, TransactionSchema } from './prisma'
import {
BlobObjectSchema,
BudgetSchema,
CategorySchema,
TransactionSchema,
UserWalletAccountSchema,
} from './prisma'

export const zCreateTransaction = z.object({
id: z.string().optional(),
Expand Down Expand Up @@ -36,6 +42,9 @@ export const TransactionPopulatedSchema = TransactionSchema.extend({
})
.nullable()
.optional(),
budget: BudgetSchema.nullable().optional(),
walletAccount: UserWalletAccountSchema.nullable().optional(),
blobAttachments: z.array(BlobObjectSchema).nullable().optional(),
amount: z.number({ coerce: true }),
amountInVnd: z.number({ coerce: true }),
})
Expand Down

0 comments on commit e1c6a39

Please sign in to comment.