Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat: add croner for scheduled tasks and implement demo user setup

Andrej Acevskicfcfd6034589
diff --git a/apps/api/package.json b/apps/api/package.json
index 593b5d3b1..ebc4e5fa7 100644
--- a/apps/api/package.json
+++ b/apps/api/package.json
@@ -5,7 +5,7 @@
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "esbuild src/index.ts --bundle --platform=node --outdir=dist --format=esm --packages=external --external:fs --external:path --external:crypto --external:os --external:util --external:stream --external:buffer --external:events --external:url --external:querystring --external:http --external:https --external:net --external:tls --external:zlib",
- "lint": "biome check --write ."
+ "lint": "biome check --write ."
},
"dependencies": {
"@hono/node-server": "^1.14.4",
@@ -15,6 +15,7 @@
"@oslojs/encoding": "^1.1.0",
"@paralleldrive/cuid2": "^2.2.2",
"bcrypt": "^6.0.0",
+ "croner": "^9.1.0",
"dotenv": "^16.5.0",
"drizzle-kit": "^0.31.1",
"drizzle-orm": "^0.43.0",
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index ded6c736b..5c6c65dc1 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -1,4 +1,5 @@
import { serve } from "@hono/node-server";
+import { Cron } from "croner";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import { Hono } from "hono";
import { getCookie } from "hono/cookie";
@@ -18,6 +19,8 @@ import timeEntry from "./time-entry";
import user from "./user";
import { validateSessionToken } from "./user/utils/validate-session-token";
import getSettings from "./utils/get-settings";
+import purgeDemoData from "./utils/purge-demo-data";
+import setDemoUser from "./utils/set-demo-user";
import workspace from "./workspace";
import workspaceUser from "./workspace-user";
@@ -52,28 +55,33 @@ if (!isDemoMode) {
app.use("*", auth);
}
-// TODO: Fix this
-// app.use("*", async (c, next) => {
-// if (isDemoMode) {
-// const session = getCookie(c, "session");
+if (isDemoMode) {
+ new Cron("0 * * * *", async () => {
+ await purgeDemoData();
+ });
+}
-// if (!session) {
-// await setDemoUser(c);
-// }
+app.use("*", async (c, next) => {
+ if (isDemoMode) {
+ const session = getCookie(c, "session");
-// const { user, session: validatedSession } = await validateSessionToken(
-// session ?? "",
-// );
+ if (!session) {
+ await setDemoUser(c);
+ }
-// if (!user || !validatedSession) {
-// await setDemoUser(c);
-// }
+ const { user, session: validatedSession } = await validateSessionToken(
+ session ?? "",
+ );
-// c.set("userEmail", user?.email ?? "");
-// }
+ if (!user || !validatedSession) {
+ await setDemoUser(c);
+ }
-// await next();
-// });
+ c.set("userEmail", user?.email ?? "");
+ }
+
+ await next();
+});
const meRoute = app.get("/me", async (c) => {
const session = getCookie(c, "session");
diff --git a/apps/api/src/utils/set-demo-user.ts b/apps/api/src/utils/set-demo-user.ts
new file mode 100644
index 000000000..4587de54a
--- /dev/null
+++ b/apps/api/src/utils/set-demo-user.ts
@@ -0,0 +1,20 @@
+import type { Context } from "hono";
+import { setCookie } from "hono/cookie";
+import isInSecureMode from "../user/utils/is-in-secure-mode";
+import { createDemoUser } from "./create-demo-user";
+
+async function setDemoUser(c: Context) {
+ const demoExpiresAt = new Date(Date.now() + 15 * 60 * 1000);
+ const { session: demoSession, expiresAt = demoExpiresAt } =
+ await createDemoUser();
+
+ setCookie(c, "session", demoSession, {
+ httpOnly: isInSecureMode(c.req),
+ path: "/",
+ secure: true,
+ sameSite: "lax",
+ expires: expiresAt,
+ });
+}
+
+export default setDemoUser;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b09e12cf0..63fb747f4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -53,6 +53,9 @@ importers:
bcrypt:
specifier: ^6.0.0
version: 6.0.0
+ croner:
+ specifier: ^9.1.0
+ version: 9.1.0
dotenv:
specifier: ^16.5.0
version: 16.5.0
@@ -3022,6 +3025,10 @@ packages:
resolution: {integrity: sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==}
+ resolution: {integrity: sha512-p9nwwR4qyT5W996vBZhdvBCnMhicY5ytZkR4D1Xj0wuTDEiMnjwR57Q3RXYY/s0EpX6Ay3vgIcfaR+ewGHsi+g==}
+ engines: {node: '>=18.0'}
+
resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==}
engines: {node: '>= 8'}
@@ -7523,6 +7530,8 @@ snapshots:
+
dependencies:
path-key: 3.1.1