refactoring: actions moved to seprate dir
This commit is contained in:
@@ -3,11 +3,11 @@
|
||||
import { z } from 'zod';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import clientPromise from './mongodb';
|
||||
import { BillAttachment, BillingLocation } from './db-types';
|
||||
import clientPromise, { getDbClient } from '../dbClient';
|
||||
import { BillAttachment, BillingLocation } from '../db-types';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { withUser } from '@/app/lib/auth';
|
||||
import { AuthenticatedUser } from './types/next-auth';
|
||||
import { AuthenticatedUser } from '../types/next-auth';
|
||||
|
||||
export type State = {
|
||||
errors?: {
|
||||
@@ -140,8 +140,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
const billPaid = formData.get('billPaid') === 'on';
|
||||
|
||||
// update the bill in the mongodb
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const billAttachment = await serializeAttachment(formData.get('billAttachment') as File);
|
||||
|
||||
@@ -163,7 +162,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
};
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
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
|
||||
@@ -177,7 +176,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
||||
});
|
||||
} else {
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
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
|
||||
@@ -211,11 +210,10 @@ export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
// find a location with the given locationID
|
||||
const billLocation = await db.collection<BillingLocation>("lokacije").findOne({ _id: locationID, userId })
|
||||
const billLocation = await dbClient.collection<BillingLocation>("lokacije").findOne({ _id: locationID, userId })
|
||||
|
||||
if(!billLocation) {
|
||||
console.log(`Location ${locationID} not found`);
|
||||
@@ -237,11 +235,10 @@ export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
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
|
||||
@@ -3,11 +3,11 @@
|
||||
import { z } from 'zod';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import clientPromise from './mongodb';
|
||||
import { BillingLocation } from './db-types';
|
||||
import clientPromise, { getDbClient } from '../dbClient';
|
||||
import { BillingLocation } from '../db-types';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { auth, withUser } from '@/app/lib/auth';
|
||||
import { AuthenticatedUser } from './types/next-auth';
|
||||
import { AuthenticatedUser } from '../types/next-auth';
|
||||
import { NormalizedRouteManifest } from 'next/dist/server/base-server';
|
||||
|
||||
export type State = {
|
||||
@@ -54,13 +54,12 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
|
||||
} = validatedFields.data;
|
||||
|
||||
// update the bill in the mongodb
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const { id: userId, email: userEmail } = user;
|
||||
|
||||
if(locationId) {
|
||||
await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
await dbClient.collection<BillingLocation>("lokacije").updateOne(
|
||||
{
|
||||
_id: locationId, // find a location with the given locationID
|
||||
userId // make sure the location belongs to the user
|
||||
@@ -72,7 +71,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
|
||||
}
|
||||
});
|
||||
} else if(yearMonth) {
|
||||
await db.collection<BillingLocation>("lokacije").insertOne({
|
||||
await dbClient.collection<BillingLocation>("lokacije").insertOne({
|
||||
_id: (new ObjectId()).toHexString(),
|
||||
userId,
|
||||
userEmail,
|
||||
@@ -90,17 +89,16 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
|
||||
});
|
||||
|
||||
|
||||
export const fetchAllLocations = withUser(async (user:AuthenticatedUser, pageIx:number=0, pageSize:number=20) => {
|
||||
export const fetchAllLocations = withUser(async (user:AuthenticatedUser, pageIx:number=0, pageSize:number=2000) => {
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
// fetch `pageSize` locations for the given page index
|
||||
const locations = await db.collection<BillingLocation>("lokacije")
|
||||
const locations = await dbClient.collection<BillingLocation>("lokacije")
|
||||
.find({ userId })
|
||||
.sort({ name: 1 })
|
||||
.sort({ yearMonth: -1, name: 1 })
|
||||
.skip(pageIx * pageSize)
|
||||
.limit(pageSize)
|
||||
.toArray();
|
||||
@@ -110,13 +108,12 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, pageIx:
|
||||
|
||||
export const fetchLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => {
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
// find a location with the given locationID
|
||||
const billLocation = await db.collection<BillingLocation>("lokacije").findOne({ _id: locationID, userId});
|
||||
const billLocation = await dbClient.collection<BillingLocation>("lokacije").findOne({ _id: locationID, userId});
|
||||
|
||||
if(!billLocation) {
|
||||
console.log(`Location ${locationID} not found`);
|
||||
@@ -128,12 +125,12 @@ export const fetchLocationById = withUser(async (user:AuthenticatedUser, locatio
|
||||
|
||||
export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => {
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const { id: userId } = user;
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<BillingLocation>("lokacije").deleteOne({ _id: locationID, userId });
|
||||
const post = await dbClient.collection<BillingLocation>("lokacije").deleteOne({ _id: locationID, userId });
|
||||
|
||||
return(post.deletedCount);
|
||||
})
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import clientPromise from './mongodb';
|
||||
import clientPromise, { getDbClient } from '../dbClient';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { BillingLocation } from './db-types';
|
||||
import { AuthenticatedUser } from './types/next-auth';
|
||||
import { withUser } from './auth';
|
||||
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
|
||||
@@ -20,14 +20,14 @@ export const addYearMonth = withUser(async (user:AuthenticatedUser, yearMonthStr
|
||||
const { id: userId } = user;
|
||||
|
||||
// update the bill in the mongodb
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const yearMonth = parseInt(yearMonthString);
|
||||
|
||||
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({
|
||||
const prevMonthLocations = await dbClient.collection<BillingLocation>("lokacije").find({
|
||||
userId, // make sure that the locations belongs to the user
|
||||
yearMonth: prevYearMonth
|
||||
});
|
||||
@@ -52,7 +52,7 @@ export const addYearMonth = withUser(async (user:AuthenticatedUser, yearMonthStr
|
||||
});
|
||||
|
||||
const newMonthLocations = await newMonthLocationsCursor.toArray()
|
||||
await db.collection<BillingLocation>("lokacije").insertMany(newMonthLocations);
|
||||
await dbClient.collection<BillingLocation>("lokacije").insertMany(newMonthLocations);
|
||||
|
||||
// clear the cache for the path
|
||||
revalidatePath('/');
|
||||
@@ -67,11 +67,10 @@ export async function gotoHome() {
|
||||
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
|
||||
const { id: userId } = user;
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
// find a location with the given locationID
|
||||
const billLocation = await db.collection<BillingLocation>("lokacije").findOne({
|
||||
const billLocation = await dbClient.collection<BillingLocation>("lokacije").findOne({
|
||||
_id: locationID,
|
||||
userId // make sure that the location belongs to the user
|
||||
})
|
||||
@@ -95,11 +94,10 @@ export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:
|
||||
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
|
||||
const { id: userId } = user;
|
||||
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
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
|
||||
@@ -31,4 +31,10 @@ if (process.env.NODE_ENV === 'development') {
|
||||
|
||||
// Export a module-scoped MongoClient promise. By doing this in a
|
||||
// separate module, the client can be shared across functions.
|
||||
export default clientPromise
|
||||
export default clientPromise
|
||||
|
||||
export const getDbClient = async () => {
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
return(db);
|
||||
}
|
||||
Reference in New Issue
Block a user