Prechádzať zdrojové kódy

feat(): 受益人接口

max 7 mesiacov pred
rodič
commit
53ccc8f2cf

+ 29 - 47
src/modules/api/controller/beneficiary.ts

@@ -4,12 +4,15 @@ import {
   Body,
   Del,
   Get,
-  Inject,
+  Inject, Param,
   Post,
   Provide,
-  Put,
+  Put, Query,
 } from '@midwayjs/decorator';
 import { BusinessEntity } from '../../payment/entity/business';
+import { SunPayAdapter } from '../../payment/adapter/sunpay.adapter';
+import { PayeeService } from '../../payment/service/payee';
+import {BeneficiaryService} from "../service/beneficiary";
 
 /**
  * 受益人
@@ -17,77 +20,56 @@ import { BusinessEntity } from '../../payment/entity/business';
 @Provide()
 @CoolController('/api/v1/Fiat')
 export class BeneficiaryController extends BaseController {
+
+  @Inject()
+  sunPayAdapter: SunPayAdapter;
+
+  @Inject()
+  payeeService: PayeeService;
+
+  @Inject()
+  beneficiaryService: BeneficiaryService;
+
   /**
    * 创建受益人
    * /api/v3/Fiat/Beneficiary
    */
   @Post('/Beneficiary', { summary: '创建受益人' })
-  async createCustomer(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async createCustomer(@Body(ALL) params: any) {
+    this.payeeService.setIsOpenApi(true);
+    return this.ok(await this.payeeService.add(params));
   }
   /**
    * 获取受益人
    */
   @Get('/Beneficiary/:id', { summary: '获取受益人' })
-  async beneficiaryForId(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async beneficiaryForId(@Param('id') id: string) {
+    return this.ok(this.beneficiaryService.info(id));
   }
   /**
    * 获取受益人列表
    * /api/v3/Fiat/Beneficiary
    */
   @Get('/Beneficiary', { summary: '获取受益人列表' })
-  async getBeneficiary(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async getBeneficiary(@Query('customer_id') customerId: string,) {
+    this.payeeService.setIsOpenApi(true);
+    return this.ok(await this.beneficiaryService.getBeneficiaryList(customerId));
   }
   /**
    * 修改受益人
    */
   @Put('/Beneficiary', { summary: '修改受益人' })
-  async updateBeneficiary(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async updateBeneficiary(@Body(ALL) params: BusinessEntity) {
+    this.payeeService.setIsOpenApi(true);
+    return this.ok(await this.payeeService.update(params));
   }
   /**
    * 删除受益人
    * /api/v3/Fiat/Beneficiary/{id}
    */
   @Del('/Beneficiary', { summary: '删除受益人' })
-  async deleteBeneficiaryForId(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async deleteBeneficiaryForId(@Body(ALL) params: BusinessEntity) {
+    this.payeeService.setIsOpenApi(true);
+    return this.ok(await this.payeeService.delete(params));
   }
 }

+ 44 - 0
src/modules/api/service/beneficiary.ts

@@ -0,0 +1,44 @@
+import {BaseService,} 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';
+import {PayeeEntity} from "../../payment/entity/payee";
+
+/**
+ * 描述
+ */
+@Provide()
+export class BeneficiaryService extends BaseService {
+  @InjectEntityModel(PayeeEntity)
+  payeeEntity: Repository<PayeeEntity>;
+  @InjectEntityModel(CustomerEntity)
+  customerEntity: Repository<CustomerEntity>;
+  @Inject()
+  paymentService: PaymentService;
+  @Inject()
+  ctx;
+
+  async info(id: any) {
+    const payee = await this.payeeEntity.findOneBy({
+      id: id,
+    });
+    return Promise.resolve(payee);
+  }
+  async getBeneficiaryList(
+    customerId: string,
+  ): Promise<PayeeEntity[]> {
+    const whereParams = {
+      merchantId: customerId,
+    }
+    return await this.payeeEntity.find({
+      where: {
+        ...whereParams,
+      },
+    });
+  }
+}
+

+ 43 - 12
src/modules/payment/service/payee.ts

@@ -5,6 +5,7 @@ import { BaseService, CoolCommException } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
 import { PayeeEntity } from '../entity/payee';
+import { MerchantEntity } from '../entity/merchant';
 
 /**
  * 描述
@@ -15,6 +16,8 @@ export class PayeeService extends BaseService {
   payeeEntity: Repository<PayeeEntity>;
   @InjectEntityModel(CustomerEntity)
   customerEntity: Repository<CustomerEntity>;
+  @InjectEntityModel(MerchantEntity)
+  merchantEntity: Repository<MerchantEntity>;
   @Inject()
   paymentService: PaymentService;
   @Inject()
@@ -25,41 +28,36 @@ export class PayeeService extends BaseService {
    * @returns
    */
   async add(params: any) {
-    const admin = this.ctx.admin;
-    const customer = await this.customerEntity.findOneBy({
-      merchantId: admin.merchant.id,
-    });
+    let customer = await this.getCustomer(params);
+    const merchant = await  this.getMerchant(customer.merchantId);
     if (!customer) {
       throw new CoolCommException('客户不存在');
     }
     const custom = {
       ...params,
       customer_id: customer.customer_id,
-      beneficiary_type: admin.merchant.bussiness ? 'COMPANY' : 'INDIVIDUAL',
+      beneficiary_type: customer.type ? 'COMPANY' : 'INDIVIDUAL',
     };
     const res = await this.paymentService
       .setChannel(customer.channel)
       .addPayee(custom);
     params.beneficiary_id = res.data.id;
     params.customerId = customer.id;
-    params.merchantId = admin.merchant.id;
+    params.merchantId = merchant.mchId;
     params.channel = customer.channel;
-    params.type = admin.merchant.bussiness ? 'COMPANY' : 'INDIVIDUAL';
+    params.type = customer.type ? 'COMPANY' : 'INDIVIDUAL';
     const payee = await super.add(params);
     return payee;
   }
   async update(params: any) {
-    const admin = this.ctx.admin;
+    let customer = await this.getCustomer(params);
     const payee = await this.payeeEntity.findOneBy({
       id: params.id,
     });
-    const customer = await this.customerEntity.findOneBy({
-      merchantId: admin.merchant.id,
-    });
     const custom = {
       ...params,
       customer_id: customer.customer_id,
-      beneficiary_type: admin.merchant.bussiness ? 'COMPANY' : 'INDIVIDUAL',
+      beneficiary_type: customer.type ? 'COMPANY' : 'INDIVIDUAL',
       id: payee.beneficiary_id,
     };
     const res = await this.paymentService
@@ -75,4 +73,37 @@ export class PayeeService extends BaseService {
     await super.delete(params);
     return res;
   }
+
+  private _isOpenApi: boolean = false
+
+  setIsOpenApi(payload = false) {
+    this._isOpenApi = payload
+  }
+
+  async getCustomer(params) {
+    let customer: CustomerEntity;
+    if (this._isOpenApi) {
+      customer = await this.customerEntity.findOneBy({
+        customer_id: params.customer_id,
+      });
+    } else {
+      const { merchant } = this.ctx.admin;
+      customer = await this.customerEntity.findOneBy({
+        merchantId: merchant.id,
+      });
+    }
+    return customer
+  }
+
+  async getMerchant(id) {
+    let merchant: MerchantEntity;
+    if (this._isOpenApi) {
+      merchant = await this.merchantEntity.findOneBy({
+        id: id,
+      });
+    } else {
+      merchant = this.ctx.admin.merchant
+    }
+    return merchant
+  }
 }