Bläddra i källkod

feat(): 受益人地址api对接

max 7 månader sedan
förälder
incheckning
cba42c319c

+ 71 - 67
src/modules/api/controller/beneficiaryAddress.ts

@@ -8,8 +8,12 @@ import {
   Post,
   Provide,
   Put,
+  Query,
+  Param,
 } from '@midwayjs/decorator';
 import { BusinessEntity } from '../../payment/entity/business';
+import { PayeeAddressService } from '../../payment/service/payee_address';
+import { BeneficiaryAddressService } from '../service/beneficiaryAddress';
 
 /**
  * 受益人地址
@@ -17,57 +21,56 @@ import { BusinessEntity } from '../../payment/entity/business';
 @Provide()
 @CoolController('/api/v1/Fiat')
 export class BeneficiaryAddressController extends BaseController {
+  @Inject()
+  payeeAddressService: PayeeAddressService;
+
+  @Inject()
+  beneficiaryAddressService: BeneficiaryAddressService;
+
   /**
    * 创建受益人地址
    * /api/v3/Fiat/BeneficiaryAddress
    */
   @Post('/BeneficiaryAddress', { summary: '创建受益人地址' })
-  async createBeneficiaryAddress(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async createBeneficiaryAddress(@Body(ALL) params: BusinessEntity) {
+    return this.ok(await this.payeeAddressService.add(params));
   }
   /**
    * 获取受益人地址必填字段
    * /api/v3/Fiat/BeneficiaryAddressRequiredFields
    */
-  @Get('/BeneficiaryAddressRequiredFields', {
-    summary: '获取受益人地址必填字段',
-  })
-  async getBeneficiaryAddressRequiredFields(
-    @Body(ALL) business: BusinessEntity
-  ) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
-  }
+  // @Get('/BeneficiaryAddressRequiredFields', {
+  //   summary: '获取受益人地址必填字段',
+  // })
+  // async getBeneficiaryAddressRequiredFields(
+  //   @Body(ALL) business: BusinessEntity
+  // ) {
+  //   // if (!this.allowKeys.includes(key)) {
+  //   //   return this.fail('非法操作');
+  //   // }
+  //   // 关键参数校验
+  //   // 数据落库
+  //   // 回调
+  //   console.log(business);
+  //   return this.ok('hello, cool-admin!!!');
+  // }
   /**
    * 验证受益人地址必填字段
    * /api/v3/Fiat/BeneficiaryAddress/Validate
    */
-  @Post('/BeneficiaryAddress/Validate', {
-    summary: '验证受益人地址必填字段',
-  })
-  async BeneficiaryAddressValidate(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
-  }
+  // @Post('/BeneficiaryAddress/Validate', {
+  //   summary: '验证受益人地址必填字段',
+  // })
+  // async BeneficiaryAddressValidate(@Body(ALL) business: BusinessEntity) {
+  //   // if (!this.allowKeys.includes(key)) {
+  //   //   return this.fail('非法操作');
+  //   // }
+  //   // 关键参数校验
+  //   // 数据落库
+  //   // 回调
+  //   console.log(business);
+  //   return this.ok('hello, cool-admin!!!');
+  // }
   /**
    * 修改受益人地址
    * /api/v3/Fiat/BeneficiaryAddress
@@ -75,15 +78,8 @@ export class BeneficiaryAddressController extends BaseController {
   @Put('/BeneficiaryAddress', {
     summary: '修改受益人地址',
   })
-  async updateBeneficiaryAddress(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async updateBeneficiaryAddress(@Body(ALL) params: BusinessEntity) {
+    return this.ok(await this.payeeAddressService.update(params));
   }
   /**
    * 获取受益人地址
@@ -92,15 +88,9 @@ export class BeneficiaryAddressController extends BaseController {
   @Get('/BeneficiaryAddress/:id', {
     summary: '获取受益人地址',
   })
-  async getBeneficiaryAddressForId(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async getBeneficiaryAddressForId(@Param('id') id: string) {
+    console.log(98989, id);
+    return this.ok(await this.beneficiaryAddressService.info(id));
   }
   /**
    * 获取受益人地址列表
@@ -109,32 +99,46 @@ export class BeneficiaryAddressController extends BaseController {
   @Get('/BeneficiaryAddress', {
     summary: '获取受益人地址列表',
   })
-  async getBeneficiaryAddress(@Body(ALL) business: BusinessEntity) {
+  async getBeneficiaryAddress(
+    @Query('customer_id') customerId: string,
+    @Query('currency') currency: string,
+    @Query('payment_type') paymentType: string,
+    @Query('page_index') pageIndex: number,
+    @Query('page_size') pageSize: number
+  ) {
     // if (!this.allowKeys.includes(key)) {
     //   return this.fail('非法操作');
     // }
     // 关键参数校验
     // 数据落库
     // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+    // http://127.0.0.1:8008/api/v1/Fiat/BeneficiaryAddress?page_size=12&page_index=1&payment_type=&currency=&customer_id=18
+    const result = await this.beneficiaryAddressService.getBeneficiaryAddress(
+      customerId,
+      currency,
+      paymentType,
+      pageIndex,
+      pageSize
+    );
+    return this.ok({
+      success: true,
+      data: result.data,
+      total: result.total,
+    });
   }
   /**
    * 删除受益人地址
    * /api/v3/Fiat/BeneficiaryAddress/{id}
    */
   // delete 请求存疑
