implemented bill deletion + var rename
This commit is contained in:
@@ -4,8 +4,7 @@ import { z } from 'zod';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import clientPromise from './mongodb';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { BillAttachment } from './db-types';
|
||||
import { BillAttachment, Bill } from './db-types';
|
||||
|
||||
export type State = {
|
||||
errors?: {
|
||||
@@ -113,7 +112,7 @@ export async function updateBill(locationId: string, billId:string, prevState:St
|
||||
|
||||
|
||||
// find a location with the given locationID
|
||||
const post = await db.collection<Location>("lokacije").updateOne(
|
||||
const post = await db.collection<BillingLocation>("lokacije").updateOne(
|
||||
{
|
||||
_id: locationId // find a location with the given locationID
|
||||
},
|
||||
@@ -121,7 +120,7 @@ export async function updateBill(locationId: string, billId:string, prevState:St
|
||||
$set: mongoDbSet
|
||||
}, {
|
||||
arrayFilters: [
|
||||
{ "elem._id": { $eq: new ObjectId(billId) } } // find a bill with the given billID
|
||||
{ "elem._id": { $eq: billId } } // find a bill with the given billID
|
||||
]
|
||||
});
|
||||
|
||||
@@ -133,4 +132,54 @@ export async function updateBill(locationId: string, billId:string, prevState:St
|
||||
|
||||
export async function gotoHome() {
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
export const fetchBillById = async (locationID:string, billID:string) => {
|
||||
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 })
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const { _id, ...billBase } = Bill;
|
||||
|
||||
return({
|
||||
id: _id.toString(),
|
||||
...billBase
|
||||
} as Bill);
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
},
|
||||
{
|
||||
// remove the bill with the given billID
|
||||
$pull: {
|
||||
bills: {
|
||||
_id: billID
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return(post.modifiedCount);
|
||||
}
|
||||
@@ -8,42 +8,20 @@ export interface BillAttachment {
|
||||
fileContentsBase64: string;
|
||||
};
|
||||
|
||||
/** Bill basic data */
|
||||
export interface LocationBase {
|
||||
/** bill object in the form returned by MongoDB */
|
||||
export interface BillingLocation {
|
||||
_id: string;
|
||||
name: string;
|
||||
/** the value is encoded as yyyymm (i.e. 202301) */
|
||||
yearMonth: number;
|
||||
bills: Bill[];
|
||||
};
|
||||
|
||||
|
||||
/** bill object in the form returned by MongoDB */
|
||||
export interface MongoLocation {
|
||||
_id: ObjectId;
|
||||
bills: MongoBill[];
|
||||
};
|
||||
|
||||
/** plain-object Location version */
|
||||
export interface PlainLocation {
|
||||
id: string;
|
||||
bills: PlainBill[];
|
||||
};
|
||||
|
||||
|
||||
/** Bill basic data */
|
||||
export interface BillBase {
|
||||
export interface Bill {
|
||||
_id: string;
|
||||
name: string;
|
||||
paid: boolean;
|
||||
attachment?: BillAttachment|null;
|
||||
notes?: string|null;
|
||||
};
|
||||
|
||||
/** bill object in the form returned by MongoDB */
|
||||
export interface MongoBill extends BillBase {
|
||||
_id: ObjectId;
|
||||
};
|
||||
|
||||
|
||||
/** plain-object bill version */
|
||||
export interface PlainBill extends BillBase {
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
@@ -1,31 +1,2 @@
|
||||
import { PlainBill, MongoLocation } from '@/app/lib/db-types';
|
||||
import clientPromise from '@/app/lib/mongodb';
|
||||
|
||||
export const fetchBillById = async (locationID:string, billID:string) => {
|
||||
const client = await clientPromise;
|
||||
const db = client.db("rezije");
|
||||
|
||||
// find a location with the given locationID
|
||||
const billLocation = await db.collection<MongoLocation>("lokacije").findOne({ _id: locationID })
|
||||
|
||||
if(!billLocation) {
|
||||
console.log(`Location ${locationID} not found`);
|
||||
return(null);
|
||||
}
|
||||
|
||||
// find a bill with the given billID
|
||||
const mongoBill = billLocation?.bills.find(({ _id }) => _id.toString() === billID);
|
||||
|
||||
if(!mongoBill) {
|
||||
console.log('Bill not found');
|
||||
return(null);
|
||||
}
|
||||
|
||||
const { _id, ...billBase } = mongoBill;
|
||||
|
||||
return({
|
||||
id: _id.toString(),
|
||||
...billBase
|
||||
} as PlainBill);
|
||||
|
||||
}
|
||||
import { Bill, BillingLocation } from '@/app/lib/db-types';
|
||||
import clientPromise from '@/app/lib/mongodb';
|
||||
Reference in New Issue
Block a user