-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteProduct.ts
40 lines (35 loc) · 1.2 KB
/
deleteProduct.ts
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
import { GraphQLError } from "graphql";
import Product from "../../../../Schema/Product/Product.model";
import Shop from "../../../../Schema/Company/Shop/Shop.model";
import useDel from "../../../../Redis/useDel/useDel";
import authenticateToken from "../../../../JWT/AuthenticateToken";
import { MutationDeleProductArgs } from "../../../Types/types";
require("dotenv").config();
interface deletedProductParams {
id: string;
firebaseCompanyID: string;
}
const deleteProduct = async (
_,
{ id, firebaseCompanyID }: MutationDeleProductArgs,
{ req, admin, client }
) => {
try {
if (process.env.NODE_ENV === "production") {
const token = await admin.auth().verifyIdToken(req.headers.authorization);
authenticateToken(token.uid, firebaseCompanyID);
}
//delete product in mongofn and in redis
const deletedProduct = await Product.updateOne(
{ _id: id },
{ status: "deleted" }
);
if (!deletedProduct) throw new Error("could not find the product");
await useDel(`product/${id}`, client);
return true;
} catch (e: any) {
console.log("error while trying to delete the product");
throw new GraphQLError(e.message);
}
};
export default deleteProduct;