-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchUserSubscription.ts
74 lines (64 loc) · 1.8 KB
/
fetchUserSubscription.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
73
74
import { FoodSubscription, Op } from "../models";
import { response } from "../utils/helper";
import moment = require('moment');
export const handler = async (event: any, context: any) => {
const { sub } = event?.requestContext?.authorizer?.claims;
let { vId }: any = event.pathParameters;
let body: any = event.body;
let { types, from, to } = JSON.parse(body);
let subscriptionList: any = {};
let from_date, to_date = {};
let where: any = {
user_uuid: sub,
};
if (vId && vId != 'all') {
where = { ...where, vendor_id: vId }
}
if (types.length) {
let typeFilter = {
[Op.in]: types,
};
where = { ...where, type: { ...typeFilter } };
}
if (from && to) {
from_date = {
[Op.and]: {
[Op.gte]: from,
},
}
to_date = {
[Op.and]: {
[Op.lte]: to,
},
}
}
if (from_date && to_date) {
where = {
...where,
from_date,
to_date
}
}
let events: { [key: string]: any[] } = {};
await FoodSubscription.findAll({
where,
})
.then((subscriptionList) => {
if (subscriptionList) {
subscriptionList.forEach((element: any) => {
if (!(element.identifier in events)) {
events[element.identifier] = [];
}
const time = element.type === 'BF' ? '09:00:00' : (element.type === 'LN' ? '12:00:00' : '20:00:00')
const endTime = element.type === 'BF' ? '09:30:00' : (element.type === 'LN' ? '12:30:00' : '20:30:00')
events[element.identifier].push({
title: element.type,
start: moment(element.from_date).format('YYYY-MM-DD ' + time),
end: moment(element.to_date).format('YYYY-MM-DD ' + endTime),
allDay: false
})
});
}
})
return await response(200, events);
};