zs преди 7 месеца
родител
ревизия
8c19425027
променени са 3 файла, в които са добавени 31 реда и са изтрити 1 реда
  1. 2 0
      src/controller/users/applyCompany.controller.ts
  2. 1 0
      src/error/service.error.ts
  3. 28 1
      src/service/users/applyCompany.service.ts

+ 2 - 0
src/controller/users/applyCompany.controller.ts

@@ -23,6 +23,7 @@ export class ApplyCompanyController implements BaseController {
   async examine(@Body('id') id: number, @Body('status') status: string) {
     if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
     const result = await this.service.update({ id }, { status });
+    if (status === '1') await this.service.apply(id);
     return result;
   }
 
@@ -63,6 +64,7 @@ export class ApplyCompanyController implements BaseController {
   @Validate()
   @ApiResponse({ type: CVO_applyCompany })
   async create(@Body() data: object) {
+    await this.service.createExamine(data);
     const dbData = await this.service.create(data);
     const result = new CVO_applyCompany(dbData);
     return result;

+ 1 - 0
src/error/service.error.ts

@@ -31,6 +31,7 @@ export enum ErrorCode {
   // contactApply
   CONTACTAPPLY_TARGET_USER_NOT_FOUND = 'TARGET_USER_NOT_FOUND',
   CONTACTAPPLY_HAS_APPLY_NOT_STATUS = 'CONTACTAPPLY_HAS_APPLY_NOT_STATUS',
+  SERVICE_APPLY = 'SERVICE_APPLY',
 }
 export class ServiceError extends Error {
   constructor(errcode: string) {

+ 28 - 1
src/service/users/applyCompany.service.ts

@@ -1,11 +1,12 @@
 import { Inject, Provide } from '@midwayjs/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
-import { Repository } from 'typeorm';
+import { Repository, Equal } from 'typeorm';
 import { ApplyCompany } from '../../entity/users/applyCompany.entity';
 import { BaseServiceV2 } from '../../frame/BaseServiceV2';
 import { get } from 'lodash';
 import { CompanyService } from './company.service';
 import { UserService } from '../system/user.service';
+import { ErrorCode, ServiceError } from '../../error/service.error';
 @Provide()
 export class ApplyCompanyService extends BaseServiceV2 {
   @InjectEntityModel(ApplyCompany)
@@ -17,6 +18,32 @@ export class ApplyCompanyService extends BaseServiceV2 {
   @Inject()
   userService: UserService;
 
+  // 认领检查
+  async createExamine(data) {
+    const { user } = data;
+    const company = await this.companyService.fetch({ user: user });
+    if (company) throw new ServiceError(ErrorCode.SERVICE_APPLY);
+  }
+  /**
+   * 如果审核通过修改该企业的用户id,修改用户角色
+   * @param {object} data 数据
+   */
+  async apply(id) {
+    const result = await this.model.findOne({ where: { id: Equal(id) } });
+    // 查询该用户是否有企业信息
+    const company = await this.companyService.fetch({ user: get(result, 'user') });
+    if (company) throw new ServiceError(ErrorCode.SERVICE_APPLY);
+    else {
+      // 修改企业用户id
+      await this.companyService.update({ id: get(result, 'company') }, { id: get(result, 'company'), user: get(result, 'user') });
+      // 查询用户角色
+      const userInfo = await this.userService.fetch({ id: get(result, 'user') });
+      const is_update = userInfo.role.find(i => i === 'Company');
+      // 如果没有企业角色 增加一个企业角色
+      if (!is_update) await this.userService.fetch({ id: get(result, 'user'), role: [...userInfo.role, ['Company']] });
+    }
+  }
+
   /**
    * 填充
    * @param {object} data 数据