1
+ import { catchAsyncError } from '../../utils/catch_async_error' ;
2
+ import { AppError } from '../../utils/app_error' ;
3
+ import { deleteOne } from '../../handler/factor' ;
4
+ import { ApiFeatures } from '../../utils/api_feature' ;
5
+ import { couponModel } from '../../models/coupon_model' ;
6
+
7
+ const createCoupon = catchAsyncError ( async ( req , res , next ) => {
8
+ const createCoupon = new couponModel ( req . body ) ;
9
+ await createCoupon . save ( ) ;
10
+
11
+ res . status ( 201 ) . json ( { message : "success" , createCoupon } ) ;
12
+ } ) ;
13
+
14
+ const getAllCoupons = catchAsyncError ( async ( req , res , next ) => {
15
+ let apiFeature = new ApiFeatures ( couponModel . find ( ) , req . query )
16
+ . pagination ( )
17
+ . fields ( )
18
+ . filteration ( )
19
+ . search ( )
20
+ . sort ( ) ;
21
+ const PAGE_NUMBER = apiFeature . queryString . page * 1 || 1 ;
22
+ const getAllCoupons = await apiFeature . mongooseQuery ;
23
+
24
+ res
25
+ . status ( 201 )
26
+ . json ( { page : PAGE_NUMBER , message : "success" , getAllCoupons } ) ;
27
+ } ) ;
28
+
29
+ const updateCoupon = catchAsyncError ( async ( req , res , next ) => {
30
+ const { id } = req . params ;
31
+ const updateCoupon = await couponModel . findByIdAndUpdate ( id , req . body , {
32
+ new : true ,
33
+ } ) ;
34
+
35
+ updateCoupon && res . status ( 201 ) . json ( { message : "success" , updateCoupon } ) ;
36
+
37
+ ! updateCoupon && next ( new AppError ( "Coupon was not found" , 404 ) ) ;
38
+ } ) ;
39
+
40
+ const deleteCoupon = deleteOne ( couponModel , "Coupon" ) ;
41
+ export {
42
+ createCoupon ,
43
+ getAllCoupons ,
44
+ getSpecificCoupon ,
45
+ updateCoupon ,
46
+ deleteCoupon ,
47
+ } ;
0 commit comments