/
Billing.ts
216 lines (206 loc) · 6.85 KB
/
Billing.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
import { types, params } from 'typed-graphqlify'
import { pick, preprocessArgs } from '../Utils'
import { ViewerBillingArgs } from '../generated/graphql'
import { UserKeys, UserResult, UserOption, buildUserQuery } from './Users'
import { RoomKeys, RoomResult, buildRoomQuery } from './Rooms'
import { UserProfileKeys } from './UserProfiles'
import { EmailKeys } from './Emails'
export const UsageLedger = {
id: types.string,
ledgerType: types.string,
paidSec: types.number,
timestamp: types.string,
amount: types.number
}
export type UsageLedgerType = typeof UsageLedger
export type UsageLedgerKeys = keyof UsageLedgerType
export type UsageLedgerResult<
UL extends UsageLedgerKeys,
U extends UserKeys | null = null,
U_P extends UserProfileKeys | null = null,
U_P_E extends EmailKeys | null = null,
R extends RoomKeys | null = null
> =
Pick<UsageLedgerType, UL> &
([U] extends [UserKeys] ? { user: UserResult<U, U_P, U_P_E> } : {}) &
([R] extends [RoomKeys] ? { room: RoomResult<R> } : {})
export type UsageLedgerOption<U, U_P, U_P_E, R> = {
user?: { fields: U[], with?: UserOption<U_P, U_P_E> },
room?: { fields: R[] }
}
export const BillingRecord = {
id: types.string,
date: types.string
}
export type BillingRecordType = typeof BillingRecord
export type BillingRecordKeys = keyof BillingRecordType
export type BillingRecordResult<
BR extends BillingRecordKeys,
UL extends UsageLedgerKeys | null,
UL_U extends UserKeys | null,
UL_U_P extends UserProfileKeys | null,
UL_U_P_E extends EmailKeys | null,
UL_R extends RoomKeys | null,
FUL extends UsageLedgerKeys | null,
TUL extends UsageLedgerKeys | null
> =
Pick<BillingRecordType, BR> &
([UL] extends [UsageLedgerKeys] ? { usages: UsageLedgerResult<UL, UL_U, UL_U_P, UL_U_P_E, UL_R>[] } : {}) &
([FUL] extends [UsageLedgerKeys] ? { freeCredits: UsageLedgerResult<FUL>[] } : {}) &
([TUL] extends [UsageLedgerKeys] ? { topups: UsageLedgerResult<TUL>[] } : {})
export type BillingRecordOption<
UL, UL_U, UL_U_P, UL_U_P_E, UL_R,
FUL,
TUL
> = {
usages?: { fields: UL[], with?: UsageLedgerOption<UL_U, UL_U_P, UL_U_P_E, UL_R> },
freeCredits?: { fields: FUL[] },
topups?: { fields: TUL[] }
}
export const Billing = {
id: types.string,
year: types.number,
month: types.number
}
export type BillingType = typeof Billing
export type BillingKeys = keyof BillingType
export type BillingResult<
B extends BillingKeys,
BR extends BillingRecordKeys | null,
BR_UL extends UsageLedgerKeys | null,
BR_UL_U extends UserKeys | null,
BR_UL_U_P extends UserProfileKeys | null,
BR_UL_U_P_E extends EmailKeys | null,
BR_UL_R extends RoomKeys | null,
BR_FUL extends UsageLedgerKeys | null,
BR_TUL extends UsageLedgerKeys | null
> =
Pick<BillingType, B> &
([BR] extends [BillingRecordKeys] ? {
records: BillingRecordResult<
BR, BR_UL, BR_UL_U, BR_UL_U_P, BR_UL_U_P_E, BR_UL_R, BR_FUL, BR_TUL
>[]
} : {})
export type BillingOption<
BR,
BR_UL, BR_UL_U, BR_UL_U_P, BR_UL_U_P_E, BR_UL_R,
BR_FUL,
BR_TUL
> = {
records?: { fields: BR[], with?: BillingRecordOption<BR_UL, BR_UL_U, BR_UL_U_P, BR_UL_U_P_E, BR_UL_R, BR_FUL, BR_TUL> }
}
/**
* @ignore
*/
function resolveUsageLedgerOption<
U extends UserKeys | null,
U_P extends UserProfileKeys | null,
U_P_E extends EmailKeys | null,
R extends RoomKeys | null,
>(option: UsageLedgerOption<U, U_P, U_P_E, R>) {
const user = option.user
? { user: buildUserQuery(option.user.fields as any, option.user.with)}
: {}
const room = option.room
? { room: buildRoomQuery(option.room.fields as any) }
: {}
return { ...user, ...room }
}
/**
* @ignore
*/
function buildUsageLedgerQuery<
UL extends UsageLedgerKeys,
U extends UserKeys | null = null,
U_P extends UserProfileKeys | null = null,
U_P_E extends EmailKeys | null = null,
R extends RoomKeys | null = null,
>(
fields: UL[],
option?: UsageLedgerOption<U, U_P, U_P_E, R>
): UsageLedgerResult<UL, U, U_P, U_P_E, R> {
const pickedField = pick(UsageLedger, fields)
const resolvedOption = option ? resolveUsageLedgerOption(option) : {} as any
return { ...pickedField, ...resolvedOption }
}
/**
*
* @ignore
*/
function resolveBillingRecordOption<
UL extends UsageLedgerKeys | null,
UL_U extends UserKeys | null,
UL_U_P extends UserProfileKeys | null,
UL_U_P_E extends EmailKeys | null,
UL_R extends RoomKeys | null,
FUL extends UsageLedgerKeys | null,
TUL extends UsageLedgerKeys | null
>(option: BillingRecordOption<UL, UL_U, UL_U_P, UL_U_P_E, UL_R, FUL, TUL>) {
const usages = option.usages
? { usages: buildUsageLedgerQuery(option.usages.fields as any, option.usages.with)}
: {}
const freeCredits = option.freeCredits
? { freeCredits: buildUsageLedgerQuery(option.freeCredits.fields as any)}
: {}
const topups = option.topups
? { topups: buildUsageLedgerQuery(option.topups.fields as any)}
: {}
return { ...usages, ...freeCredits, ...topups }
}
/**
* @ignore
*/
function buildBillingRecordQuery<
BR extends BillingRecordKeys,
UL extends UsageLedgerKeys | null,
UL_U extends UserKeys | null,
UL_U_P extends UserProfileKeys | null,
UL_U_P_E extends EmailKeys | null,
UL_R extends RoomKeys | null,
FUL extends UsageLedgerKeys | null,
TUL extends UsageLedgerKeys | null
>(
fields: BR[],
option?: BillingRecordOption<UL, UL_U, UL_U_P, UL_U_P_E, UL_R, FUL, TUL>
): BillingRecordResult<BR, UL, UL_U, UL_U_P, UL_U_P_E, UL_R, FUL, TUL> {
const pickedField = pick(BillingRecord, fields)
const resolvedOption = option ? resolveBillingRecordOption(option) : {} as any
return { ...pickedField, ...resolvedOption }
}
/**
* @ignore
*/
function resolveBillingOption<
BR extends BillingRecordKeys | null,
BR_UL extends UsageLedgerKeys | null,
BR_UL_U extends UserKeys | null,
BR_UL_U_P extends UserProfileKeys | null,
BR_UL_U_P_E extends EmailKeys | null,
BR_UL_R extends RoomKeys | null,
BR_FUL extends UsageLedgerKeys | null,
BR_TUL extends UsageLedgerKeys | null
>(option: BillingOption<BR, BR_UL, BR_UL_U, BR_UL_U_P, BR_UL_U_P_E, BR_UL_R, BR_FUL, BR_TUL>) {
const records = option.records
? { records: buildBillingRecordQuery(option.records.fields as any, option.records.with) }
: {}
return { ...records }
}
export function buildBillingQuery<
B extends BillingKeys,
BR extends BillingRecordKeys | null,
BR_UL extends UsageLedgerKeys | null,
BR_UL_U extends UserKeys | null,
BR_UL_U_P extends UserProfileKeys | null,
BR_UL_U_P_E extends EmailKeys | null,
BR_UL_R extends RoomKeys | null,
BR_FUL extends UsageLedgerKeys | null,
BR_TUL extends UsageLedgerKeys | null
>(
fields: B[],
args: ViewerBillingArgs,
option?: BillingOption<BR, BR_UL, BR_UL_U, BR_UL_U_P, BR_UL_U_P_E, BR_UL_R, BR_FUL, BR_TUL>
) {
const pickedField = pick(Billing, fields)
const resolvedOption = option ? resolveBillingOption(option) : {} as any
return params(preprocessArgs(args), { ...pickedField, ...resolvedOption })
}