Spaces:
Sleeping
Sleeping
File size: 4,378 Bytes
5ec491a a1c5622 71679ee 009c95b 5ec491a a1c5622 5ec491a 009c95b 4af6326 009c95b 5ec491a 4af6326 5ec491a a1c5622 5ec491a a1c5622 5ec491a 009c95b a1c5622 5ec491a 009c95b a1c5622 5ec491a a1c5622 5ec491a 71679ee 5ec491a a1c5622 009c95b 5ec491a a1c5622 5ec491a 71679ee 009c95b 71679ee 5ec491a a1c5622 5ec491a a1c5622 5ec491a a1c5622 5ec491a a1c5622 5ec491a a1c5622 5ec491a 92f037b 5ec491a 71679ee 5ec491a 71679ee 009c95b 71679ee 5ec491a |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
'use server';
import { sessionUser } from '@/auth';
import prisma from './prisma';
import {
ChatWithMessages,
MessageAssistantResponse,
MessageUserInput,
} from '../types';
import { revalidatePath } from 'next/cache';
import { Chat } from '@prisma/client';
async function getUserConnect() {
const { id: userId } = await sessionUser();
return userId
? {
user: {
connect: { id: userId }, // Connect the chat to an existing user
},
}
: {};
}
/**
* Finds or creates a user in the database based on the provided email and name.
* If the user already exists, it returns the existing user.
* If the user doesn't exist, it creates a new user and returns it.
*
* @param email - The email of the user.
* @param name - The name of the user.
* @returns A promise that resolves to the user object.
*/
export async function dbFindOrCreateUser(email: string, name: string) {
// Try to find the user by email
const user = await prisma.user.findUnique({
where: { email: email },
});
// If the user doesn't exist, create it
if (user) {
return user;
} else {
return prisma.user.create({
data: {
email: email,
name: name,
},
});
}
}
/**
* Retrieves all chat records from the database for the current user.
* @returns A promise that resolves to an array of Chat objects.
*/
export async function dbGetMyChatList(): Promise<Chat[]> {
const { id: userId } = await sessionUser();
if (!userId) return [];
return prisma.chat.findMany({
where: { userId },
orderBy: {
createdAt: 'desc',
},
});
}
/**
* Retrieves a chat with messages from the database based on the provided ID.
* @param id - The ID of the chat to retrieve.
* @returns A Promise that resolves to a ChatWithMessages object if found, or null if not found.
*/
export async function dbGetChat(id: string): Promise<ChatWithMessages | null> {
return prisma.chat.findUnique({
where: { id },
include: {
messages: {
orderBy: {
createdAt: 'asc',
},
},
},
});
}
/**
* Creates a new chat in the database with the provided information.
* @param {Object} options - The options for creating the chat.
* @param {string} options.id - The ID of the chat (optional).
* @param {string} options.title - The title of the chat (optional).
* @param {MessageUserInput} options.message - The message to be added to the chat.
* @returns {Promise<Chat>} - A promise that resolves to the created chat.
*/
export async function dbPostCreateChat({
id,
title,
mediaUrl,
message,
}: {
id?: string;
title?: string;
mediaUrl: string;
message: MessageUserInput;
}) {
const userConnect = await getUserConnect();
try {
const response = await prisma.chat.create({
data: {
id,
...userConnect,
mediaUrl,
title,
messages: {
create: [{ ...message, ...userConnect }],
},
},
include: {
messages: true,
},
});
revalidatePath('/chat');
return response;
} catch (error) {
console.error(error);
}
}
/**
* Creates a new message in the database for a given chat.
* @param chatId - The ID of the chat where the message will be created.
* @param message - The message to be created.
*/
export async function dbPostCreateMessage(
chatId: string,
message: MessageUserInput,
) {
const userConnect = await getUserConnect();
const resp = await prisma.message.create({
data: {
...message,
...userConnect,
chat: {
connect: { id: chatId },
},
},
});
revalidatePath('/chat');
return resp;
}
/**
* Updates a message in the database with the provided response.
* @param messageId - The ID of the message to update.
* @param messageResponse - The response to update the message with.
*/
export async function dbPostUpdateMessageResponse(
messageId: string,
messageResponse: MessageAssistantResponse,
) {
await prisma.message.update({
data: {
response: messageResponse.response,
result: messageResponse.result ?? undefined,
},
where: {
id: messageId,
},
});
revalidatePath('/chat');
}
export async function dbDeleteChat(chatId: string) {
await prisma.chat.delete({
where: { id: chatId },
});
revalidatePath('/chat');
return;
}
|