-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-actions.js
66 lines (61 loc) · 2.23 KB
/
server-actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use server'
import { addDoc, collection, deleteDoc, doc, getDoc, getDocs, query, setDoc, where } from "@firebase/firestore";
import { db } from "./firebase";
import { redirect } from "next/navigation";
import { revalidatePath } from "next/cache";
import { cookies } from "next/headers";
import { verify } from "jsonwebtoken";
import { COLLECTIONS } from "./lib/config";
import { getDataFromQuerySnapshot } from "./lib/utils";
export async function addExpense(uid, trxnId, formData){
const transactionDetails = {
expenseName: formData.get('expenseName'),
currency: formData.get('currency'),
amount: formData.get('amount'),
type: formData.get('type'),
dot: formData.get('dot'),
category: formData.get('category'),
description: formData.get('description'),
paymentMethod: formData.get('payment-method'),
uid
};
const transactionCollection = collection(db, COLLECTIONS.TRANSACTIONS);
trxnId ? await setDoc(doc(db, COLLECTIONS.TRANSACTIONS, trxnId), transactionDetails) : await addDoc(transactionCollection, transactionDetails);
revalidatePath('/', 'layout');
redirect('/all-transactions');
}
export async function deleteExpense(trxnId){
const token = cookies().get("token");
if (!token) {
redirect("/login");
}
let { email, userId } = verify(token.value, process.env.NEXT_PUBLIC_JWT_SECRET);
let usersSnapshot = await getDocs(
query(collection(db, COLLECTIONS.USERS), where("email", "==", email))
);
let users = getDataFromQuerySnapshot(usersSnapshot);
if (users.length === 0) {
throw new Error("User not found!");
}
const user = users[0];
let transactionSnapshot = await getDoc(doc(db, COLLECTIONS.TRANSACTIONS, trxnId));
let reqTransaction = {
id: transactionSnapshot.id,
...transactionSnapshot.data()
}
if(reqTransaction.uid !== user.id){
throw new Error("Transaction does not belong to the user or this transaction/user does not exist.");
}
await deleteDoc(doc(db, COLLECTIONS.TRANSACTIONS, reqTransaction.id));
revalidatePath('/')
}
export async function fetchTransaction(trxnId){
const tSnapshot = await getDoc(doc(db, COLLECTIONS.TRANSACTIONS, trxnId));
if(!tSnapshot.exists()){
return null;
}
return {
id: tSnapshot.id,
...tSnapshot.data(),
}
}