Skip to content

Commit

Permalink
feat: soft delete (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwisecodes authored Dec 17, 2024
1 parent dea3106 commit 9dee4ad
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function getChatById(
id: string,
): Promise<AilaPersistedChat | null> {
const session = await prisma?.appSession.findUnique({
where: { id },
where: { id, deletedAt: null },
});

if (!session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const ClearChatHistory = ({
$justifyContent="center"
$gap="space-between-m"
>
<OakP>This will permanently delete all of your lesson history.</OakP>
<OakP $textAlign="center">
This will permanently delete all of your lesson history.
</OakP>
<ModalFooterButtons
closeDialog={handleCloseDialog}
actionButtonStates={handleDeleteChat}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const ClearSingleChatFromChatHistory = ({
$gap="space-between-m"
$justifyContent="center"
>
<OakP>This will permanently delete this lesson.</OakP>
<OakP $textAlign="center">This will permanently delete this lesson.</OakP>
{isLoading ? (
<OakFlex $justifyContent="center" $alignItems="center">
<LoadingWheel />
Expand Down
4 changes: 4 additions & 0 deletions apps/nextjs/src/components/DialogControl/DialogContents.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useState } from "react";

import type { LooseLessonPlan } from "@oakai/aila/src/protocol/schema";
import {
OakModal,
OakModalCenter,
OakModalCenterBody,
type OakIconName,
Expand All @@ -8,6 +11,7 @@ import type { Message } from "ai";
import styled from "styled-components";

import type { DialogTypes } from "../AppComponents/Chat/Chat/types";
import { ChatHistory } from "../AppComponents/Chat/chat-history";
import { useDialog } from "../AppComponents/DialogContext";
import ClearChatHistory from "./ContentOptions/ClearChatHistory";
import ClearSingleChatFromChatHistory from "./ContentOptions/ClearSingleChatFromChatHistory";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class AilaPrismaPersistence extends AilaPersistence {
const appSession = await this._prisma.appSession.findFirst({
where: {
id,
deletedAt: null,
},
});

Expand Down
14 changes: 11 additions & 3 deletions packages/api/src/router/appSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export async function getChat(id: string, prisma: PrismaClientWithAccelerate) {
const chatRecord = await prisma.appSession.findUnique({
where: {
id: id,
deletedAt: null,
},
});
if (!chatRecord) {
Expand Down Expand Up @@ -205,6 +206,7 @@ export const appSessionsRouter = router({
"app_sessions"
WHERE
"user_id" = ${userId} AND "app_id" = 'lesson-planner'
AND "deleted_at" IS NULL
ORDER BY
"updated_at" DESC
`;
Expand Down Expand Up @@ -245,22 +247,27 @@ export const appSessionsRouter = router({
const { userId } = ctx.auth;
const { id } = input;

await ctx.prisma.appSession.deleteMany({
await ctx.prisma.appSession.update({
where: {
id,
appId: "lesson-planner",
userId,
},
data: {
deletedAt: new Date(),
},
});
}),
deleteAllChats: protectedProcedure.mutation(async ({ ctx }) => {
const { userId } = ctx.auth;

await ctx.prisma.appSession.deleteMany({
await ctx.prisma.appSession.updateMany({
where: {
userId,
appId: "lesson-planner",
},
data: {
deletedAt: new Date(),
},
});
}),
shareChat: protectedProcedure
Expand All @@ -278,6 +285,7 @@ export const appSessionsRouter = router({
id,
userId,
appId: "lesson-planner",
deletedAt: null,
},
});

Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/router/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const chatsRouter = router({
const { id } = input;

const session = await prisma?.appSession.findUnique({
where: { id },
where: { id, deletedAt: null },
});

if (!session) {
Expand All @@ -72,6 +72,7 @@ export const chatsRouter = router({
where: {
userId,
appId: "lesson-planner",
deletedAt: null,
},
});

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/models/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class Apps {
where: {
id: sessionId,
userId: userId,
deletedAt: null,
output: {
not: Prisma.AnyNull,
},
Expand All @@ -59,6 +60,7 @@ export class Apps {
return this.prisma.appSession.findMany({
where: {
userId: userId,
deletedAt: null,
output: {
not: Prisma.AnyNull,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

-- AlterTable
ALTER TABLE "public"."app_sessions" ADD COLUMN "deleted_at" TIMESTAMP(3);
5 changes: 3 additions & 2 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,15 @@ model AppSession {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
app App @relation(fields: [appId], references: [id], onDelete: Cascade)
appId String @map("app_id")
userId String @map("user_id") // The user that requested this generation
output Json? // The final output of the session
userTweaks UserTweak[]
reGenerations ReGeneration[]
generationUserFlags GenerationUserFlag[]
Expand Down

0 comments on commit 9dee4ad

Please sign in to comment.