-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomerController.ts
425 lines (352 loc) · 11.9 KB
/
customerController.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import { NextFunction, Request, Response } from 'express';
import {
CartItem,
CreateCustomerInputs,
EditCustomerProfileInputs,
OrderInputs,
UserLoginInputs,
} from '../dto/customer.dto';
import { Customer, DeliveryUser, Food, Offer, Vendor } from '../models';
import { Order } from '../models/Order';
import {
GenerateOtp,
GeneratePassword,
GenerateSalt,
GenerateSignature,
onRequestOtp,
ValidatePassword,
} from '../utility';
import { Transaction } from './../models/Transaction';
export const CustomerSignUp = async (req: Request, res: Response, next: NextFunction) => {
const customerInputs = plainToClass(CreateCustomerInputs, req.body);
const inputErrors = await validate(customerInputs, { validationError: { target: true } });
if (inputErrors.length > 0) {
return res.status(400).json(inputErrors);
}
const { email, phone, password } = customerInputs;
const salt = await GenerateSalt();
const userPassword = await GeneratePassword(password, salt);
const { otp, expiry } = GenerateOtp();
const existingCustomer = Customer.findOne({ email: email });
if (existingCustomer !== null) {
return res.status(409).json({ message: 'Customer already exists' });
}
const result = await Customer.create({
email: email,
password: userPassword,
salt: salt,
phone: phone,
otp: otp,
otp_expiry: expiry,
firstName: '',
lastName: '',
address: '',
verified: false,
lat: 0,
lng: 0,
orders: [],
});
if (result) {
await onRequestOtp(otp, phone);
const signature = GenerateSignature({
_id: result._id,
email: result.email,
verified: result.verified,
});
return res
.status(201)
.json({ signature: signature, verified: result.verified, email: result.email });
}
return res.status(400).json({ message: 'Error with Signup' });
};
export const CustomerLogin = async (req: Request, res: Response, next: NextFunction) => {
const loginInputs = plainToClass(UserLoginInputs, req.body);
const loginErrors = await validate(loginInputs, { validationError: { target: true } });
if (loginErrors.length > 0) {
return res.status(400).json(loginErrors);
}
const { email, password } = loginInputs;
const customer = await Customer.findOne({ email: email });
if (customer) {
const validation = await ValidatePassword(password, customer.password, customer.salt);
if (validation) {
const signature = GenerateSignature({
_id: customer._id,
email: customer.email,
verified: customer.verified,
});
return res
.status(201)
.json({ signature: signature, verified: customer.verified, email: customer.email });
}
}
return res.status(404).json({ message: 'Error with Login' });
};
export const CustomerVerify = async (req: Request, res: Response, next: NextFunction) => {
const { otp } = req.body;
const customer = req.user;
if (customer) {
const profile = await Customer.findById(customer._id);
if (profile) {
if (profile.otp === parseInt(otp) && profile.otp_expiry >= new Date()) {
profile.verified = true;
const updatedCustomerResponse = await profile.save();
const signature = GenerateSignature({
_id: updatedCustomerResponse._id,
email: updatedCustomerResponse.email,
verified: updatedCustomerResponse.verified,
});
return res.status(201).json({
signature: signature,
verified: updatedCustomerResponse.verified,
email: updatedCustomerResponse.email,
});
}
}
}
return res.status(400).json({ message: 'Error with OTP Validation' });
};
export const RequestOtp = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
if (customer) {
const profile = await Customer.findById(customer._id);
if (profile) {
const { otp, expiry } = GenerateOtp();
profile.otp = otp;
profile.otp_expiry = expiry;
await profile.save();
await onRequestOtp(otp, profile.phone);
res.status(200).json({ message: 'OTP sent your registered phone number!' });
}
}
res.status(400).json({ message: 'Error with OTP Request' });
};
export const GetCustomerProfile = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
if (customer) {
const profile = await Customer.findById(customer._id);
if (profile) {
return res.status(200).json(profile);
}
}
return res.status(400).json({ message: 'Error with Fetch Profile' });
};
export const EditCustomerProfile = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
const profileInputs = plainToClass(EditCustomerProfileInputs, req.body);
const profileErrors = await validate(profileInputs, { validationError: { target: false } });
if (profileErrors.length > 0) {
return res.status(400).json(profileErrors);
}
const { firstName, lastName, address } = profileInputs;
if (customer) {
const profile = await Customer.findById(customer._id);
if (profile) {
profile.firstName;
profile.lastName;
profile.address;
const result = await profile.save();
res.status(200).json(result);
}
}
};
export const AddToCart = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
if (customer) {
const profile = await Customer.findById(customer._id).populate('cart.food');
let cartItems = [];
const { _id, unit } = <CartItem>req.body;
const food = await Food.findById(_id);
if (food) {
if (profile !== null) {
//check for cart items
cartItems = profile.cart;
if (cartItems.length > 0) {
// check and update unit
let existFoodItem = cartItems.filter((item) => item.food._id.toString() === _id);
if (existFoodItem.length > 0) {
const index = cartItems.indexOf(existFoodItem[0]);
if (unit > 0) {
cartItems[index] = { food, unit };
} else {
cartItems.splice(index, 1);
}
} else {
cartItems.push({ food, unit });
}
} else {
// add new item to cart
cartItems.push({ food, unit });
}
if (cartItems) {
profile.cart = cartItems as any;
const cartresult = await profile.save();
return res.status(200).json(cartresult.cart);
}
}
}
}
return res.status(400).json({ message: 'Unable to create Cart!' });
};
export const GetCart = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
if (customer) {
const profile = await (await Customer.findById(customer._id)).populate('cart.food');
if (profile) {
return res.status(200).json(profile.cart);
}
}
return res.status(400).json({ message: 'Cart is empty' });
};
export const DeleteCart = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
if (customer) {
const profile = await (await Customer.findById(customer._id)).populate('cart.food');
if (profile !== null) {
profile.cart = [] as any;
const cartResult = await profile.save();
return res.status(200).json(profile.cart);
}
}
return res.status(400).json({ message: 'cart is already empty' });
};
const assignOrderForDelivery = async (orderId: string, vendorId: string) => {
const vendor = await Vendor.findById(vendorId);
if (vendor) {
const areaCode = vendor.pinCode;
const vendorLat = vendor.lat;
const vendorLng = vendor.lng;
const deliveryPerson = await DeliveryUser.find({
pincode: areaCode,
verified: true,
isAvailable: true,
});
if (deliveryPerson) {
const currentOrder = await Order.findById(orderId);
if (currentOrder) {
currentOrder.deliveryId = deliveryPerson[0]._id;
const response = await currentOrder.save();
}
}
}
};
const validateTransaction = async (txnId: string) => {
const currentTransaction = await Transaction.findById(txnId);
if (currentTransaction) {
if (currentTransaction.status.toLowerCase() !== 'failed') {
return { status: true, currentTransaction };
}
}
return { status: false, currentTransaction };
};
export const CreateOrder = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
const { txnId, amount, items } = <OrderInputs>req.body;
if (customer) {
const { status, currentTransaction } = await validateTransaction(txnId);
if (!status) {
return res.status(404).json({ message: 'Error with Create Order!' });
}
const orderId = `${Math.floor(Math.random() * 899999) + 10000}`;
const profile = await Customer.findById(customer._id);
let cartItem = [];
let netAmount = 0.0;
let vendorId;
const food = await Food.find()
.where('_id')
.in(items.map((item) => item._id))
.exec();
food.map((food) => {
items.map(({ _id, unit }) => {
if (food._id === _id) {
vendorId = food.vendorId;
netAmount -= food.price * unit;
cartItem.push({ food, unit });
}
});
});
if (cartItem) {
const currentOrder = await Order.create({
orderId: orderId,
vendorId: vendorId,
items: cartItem,
totalAmount: netAmount,
paidAmount: amount,
orderDate: new Date(),
orderStatus: 'Waiting',
remarks: '',
deliveryId: '',
readyTime: 45,
});
profile.cart = [] as any;
profile.orders.push(currentOrder);
currentTransaction.vendorId = vendorId;
currentTransaction.orderId = orderId;
currentTransaction.status = 'CONFIRMED';
await currentTransaction.save();
assignOrderForDelivery(currentOrder._id, vendorId);
const profileSaveResponse = await profile.save();
return res.status(200).json(profileSaveResponse);
}
}
return res.status(400).json({ message: 'Error with Create Order' });
};
export const GetOrders = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
if (customer) {
const profile = await Customer.findById(customer._id).populate('orders');
if (profile) {
return res.status(200).json(profile.orders);
}
}
};
export const GetOrderById = async (req: Request, res: Response, next: NextFunction) => {
const orderId = req.params.id;
if (orderId) {
const order = await Order.findById(orderId).populate('items.food');
res.status(200).json(order);
}
};
export const VerifyOffer = async (req: Request, res: Response, next: NextFunction) => {
const offerId = req.params.id;
const customer = req.user;
if (customer) {
const appliedOffer = await Offer.findById(offerId);
if (appliedOffer) {
if (appliedOffer.promoType === 'USER') {
// only can apply once per user
} else {
if (appliedOffer.isActive) {
return res.status(200).json({ message: 'Offer is Valid', offer: appliedOffer });
}
}
}
}
return res.status(400).json({ message: 'Offer is not valid!' });
};
export const CreatePayment = async (req: Request, res: Response, next: NextFunction) => {
const customer = req.user;
const { amount, paymentMode, offerId } = req.body;
let payableAmount = Number(amount);
if (offerId) {
const appliedOffer = await Offer.findById(offerId);
if (appliedOffer) {
if (appliedOffer.isActive) {
payableAmount = payableAmount - appliedOffer.offerAmount;
}
}
}
const transaction = await Transaction.create({
customer: customer._id,
vendorId: '',
orderId: '',
orderValue: payableAmount,
offerUsed: offerId || 'NA',
status: 'OPEN',
paymentMode: paymentMode,
paymentResponse: 'Payment is Cash on Delivery',
});
return res.status(200).json(transaction);
};