aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/models/backups.ts
blob: c7ab99bafe658f23b664d98d4030f5501db6d34f (plain) (blame)
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
import { TRPCError } from "@trpc/server";
import { and, desc, eq, lt } from "drizzle-orm";
import { z } from "zod";

import { assets, backupsTable } from "@karakeep/db/schema";
import { BackupQueue } from "@karakeep/shared-server";
import { deleteAsset } from "@karakeep/shared/assetdb";
import { zBackupSchema } from "@karakeep/shared/types/backups";

import { AuthedContext } from "..";

export class Backup {
  private constructor(
    private ctx: AuthedContext,
    private backup: z.infer<typeof zBackupSchema>,
  ) {}

  static async fromId(ctx: AuthedContext, backupId: string): Promise<Backup> {
    const backup = await ctx.db.query.backupsTable.findFirst({
      where: and(
        eq(backupsTable.id, backupId),
        eq(backupsTable.userId, ctx.user.id),
      ),
    });

    if (!backup) {
      throw new TRPCError({
        code: "NOT_FOUND",
        message: "Backup not found",
      });
    }

    return new Backup(ctx, backup);
  }

  private static fromData(
    ctx: AuthedContext,
    backup: z.infer<typeof zBackupSchema>,
  ): Backup {
    return new Backup(ctx, backup);
  }

  static async getAll(ctx: AuthedContext): Promise<Backup[]> {
    const backups = await ctx.db.query.backupsTable.findMany({
      where: eq(backupsTable.userId, ctx.user.id),
      orderBy: [desc(backupsTable.createdAt)],
    });

    return backups.map((b) => new Backup(ctx, b));
  }

  static async create(ctx: AuthedContext): Promise<Backup> {
    const [backup] = await ctx.db
      .insert(backupsTable)
      .values({
        userId: ctx.user.id,
        size: 0,
        bookmarkCount: 0,
        status: "pending",
      })
      .returning();
    return new Backup(ctx, backup!);
  }

  async triggerBackgroundJob({
    delayMs,
    idempotencyKey,
  }: { delayMs?: number; idempotencyKey?: string } = {}): Promise<void> {
    await BackupQueue.enqueue(
      {
        userId: this.ctx.user.id,
        backupId: this.backup.id,
      },
      {
        delayMs,
        idempotencyKey,
      },
    );
  }

  /**
   * Generic update method for backup records
   */
  async update(
    data: Partial<{
      size: number;
      bookmarkCount: number;
      status: "pending" | "success" | "failure";
      assetId: string | null;
      errorMessage: string | null;
    }>,
  ): Promise<void> {
    await this.ctx.db
      .update(backupsTable)
      .set(data)
      .where(
        and(
          eq(backupsTable.id, this.backup.id),
          eq(backupsTable.userId, this.ctx.user.id),
        ),
      );

    // Update local state
    this.backup = { ...this.backup, ...data };
  }

  async delete(): Promise<void> {
    if (this.backup.assetId) {
      // Delete asset
      await deleteAsset({
        userId: this.ctx.user.id,
        assetId: this.backup.assetId,
      });
    }

    await this.ctx.db.transaction(async (db) => {
      // Delete asset first
      if (this.backup.assetId) {
        await db
          .delete(assets)
          .where(
            and(
              eq(assets.id, this.backup.assetId),
              eq(assets.userId, this.ctx.user.id),
            ),
          );
      }

      // Delete backup record
      await db
        .delete(backupsTable)
        .where(
          and(
            eq(backupsTable.id, this.backup.id),
            eq(backupsTable.userId, this.ctx.user.id),
          ),
        );
    });
  }

  /**
   * Finds backups older than the retention period
   */
  static async findOldBackups(
    ctx: AuthedContext,
    retentionDays: number,
  ): Promise<Backup[]> {
    const cutoffDate = new Date();
    cutoffDate.setDate(cutoffDate.getDate() - retentionDays);

    const oldBackups = await ctx.db.query.backupsTable.findMany({
      where: and(
        eq(backupsTable.userId, ctx.user.id),
        lt(backupsTable.createdAt, cutoffDate),
      ),
    });

    return oldBackups.map((backup) => Backup.fromData(ctx, backup));
  }

  asPublic(): z.infer<typeof zBackupSchema> {
    return this.backup;
  }

  get id() {
    return this.backup.id;
  }

  get assetId() {
    return this.backup.assetId;
  }
}