multi-user support

This commit is contained in:
2024-01-08 16:32:08 +01:00
parent 9314d78c9c
commit 8a90c58417
9 changed files with 117 additions and 58 deletions

View File

@@ -5,6 +5,8 @@ import { redirect } from 'next/navigation';
import clientPromise from './mongodb';
import { ObjectId } from 'mongodb';
import { BillingLocation } from './db-types';
import { AuthenticatedUser } from './types/next-auth';
import { withUser } from './auth';
/**
* Server-side action which adds a new month to the database
@@ -14,7 +16,8 @@ import { BillingLocation } from './db-types';
* @param formData form data
* @returns
*/
export async function addYearMonth(yearMonthString: string) {
export const addYearMonth = withUser(async (user:AuthenticatedUser, yearMonthString: string) => {
const { id: userId } = user;
// update the bill in the mongodb
const client = await clientPromise;
@@ -24,7 +27,10 @@ export async function addYearMonth(yearMonthString: string) {
const prevYearMonth = (yearMonth - 1) % 100 === 0 ? yearMonth - 89 : yearMonth - 1;
// find all locations for the previous month
const prevMonthLocations = await db.collection<BillingLocation>("lokacije").find({ yearMonth: prevYearMonth });
const prevMonthLocations = await db.collection<BillingLocation>("lokacije").find({
userId, // make sure that the locations belongs to the user
yearMonth: prevYearMonth
});
const newMonthLocationsCursor = prevMonthLocations.map((prevLocation) => {
return({
@@ -52,18 +58,23 @@ export async function addYearMonth(yearMonthString: string) {
revalidatePath('/');
// go to the bill list
redirect('/');
}
});
export async function gotoHome() {
redirect('/');
}
export const fetchBillById = async (locationID:string, billID:string) => {
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
const { id: userId } = user;
const client = await clientPromise;
const db = client.db("rezije");
// find a location with the given locationID
const billLocation = await db.collection<BillingLocation>("lokacije").findOne({ _id: locationID })
const billLocation = await db.collection<BillingLocation>("lokacije").findOne({
_id: locationID,
userId // make sure that the location belongs to the user
})
if(!billLocation) {
console.log(`Location ${locationID} not found`);
@@ -79,16 +90,19 @@ export const fetchBillById = async (locationID:string, billID:string) => {
}
return(bill);
}
})
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
const { id: userId } = user;
export const deleteBillById = async (locationID:string, billID:string) => {
const client = await clientPromise;
const db = client.db("rezije");
// find a location with the given locationID
const post = await db.collection<BillingLocation>("lokacije").updateOne(
{
_id: locationID // find a location with the given locationID
_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
@@ -100,4 +114,4 @@ export const deleteBillById = async (locationID:string, billID:string) => {
});
return(post.modifiedCount);
}
});