@@ -8,27 +8,18 @@ import {
8
8
import { PrismaPromise } from '@prisma/client' ;
9
9
import { PrismaService } from 'src/shared/global/prisma.service' ;
10
10
11
- import {
12
- DateFilterType ,
13
- ResponseDto ,
14
- WinningAuditDto ,
15
- WinningRequestDto ,
16
- SearchWinningResult ,
17
- WinningUpdateRequestDto ,
18
- PaymentStatus ,
19
- AuditPayoutDto ,
20
- WinningsCategory ,
21
- } from 'src/dto/adminWinning.dto' ;
22
11
import { TaxFormRepository } from '../repository/taxForm.repo' ;
23
12
import { PaymentMethodRepository } from '../repository/paymentMethod.repo' ;
24
-
25
- const ONE_DAY = 24 * 60 * 60 * 1000 ;
13
+ import { ResponseDto } from 'src/dto/api-response.dto' ;
14
+ import { PaymentStatus } from 'src/dto/payment.dto' ;
15
+ import { WinningAuditDto , AuditPayoutDto } from './dto/audit.dto' ;
16
+ import { WinningUpdateRequestDto } from './dto/winnings.dto' ;
26
17
27
18
/**
28
19
* The admin winning service.
29
20
*/
30
21
@Injectable ( )
31
- export class AdminWinningService {
22
+ export class AdminService {
32
23
/**
33
24
* Constructs the admin winning service with the given dependencies.
34
25
* @param prisma the prisma service.
@@ -39,202 +30,6 @@ export class AdminWinningService {
39
30
private readonly paymentMethodRepo : PaymentMethodRepository ,
40
31
) { }
41
32
42
- /**
43
- * Search winnings with parameters
44
- * @param body the request body
45
- * @returns the Promise with response result
46
- */
47
- async searchWinnings (
48
- body : WinningRequestDto ,
49
- ) : Promise < ResponseDto < SearchWinningResult > > {
50
- const result = new ResponseDto < SearchWinningResult > ( ) ;
51
-
52
- try {
53
- let winnerIds : string [ ] | undefined ;
54
- let externalIds : string [ ] | undefined ;
55
- if ( body . winnerId ) {
56
- winnerIds = [ body . winnerId ] ;
57
- } else if ( body . winnerIds ) {
58
- winnerIds = [ ...body . winnerIds ] ;
59
- } else if ( body . externalIds ?. length > 0 ) {
60
- externalIds = body . externalIds ;
61
- }
62
-
63
- const queryWhere = this . getQueryByWinnerId ( body , winnerIds , externalIds ) ;
64
- const orderBy = this . getOrderByWithWinnerId (
65
- body ,
66
- ! winnerIds && ! ! externalIds ?. length ,
67
- ) ;
68
-
69
- const [ winnings , count ] = await this . prisma . $transaction ( [
70
- this . prisma . winnings . findMany ( {
71
- ...queryWhere ,
72
- include : {
73
- payment : {
74
- where : {
75
- installment_number : 1 ,
76
- } ,
77
- orderBy : [
78
- {
79
- created_at : 'desc' ,
80
- } ,
81
- ] ,
82
- } ,
83
- origin : true ,
84
- } ,
85
- orderBy,
86
- skip : body . offset ,
87
- take : body . limit ,
88
- } ) ,
89
- this . prisma . winnings . count ( { where : queryWhere . where } ) ,
90
- ] ) ;
91
-
92
- result . data = {
93
- winnings : winnings . map ( ( item ) => ( {
94
- id : item . winning_id ,
95
- type : item . type ,
96
- winnerId : item . winner_id ,
97
- origin : item . origin ?. origin_name ,
98
- category : ( item . category ?? '' ) as WinningsCategory ,
99
- title : item . title as string ,
100
- description : item . description as string ,
101
- externalId : item . external_id as string ,
102
- attributes : ( item . attributes ?? { } ) as object ,
103
- details : item . payment ?. map ( ( paymentItem ) => ( {
104
- id : paymentItem . payment_id ,
105
- netAmount : Number ( paymentItem . net_amount ) ,
106
- grossAmount : Number ( paymentItem . gross_amount ) ,
107
- totalAmount : Number ( paymentItem . total_amount ) ,
108
- installmentNumber : paymentItem . installment_number as number ,
109
- datePaid : ( paymentItem . date_paid ?? undefined ) as Date ,
110
- status : paymentItem . payment_status as PaymentStatus ,
111
- currency : paymentItem . currency as string ,
112
- releaseDate : paymentItem . release_date as Date ,
113
- category : item . category as string ,
114
- billingAccount : paymentItem . billing_account ,
115
- } ) ) ,
116
- createdAt : item . created_at as Date ,
117
- updatedAt : ( item . payment ?. [ 0 ] . date_paid ??
118
- item . payment ?. [ 0 ] . updated_at ??
119
- undefined ) as Date ,
120
- releaseDate : item . payment ?. [ 0 ] ?. release_date as Date ,
121
- } ) ) ,
122
- pagination : {
123
- totalItems : count ,
124
- totalPages : Math . ceil ( count / body . limit ) ,
125
- pageSize : body . limit ,
126
- currentPage : Math . ceil ( body . offset / body . limit ) + 1 ,
127
- } ,
128
- } ;
129
- // response.data = winnings as any
130
- } catch ( error ) {
131
- console . error ( 'Searching winnings failed' , error ) ;
132
- const message = 'Searching winnings failed. ' + error ;
133
- result . error = {
134
- code : HttpStatus . INTERNAL_SERVER_ERROR ,
135
- message,
136
- } ;
137
- }
138
-
139
- return result ;
140
- }
141
-
142
- private generateFilterDate ( body : WinningRequestDto ) {
143
- let filterDate : object | undefined ;
144
- const currentDay = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
145
- switch ( body . date ) {
146
- case DateFilterType . LAST7DAYS :
147
- // eslint-disable-next-line no-case-declarations
148
- const last7days = new Date ( currentDay . getTime ( ) - 6 * ONE_DAY ) ;
149
- filterDate = {
150
- gte : last7days ,
151
- } ;
152
- break ;
153
- case DateFilterType . LAST30DAYS :
154
- // eslint-disable-next-line no-case-declarations
155
- const last30days = new Date ( currentDay . getTime ( ) - 29 * ONE_DAY ) ;
156
- filterDate = {
157
- gte : last30days ,
158
- } ;
159
- break ;
160
- case DateFilterType . ALL :
161
- filterDate = undefined ;
162
- break ;
163
- default :
164
- break ;
165
- }
166
- return filterDate ;
167
- }
168
-
169
- private getQueryByWinnerId (
170
- body : WinningRequestDto ,
171
- winnerIds : string [ ] | undefined ,
172
- externalIds : string [ ] | undefined ,
173
- ) {
174
- const filterDate : object | undefined = this . generateFilterDate ( body ) ;
175
-
176
- const query = {
177
- where : {
178
- winner_id : winnerIds
179
- ? {
180
- in : winnerIds ,
181
- }
182
- : undefined ,
183
- external_id : externalIds
184
- ? {
185
- in : body . externalIds ,
186
- }
187
- : undefined ,
188
- category : body . type
189
- ? {
190
- equals : body . type ,
191
- }
192
- : undefined ,
193
- created_at : filterDate ,
194
- payment : body . status
195
- ? {
196
- some : {
197
- payment_status : {
198
- equals : body . status ,
199
- } ,
200
- installment_number : {
201
- equals : 1 ,
202
- } ,
203
- } ,
204
- }
205
- : {
206
- some : {
207
- installment_number : {
208
- equals : 1 ,
209
- } ,
210
- } ,
211
- } ,
212
- } ,
213
- } ;
214
-
215
- return query ;
216
- }
217
-
218
- private getOrderByWithWinnerId (
219
- body : WinningRequestDto ,
220
- externalIds ?: boolean ,
221
- ) {
222
- const orderBy : object = [
223
- {
224
- created_at : 'desc' ,
225
- } ,
226
- ...( externalIds ? [ { external_id : 'asc' } ] : [ ] ) ,
227
- ] ;
228
-
229
- if ( body . sortBy && body . sortOrder ) {
230
- orderBy [ 0 ] = {
231
- [ body . sortBy ] : body . sortOrder . toString ( ) ,
232
- } ;
233
- }
234
-
235
- return orderBy ;
236
- }
237
-
238
33
private getPaymentsByWinningsId ( winningsId : string , paymentId ?: string ) {
239
34
return this . prisma . payment . findMany ( {
240
35
where : {
0 commit comments