File size: 1,253 Bytes
09515ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cbf49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use server";

import { isAuthenticated } from "@/lib/auth";
import { NextResponse } from "next/server";
import dbConnect from "@/lib/mongodb";
import Project from "@/models/Project";
import { Project as ProjectType } from "@/types";

export async function getProjects(): Promise<{
  ok: boolean;
  projects: ProjectType[];
}> {
  const user = await isAuthenticated();

  if (user instanceof NextResponse || !user) {
    return {
      ok: false,
      projects: [],
    };
  }

  await dbConnect();
  const projects = await Project.find({
    user_id: user?.id,
  })
    .sort({ _createdAt: -1 })
    .limit(100)
    .lean();
  if (!projects) {
    return {
      ok: false,
      projects: [],
    };
  }
  return {
    ok: true,
    projects: JSON.parse(JSON.stringify(projects)) as ProjectType[],
  };
}

export async function getProject(
  namespace: string,
  repoId: string
): Promise<ProjectType | null> {
  const user = await isAuthenticated();

  if (user instanceof NextResponse || !user) {
    return null;
  }

  await dbConnect();
  const project = await Project.findOne({
    user_id: user.id,
    namespace,
    repoId,
  }).lean();

  if (!project) {
    return null;
  }

  return JSON.parse(JSON.stringify(project)) as ProjectType;
}