-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathuser.controller.ts
More file actions
199 lines (183 loc) · 6.07 KB
/
user.controller.ts
File metadata and controls
199 lines (183 loc) · 6.07 KB
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
import { ApiTags, ApiOperation, ApiSecurity } from '@nestjs/swagger';
import { Body, Controller, Delete, Get, Param, Put, Query, UseGuards } from '@nestjs/common';
import {
GetImportCounts,
CancelSubscription,
GetActiveSubscription,
UpdatePaymentMethod,
ConfirmIntentId,
DeleteUserPaymentMethod,
GetTransactionHistory,
ApplyCoupon,
Checkout,
Subscription,
RetrievePaymentMethods,
UpdateSubscriptionPaymentMethod,
} from './usecases';
import { JwtAuthGuard } from '@shared/framework/auth.gaurd';
import { IJwtPayload, ACCESS_KEY_NAME, Defaults } from '@impler/shared';
import { UserSession } from '@shared/framework/user.decorator';
import { CancelSubscriptionDto } from './dto/cancel-subscription.dto';
@ApiTags('User')
@Controller('/user')
@UseGuards(JwtAuthGuard)
@ApiSecurity(ACCESS_KEY_NAME)
export class UserController {
constructor(
private checkout: Checkout,
private applyCoupon: ApplyCoupon,
private getImportsCount: GetImportCounts,
private getActiveSubscription: GetActiveSubscription,
private cancelSubscription: CancelSubscription,
private updatePaymentMethod: UpdatePaymentMethod,
private confirmIntentId: ConfirmIntentId,
private getTransactionHistory: GetTransactionHistory,
private retrivePaymentMethods: RetrievePaymentMethods,
private deleteUserPaymentMethod: DeleteUserPaymentMethod,
private subscription: Subscription,
private updateSubscriptionPaymentMethod: UpdateSubscriptionPaymentMethod
) {}
@Get('/import-count')
@ApiOperation({
summary: 'Get Import Count',
})
@UseGuards(JwtAuthGuard)
async getImportCountRoute(
@UserSession() user: IJwtPayload,
@Query('start') start: string,
@Query('end') end: string
) {
return this.getImportsCount.execute({
_userId: user._id,
start,
end,
});
}
@Get('/subscription')
@ApiOperation({
summary: 'Get Active Subscription Information',
})
async getActiveSubscriptionRoute(@UserSession() user: IJwtPayload) {
return this.getActiveSubscription.execute(user._projectId);
}
@Delete('/subscription')
@ApiOperation({
summary: 'Cancel active subscription for user',
})
async cancelSubscriptionRoute(
@UserSession() user: IJwtPayload,
@Body() cancelSubscriptionDto: CancelSubscriptionDto
) {
return this.cancelSubscription.execute(user._projectId, cancelSubscriptionDto.reasons);
}
@Put('/setup-intent/:paymentMethodId')
@ApiOperation({
summary: 'Setup User Payment Intent',
})
async setupEmandateIntent(@UserSession() user: IJwtPayload, @Param('paymentMethodId') paymentMethodId: string) {
return this.updatePaymentMethod.execute(user._projectId, paymentMethodId);
}
@Put('/payment-method/:paymentMethodId')
@ApiOperation({
summary: 'Update User Payment Method',
})
async updatePaymentMethodRoute(@UserSession() user: IJwtPayload, @Param('paymentMethodId') paymentMethodId: string) {
return this.updatePaymentMethod.execute(user._projectId, paymentMethodId);
}
@Put('/subscription-payment-method/:paymentMethodId')
@ApiOperation({
summary: 'Update User Payment Method',
})
async updateSubscriptionPaymentMethodRoute(
@UserSession() user: IJwtPayload,
@Param('paymentMethodId') paymentMethodId: string
) {
return this.updateSubscriptionPaymentMethod.execute(user._projectId, paymentMethodId);
}
@Put('/confirm-payment-intent-id/:intentId')
@ApiOperation({
summary: 'Pass the Payment Intent Id If user cancels the E-Mandate Authorization',
})
async savePaymentIntentIdRoute(@UserSession() user: IJwtPayload, @Param('intentId') intentId: string) {
return this.confirmIntentId.execute(user._projectId, intentId);
}
@Get('/payment-methods')
@ApiOperation({
summary: 'Retrieve the cards of the User',
})
async retriveUserPaymentMethods(@UserSession() user: IJwtPayload) {
return this.retrivePaymentMethods.execute(user._projectId);
}
@Delete('/payment-methods/:paymentMethodId')
@ApiOperation({
summary: 'Detach or Delete the card of the User',
})
async deletePaymentMethodRoute(@Param('paymentMethodId') paymentMethodId: string) {
return this.deleteUserPaymentMethod.execute(paymentMethodId);
}
@Get('/transactions/history')
@ApiOperation({
summary: 'Get Transaction History for User',
})
async getTransactionHistoryRoute(
@UserSession() user: IJwtPayload,
@Query('page') page = Defaults.ONE,
@Query('limit') limit = Defaults.PAGE_LIMIT
) {
if (isNaN(page)) page = Defaults.ONE;
else page = Number(page);
if (isNaN(limit)) limit = Defaults.PAGE_LIMIT;
else limit = Number(limit);
return this.getTransactionHistory.execute({
projectId: user._projectId,
limit,
page,
});
}
@Get('/coupons/:couponCode/apply/:planCode')
@ApiOperation({
summary:
'Check if a Particular coupon is available to apply for a particular plan and if the coupon is valid or not',
})
async applyCouponRoute(
@UserSession() user: IJwtPayload,
@Param('couponCode') couponCode: string,
@Param('planCode') planCode: string
) {
return this.applyCoupon.execute(couponCode, user.email, planCode);
}
@Get('/checkout')
@ApiOperation({
summary: 'Get Tax Information and checkout details',
})
async checkoutRoute(
@Query('planCode') planCode: string,
@UserSession() user: IJwtPayload,
@Query('paymentMethodId') paymentMethodId: string,
@Query('couponCode') couponCode?: string
) {
return this.checkout.execute({
planCode: planCode,
projectId: user._projectId,
paymentMethodId: paymentMethodId,
couponCode: couponCode,
});
}
@Get('/subscribe')
@ApiOperation({
summary: 'Make successful Plan Purchase and begin subscription',
})
async newSubscriptionRoute(
@Query('planCode') planCode: string,
@UserSession() user: IJwtPayload,
@Query('paymentMethodId') paymentMethodId: string,
@Query('couponCode') couponCode?: string
) {
return await this.subscription.execute({
projectId: user._projectId,
planCode,
selectedPaymentMethod: paymentMethodId,
couponCode,
});
}
}