year & month replaced by yearMonth object
This commit is contained in:
@@ -4,7 +4,7 @@ import { z } from 'zod';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import clientPromise, { getDbClient } from '../dbClient';
|
||||
import { BillingLocation } from '../db-types';
|
||||
import { BillingLocation, YearMonth } from '../db-types';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { auth, withUser } from '@/app/lib/auth';
|
||||
import { AuthenticatedUser } from '../types/next-auth';
|
||||
@@ -33,7 +33,7 @@ const UpdateLocation = FormSchema.omit({ _id: true });
|
||||
* @param formData form data
|
||||
* @returns
|
||||
*/
|
||||
export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locationId?: string, year?: string, month?: string, prevState:State, formData: FormData) => {
|
||||
export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locationId?: string, yearMonth?: YearMonth, prevState:State, formData: FormData) => {
|
||||
|
||||
const validatedFields = UpdateLocation.safeParse({
|
||||
locationName: formData.get('locationName'),
|
||||
@@ -70,15 +70,14 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
|
||||
notes: locationNotes,
|
||||
}
|
||||
});
|
||||
} else if(year && month) {
|
||||
} else if(yearMonth) {
|
||||
await dbClient.collection<BillingLocation>("lokacije").insertOne({
|
||||
_id: (new ObjectId()).toHexString(),
|
||||
userId,
|
||||
userEmail,
|
||||
name: locationName,
|
||||
notes: locationNotes,
|
||||
year: parseInt(year), // ToDo: get the current year and month
|
||||
month: parseInt(month), // ToDo: get the current year and month
|
||||
yearMonth: yearMonth,
|
||||
bills: [],
|
||||
});
|
||||
}
|
||||
@@ -99,7 +98,7 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, pageIx:
|
||||
// fetch `pageSize` locations for the given page index
|
||||
const locations = await dbClient.collection<BillingLocation>("lokacije")
|
||||
.find({ userId })
|
||||
.sort({ year: -1, month: -1, name: 1 })
|
||||
.sort({ yearMonth: -1, name: 1 })
|
||||
.skip(pageIx * pageSize)
|
||||
.limit(pageSize)
|
||||
.toArray();
|
||||
|
||||
Reference in New Issue
Block a user