@@ -14,15 +14,17 @@ import { AccountLimitError } from './errors';
14
14
15
15
const config = useRuntimeConfig ( ) ;
16
16
17
- export default class AccountService {
18
- async getAccountById ( account_id : number ) : Promise < AccountWithMembers > {
17
+ export namespace AccountService {
18
+ export async function getAccountById (
19
+ account_id : number
20
+ ) : Promise < AccountWithMembers > {
19
21
return prisma_client . account . findFirstOrThrow ( {
20
22
where : { id : account_id } ,
21
23
...accountWithMembers
22
24
} ) ;
23
25
}
24
26
25
- async getAccountByJoinPassword (
27
+ export async function getAccountByJoinPassword (
26
28
join_password : string
27
29
) : Promise < AccountWithMembers > {
28
30
return prisma_client . account . findFirstOrThrow ( {
@@ -31,14 +33,16 @@ export default class AccountService {
31
33
} ) ;
32
34
}
33
35
34
- async getAccountMembers ( account_id : number ) : Promise < MembershipWithUser [ ] > {
36
+ export async function getAccountMembers (
37
+ account_id : number
38
+ ) : Promise < MembershipWithUser [ ] > {
35
39
return prisma_client . membership . findMany ( {
36
40
where : { account_id } ,
37
41
...membershipWithUser
38
42
} ) ;
39
43
}
40
44
41
- async updateAccountStipeCustomerId (
45
+ export async function updateAccountStipeCustomerId (
42
46
account_id : number ,
43
47
stripe_customer_id : string
44
48
) {
@@ -50,7 +54,7 @@ export default class AccountService {
50
54
} ) ;
51
55
}
52
56
53
- async updateStripeSubscriptionDetailsForAccount (
57
+ export async function updateStripeSubscriptionDetailsForAccount (
54
58
stripe_customer_id : string ,
55
59
stripe_subscription_id : string ,
56
60
current_period_ends : Date ,
@@ -93,7 +97,7 @@ export default class AccountService {
93
97
}
94
98
}
95
99
96
- async acceptPendingMembership (
100
+ export async function acceptPendingMembership (
97
101
account_id : number ,
98
102
membership_id : number
99
103
) : Promise < MembershipWithAccount > {
@@ -118,7 +122,7 @@ export default class AccountService {
118
122
} ) ;
119
123
}
120
124
121
- async deleteMembership (
125
+ export async function deleteMembership (
122
126
account_id : number ,
123
127
membership_id : number
124
128
) : Promise < MembershipWithAccount > {
@@ -140,7 +144,7 @@ export default class AccountService {
140
144
} ) ;
141
145
}
142
146
143
- async joinUserToAccount (
147
+ export async function joinUserToAccount (
144
148
user_id : number ,
145
149
account_id : number ,
146
150
pending : boolean
@@ -179,7 +183,10 @@ export default class AccountService {
179
183
} ) ;
180
184
}
181
185
182
- async changeAccountName ( account_id : number , new_name : string ) {
186
+ export async function changeAccountName (
187
+ account_id : number ,
188
+ new_name : string
189
+ ) {
183
190
return prisma_client . account . update ( {
184
191
where : { id : account_id } ,
185
192
data : {
@@ -188,7 +195,7 @@ export default class AccountService {
188
195
} ) ;
189
196
}
190
197
191
- async changeAccountPlan ( account_id : number , plan_id : number ) {
198
+ export async function changeAccountPlan ( account_id : number , plan_id : number ) {
192
199
const plan = await prisma_client . plan . findFirstOrThrow ( {
193
200
where : { id : plan_id }
194
201
} ) ;
@@ -202,7 +209,7 @@ export default class AccountService {
202
209
} ) ;
203
210
}
204
211
205
- async rotateJoinPassword ( account_id : number ) {
212
+ export async function rotateJoinPassword ( account_id : number ) {
206
213
const join_password : string = generator . generate ( {
207
214
length : 10 ,
208
215
numbers : true
@@ -217,7 +224,7 @@ export default class AccountService {
217
224
// User must already be an ADMIN for the Account
218
225
// Existing OWNER memberships are downgraded to ADMIN
219
226
// In future, some sort of Billing/Stripe tie in here e.g. changing email details on the Account, not sure.
220
- async claimOwnershipOfAccount (
227
+ export async function claimOwnershipOfAccount (
221
228
user_id : number ,
222
229
account_id : number
223
230
) : Promise < MembershipWithUser [ ] > {
@@ -278,7 +285,7 @@ export default class AccountService {
278
285
}
279
286
280
287
// Upgrade access of a membership. Cannot use this method to upgrade to or downgrade from OWNER access
281
- async changeUserAccessWithinAccount (
288
+ export async function changeUserAccessWithinAccount (
282
289
user_id : number ,
283
290
account_id : number ,
284
291
access : ACCOUNT_ACCESS
@@ -333,15 +340,14 @@ export default class AccountService {
333
340
Note.. for each usage limit, you will need another pair of check/increment methods and of course the count and max limit in the account schema
334
341
335
342
How to use in a service method....
336
- async someServiceMethod(account_id: number, .....etc) {
337
- const accountService = new AccountService();
338
- const account = await accountService.checkAIGenCount(account_id);
343
+ export async function someServiceMethod(account_id: number, .....etc) {
344
+ const account = await AccountService.checkAIGenCount(account_id);
339
345
... User is under the limit so do work
340
- await accountService .incrementAIGenCount(account);
346
+ await AccountService .incrementAIGenCount(account);
341
347
}
342
348
*/
343
349
344
- async getAccountWithPeriodRollover ( account_id : number ) {
350
+ export async function getAccountWithPeriodRollover ( account_id : number ) {
345
351
const account = await prisma_client . account . findFirstOrThrow ( {
346
352
where : { id : account_id }
347
353
} ) ;
@@ -366,8 +372,8 @@ export default class AccountService {
366
372
return account ;
367
373
}
368
374
369
- async checkAIGenCount ( account_id : number ) {
370
- const account = await this . getAccountWithPeriodRollover ( account_id ) ;
375
+ export async function checkAIGenCount ( account_id : number ) {
376
+ const account = await getAccountWithPeriodRollover ( account_id ) ;
371
377
372
378
if ( account . ai_gen_count >= account . ai_gen_max_pm ) {
373
379
throw new AccountLimitError (
@@ -378,7 +384,7 @@ export default class AccountService {
378
384
return account ;
379
385
}
380
386
381
- async incrementAIGenCount ( account : any ) {
387
+ export async function incrementAIGenCount ( account : any ) {
382
388
return await prisma_client . account . update ( {
383
389
where : { id : account . id } ,
384
390
data : {
0 commit comments