yearMonth split into year + month

This commit is contained in:
2024-01-09 15:43:01 +01:00
parent 90edcf14e1
commit 46b65711a8
12 changed files with 73 additions and 88 deletions

View File

@@ -33,7 +33,7 @@ const UpdateLocation = FormSchema.omit({ _id: true });
* @param formData form data
* @returns
*/
export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locationId?: string, yearMonth?: string, prevState:State, formData: FormData) => {
export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locationId?: string, year?: string, month?: string, prevState:State, formData: FormData) => {
const validatedFields = UpdateLocation.safeParse({
locationName: formData.get('locationName'),
@@ -70,14 +70,15 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
notes: locationNotes,
}
});
} else if(yearMonth) {
} else if(year && month) {
await dbClient.collection<BillingLocation>("lokacije").insertOne({
_id: (new ObjectId()).toHexString(),
userId,
userEmail,
name: locationName,
notes: locationNotes,
yearMonth: parseInt(yearMonth), // ToDo: get the current year and month
year: parseInt(year), // ToDo: get the current year and month
month: parseInt(month), // ToDo: get the current year and month
bills: [],
});
}
@@ -98,7 +99,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({ yearMonth: -1, name: 1 })
.sort({ year: -1, month: -1, name: 1 })
.skip(pageIx * pageSize)
.limit(pageSize)
.toArray();

View File

@@ -16,20 +16,23 @@ import { withUser } from '../auth';
* @param formData form data
* @returns
*/
export const addYearMonth = withUser(async (user:AuthenticatedUser, yearMonthString: string) => {
export const addMonth = withUser(async (user:AuthenticatedUser, yearString: string, monthString: string) => {
const { id: userId } = user;
// update the bill in the mongodb
const dbClient = await getDbClient();
const yearMonth = parseInt(yearMonthString);
const year = parseInt(yearString);
const month = parseInt(monthString);
const prevYearMonth = (yearMonth - 1) % 100 === 0 ? yearMonth - 89 : yearMonth - 1;
const prevYear = month === 1 ? year - 1 : year;
const prevMonth = month === 1 ? 12 : month - 1;
// 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
yearMonth: prevYearMonth
year: prevYear,
month: prevMonth,
});
const newMonthLocationsCursor = prevMonthLocations.map((prevLocation) => {
@@ -38,7 +41,8 @@ export const addYearMonth = withUser(async (user:AuthenticatedUser, yearMonthStr
...prevLocation,
// assign a new ID
_id: (new ObjectId()).toHexString(),
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 {
@@ -64,52 +68,13 @@ export async function gotoHome() {
redirect('/');
}
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
export const fetchAvailableYears = withUser(async (user:AuthenticatedUser) => {
const { id: userId } = user;
const dbClient = await getDbClient();
// find a location with the given locationID
const billLocation = await dbClient.collection<BillingLocation>("lokacije").findOne({
_id: locationID,
userId // make sure that the location belongs to the user
})
// query mnogodb for all `yearMonth` values
const yearMonths = await dbClient.collection<BillingLocation>("lokacije").distinct("year", { userId });
if(!billLocation) {
console.log(`Location ${locationID} not found`);
return(null);
}
// find a bill with the given billID
const bill = billLocation?.bills.find(({ _id }) => _id.toString() === billID);
if(!bill) {
console.log('Bill not found');
return(null);
}
return(bill);
return(yearMonths);
})
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
const { id: userId } = user;
const dbClient = await getDbClient();
// find a location with the given locationID
const post = await dbClient.collection<BillingLocation>("lokacije").updateOne(
{
_id: locationID, // find a location with the given locationID
userId // make sure that the location belongs to the user
},
{
// remove the bill with the given billID
$pull: {
bills: {
_id: billID
}
}
});
return(post.modifiedCount);
});