-  @Del('/BeneficiaryAddress/:id', {
+  @Del('/DelBeneficiaryAddress/:id', {
     summary: '删除受益人地址',
   })
   async deleteBeneficiaryAddressForId(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+    return this.ok(
+      await this.payeeAddressService.delete({
+        id: business.id,
+      })
+    );
   }
 }

+ 64 - 0
src/modules/api/service/beneficiaryAddress.ts

@@ -0,0 +1,64 @@
+import {
+  BaseService,
+  CoolCommException,
+  CoolTransaction,
+} from '@cool-midway/core';
+import { Provide } from '@midwayjs/core';
+import { Inject } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typeorm';
+import { PayeeAddressEntity } from '../../payment/entity/payee_address';
+import { Repository } from 'typeorm';
+import { CustomerEntity } from '../../payment/entity/customer';
+import { PaymentService } from '../../payment/service/payment';
+
+/**
+ * 描述
+ */
+@Provide()
+export class BeneficiaryAddressService extends BaseService {
+  @InjectEntityModel(PayeeAddressEntity)
+  payeeAddressEntity: Repository<PayeeAddressEntity>;
+  @InjectEntityModel(CustomerEntity)
+  customerEntity: Repository<CustomerEntity>;
+  @Inject()
+  paymentService: PaymentService;
+  @Inject()
+  ctx;
+
+  async info(id: any) {
+    const payee = await this.payeeAddressEntity.findOneBy({
+      id: id,
+    });
+    return Promise.resolve(payee);
+  }
+  async getBeneficiaryAddress(
+    customerId: string,
+    currency: string,
+    paymentType: string,
+    pageIndex: number,
+    pageSize: number
+  ): Promise<{ data: PayeeAddressEntity[]; total: number }> {
+    const skip = (pageIndex - 1) * pageSize;
+    const whereParams = {
+      merchantId: customerId,
+      currency: currency,
+      payment_type: paymentType,
+
+    }
+    if (!currency) {
+      delete  whereParams.currency
+    }
+    if (!paymentType) {
+      delete  whereParams.payment_type
+    }
+    const [data, total] = await this.payeeAddressEntity.findAndCount({
+      where: {
+        ...whereParams
+      },
+      skip: skip,
+      take: pageSize,
+    });
+
+    return { data, total };
+  }
+}

+ 22 - 6
src/modules/payment/service/payee_address.ts

@@ -25,19 +25,27 @@ export class PayeeAddressService extends BaseService {
    * @returns
    */
   async add(params: any) {
-    const admin = this.ctx.admin;
+    let merchantId;
+    const isOpenApi = params.hasOwnProperty('isOpenApi')
+      ? params.isOpenApi
+      : false;
+    if (isOpenApi) {
+      merchantId = params.out_user_id;
+    } else {
+      const { merchant } = this.ctx.admin;
+      merchantId = merchant.id;
+    }
     if (!['TRC20', 'ERC20'].includes(params.currency)) {
       const res = await this.paymentService
         .setChannel(params.channel)
         .createBeneficiaryAddress(params);
       params.beneficiary_address_id = res.data.id;
     }
-    params.merchantId = admin.merchant.id;
+    params.merchantId = merchantId;
     const payee = await super.add(params);
     return payee;
   }
   async update(params: any) {
-    const admin = this.ctx.admin;
     const payee = await this.payeeAddressEntity.findOneBy({
       id: params.id,
     });
@@ -64,12 +72,20 @@ export class PayeeAddressService extends BaseService {
     await super.delete(params);
   }
   async list(params: any, option: any, connectionName?: any) {
-    console.log(params, option);
     const { channel, currency } = params;
-    const { merchant } = this.ctx.admin;
+    let merchantId;
+    const isOpenApi = params.hasOwnProperty('isOpenApi')
+      ? params.isOpenApi
+      : false;
+    if (isOpenApi) {
+      merchantId = params.out_user_id;
+    } else {
+      const { merchant } = this.ctx.admin;
+      merchantId = merchant.id;
+    }
     const res = await this.payeeAddressEntity.find({
       where: {
-        merchantId: merchant.id,
+        merchantId: merchantId,
         channel,
         currency,
       },