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);
|
||||
}
|
||||
Reference in New Issue
Block a user