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 { 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 {