-
Notifications
You must be signed in to change notification settings - Fork 0
/
closeShops.ts
72 lines (65 loc) · 1.79 KB
/
closeShops.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
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
import Shop from "../../../../Schema/Company/Shop/Shop.model";
import MongoFilter from "../../../MongoFilter/MongoFilter";
import { GraphQLError } from "graphql";
import { LargeNumberLike } from "crypto";
import { QueryCloseShopsArgs } from "../../../Types/types";
const closeShops = async (
_: any,
{ location, category, cashBack, range, limit, offset }: QueryCloseShopsArgs,
__: any,
info
) => {
try {
if (limit < 0 || offset < 0) {
throw new Error("limit and offset cannot be negative");
}
//get the requested fields and store them in a filter const
const filter = MongoFilter(info);
const setCategories = () => {
if (category === null) {
return { $exists: true }; //all categories
}
return category;
};
const setCashBack = () => {
if (cashBack === null) {
return 0; //all types of cashback
}
return cashBack;
};
var closeShops = await Shop.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [location.coordinates[0], location.coordinates[1]],
},
spherical: true,
query: { categories: setCategories() },
distanceField: "coordinates",
minDistance: 0,
maxDistance: range,
},
},
{
$skip: offset,
},
{ $limit: limit },
{
$match: {
// Filter documents that don't have balance > 0
"cashbackInfo.cashBack": { $gte: setCashBack() },
isActive: true,
},
},
{ $project: filter },
]);
console.log(closeShops);
return closeShops;
} catch (e: any) {
console.log("error while fetching the close shops");
throw new GraphQLError(e.message);
return null;
}
};
export default closeShops;