瀏覽代碼

🐛 fix(platform/company/channel): 修复软删除后列表仍显示已删除记录的问题

修复平台、公司、渠道模块的软删除过滤问题:
1. 在findAll()、searchByName()、findOne()方法中添加status=1过滤
2. 名称唯一性检查只检查正常状态的记录
3. 添加相应的测试用例验证过滤功能

测试结果:所有模块测试通过

🤖 Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 5 小時之前
父節點
當前提交
79985f5bee

+ 10 - 8
allin-packages/channel-module/src/services/channel.service.ts

@@ -11,10 +11,10 @@ export class ChannelService extends GenericCrudService<Channel> {
    * 创建渠道 - 覆盖父类方法,添加名称唯一性检查
    */
   async create(data: Partial<Channel>, userId?: string | number): Promise<Channel> {
-    // 检查渠道名称是否已存在
+    // 检查渠道名称是否已存在(只检查正常状态的渠道)
     if (data.channelName) {
       const existingChannel = await this.repository.findOne({
-        where: { channelName: data.channelName }
+        where: { channelName: data.channelName, status: 1 }
       });
       if (existingChannel) {
         throw new Error('渠道名称已存在');
@@ -40,16 +40,16 @@ export class ChannelService extends GenericCrudService<Channel> {
    * 更新渠道 - 覆盖父类方法,添加存在性和名称重复检查
    */
   async update(id: number, data: Partial<Channel>, userId?: string | number): Promise<Channel | null> {
-    // 检查渠道是否存在
-    const channel = await this.repository.findOne({ where: { id } });
+    // 检查渠道是否存在(只检查正常状态的渠道)
+    const channel = await this.repository.findOne({ where: { id, status: 1 } });
     if (!channel) {
       throw new Error('渠道不存在');
     }
 
-    // 检查渠道名称是否与其他渠道重复
+    // 检查渠道名称是否与其他渠道重复(只检查正常状态的渠道)
     if (data.channelName && data.channelName !== channel.channelName) {
       const existingChannel = await this.repository.findOne({
-        where: { channelName: data.channelName, id: Not(id) }
+        where: { channelName: data.channelName, id: Not(id), status: 1 }
       });
       if (existingChannel) {
         throw new Error('渠道名称已存在');
@@ -80,6 +80,7 @@ export class ChannelService extends GenericCrudService<Channel> {
    */
   async findAll(skip?: number, take?: number): Promise<{ data: Channel[], total: number }> {
     const [data, total] = await this.repository.findAndCount({
+      where: { status: 1 }, // 只返回正常状态的渠道
       skip: skip ?? 0,
       take: take ?? 10,
       order: { id: 'DESC' }
@@ -93,7 +94,8 @@ export class ChannelService extends GenericCrudService<Channel> {
   async searchByName(name: string, skip?: number, take?: number): Promise<{ data: Channel[], total: number }> {
     const [data, total] = await this.repository.findAndCount({
       where: {
-        channelName: Like(`%${name}%`)
+        channelName: Like(`%${name}%`),
+        status: 1 // 只返回正常状态的渠道
       },
       skip: skip ?? 0,
       take: take ?? 10,
@@ -106,6 +108,6 @@ export class ChannelService extends GenericCrudService<Channel> {
    * 获取单个渠道 - 自定义方法
    */
   async findOne(id: number): Promise<Channel | null> {
-    return this.repository.findOne({ where: { id } });
+    return this.repository.findOne({ where: { id, status: 1 } }); // 只返回正常状态的渠道
   }
 }

+ 99 - 0
allin-packages/channel-module/tests/integration/channel.integration.test.ts

@@ -302,6 +302,59 @@ describe('渠道管理API集成测试', () => {
 
       expect(response.status).toBe(200);
     });
+
+    it('应该过滤已删除的渠道(status=0)', async () => {
+      // 创建测试数据:3个正常渠道,2个已删除渠道
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const channelRepository = dataSource.getRepository(Channel);
+
+      // 创建3个正常渠道
+      for (let i = 1; i <= 3; i++) {
+        const channel = channelRepository.create({
+          channelName: `正常渠道${i}`,
+          channelType: `类型${i}`,
+          contactPerson: `联系人${i}`,
+          contactPhone: `1380013800${i}`,
+          status: 1
+        });
+        await channelRepository.save(channel);
+      }
+
+      // 创建2个已删除渠道
+      for (let i = 4; i <= 5; i++) {
+        const channel = channelRepository.create({
+          channelName: `已删除渠道${i}`,
+          channelType: `类型${i}`,
+          contactPerson: `联系人${i}`,
+          contactPhone: `1380013800${i}`,
+          status: 0
+        });
+        await channelRepository.save(channel);
+      }
+
+      const response = await client.getAllChannels.$get({
+        query: { skip: 0, take: 10 }
+      }, {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+
+      if (response.status === 200) {
+        const data = await response.json();
+        // 应该只返回3个正常渠道,不返回已删除的渠道
+        expect(data.data).toHaveLength(3);
+        expect(data.total).toBe(3);
+
+        // 验证返回的都是正常渠道
+        data.data.forEach((channel: any) => {
+          expect(channel.status).toBe(1);
+          expect(channel.channelName).toMatch(/^正常渠道/);
+        });
+      }
+    });
   });
 
   describe('GET /channel/searchChannels', () => {
@@ -336,6 +389,52 @@ describe('渠道管理API集成测试', () => {
         expect(data.data[0].channelName).toContain('微信');
       }
     });
+
+    it('应该过滤已删除的渠道(搜索时)', async () => {
+      // 创建测试数据
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const channelRepository = dataSource.getRepository(Channel);
+
+      // 创建正常渠道
+      const normalChannel = channelRepository.create({
+        channelName: '测试搜索渠道正常',
+        channelType: '正常类型',
+        contactPerson: '张三',
+        contactPhone: '13800138000',
+        status: 1
+      });
+      await channelRepository.save(normalChannel);
+
+      // 创建已删除的渠道(不同名称)
+      const deletedChannel = channelRepository.create({
+        channelName: '测试搜索渠道已删除',
+        channelType: '删除类型',
+        contactPerson: '李四',
+        contactPhone: '13900139000',
+        status: 0
+      });
+      await channelRepository.save(deletedChannel);
+
+      const response = await client.searchChannels.$get({
+        query: { name: '测试搜索', skip: 0, take: 10 }
+      }, {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+
+      if (response.status === 200) {
+        const data = await response.json();
+        // 应该只返回1个正常渠道,不返回已删除的渠道
+        expect(data.data).toHaveLength(1);
+        expect(data.total).toBe(1);
+        expect(data.data[0].channelName).toBe('测试搜索渠道正常');
+        expect(data.data[0].status).toBe(1);
+        expect(data.data[0].contactPerson).toBe('张三'); // 正常渠道的联系人
+      }
+    });
   });
 
   describe('GET /channel/getChannel/:id', () => {

+ 3 - 1
allin-packages/company-module/src/services/company.service.ts

@@ -87,6 +87,7 @@ export class CompanyService extends GenericCrudService<Company> {
   async findAll(skip?: number, take?: number): Promise<{ data: Company[], total: number }> {
     const [data, total] = await this.repository.findAndCount({
       relations: ['platform'],
+      where: { status: 1 }, // 只返回正常状态的公司
       skip: skip ?? 0,
       take: take ?? 10,
       order: { id: 'DESC' }
@@ -101,7 +102,8 @@ export class CompanyService extends GenericCrudService<Company> {
     const [data, total] = await this.repository.findAndCount({
       relations: ['platform'],
       where: {
-        companyName: Like(`%${name}%`)
+        companyName: Like(`%${name}%`),
+        status: 1 // 只返回正常状态的公司
       },
       skip: skip ?? 0,
       take: take ?? 10,

+ 99 - 0
allin-packages/company-module/tests/integration/company.integration.test.ts

@@ -427,6 +427,59 @@ describe('公司管理API集成测试', () => {
 
       expect(response.status).toBe(200);
     });
+
+    it('应该过滤已删除的公司(status=0)', async () => {
+      // 创建测试数据:3个正常公司,2个已删除公司
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const companyRepository = dataSource.getRepository(Company);
+
+      // 创建3个正常公司
+      for (let i = 1; i <= 3; i++) {
+        const company = companyRepository.create({
+          platformId: testPlatform.id,
+          companyName: `正常公司${i}`,
+          contactPerson: `联系人${i}`,
+          contactPhone: `1380013800${i}`,
+          status: 1
+        });
+        await companyRepository.save(company);
+      }
+
+      // 创建2个已删除公司
+      for (let i = 4; i <= 5; i++) {
+        const company = companyRepository.create({
+          platformId: testPlatform.id,
+          companyName: `已删除公司${i}`,
+          contactPerson: `联系人${i}`,
+          contactPhone: `1380013800${i}`,
+          status: 0
+        });
+        await companyRepository.save(company);
+      }
+
+      const response = await client.getAllCompanies.$get({
+        query: { skip: 0, take: 10 }
+      }, {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+
+      if (response.status === 200) {
+        const data = await response.json();
+        // 应该只返回3个正常公司,不返回已删除的公司
+        expect(data.data).toHaveLength(3);
+        expect(data.total).toBe(3);
+
+        // 验证返回的都是正常公司
+        data.data.forEach((company: any) => {
+          expect(company.status).toBe(1);
+          expect(company.companyName).toMatch(/^正常公司/);
+        });
+      }
+    });
   });
 
   describe('GET /company/searchCompanies', () => {
@@ -461,6 +514,52 @@ describe('公司管理API集成测试', () => {
         expect(data.data[0].companyName).toContain('科技');
       }
     });
+
+    it('应该过滤已删除的公司(搜索时)', async () => {
+      // 创建测试数据
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const companyRepository = dataSource.getRepository(Company);
+
+      // 创建正常公司
+      const normalCompany = companyRepository.create({
+        platformId: testPlatform.id,
+        companyName: '测试搜索公司正常',
+        contactPerson: '张三',
+        contactPhone: '13800138000',
+        status: 1
+      });
+      await companyRepository.save(normalCompany);
+
+      // 创建已删除的公司(不同名称)
+      const deletedCompany = companyRepository.create({
+        platformId: testPlatform.id,
+        companyName: '测试搜索公司已删除',
+        contactPerson: '李四',
+        contactPhone: '13900139000',
+        status: 0
+      });
+      await companyRepository.save(deletedCompany);
+
+      const response = await client.searchCompanies.$get({
+        query: { name: '测试搜索', skip: 0, take: 10 }
+      }, {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+
+      if (response.status === 200) {
+        const data = await response.json();
+        // 应该只返回1个正常公司,不返回已删除的公司
+        expect(data.data).toHaveLength(1);
+        expect(data.total).toBe(1);
+        expect(data.data[0].companyName).toBe('测试搜索公司正常');
+        expect(data.data[0].status).toBe(1);
+        expect(data.data[0].contactPerson).toBe('张三'); // 正常公司的联系人
+      }
+    });
   });
 
   describe('GET /company/getCompaniesByPlatform/:platformId', () => {

+ 10 - 8
allin-packages/platform-module/src/services/platform.service.ts

@@ -11,10 +11,10 @@ export class PlatformService extends GenericCrudService<Platform> {
    * 创建平台 - 覆盖父类方法,添加名称唯一性检查
    */
   async create(data: Partial<Platform>, userId?: string | number): Promise<Platform> {
-    // 检查平台名称是否已存在
+    // 检查平台名称是否已存在(只检查正常状态的平台)
     if (data.platformName) {
       const existingPlatform = await this.repository.findOne({
-        where: { platformName: data.platformName }
+        where: { platformName: data.platformName, status: 1 }
       });
       if (existingPlatform) {
         throw new Error('平台名称已存在');
@@ -39,16 +39,16 @@ export class PlatformService extends GenericCrudService<Platform> {
    * 更新平台 - 覆盖父类方法,添加存在性和名称重复检查
    */
   async update(id: number, data: Partial<Platform>, userId?: string | number): Promise<Platform | null> {
-    // 检查平台是否存在
-    const platform = await this.repository.findOne({ where: { id } });
+    // 检查平台是否存在(只检查正常状态的平台)
+    const platform = await this.repository.findOne({ where: { id, status: 1 } });
     if (!platform) {
       throw new Error('平台不存在');
     }
 
-    // 检查平台名称是否与其他平台重复
+    // 检查平台名称是否与其他平台重复(只检查正常状态的平台)
     if (data.platformName && data.platformName !== platform.platformName) {
       const existingPlatform = await this.repository.findOne({
-        where: { platformName: data.platformName, id: Not(id) }
+        where: { platformName: data.platformName, id: Not(id), status: 1 }
       });
       if (existingPlatform) {
         throw new Error('平台名称已存在');
@@ -79,6 +79,7 @@ export class PlatformService extends GenericCrudService<Platform> {
    */
   async findAll(skip?: number, take?: number): Promise<{ data: Platform[], total: number }> {
     const [data, total] = await this.repository.findAndCount({
+      where: { status: 1 }, // 只返回正常状态的平台
       skip: skip ?? 0,
       take: take ?? 10,
       order: { id: 'DESC' }
@@ -92,7 +93,8 @@ export class PlatformService extends GenericCrudService<Platform> {
   async searchByName(name: string, skip?: number, take?: number): Promise<{ data: Platform[], total: number }> {
     const [data, total] = await this.repository.findAndCount({
       where: {
-        platformName: Like(`%${name}%`)
+        platformName: Like(`%${name}%`),
+        status: 1 // 只返回正常状态的平台
       },
       skip: skip ?? 0,
       take: take ?? 10,
@@ -105,6 +107,6 @@ export class PlatformService extends GenericCrudService<Platform> {
    * 获取单个平台 - 自定义方法
    */
   async findOne(id: number): Promise<Platform | null> {
-    return this.repository.findOne({ where: { id } });
+    return this.repository.findOne({ where: { id, status: 1 } }); // 只返回正常状态的平台
   }
 }

+ 102 - 0
allin-packages/platform-module/tests/integration/platform.integration.test.ts

@@ -335,6 +335,60 @@ describe('平台管理API集成测试', () => {
         expect(data.total).toBe(15);
       }
     });
+
+    it('应该过滤已删除的平台(status=0)', async () => {
+      // 创建测试数据:3个正常平台,2个已删除平台
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const platformRepository = dataSource.getRepository(Platform);
+
+      // 创建3个正常平台
+      for (let i = 1; i <= 3; i++) {
+        const platform = platformRepository.create({
+          platformName: `正常平台${i}`,
+          contactPerson: `联系人${i}`,
+          contactPhone: `1380013800${i}`,
+          status: 1
+        });
+        await platformRepository.save(platform);
+      }
+
+      // 创建2个已删除平台
+      for (let i = 4; i <= 5; i++) {
+        const platform = platformRepository.create({
+          platformName: `已删除平台${i}`,
+          contactPerson: `联系人${i}`,
+          contactPhone: `1380013800${i}`,
+          status: 0
+        });
+        await platformRepository.save(platform);
+      }
+
+      const response = await client.getAllPlatforms.$get({
+        query: {
+          skip: 0,
+          take: 10
+        }
+      }, {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+
+      if (response.status === 200) {
+        const data = await response.json();
+        // 应该只返回3个正常平台,不返回已删除的平台
+        expect(data.data).toHaveLength(3);
+        expect(data.total).toBe(3);
+
+        // 验证返回的都是正常平台
+        data.data.forEach((platform: any) => {
+          expect(platform.status).toBe(1);
+          expect(platform.platformName).toMatch(/^正常平台/);
+        });
+      }
+    });
   });
 
   describe('GET /platform/searchPlatforms', () => {
@@ -395,6 +449,54 @@ describe('平台管理API集成测试', () => {
 
       expect(response.status).toBe(400);
     });
+
+    it('应该过滤已删除的平台(搜索时)', async () => {
+      // 创建测试数据
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const platformRepository = dataSource.getRepository(Platform);
+
+      // 创建正常平台
+      const normalPlatform = platformRepository.create({
+        platformName: '测试搜索平台正常',
+        contactPerson: '张三',
+        contactPhone: '13800138000',
+        status: 1
+      });
+      await platformRepository.save(normalPlatform);
+
+      // 创建已删除的平台(不同名称)
+      const deletedPlatform = platformRepository.create({
+        platformName: '测试搜索平台已删除',
+        contactPerson: '李四',
+        contactPhone: '13900139000',
+        status: 0
+      });
+      await platformRepository.save(deletedPlatform);
+
+      const response = await client.searchPlatforms.$get({
+        query: {
+          name: '测试搜索',
+          skip: 0,
+          take: 10
+        }
+      }, {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+
+      if (response.status === 200) {
+        const data = await response.json();
+        // 应该只返回1个正常平台,不返回已删除的平台
+        expect(data.data).toHaveLength(1);
+        expect(data.total).toBe(1);
+        expect(data.data[0].platformName).toBe('测试搜索平台正常');
+        expect(data.data[0].status).toBe(1);
+        expect(data.data[0].contactPerson).toBe('张三'); // 正常平台的联系人
+      }
+    });
   });
 
   describe('GET /platform/getPlatform/{id}', () => {