|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import prismadb from "@/lib/prismadb"; |
| 4 | +import { auth, currentUser } from "@clerk/nextjs/server"; |
| 5 | + |
| 6 | +export async function updatePasswordItem( |
| 7 | + id: string, |
| 8 | + newUsername: string, |
| 9 | + newWebsite: string, |
| 10 | + newPassword: string |
| 11 | +) { |
| 12 | + const clerkUser = await currentUser(); |
| 13 | + |
| 14 | + const user = await prismadb.user.findUnique({ |
| 15 | + where: { |
| 16 | + id: clerkUser?.id, |
| 17 | + }, |
| 18 | + include: { |
| 19 | + passwordItems: true, |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + if (!user) { |
| 24 | + throw new Error("User not found"); |
| 25 | + } |
| 26 | + |
| 27 | + const passwordItem = user.passwordItems.find((item) => item.id === id); |
| 28 | + |
| 29 | + if (!passwordItem) { |
| 30 | + throw new Error("Password item not found"); |
| 31 | + } |
| 32 | + |
| 33 | + await prismadb.passwordItem.update({ |
| 34 | + where: { |
| 35 | + id: passwordItem.id, |
| 36 | + }, |
| 37 | + data: { |
| 38 | + username: newUsername, |
| 39 | + website: newWebsite, |
| 40 | + password: newPassword, |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + return { success: true }; |
| 45 | +} |
| 46 | + |
| 47 | +export async function deletePasswordItem(id: string) { |
| 48 | + const clerkUser = await currentUser(); |
| 49 | + |
| 50 | + const user = await prismadb.user.findUnique({ |
| 51 | + where: { |
| 52 | + id: clerkUser?.id, |
| 53 | + }, |
| 54 | + include: { |
| 55 | + passwordItems: true, |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + if (!user) { |
| 60 | + throw new Error("User not found"); |
| 61 | + } |
| 62 | + |
| 63 | + const passwordItem = user.passwordItems.find((item) => item.id === id); |
| 64 | + |
| 65 | + if (!passwordItem) { |
| 66 | + throw new Error("Password item not found"); |
| 67 | + } |
| 68 | + |
| 69 | + await prismadb.passwordItem.delete({ |
| 70 | + where: { |
| 71 | + id: passwordItem.id, |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + return { success: true }; |
| 76 | +} |
| 77 | + |
| 78 | +export async function createPasswordItem( |
| 79 | + username: string, |
| 80 | + website: string, |
| 81 | + password: string |
| 82 | +) { |
| 83 | + const clerkUser = await currentUser(); |
| 84 | + |
| 85 | + if (!clerkUser) { |
| 86 | + throw new Error("User not authenticated"); |
| 87 | + } |
| 88 | + |
| 89 | + const user = await prismadb.user.findUnique({ |
| 90 | + where: { |
| 91 | + id: clerkUser.id, |
| 92 | + }, |
| 93 | + include: { |
| 94 | + passwordItems: true, |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + if (!user) { |
| 99 | + throw new Error("User not found"); |
| 100 | + } |
| 101 | + |
| 102 | + const newPasswordItem = await prismadb.passwordItem.create({ |
| 103 | + data: { |
| 104 | + username, |
| 105 | + website, |
| 106 | + password, |
| 107 | + updatedAt: new Date().toISOString(), |
| 108 | + createdAt: new Date().toISOString(), |
| 109 | + userId: user.id, |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + return newPasswordItem; |
| 114 | +} |
| 115 | + |
| 116 | +export async function instantiateVault() { |
| 117 | + const clerkUser = await currentUser(); |
| 118 | + |
| 119 | + const vault = await prismadb.user.create({ |
| 120 | + data: { |
| 121 | + id: clerkUser?.id, |
| 122 | + username: clerkUser?.username!, |
| 123 | + }, |
| 124 | + include: { |
| 125 | + passwordItems: true, |
| 126 | + cardItems: true, |
| 127 | + pinItems: true, |
| 128 | + noteItems: true, |
| 129 | + }, |
| 130 | + }); |
| 131 | + |
| 132 | + return vault; |
| 133 | +} |
0 commit comments