mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-24 08:26:13 +01:00
Attach documents to expenses (#64)
* Upload documents to receipts * Improve documents * Make the feature opt-in * Fix file name issue
This commit is contained in:
committed by
GitHub
parent
11d2e298e8
commit
d43e731fe1
@@ -62,6 +62,16 @@ export async function createExpense(
|
||||
},
|
||||
},
|
||||
isReimbursement: expenseFormValues.isReimbursement,
|
||||
documents: {
|
||||
createMany: {
|
||||
data: expenseFormValues.documents.map((doc) => ({
|
||||
id: randomId(),
|
||||
url: doc.url,
|
||||
width: doc.width,
|
||||
height: doc.height,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -159,6 +169,22 @@ export async function updateExpense(
|
||||
),
|
||||
},
|
||||
isReimbursement: expenseFormValues.isReimbursement,
|
||||
documents: {
|
||||
connectOrCreate: expenseFormValues.documents.map((doc) => ({
|
||||
create: doc,
|
||||
where: { id: doc.id },
|
||||
})),
|
||||
deleteMany: existingExpense.documents
|
||||
.filter(
|
||||
(existingDoc) =>
|
||||
!expenseFormValues.documents.some(
|
||||
(doc) => doc.id === existingDoc.id,
|
||||
),
|
||||
)
|
||||
.map((doc) => ({
|
||||
id: doc.id,
|
||||
})),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -231,6 +257,6 @@ export async function getExpense(groupId: string, expenseId: string) {
|
||||
const prisma = await getPrisma()
|
||||
return prisma.expense.findUnique({
|
||||
where: { id: expenseId },
|
||||
include: { paidBy: true, paidFor: true, category: true },
|
||||
include: { paidBy: true, paidFor: true, category: true, documents: true },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,16 +1,37 @@
|
||||
import { z } from 'zod'
|
||||
import { ZodIssueCode, z } from 'zod'
|
||||
|
||||
const envSchema = z.object({
|
||||
POSTGRES_URL_NON_POOLING: z.string().url(),
|
||||
POSTGRES_PRISMA_URL: z.string().url(),
|
||||
NEXT_PUBLIC_BASE_URL: z
|
||||
.string()
|
||||
.optional()
|
||||
.default(
|
||||
process.env.VERCEL_URL
|
||||
? `https://${process.env.VERCEL_URL}`
|
||||
: 'http://localhost:3000',
|
||||
),
|
||||
})
|
||||
const envSchema = z
|
||||
.object({
|
||||
POSTGRES_URL_NON_POOLING: z.string().url(),
|
||||
POSTGRES_PRISMA_URL: z.string().url(),
|
||||
NEXT_PUBLIC_BASE_URL: z
|
||||
.string()
|
||||
.optional()
|
||||
.default(
|
||||
process.env.VERCEL_URL
|
||||
? `https://${process.env.VERCEL_URL}`
|
||||
: 'http://localhost:3000',
|
||||
),
|
||||
NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS: z.coerce.boolean().default(false),
|
||||
S3_UPLOAD_KEY: z.string().optional(),
|
||||
S3_UPLOAD_SECRET: z.string().optional(),
|
||||
S3_UPLOAD_BUCKET: z.string().optional(),
|
||||
S3_UPLOAD_REGION: z.string().optional(),
|
||||
})
|
||||
.superRefine((env, ctx) => {
|
||||
if (
|
||||
env.NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS &&
|
||||
(!env.S3_UPLOAD_BUCKET ||
|
||||
!env.S3_UPLOAD_KEY ||
|
||||
!env.S3_UPLOAD_REGION ||
|
||||
!env.S3_UPLOAD_SECRET)
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: ZodIssueCode.custom,
|
||||
message:
|
||||
'If NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS is specified, then S3_* must be specified too',
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export const env = envSchema.parse(process.env)
|
||||
|
||||
@@ -105,6 +105,16 @@ export const expenseFormSchema = z
|
||||
)
|
||||
.default('EVENLY'),
|
||||
isReimbursement: z.boolean(),
|
||||
documents: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
url: z.string().url(),
|
||||
width: z.number().int().min(1),
|
||||
height: z.number().int().min(1),
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
})
|
||||
.superRefine((expense, ctx) => {
|
||||
let sum = 0
|
||||
|
||||
Reference in New Issue
Block a user