-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchPartner.ts
69 lines (58 loc) · 1.4 KB
/
fetchPartner.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
import { Op, Vendor, sequelize } from "../models";
import { response } from "../utils/helper";
export const handler = async (event: any, context: any) => {
let body: any = event.body;
let { rating_avg, distance, is_veg, lat, lng, limit } = JSON.parse(body);
limit = limit ? limit : 10;
let haversine = "";
let having = null;
let order = null;
const where: any = {
status: "ACTIVE",
};
if (rating_avg >= 0) {
where.rating_avg = {
[Op.gte]: rating_avg,
};
}
if (lat && lng && distance > 0) {
haversine = `(
6371 * acos(
cos(radians(${lat}))
* cos(radians(lat))
* cos(radians(lng) - radians(${lng}))
+ sin(radians(${lat})) * sin(radians(lat))
)
)`;
}
if (is_veg && is_veg == "YES") {
where.is_veg = "YES";
}
order = distance ? sequelize.col("distance") : undefined;
having = distance ? sequelize.literal(`distance <= ${distance}`) : undefined;
const vendors = await Vendor.findAll({
attributes: [
"id",
"name",
"postcode",
"address",
"rating_avg",
"rating_count",
"is_veg",
"is_promoted",
"logo_url",
[sequelize.literal(haversine), "distance"],
],
where,
order,
having,
limit,
})
.then((records) => {
return records;
})
.catch((err) => {
return [];
});
return response(200, vendors, body);
};