File size: 7,117 Bytes
97f53b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
var axios = require('axios');
const Order = require("../Database/models/order");
const Product = require("../Database/models/product");
var User = require('../Database/models/user');
const pushApi = "https://fcm.googleapis.com/fcm/send";
const { responseAddProduct, responseFetchProduct, failedResponse, successResponse } = require("../utils/responseModel");

//Post order
router.post("/", async (req, res, next) => {
    try {
        console.log(req.body);
        const order = new Order(req.body);
        const orders = await order.save();

        if (orders) {
            const data = await User.findOne({ unique_id: req.body.unique_id })
            console.log(data.pushToken);
            if (data) {
                /* var data = JSON.stringify({
                    "content_available": true,
                    "priority": "high",
                    "to": data.pushToken,
                    "notification": {
                        "title": "Order Placed",
                        "body": "We are preparing your orders..."
                    }
                }); */

                var config = {
                    method: 'post',
                    url: pushApi,
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': 'key=AAAAKXRRooI:APA91bH9pMJziYPRNRI2XyMaSIG_e5a-eJzxMSkaozaCrmCencitDrTul4XyrVAV87K6d-56zGJC49y7Cz6mTRpcxca16QzmF1TF8EW7OmxHPvcQdseHWoD3TIAe62u2gfY0pVXlhJ8Y'
                    },
                    data: data
                };

                axios(config)
                    .then(function (response) {
                        console.log(JSON.stringify(response.data));
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }

            // If order placed it will send push notification to admin
            // Who will deliver the order
            const adminUsers = await User.find({ role: 'admin' });

            if (adminUsers) {
                await Promise.all(adminUsers.map(async (data) => {
                    console.log("Hope it will work" + data.pushToken)
                    var data = JSON.stringify({
                        "content_available": true,
                        "priority": "high",
                        "to": data.pushToken,
                        "notification": {
                            "title": "Order Placed",
                            "body": "New order placed. Take ready :)"
                        }
                    });

                    var config = {
                        method: 'post',
                        url: pushApi,
                        headers: {
                            'Content-Type': 'application/json',
                            'Authorization': 'key=AAAAKXRRooI:APA91bH9pMJziYPRNRI2XyMaSIG_e5a-eJzxMSkaozaCrmCencitDrTul4XyrVAV87K6d-56zGJC49y7Cz6mTRpcxca16QzmF1TF8EW7OmxHPvcQdseHWoD3TIAe62u2gfY0pVXlhJ8Y'
                        },
                        data: data
                    };

                    axios(config)
                        .then(function (response) {
                            console.log(JSON.stringify(response.data));
                        })
                        .catch(function (error) {
                            console.log(error);
                        });

                }));
            } else {
                console.log("Admin Users not found!")
            }
        }

        res.status(200).json(responseAddProduct(true, "Order placed!"));

    } catch (err) {
        res.status(500).json(responseAddProduct(false, err));
    }
});

//UPDATE
router.put("/:id", async (req, res) => {
    try {
        console.log(req.params.id);
        console.log(req.body);

        const data = await Order.updateOne({ _id: req.params.id },
            { $set: { status: req.body.status } }).exec();

        if (data) {
            try {
                var statusText = ""

                if (req.body.status == "Cancelled" || req.body.status == "cancelled") {
                    statusText = "You're order is cancelled"
                } else if (req.body.status == "Accepted" || req.body.status == "accepted") {
                    statusText = "You're order is accepted by the dealer :) "
                } else if (req.body.status == "Preparing" || req.body.status == "preparing") {
                    statusText = "You're order is about to packing :) "
                }

                const data = await User.findOne({ unique_id: req.body.unique_id }).exec();
                if (data) {
                    console.log(data.pushToken);
                    var config = {
                        method: 'post',
                        url: pushApi,
                        headers: {
                            'Content-Type': 'application/json',
                            'Authorization': 'key=AAAAKXRRooI:APA91bH9pMJziYPRNRI2XyMaSIG_e5a-eJzxMSkaozaCrmCencitDrTul4XyrVAV87K6d-56zGJC49y7Cz6mTRpcxca16QzmF1TF8EW7OmxHPvcQdseHWoD3TIAe62u2gfY0pVXlhJ8Y'
                        },
                        data: data
                    };
                    axios(config)
                        .then(function (response) {
                            console.log(JSON.stringify(response.data));
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            } catch (e) {
                console.log(e)
            }
        } else {
            return res.status(201).send(failedResponse(err))
        }
    } catch (err) {
        console.log(err);
        res.status(500).json(err);
    }
});

// //GET USER ORDERS
// router.get("/:id", async (req, res) => {

//     console.log(req.params.unique_id);

//     try {
//         const orders = await Order.find({ id: req.params.id });
//         res.status(200).json(orders);
//     } catch (err) {
//         res.status(500).json(err);
//     }
// });

//find order by unique_id
router.get("/:unique_id", async (req, res, next) => {
    try {
        console.log(req.params.unique_id);
        const orders = await Order.find({ unique_id: req.params.unique_id });
        res.status(200).json(orders);
    } catch (err) {
        res.status(500).json(err);
    }
});

// //GET ALL
router.get("/", async (req, res) => {
    try {
        const orders = await Order.find();
        res.status(200).send(successResponse(orders));
    } catch (err) {
        res.status(500).send(failedResponse(err));
    }
});


//delete order
router.delete("/:orderId", async (req, res, next) => {
    await Order.remove({ _id: req.params.orderId })
        .exec()
        .then(result => {
            res.status(200).json(successResponse(result));
        })
        .catch(err => {
            res.status(500).json(failedResponse(err));
        });
});

module.exports = router;