File size: 3,163 Bytes
1804b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Router } from "express";
import { db, clientsTable, projectsTable } from "@workspace/db";
import { eq, sql, and } from "drizzle-orm";
import {
  CreateClientBody,
  UpdateClientBody,
  GetClientParams,
  UpdateClientParams,
  DeleteClientParams,
} from "@workspace/api-zod";

const router = Router();

router.get("/clients", async (req, res) => {
  const clients = await db.select().from(clientsTable).where(eq(clientsTable.userId, req.user!.id)).orderBy(clientsTable.createdAt);

  const withCounts = await Promise.all(
    clients.map(async (c) => {
      const [{ count }] = await db
        .select({ count: sql<number>`cast(count(*) as integer)` })
        .from(projectsTable)
        .where(eq(projectsTable.clientId, c.id));
      return { ...c, projectCount: count };
    })
  );

  res.json(withCounts);
});

router.post("/clients", async (req, res) => {
  const body = CreateClientBody.safeParse(req.body);
  if (!body.success) {
    res.status(400).json({ error: body.error.message });
    return;
  }

  const [client] = await db.insert(clientsTable).values({ ...body.data, userId: req.user!.id }).returning();
  res.status(201).json({ ...client, projectCount: 0 });
});

router.get("/clients/:id", async (req, res) => {
  const params = GetClientParams.safeParse({ id: Number(req.params.id) });
  if (!params.success) {
    res.status(400).json({ error: params.error.message });
    return;
  }

  const [client] = await db.select().from(clientsTable).where(and(eq(clientsTable.id, params.data.id), eq(clientsTable.userId, req.user!.id)));
  if (!client) {
    res.status(404).json({ error: "Client not found" });
    return;
  }

  const [{ count }] = await db
    .select({ count: sql<number>`cast(count(*) as integer)` })
    .from(projectsTable)
    .where(eq(projectsTable.clientId, client.id));

  res.json({ ...client, projectCount: count });
});

router.patch("/clients/:id", async (req, res) => {
  const params = UpdateClientParams.safeParse({ id: Number(req.params.id) });
  if (!params.success) {
    res.status(400).json({ error: params.error.message });
    return;
  }

  const body = UpdateClientBody.safeParse(req.body);
  if (!body.success) {
    res.status(400).json({ error: body.error.message });
    return;
  }

  const [client] = await db
    .update(clientsTable)
    .set(body.data)
    .where(and(eq(clientsTable.id, params.data.id), eq(clientsTable.userId, req.user!.id)))
    .returning();

  if (!client) {
    res.status(404).json({ error: "Client not found" });
    return;
  }

  const [{ count }] = await db
    .select({ count: sql<number>`cast(count(*) as integer)` })
    .from(projectsTable)
    .where(eq(projectsTable.clientId, client.id));

  res.json({ ...client, projectCount: count });
});

router.delete("/clients/:id", async (req, res) => {
  const params = DeleteClientParams.safeParse({ id: Number(req.params.id) });
  if (!params.success) {
    res.status(400).json({ error: params.error.message });
    return;
  }

  await db.delete(clientsTable).where(and(eq(clientsTable.id, params.data.id), eq(clientsTable.userId, req.user!.id)));
  res.status(204).send();
});

export default router;