refactoring: actions moved to seprate dir
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { fetchBillById } from '@/app/lib/billActions';
|
import { fetchBillById } from '@/app/lib/actions/billActions';
|
||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
export async function GET(request: Request, { params:{ id } }: { params: { id:string } }) {
|
export async function GET(request: Request, { params:{ id } }: { params: { id:string } }) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { deleteBillById } from '@/app/lib/billActions';
|
import { deleteBillById } from '@/app/lib/actions/billActions';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
import { notFound, redirect } from 'next/navigation';
|
import { notFound, redirect } from 'next/navigation';
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { BillingLocation, Bill } from '@/app/lib/db-types';
|
import { BillingLocation, Bill } from '@/app/lib/db-types';
|
||||||
import { fetchBillById } from '@/app/lib/billActions';
|
import { fetchBillById } from '@/app/lib/actions/billActions';
|
||||||
import clientPromise from '@/app/lib/mongodb';
|
import clientPromise from '@/app/lib/mongodb';
|
||||||
import { BillEditForm } from '@/app/ui/BillEditForm';
|
import { BillEditForm } from '@/app/ui/BillEditForm';
|
||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import clientPromise from './mongodb';
|
import clientPromise, { getDbClient } from '../dbClient';
|
||||||
import { BillAttachment, BillingLocation } from './db-types';
|
import { BillAttachment, BillingLocation } from '../db-types';
|
||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { withUser } from '@/app/lib/auth';
|
import { withUser } from '@/app/lib/auth';
|
||||||
import { AuthenticatedUser } from './types/next-auth';
|
import { AuthenticatedUser } from '../types/next-auth';
|
||||||
|
|
||||||
export type State = {
|
export type State = {
|
||||||
errors?: {
|
errors?: {
|
||||||
@@ -140,8 +140,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
|||||||
const billPaid = formData.get('billPaid') === 'on';
|
const billPaid = formData.get('billPaid') === 'on';
|
||||||
|
|
||||||
// update the bill in the mongodb
|
// update the bill in the mongodb
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
const billAttachment = await serializeAttachment(formData.get('billAttachment') as File);
|
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
|
// 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
|
_id: locationId, // find a location with the given locationID
|
||||||
userId // make sure that the location belongs to the user
|
userId // make sure that the location belongs to the user
|
||||||
@@ -177,7 +176,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// find a location with the given locationID
|
// 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
|
_id: locationId, // find a location with the given locationID
|
||||||
userId // make sure that the location belongs to the user
|
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 { id: userId } = user;
|
||||||
|
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
// find a location with the given locationID
|
// 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) {
|
if(!billLocation) {
|
||||||
console.log(`Location ${locationID} not found`);
|
console.log(`Location ${locationID} not found`);
|
||||||
@@ -237,11 +235,10 @@ export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID
|
|||||||
|
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
// find a location with the given locationID
|
// 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
|
_id: locationID, // find a location with the given locationID
|
||||||
userId // make sure that the location belongs to the user
|
userId // make sure that the location belongs to the user
|
||||||
@@ -3,11 +3,11 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import clientPromise from './mongodb';
|
import clientPromise, { getDbClient } from '../dbClient';
|
||||||
import { BillingLocation } from './db-types';
|
import { BillingLocation } from '../db-types';
|
||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { auth, withUser } from '@/app/lib/auth';
|
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';
|
import { NormalizedRouteManifest } from 'next/dist/server/base-server';
|
||||||
|
|
||||||
export type State = {
|
export type State = {
|
||||||
@@ -54,13 +54,12 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
|
|||||||
} = validatedFields.data;
|
} = validatedFields.data;
|
||||||
|
|
||||||
// update the bill in the mongodb
|
// update the bill in the mongodb
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
const { id: userId, email: userEmail } = user;
|
const { id: userId, email: userEmail } = user;
|
||||||
|
|
||||||
if(locationId) {
|
if(locationId) {
|
||||||
await db.collection<BillingLocation>("lokacije").updateOne(
|
await dbClient.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 the location belongs to the user
|
userId // make sure the location belongs to the user
|
||||||
@@ -72,7 +71,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if(yearMonth) {
|
} else if(yearMonth) {
|
||||||
await db.collection<BillingLocation>("lokacije").insertOne({
|
await dbClient.collection<BillingLocation>("lokacije").insertOne({
|
||||||
_id: (new ObjectId()).toHexString(),
|
_id: (new ObjectId()).toHexString(),
|
||||||
userId,
|
userId,
|
||||||
userEmail,
|
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 dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
// fetch `pageSize` locations for the given page index
|
// fetch `pageSize` locations for the given page index
|
||||||
const locations = await db.collection<BillingLocation>("lokacije")
|
const locations = await dbClient.collection<BillingLocation>("lokacije")
|
||||||
.find({ userId })
|
.find({ userId })
|
||||||
.sort({ name: 1 })
|
.sort({ yearMonth: -1, name: 1 })
|
||||||
.skip(pageIx * pageSize)
|
.skip(pageIx * pageSize)
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.toArray();
|
.toArray();
|
||||||
@@ -110,13 +108,12 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, pageIx:
|
|||||||
|
|
||||||
export const fetchLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => {
|
export const fetchLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => {
|
||||||
|
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
// find a location with the given locationID
|
// 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) {
|
if(!billLocation) {
|
||||||
console.log(`Location ${locationID} not found`);
|
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) => {
|
export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => {
|
||||||
|
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
// find a location with the given locationID
|
// 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);
|
return(post.deletedCount);
|
||||||
})
|
})
|
||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import clientPromise from './mongodb';
|
import clientPromise, { getDbClient } from '../dbClient';
|
||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { BillingLocation } from './db-types';
|
import { BillingLocation } from '../db-types';
|
||||||
import { AuthenticatedUser } from './types/next-auth';
|
import { AuthenticatedUser } from '../types/next-auth';
|
||||||
import { withUser } from './auth';
|
import { withUser } from '../auth';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Server-side action which adds a new month to the database
|
* 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;
|
const { id: userId } = user;
|
||||||
|
|
||||||
// update the bill in the mongodb
|
// update the bill in the mongodb
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
const yearMonth = parseInt(yearMonthString);
|
const yearMonth = parseInt(yearMonthString);
|
||||||
|
|
||||||
const prevYearMonth = (yearMonth - 1) % 100 === 0 ? yearMonth - 89 : yearMonth - 1;
|
const prevYearMonth = (yearMonth - 1) % 100 === 0 ? yearMonth - 89 : yearMonth - 1;
|
||||||
|
|
||||||
// find all locations for the previous month
|
// 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
|
userId, // make sure that the locations belongs to the user
|
||||||
yearMonth: prevYearMonth
|
yearMonth: prevYearMonth
|
||||||
});
|
});
|
||||||
@@ -52,7 +52,7 @@ export const addYearMonth = withUser(async (user:AuthenticatedUser, yearMonthStr
|
|||||||
});
|
});
|
||||||
|
|
||||||
const newMonthLocations = await newMonthLocationsCursor.toArray()
|
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
|
// clear the cache for the path
|
||||||
revalidatePath('/');
|
revalidatePath('/');
|
||||||
@@ -67,11 +67,10 @@ export async function gotoHome() {
|
|||||||
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
|
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
// find a location with the given locationID
|
// 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,
|
_id: locationID,
|
||||||
userId // make sure that the location belongs to the user
|
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) => {
|
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
const client = await clientPromise;
|
const dbClient = await getDbClient();
|
||||||
const db = client.db("rezije");
|
|
||||||
|
|
||||||
// find a location with the given locationID
|
// 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
|
_id: locationID, // find a location with the given locationID
|
||||||
userId // make sure that the location belongs to the user
|
userId // make sure that the location belongs to the user
|
||||||
@@ -32,3 +32,9 @@ if (process.env.NODE_ENV === 'development') {
|
|||||||
// Export a module-scoped MongoClient promise. By doing this in a
|
// Export a module-scoped MongoClient promise. By doing this in a
|
||||||
// separate module, the client can be shared across functions.
|
// 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);
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { deleteBillById } from '@/app/lib/billActions';
|
import { deleteBillById } from '@/app/lib/actions/billActions';
|
||||||
import { deleteLocationById } from '@/app/lib/locationActions';
|
import { deleteLocationById } from '@/app/lib/actions/locationActions';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
import { notFound, redirect } from 'next/navigation';
|
import { notFound, redirect } from 'next/navigation';
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { BillingLocation, Bill } from '@/app/lib/db-types';
|
import { BillingLocation, Bill } from '@/app/lib/db-types';
|
||||||
import { fetchBillById } from '@/app/lib/billActions';
|
import { fetchBillById } from '@/app/lib/actions/billActions';
|
||||||
import clientPromise from '@/app/lib/mongodb';
|
import clientPromise from '@/app/lib/mongodb';
|
||||||
import { BillEditForm } from '@/app/ui/BillEditForm';
|
import { BillEditForm } from '@/app/ui/BillEditForm';
|
||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
import { LocationEditForm } from '@/app/ui/LocationEditForm';
|
import { LocationEditForm } from '@/app/ui/LocationEditForm';
|
||||||
import { fetchLocationById } from '@/app/lib/locationActions';
|
import { fetchLocationById } from '@/app/lib/actions/locationActions';
|
||||||
|
|
||||||
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { AddMonthButton } from './ui/AddMonthButton';
|
|||||||
import { AddLocationButton } from './ui/AddLocationButton';
|
import { AddLocationButton } from './ui/AddLocationButton';
|
||||||
import { PageFooter } from './ui/PageFooter';
|
import { PageFooter } from './ui/PageFooter';
|
||||||
import { isAuthErrorMessage } from '@/app/lib/auth';
|
import { isAuthErrorMessage } from '@/app/lib/auth';
|
||||||
import { fetchAllLocations } from './lib/locationActions';
|
import { fetchAllLocations } from './lib/actions/locationActions';
|
||||||
import { formatCurrency } from './lib/formatStrings';
|
import { formatCurrency } from './lib/formatStrings';
|
||||||
|
|
||||||
const getNextYearMonth = (yearMonth:number) => {
|
const getNextYearMonth = (yearMonth:number) => {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { DocumentIcon, TrashIcon } from "@heroicons/react/24/outline";
|
|||||||
import { Bill } from "../lib/db-types";
|
import { Bill } from "../lib/db-types";
|
||||||
import React, { FC } from "react";
|
import React, { FC } from "react";
|
||||||
import { useFormState } from "react-dom";
|
import { useFormState } from "react-dom";
|
||||||
import { gotoHome, updateOrAddBill } from "../lib/billActions";
|
import { gotoHome, updateOrAddBill } from "../lib/actions/billActions";
|
||||||
|
|
||||||
// Next.js does not encode an utf-8 file name correctly when sending a form with a file attachment
|
// Next.js does not encode an utf-8 file name correctly when sending a form with a file attachment
|
||||||
// This is a workaround for that
|
// This is a workaround for that
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
import { TrashIcon } from "@heroicons/react/24/outline";
|
import { TrashIcon } from "@heroicons/react/24/outline";
|
||||||
import { FC } from "react";
|
import { FC } from "react";
|
||||||
import { BillingLocation } from "../lib/db-types";
|
import { BillingLocation } from "../lib/db-types";
|
||||||
import { updateOrAddLocation } from "../lib/locationActions";
|
import { updateOrAddLocation } from "../lib/actions/locationActions";
|
||||||
import { useFormState } from "react-dom";
|
import { useFormState } from "react-dom";
|
||||||
import { gotoHome } from "../lib/billActions";
|
import { gotoHome } from "../lib/actions/billActions";
|
||||||
|
|
||||||
export interface LocationEditFormProps {
|
export interface LocationEditFormProps {
|
||||||
/** location which should be edited */
|
/** location which should be edited */
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { addYearMonth } from '@/app/lib/yearMonthActions';
|
import { addYearMonth } from '@/app/lib/actions/yearMonthActions';
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user