File size: 3,791 Bytes
6a30288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use server";

import { db } from "@/db/db";
import { Product, products } from "@/db/schema";
import { requireUser } from "@/lib/auth";
import { and, eq } from "drizzle-orm";
import { z } from "zod";

export async function createProduct(
  productValues: Omit<Product, "id" | "storeId">
) {
  const schema = z.object({
    name: z.string().nonempty(),
    description: z.string(),
    price: z.string().nullable(),
    inventory: z.string().nullable(),
  });

  try {
    schema.parse(productValues);

    const user = await requireUser();
    if (!user.storeId) throw new Error("Store not found for current user");

    const values = {
      name: productValues.name,
      description: productValues.description,
      price:
        isNaN(Number(productValues.price)) || Number(productValues.price) < 0
          ? "0"
          : String(productValues.price),
      inventory: isNaN(Number(productValues.inventory))
        ? "0"
        : String(productValues.inventory),
      images: productValues.images,
      storeId: Number(user.storeId),
    };

    const dbRes = await db.insert(products).values(values);

    const res = {
      error: false,
      message: "Product created",
      action: "Success, your new product has been created",
      productId: dbRes[0].insertId,
    };

    return res;
  } catch (err) {
    console.log("CATCH ERR", err);
    const res = {
      error: true,
      message: "Sorry, an error occured creating your product.",
      action: "Please try again.",
      productId: null,
    };
    return res;
  }
}

export async function updateProduct(productValues: Omit<Product, "storeId">) {
  const schema = z.object({
    name: z.string().nonempty(),
    description: z.string(),
    price: z.string().nullable(),
    inventory: z.string().nullable(),
    id: z.number(),
  });

  try {
    schema.parse(productValues);

    const user = await requireUser();
    if (!user.storeId) throw new Error("Store not found for current user");

    const values = {
      name: productValues.name,
      description: productValues.description,
      price:
        isNaN(Number(productValues.price)) || Number(productValues.price) < 0
          ? "0"
          : String(productValues.price),
      inventory: isNaN(Number(productValues.inventory))
        ? "0"
        : String(productValues.inventory),
      images: productValues.images,
      storeId: Number(user.storeId),
    };

    const dbRes = await db
      .update(products)
      .set(values)
      .where(
        and(
          eq(products.id, productValues.id),
          eq(products.storeId, values.storeId)
        )
      );
    console.log({ dbRes });
    const res = {
      error: false,
      message: "Product updated",
      action: "Success, your product has been updated",
    };

    return res;
  } catch (err) {
    console.log(err);
    const res = {
      error: true,
      message: "Sorry, an error occured updating your product.",
      action: "Please try again.",
    };
    return res;
  }
}

export async function deleteProduct(productId: number | undefined) {
  const schema = z.number();

  try {
    schema.parse(productId);

    if (!productId) throw new Error("No product id provided");

    const user = await requireUser();
    if (!user.storeId) throw new Error("Store not found for current user");

    await db
      .delete(products)
      .where(
        and(eq(products.id, productId), eq(products.storeId, Number(user.storeId)))
      );

    return {
      error: false,
      message: "Product deleted",
      action: "Success, your product has been deleted",
    };
  } catch (err) {
    console.log(err);
    return {
      error: true,
      message: "Sorry, an error occured deleting your product.",
      action: "Please try again.",
    };
  }
}