year & month replaced by yearMonth object

This commit is contained in:
2024-01-09 16:20:49 +01:00
parent 46b65711a8
commit d627ad757d
12 changed files with 82 additions and 64 deletions

View File

@@ -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();

View File

@@ -4,7 +4,7 @@ import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import clientPromise, { getDbClient } from '../dbClient';
import { ObjectId } from 'mongodb';
import { BillingLocation } from '../db-types';
import { BillingLocation, YearMonth } from '../db-types';
import { AuthenticatedUser } from '../types/next-auth';
import { withUser } from '../auth';
@@ -16,14 +16,12 @@ import { withUser } from '../auth';
* @param formData form data
* @returns
*/
export const addMonth = withUser(async (user:AuthenticatedUser, yearString: string, monthString: string) => {
export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }: YearMonth) => {
const { id: userId } = user;
// update the bill in the mongodb
const dbClient = await getDbClient();
const year = parseInt(yearString);
const month = parseInt(monthString);
const prevYear = month === 1 ? year - 1 : year;
const prevMonth = month === 1 ? 12 : month - 1;
@@ -31,8 +29,10 @@ export const addMonth = withUser(async (user:AuthenticatedUser, yearString: stri
// find all locations for the previous month
const prevMonthLocations = await dbClient.collection<BillingLocation>("lokacije").find({
userId, // make sure that the locations belongs to the user
year: prevYear,
month: prevMonth,
yearMonth: {
year: prevYear,
month: prevMonth,
}
});
const newMonthLocationsCursor = prevMonthLocations.map((prevLocation) => {
@@ -41,8 +41,10 @@ export const addMonth = withUser(async (user:AuthenticatedUser, yearString: stri
...prevLocation,
// assign a new ID
_id: (new ObjectId()).toHexString(),
year: year,
month: month,
yearMonth: {
year: year,
month: month,
},
// copy bill array, but set all bills to unpaid and remove attachments and notes
bills: prevLocation.bills.map((bill) => {
return {