Skip to content

Commit

Permalink
refactor(server): refactor auth api(profile/pat) (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Jun 7, 2023
1 parent 95bc353 commit b3d03ae
Show file tree
Hide file tree
Showing 53 changed files with 251 additions and 155 deletions.
2 changes: 1 addition & 1 deletion cli/src/api/pat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { request } from '../util/request'

export async function pat2token(server: string, data: { pat: string }): Promise<any> {
return request({
url: server + `/v1/pat2token`,
url: server + `/v1/auth/pat2token`,
method: 'POST',
data: data,
})
Expand Down
4 changes: 2 additions & 2 deletions cli/src/api/v1/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import {
* @tags Authentication
* @name AuthControllerPat2Token
* @summary Get user token by PAT
* @request POST:/v1/pat2token
* @request POST:/v1/auth/pat2token
*/
export async function authControllerPat2Token(data: Pat2TokenDto, configParams: RequestParams = {}): Promise<any> {
return request({
url: `/v1/pat2token`,
url: `/v1/auth/pat2token`,
method: "POST",
data: data,
...configParams,
Expand Down
2 changes: 1 addition & 1 deletion server/src/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
UseGuards,
} from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { JwtAuthGuard } from 'src/auth/jwt.auth.guard'
import { IRequest, IResponse } from 'src/utils/interface'
import { ApiResponseObject, ResponseUtil } from 'src/utils/response'
import { AccountService } from './account.service'
Expand All @@ -35,6 +34,7 @@ import { ObjectId } from 'mongodb'
import { SystemDatabase } from 'src/system-database'
import { Account } from './entities/account'
import { AccountTransaction } from './entities/account-transaction'
import { JwtAuthGuard } from 'src/authentication/jwt.auth.guard'

@ApiTags('Account')
@Controller('accounts')
Expand Down
63 changes: 62 additions & 1 deletion server/src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { PaymentChannelService } from './payment/payment-channel.service'
import { TASK_LOCK_INIT_TIME } from 'src/constants'
import { SystemDatabase } from 'src/system-database'
import { Account, BaseState } from './entities/account'
import { ObjectId } from 'mongodb'
import { ObjectId, ClientSession } from 'mongodb'
import {
AccountChargeOrder,
AccountChargePhase,
Currency,
PaymentChannelType,
} from './entities/account-charge-order'
import { AccountTransaction } from './entities/account-transaction'

@Injectable()
export class AccountService {
Expand Down Expand Up @@ -47,6 +48,66 @@ export class AccountService {
return await this.create(userid)
}

async chargeWithTransaction(
accountId: ObjectId,
amount: number,
message: string,
) {
const client = SystemDatabase.client
const session = client.startSession()
session.startTransaction()

try {
const result = await this.chargeWithSession(
accountId,
amount,
message,
session,
)
await session.commitTransaction()
return result
} catch (error) {
await session.abortTransaction()
throw error
} finally {
session.endSession()
}
}

async chargeWithSession(
accountId: ObjectId,
amount: number,
message: string,
session: ClientSession,
) {
const _amount = Math.round(amount * 100)

// update account balance
const res = await this.db
.collection<Account>('Account')
.findOneAndUpdate(
{ _id: accountId },
{ $inc: { balance: _amount }, $set: { updatedAt: new Date() } },
{ session, returnDocument: 'after' },
)

// add transaction record
await this.db
.collection<AccountTransaction>('AccountTransaction')
.insertOne(
{
accountId: accountId,
amount: _amount,
balance: res.value.balance,
message: message,
createdAt: new Date(),
},
{ session },
)

return res.value
}

async createChargeOrder(
userid: ObjectId,
amount: number,
Expand Down
16 changes: 16 additions & 0 deletions server/src/account/entities/account-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { ObjectId } from 'mongodb'

export class AccountTransaction {
@ApiProperty({ type: String })
_id?: ObjectId

@ApiProperty({ type: String })
accountId: ObjectId

@ApiProperty()
amount: number

@ApiProperty()
balance: number

@ApiProperty()
message: string

@ApiPropertyOptional({ type: String })
orderId?: ObjectId

@ApiPropertyOptional({ type: String })
billingId?: ObjectId

@ApiProperty()
createdAt: Date

constructor(partial: Partial<AccountTransaction>) {
Expand Down
14 changes: 14 additions & 0 deletions server/src/account/entities/payment-channel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { ObjectId } from 'mongodb'
import { PaymentChannelType } from './account-charge-order'
import { BaseState } from './account'
import { ApiProperty } from '@nestjs/swagger'

export class PaymentChannel<S = any> {
@ApiProperty({ type: String })
_id?: ObjectId

@ApiProperty({ enum: PaymentChannelType, type: String })
type: PaymentChannelType

@ApiProperty()
name: string

@ApiProperty()
spec: S

@ApiProperty({ type: String, enum: BaseState })
state: BaseState

@ApiProperty()
createdAt: Date

@ApiProperty()
updatedAt: Date

constructor(partial: Partial<PaymentChannel<S>>) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Logger } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { ResponseUtil } from './utils/response'
import { ApiResponseArray, ResponseUtil } from './utils/response'
import { SystemDatabase } from './system-database'
import { Runtime } from './application/entities/runtime'

Expand All @@ -15,6 +15,7 @@ export class AppController {
* @returns
*/
@ApiOperation({ summary: 'Get application runtime list' })
@ApiResponseArray(Runtime)
@Get('runtimes')
async getRuntimes() {
const data = await this.db.collection<Runtime>('Runtime').find({}).toArray()
Expand Down
4 changes: 2 additions & 2 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { WebsiteModule } from './website/website.module'
import { FunctionModule } from './function/function.module'
import { HttpModule } from '@nestjs/axios'
import { ApplicationModule } from './application/application.module'
import { AuthModule } from './auth/auth.module'
import { ThrottlerModule } from '@nestjs/throttler'
import { InitializerModule } from './initializer/initializer.module'
import { InstanceModule } from './instance/instance.module'
Expand All @@ -22,6 +21,7 @@ import { SettingModule } from './setting/setting.module'
import * as path from 'path'
import { AcceptLanguageResolver, I18nModule, QueryResolver } from 'nestjs-i18n'
import { BillingModule } from './billing/billing.module'
import { AuthenticationModule } from './authentication/authentication.module'

@Module({
imports: [
Expand All @@ -33,7 +33,7 @@ import { BillingModule } from './billing/billing.module'
FunctionModule,
WebsiteModule,
HttpModule,
AuthModule,
AuthenticationModule,
ApplicationModule,
InitializerModule,
InstanceModule,
Expand Down
4 changes: 2 additions & 2 deletions server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
} from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { IRequest } from '../utils/interface'
import { JwtAuthGuard } from '../auth/jwt.auth.guard'
import { JwtAuthGuard } from '../authentication/jwt.auth.guard'
import {
ApiResponseArray,
ApiResponseObject,
ResponseUtil,
} from '../utils/response'
import { ApplicationAuthGuard } from '../auth/application.auth.guard'
import { ApplicationAuthGuard } from '../authentication/application.auth.guard'
import {
UpdateApplicationBundleDto,
UpdateApplicationNameDto,
Expand Down
4 changes: 2 additions & 2 deletions server/src/application/environment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
import { ApplicationAuthGuard } from 'src/auth/application.auth.guard'
import { JwtAuthGuard } from 'src/auth/jwt.auth.guard'
import { ApplicationAuthGuard } from 'src/authentication/application.auth.guard'
import { JwtAuthGuard } from 'src/authentication/jwt.auth.guard'
import { ResponseUtil } from 'src/utils/response'
import { EnvironmentVariableService } from './environment.service'
import { CreateEnvironmentDto } from './dto/create-env.dto'
Expand Down
50 changes: 0 additions & 50 deletions server/src/auth/auth.controller.ts

This file was deleted.

40 changes: 0 additions & 40 deletions server/src/auth/auth.service.ts

This file was deleted.

File renamed without changes.
Loading

0 comments on commit b3d03ae

Please sign in to comment.