added payedAmount
This commit is contained in:
@@ -6,7 +6,7 @@ import { redirect } from 'next/navigation';
|
||||
import clientPromise from './mongodb';
|
||||
import { BillAttachment, BillingLocation } from './db-types';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { auth, withUser } from '@/app/lib/auth';
|
||||
import { withUser } from '@/app/lib/auth';
|
||||
import { AuthenticatedUser } from './types/next-auth';
|
||||
|
||||
export type State = {
|
||||
@@ -14,6 +14,7 @@ export type State = {
|
||||
billName?: string[];
|
||||
billAttachment?: string[],
|
||||
billNotes?: string[],
|
||||
payedAmount?: string[],
|
||||
};
|
||||
message?:string | null;
|
||||
}
|
||||
@@ -22,8 +23,47 @@ const FormSchema = z.object({
|
||||
_id: z.string(),
|
||||
billName: z.coerce.string().min(1, "Bill Name is required."),
|
||||
billNotes: z.string(),
|
||||
payedAmount: z.string().nullable().transform((val, ctx) => {
|
||||
|
||||
if(!val || val === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = parseFloat(val.replace(',', '.'));
|
||||
|
||||
if (isNaN(parsed)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Not a number",
|
||||
});
|
||||
|
||||
// This is a special symbol you can use to
|
||||
// return early from the transform function.
|
||||
// It has type `never` so it does not affect the
|
||||
// inferred return type.
|
||||
return z.NEVER;
|
||||
}
|
||||
|
||||
if (parsed < 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Value must be a positive number",
|
||||
});
|
||||
|
||||
// This is a special symbol you can use to
|
||||
// return early from the transform function.
|
||||
// It has type `never` so it does not affect the
|
||||
// inferred return type.
|
||||
return z.NEVER;
|
||||
}
|
||||
|
||||
return Math.floor(parsed * 100); // value is stored in cents
|
||||
|
||||
}),
|
||||
});
|
||||
|
||||
parseFloat
|
||||
|
||||
const UpdateBill = FormSchema.omit({ _id: true });
|
||||
|
||||
/**
|
||||
@@ -52,7 +92,6 @@ const serializeAttachment = async (billAttachment: File | null) => {
|
||||
const fileContents = await billAttachment.arrayBuffer();
|
||||
const fileContentsBase64 = Buffer.from(fileContents).toString('base64');
|
||||
|
||||
|
||||
// create an object to store the file in the database
|
||||
return({
|
||||
fileName,
|
||||
@@ -75,9 +114,12 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
const x = formData.get('payedAmount');
|
||||
|
||||
const validatedFields = UpdateBill.safeParse({
|
||||
billName: formData.get('billName'),
|
||||
billNotes: formData.get('billNotes'),
|
||||
payedAmount: formData.get('payedAmount'),
|
||||
});
|
||||
|
||||
// If form validation fails, return errors early. Otherwise, continue...
|
||||
@@ -92,6 +134,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
const {
|
||||
billName,
|
||||
billNotes,
|
||||
payedAmount,
|
||||
} = validatedFields.data;
|
||||
|
||||
const billPaid = formData.get('billPaid') === 'on';
|
||||
@@ -102,20 +145,23 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
|
||||
const billAttachment = await serializeAttachment(formData.get('billAttachment') as File);
|
||||
|
||||
// if there is an attachment, update the attachment field
|
||||
// otherwise, do not update the attachment field
|
||||
const mongoDbSet = billAttachment ? {
|
||||
"bills.$[elem].name": billName,
|
||||
"bills.$[elem].paid": billPaid,
|
||||
"bills.$[elem].attachment": billAttachment,
|
||||
"bills.$[elem].notes": billNotes,
|
||||
}: {
|
||||
"bills.$[elem].name": billName,
|
||||
"bills.$[elem].paid": billPaid,
|
||||
"bills.$[elem].notes": billNotes,
|
||||
};
|
||||
|
||||
if(billId) {
|
||||
|
||||
// if there is an attachment, update the attachment field
|
||||
// otherwise, do not update the attachment field
|
||||
const mongoDbSet = billAttachment ? {
|
||||
"bills.$[elem].name": billName,
|
||||
"bills.$[elem].paid": billPaid,
|
||||
"bills.$[elem].attachment": billAttachment,
|
||||
"bills.$[elem].notes": billNotes,
|
||||
"bills.$[elem].payedAmount": payedAmount,
|
||||
}: {
|
||||
"bills.$[elem].name": billName,
|
||||
"bills.$[elem].paid": billPaid,
|
||||
"bills.$[elem].notes": billNotes,
|
||||
"bills.$[elem].payedAmount": payedAmount,
|
||||
};
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
{
|
||||
@@ -144,6 +190,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
paid: billPaid,
|
||||
attachment: billAttachment,
|
||||
notes: billNotes,
|
||||
payedAmount
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user