);
};
diff --git a/app/api/cron/send-daily-task-ai/route.ts b/app/api/cron/send-daily-task-ai/route.ts
index 6133bf8..116bd08 100644
--- a/app/api/cron/send-daily-task-ai/route.ts
+++ b/app/api/cron/send-daily-task-ai/route.ts
@@ -1,20 +1,22 @@
/*
-This API endpoint is used to add a new task to a section.
+This API endpoint is used to create a cron job that will send an email to all users with their tasks for the day and the next 7 days.
*/
+import { getUserAiTasks } from "@/actions/cron/get-user-ai-tasks";
import { prismadb } from "@/lib/prisma";
-import sendEmail from "@/lib/sendmail";
-import { data } from "autoprefixer";
-import axios from "axios";
-import dayjs from "dayjs";
import { NextResponse } from "next/server";
export async function GET(req: Request) {
- /* try {
- const today = dayjs().startOf("day");
- const nextWeek = dayjs().add(7, "day").startOf("day");
- let prompt = "";
+ /*
+This endpoint is not available in the demo version of NextCRM.
+*/
+ if (process.env.NEXT_PUBLIC_APP_URL === "demo.nextcrm.io") {
+ return NextResponse.json({
+ message: "AI assistant is not available in Demo version",
+ });
+ }
+ try {
const users = await prismadb.users.findMany({
where: {
userStatus: "ACTIVE",
@@ -24,131 +26,18 @@ export async function GET(req: Request) {
if (!users) return NextResponse.json({ message: "No users found" });
for (const user of users) {
- const getTaskPastDue = await prismadb.tasks.findMany({
- where: {
- AND: [
- {
- user: user.id,
- },
- {
- dueDateAt: {
- lte: new Date(),
- },
- },
- ],
- },
- });
-
- const getTaskPastDueInSevenDays = await prismadb.tasks.findMany({
- where: {
- AND: [
- {
- user: user.id,
- },
- {
- dueDateAt: {
- //lte: dayjs().add(7, "day").toDate(),
- gt: today.toDate(), // Due date is greater than or equal to today
- lt: nextWeek.toDate(), // Due date is less than next week (not including today)
- },
- },
- ],
- },
- });
-
- //console.log(user.userLanguage, "users.userLanguage");
- switch (user.userLanguage) {
- case "en":
- prompt = `Hi, Iam ${process.env.NEXT_PUBLIC_APP_URL} API Bot.
- \n\n
- There are ${getTaskPastDue.length} tasks past due and ${
- getTaskPastDueInSevenDays.length
- } tasks due in the next 7 days.
- \n\n
- Details today tasks: ${JSON.stringify(getTaskPastDue, null, 2)}
- \n\n
- Details next 7 days tasks: ${JSON.stringify(
- getTaskPastDueInSevenDays,
- null,
- 2
- )}
- \n\n
- As a personal assistant, write a message to ${
- user.name
- } to remind them of their tasks. And also do not forget to send them a some positive vibes.
- \n\n
- `;
- break;
- case "cz":
- prompt = `Jako profesionální asistentka Emma s perfektní znalostí projektového řízení, který má na starosti projekty na adrese${
- process.env.NEXT_PUBLIC_APP_URL
- }, připrave manažerské shrnutí o úkolech včetně jejich detailů a termínů. Vše musí být perfektně česky a výstižně.
- \n\n
- Zde jsou informace k úkolům:
- \n\n
- Informace o projektu: Počet úkolů které jsou k řešení dnes: ${
- getTaskPastDue.length
- }, Počet úkolů, které musí být vyřešeny nejpozději do sedmi dnů: ${
- getTaskPastDueInSevenDays.length
- }.
- \n\n
- Detailní informace v JSON formátu k úkolům, které musí být hotové dnes: ${JSON.stringify(
- getTaskPastDue,
- null,
- 2
- )}
- \n\n
- Detailní informace k úkolům, které musí být hotové během následujících sedmi dní: ${JSON.stringify(
- getTaskPastDueInSevenDays,
- null,
- 2
- )}
-
- \n\n
- Na konec napiš manažerské shrnutí včetně milého uvítání napiš pro uživatele: ${
- user.name
- } a přidej odkaz ${
- process.env.NEXT_PUBLIC_APP_URL + "/projects/dashboard"
- } jako odkaz na detail k úkolům . Na konci manažerského shrnutí přidej. 1 tip na manažerskou dovednost z oblasti projektového řízení a timemanagementu, 2-3 věty s pozitivním naladěním a podporou, nakonec popřej hezký pracovní den a infomaci, že tato zpráva byla vygenerována pomocí umělé inteligence OpenAi.
- \n\n
- `;
- break;
+ const action = await getUserAiTasks(user.id);
+ if (action.message!) {
+ console.log(action.message);
+ return NextResponse.json({ message: action.message });
}
-
- if (!prompt) return NextResponse.json({ message: "No prompt found" });
-
- const getAiResponse = await fetch(
- `${process.env.NEXT_PUBLIC_APP_URL}/api/openai/create-chat-completion`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- prompt: prompt,
- }),
- }
- ).then((res) => res.json());
-
- console.log(getAiResponse.response, "getAiResponse");
-
- //skip if api response is error
- if (getAiResponse.error) {
- console.log("Error from OpenAI API");
- } else {
- await sendEmail({
- from: process.env.EMAIL_FROM,
- to: user.email!,
- subject: `${process.env.NEXT_PUBLIC_APP_NAME} OpenAI Project manager assistant`,
- text: getAiResponse.response.message.content,
- });
+ if (action.user) {
+ console.log("Emails sent to:", action.user);
+ return NextResponse.json({ message: "Emails sent to:" + action.user });
}
}
-
- return NextResponse.json({ message: "Emails sent" });
} catch (error) {
console.log("[TASK_CRON_API]", error);
return new NextResponse("Initial error", { status: 500 });
- } */
- return new NextResponse("Initial error", { status: 500 });
+ }
}
diff --git a/app/api/databox/route.ts b/app/api/databox/route.ts
new file mode 100644
index 0000000..fd1c93d
--- /dev/null
+++ b/app/api/databox/route.ts
@@ -0,0 +1,17 @@
+import { authOptions } from "@/lib/auth";
+import { getServerSession } from "next-auth";
+import { NextResponse } from "next/server";
+
+export async function GET(req: Request, res: Response) {
+ const session = await getServerSession(authOptions);
+ if (!session) {
+ return NextResponse.json({ message: "unauthorized" }, { status: 401 });
+ }
+ try {
+ console.log("This endpoint works!");
+ return NextResponse.json({ message: "ok" }, { status: 200 });
+ } catch (error) {
+ console.log(error);
+ return NextResponse.json({ message: "error" }, { status: 500 });
+ }
+}