feat(api): add GroupsModule with GET /groups endpoint
Implements TDD-driven GroupsService and GroupsController with full unit test coverage. GET /groups returns all groups ordered by name, selecting id, name, platform, platformId, isActive, and accountId fields. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GroupsController } from './groups.controller';
|
||||
import { GroupsService } from './groups.service';
|
||||
|
||||
const mockGroups = [
|
||||
{ id: 'grp_1', name: 'Alpha', platform: 'whatsapp', platformId: '111@g.us', isActive: true, accountId: 'acc_1' },
|
||||
];
|
||||
const mockService = { list: jest.fn().mockResolvedValue(mockGroups) };
|
||||
|
||||
describe('GroupsController', () => {
|
||||
let controller: GroupsController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [GroupsController],
|
||||
providers: [{ provide: GroupsService, useValue: mockService }],
|
||||
}).compile();
|
||||
controller = module.get<GroupsController>(GroupsController);
|
||||
});
|
||||
|
||||
it('returns groups from service', async () => {
|
||||
const result = await controller.list();
|
||||
expect(result).toEqual(mockGroups);
|
||||
expect(mockService.list).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { GroupsService } from './groups.service';
|
||||
|
||||
@Controller('groups')
|
||||
export class GroupsController {
|
||||
constructor(private readonly groupsService: GroupsService) {}
|
||||
|
||||
@Get()
|
||||
list() {
|
||||
return this.groupsService.list();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GroupsController } from './groups.controller';
|
||||
import { GroupsService } from './groups.service';
|
||||
|
||||
@Module({
|
||||
controllers: [GroupsController],
|
||||
providers: [GroupsService],
|
||||
})
|
||||
export class GroupsModule {}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GroupsService } from './groups.service';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
|
||||
const mockGroups = [
|
||||
{ id: 'grp_1', name: 'Alpha', platform: 'whatsapp', platformId: '111@g.us', isActive: true, accountId: 'acc_1' },
|
||||
{ id: 'grp_2', name: 'Beta', platform: 'whatsapp', platformId: '222@g.us', isActive: true, accountId: null },
|
||||
];
|
||||
|
||||
describe('GroupsService', () => {
|
||||
let service: GroupsService;
|
||||
const mockPrisma = { group: { findMany: jest.fn().mockResolvedValue(mockGroups) } };
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
GroupsService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
],
|
||||
}).compile();
|
||||
service = module.get<GroupsService>(GroupsService);
|
||||
});
|
||||
|
||||
it('returns all groups ordered by name', async () => {
|
||||
const result = await service.list();
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].name).toBe('Alpha');
|
||||
expect(mockPrisma.group.findMany).toHaveBeenCalledWith({
|
||||
orderBy: { name: 'asc' },
|
||||
select: { id: true, name: true, platform: true, platformId: true, isActive: true, accountId: true },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class GroupsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
list() {
|
||||
return this.prisma.group.findMany({
|
||||
orderBy: { name: 'asc' },
|
||||
select: { id: true, name: true, platform: true, platformId: true, isActive: true, accountId: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user