Skip to content

Commit

Permalink
feat(api): include blobAttachments to transaction services (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Sep 12, 2024
1 parent 8c437aa commit 954337f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
5 changes: 3 additions & 2 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ const router = new Hono()
const logger = getLogger(`${c.req.method} ${c.req.path}`)
const user = getAuthUserStrict(c)
const data = c.req.valid('json')
const { budgetId, walletAccountId: walletId, categoryId } = data
const { budgetId, walletAccountId: walletId, categoryId, date } = data

logger.debug('Creating transaction %o', data)

const budget = budgetId
? await findBudget({ budgetId, anchorDate: data.date })
? await findBudget({ budgetId, anchorDate: date })
: null
if (budgetId && (!budget || !(await canUserReadBudget({ user, budget })))) {
logger.error(`Budget not found or user doesn't have read access %o`, {
Expand Down Expand Up @@ -289,6 +289,7 @@ const router = new Hono()
const transactionData = await generateTransactionDataFromFile({
file,
categories: userCategories,
performUser: user,
})
return c.json(transactionData)
} catch {
Expand Down
5 changes: 4 additions & 1 deletion apps/api/v1/services/ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CategoryType } from '@prisma/client'
import type { CategoryType, User } from '@prisma/client'
import { OpenAI } from 'openai'
import { getLogger } from '../../lib/log'
import { createAiCache, findAiCacheByQuery } from './ai-cache.service'
Expand Down Expand Up @@ -79,6 +79,7 @@ export async function generateTransactionDataFromFile({
file: inputFile,
noteLanguage = 'English',
categories = [],
performUser,
}: {
file: File
noteLanguage?: string
Expand All @@ -88,6 +89,7 @@ export async function generateTransactionDataFromFile({
icon?: string | null
type?: CategoryType
}[]
performUser?: User
}) {
const log = getLogger(`ai.service:${generateTransactionDataFromFile.name}`)
const additionalInstructions = generateAdditionalInstruction({
Expand Down Expand Up @@ -117,6 +119,7 @@ export async function generateTransactionDataFromFile({
putBlobObject({
file: inputFile,
pathname: blobObjectPathname,
uploadedUser: performUser,
}),
])

Expand Down
40 changes: 14 additions & 26 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,14 @@ export async function findTransaction({
transactionId: string
}) {
return prisma.transaction.findUnique({
where: {
id: transactionId,
},
include: {
category: true,
},
where: { id: transactionId },
include: { category: true, blobAttachments: true },
})
}

export async function createTransaction({
user,
data,
data: { blobAttachmentIds, ...data },
}: {
user: User
data: CreateTransaction
Expand Down Expand Up @@ -149,18 +145,19 @@ export async function createTransaction({
...data,
amountInVnd,
createdByUserId: user.id,
blobAttachments: {
connect: blobAttachmentIds?.map((id) => ({ id })),
},
},
include: {
category: true,
},
include: { category: true, blobAttachments: true },
})

return transaction
}

export async function updateTransaction({
transactionId,
data,
data: { blobAttachmentIds, ...data },
}: {
transactionId: string
data: UpdateTransaction
Expand Down Expand Up @@ -203,16 +200,13 @@ export async function updateTransaction({
}

transaction = await prisma.transaction.update({
where: {
id: transactionId,
},
where: { id: transactionId },
data: {
...data,
amountInVnd,
blobAttachments: { set: blobAttachmentIds?.map((id) => ({ id })) },
},
include: {
category: true,
},
include: { category: true, blobAttachments: true },
})

return transaction
Expand All @@ -224,9 +218,7 @@ export async function deleteTransaction({
transactionId: string
}) {
await prisma.transaction.delete({
where: {
id: transactionId,
},
where: { id: transactionId },
})
}

Expand Down Expand Up @@ -258,13 +250,9 @@ export async function listTransactions({
}),
},
},
orderBy: {
date: 'desc',
},
orderBy: { date: 'desc' },
take: pagination.first || pagination.last,
include: {
category: true,
},
include: { category: true, blobAttachments: true },
})

const totalCount = await prisma.transaction.count({
Expand Down
2 changes: 2 additions & 0 deletions packages/validation/src/transaction.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const zCreateTransaction = z.object({
budgetId: z.string().nullable().optional(),
walletAccountId: z.string(),
categoryId: z.string().nullable().optional(),
blobAttachmentIds: z.array(z.string()).optional(),
})
export type CreateTransaction = z.infer<typeof zCreateTransaction>

Expand All @@ -22,6 +23,7 @@ export const zUpdateTransaction = z.object({
budgetId: z.string().nullable().optional(),
walletAccountId: z.string().optional(),
categoryId: z.string().nullable().optional(),
blobAttachmentIds: z.array(z.string()).optional(),
})
export type UpdateTransaction = z.infer<typeof zUpdateTransaction>

Expand Down

0 comments on commit 954337f

Please sign in to comment.