-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop.ts
32 lines (27 loc) · 1.01 KB
/
shop.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
import Shop from "../../../../Schema/Company/Shop/Shop.model";
import useGet from "../../../../Redis/useGet/useGet";
import useSet from "../../../../Redis/useSet/useSet";
import { GraphQLError } from "graphql";
import { QueryShopArgs } from "../../../Types/types";
const shop = async (_: any, { id }: QueryShopArgs, { client }) => {
try {
//check if the shop is cached than return it
const redisShop = await useGet(`shop/${id}`, client);
if (redisShop) {
if (!redisShop.isActive) throw new Error(`shop is not active`);
return redisShop;
}
//get the shop from mongodb
const shop = await Shop.findById(id).lean();
if (!shop) throw new Error(`shop with id ${id} does not exists`);
if (!shop.isActive) throw new Error(`shop is not active`);
//set shop in the cache
await useSet(`shop/${id}`, shop, client);
return shop;
} catch (e: any) {
console.log("error while fetching the shop");
throw new GraphQLError(e.message);
return null;
}
};
export default shop;