File size: 2,726 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use server";

import { archiveRepository } from "lib/db/repository";
import { getSession } from "auth/server";
import { ArchiveCreateSchema, ArchiveUpdateSchema } from "app-types/archive";

async function getUserId() {
  const session = await getSession();
  const userId = session?.user?.id;
  if (!userId) {
    throw new Error("User not found");
  }
  return userId;
}

export async function createArchiveAction(data: {
  name: string;
  description?: string;
}) {
  const userId = await getUserId();
  const validatedData = ArchiveCreateSchema.parse(data);

  return await archiveRepository.createArchive({
    name: validatedData.name,
    description: validatedData.description || null,
    userId,
  });
}

export async function updateArchiveAction(
  id: string,
  data: { name?: string; description?: string },
) {
  const userId = await getUserId();

  // Check if user owns the archive
  const existingArchive = await archiveRepository.getArchiveById(id);
  if (!existingArchive || existingArchive.userId !== userId) {
    throw new Error("Archive not found or access denied");
  }

  const validatedData = ArchiveUpdateSchema.parse(data);

  return await archiveRepository.updateArchive(id, {
    name: validatedData.name,
    description: validatedData.description || null,
  });
}

export async function deleteArchiveAction(id: string) {
  const userId = await getUserId();

  // Check if user owns the archive
  const existingArchive = await archiveRepository.getArchiveById(id);
  if (!existingArchive || existingArchive.userId !== userId) {
    throw new Error("Archive not found or access denied");
  }

  await archiveRepository.deleteArchive(id);
}

export async function addItemToArchiveAction(
  archiveId: string,
  itemId: string,
) {
  const userId = await getUserId();

  // Check if user owns the archive
  const existingArchive = await archiveRepository.getArchiveById(archiveId);
  if (!existingArchive || existingArchive.userId !== userId) {
    throw new Error("Archive not found or access denied");
  }

  return await archiveRepository.addItemToArchive(archiveId, itemId, userId);
}

export async function removeItemFromArchiveAction(
  archiveId: string,
  itemId: string,
) {
  const userId = await getUserId();

  // Check if user owns the archive
  const existingArchive = await archiveRepository.getArchiveById(archiveId);
  if (!existingArchive || existingArchive.userId !== userId) {
    throw new Error("Archive not found or access denied");
  }

  await archiveRepository.removeItemFromArchive(archiveId, itemId);
}

export async function getItemArchivesAction(itemId: string) {
  const userId = await getUserId();
  return await archiveRepository.getItemArchives(itemId, userId);
}