Skip to content

Commit

Permalink
Merge pull request #101 from zhumeisongsong/feature/tasks-interface-a…
Browse files Browse the repository at this point in the history
…dapters

feat: ✨ add tasks resolver: getTasks / getUserTasks / createUserTasks/updateUserTasks
  • Loading branch information
zhumeisongsong authored Dec 9, 2024
2 parents b38ea04 + 0db533f commit 4e1d9f8
Show file tree
Hide file tree
Showing 49 changed files with 837 additions and 143 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ You can use `npx nx list` to get a list of installed plugins. Then, run `npx nx
| resolver(interface-adapters) | Handles GraphQL queries and mutations by converting them into calls to the application layer.<br/> Responsible for input validation and response formatting specific to GraphQL. |
| dto(interface-adapters) | Define DTOs for GraphQL schema. |
| infrastructure | Implements the technical capabilities needed to support the higher layers of the application.<br/> Handles database connections, external service integrations, and other technical concerns.<br/> Contains concrete implementations of repository interfaces defined in the domain layer. |

| mongoose(infrastructure) | Implements the repository interfaces defined in the domain layer using Mongoose as the ODM (Object Document Mapper).<br/> Includes Mongoose Schema definitions, database connection management, and concrete implementations of repository interfaces (e.g., MongooseUsersRepository).<br/> Adding validation in the Mongoose schema ensures that any data persisted to the database adheres to the required constraints. This helps maintain data integrity and prevents invalid or duplicate entries at the database level. |
| service(application) | As the core of the application layer, it mainly interacts with the domain layer and interface-adapter layer.<br/> If you migrate to a non-NestJS architecture in the future (e.g. other frameworks or microservices), the application tier code can be left unaffected. |
| use-case(application) | Define business use cases and encapsulate business logic.<br/> Implementing validation in the use-case layer allows you to enforce business logic and provide immediate feedback to users or calling services. This is where you can handle complex validation rules and provide detailed error messages.|
Expand Down
12 changes: 0 additions & 12 deletions libs/auth/application/src/lib/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AwsCognitoService } from '@shared/infrastructure-aws-cognito';
import { UsersService } from '@users/application';
import { JwtService } from '@nestjs/jwt';

import { AuthService } from './auth.service';

describe('AuthService', () => {
let service: AuthService;
let awsCognitoService: jest.Mocked<AwsCognitoService>;
let usersService: jest.Mocked<UsersService>;
let jwtService: jest.Mocked<JwtService>;

beforeEach(async () => {
Expand All @@ -22,12 +20,6 @@ describe('AuthService', () => {
signIn: jest.fn(),
},
},
{
provide: UsersService,
useValue: {
findByEmail: jest.fn(),
},
},
{
provide: JwtService,
useValue: {
Expand All @@ -39,7 +31,6 @@ describe('AuthService', () => {

service = module.get<AuthService>(AuthService);
awsCognitoService = module.get(AwsCognitoService);
usersService = module.get(UsersService);
jwtService = module.get(JwtService);
});

Expand All @@ -50,8 +41,6 @@ describe('AuthService', () => {
describe('signIn', () => {
const email = '[email protected]';
const password = 'password123';
const userId = '123';
const user = { id: userId, email, firstName: null, lastName: null };

// it('should throw UnauthorizedException when AWS Cognito sign in fails', async () => {
// const error = new Error('Invalid credentials');
Expand All @@ -65,7 +54,6 @@ describe('AuthService', () => {
it('should throw UnauthorizedException when JWT signing fails', async () => {
const error = new Error('JWT signing failed');
awsCognitoService.signIn.mockResolvedValue(undefined);
usersService.findByEmail.mockResolvedValue(user);
jwtService.signAsync.mockRejectedValue(error);

await expect(service.signIn(email, password)).rejects.toThrow(
Expand Down
3 changes: 2 additions & 1 deletion libs/tasks/application/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lib/tasks-application';
export * from './lib/tasks.service';
export * from './lib/user-tasks.service';
7 changes: 0 additions & 7 deletions libs/tasks/application/src/lib/tasks-application.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions libs/tasks/application/src/lib/tasks-application.ts

This file was deleted.

26 changes: 26 additions & 0 deletions libs/tasks/application/src/lib/tasks.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Test, TestingModule } from '@nestjs/testing';

import { TasksService } from './tasks.service';

describe('TasksService', () => {
let service: TasksService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TasksService],
}).compile();

service = module.get<TasksService>(TasksService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findAll', () => {
it('should return an array of tasks', async () => {
const result = await service.findAll();
expect(Array.isArray(result)).toBe(true);
});
});
});
10 changes: 10 additions & 0 deletions libs/tasks/application/src/lib/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Task } from '@tasks/domain';

@Injectable()
export class TasksService {
async findAll(): Promise<Task[]> {
// TODO: Implement this
return [];
}
}
39 changes: 39 additions & 0 deletions libs/tasks/application/src/lib/user-tasks.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserTasksService } from './user-tasks.service';

describe('UserTasksService', () => {
let service: UserTasksService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UserTasksService],
}).compile();

service = module.get<UserTasksService>(UserTasksService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findMany', () => {
it('should return user tasks', async () => {
const result = await service.findMany('userId', { from: new Date(), to: new Date() });
expect(result).toEqual([]);
});
});

describe('createSome', () => {
it('should create user tasks', async () => {
const result = await service.createSome('userId', [{ id: '1', createdAt: new Date() }]);
expect(result).toEqual('success');
});
});

describe('updateSome', () => {
it('should update user tasks', async () => {
const result = await service.updateSome('userId', [{ id: '1', updatedAt: new Date() }]);
expect(result).toEqual('success');
});
});
});
29 changes: 29 additions & 0 deletions libs/tasks/application/src/lib/user-tasks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import { UserTask } from '@tasks/domain';

@Injectable()
export class UserTasksService {
async findMany(
userId: string,
range?: { from: Date; to: Date },
): Promise<UserTask[]> {
// TODO: Implement this
return [];
}

async createSome(
userId: string,
tasks: { id: string; createdAt: Date }[],
): Promise<string> {
// TODO: Implement this
return 'success';
}

async updateSome(
userId: string,
userTasks: { id: string; updatedAt: Date }[],
): Promise<string> {
// TODO: Implement this
return 'success';
}
}
5 changes: 4 additions & 1 deletion libs/tasks/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './lib/tasks-domain';
export * from './lib/entities/task.entity';
export * from './lib/entities/user-task.entity';

export * from './lib/tasks.repository';
39 changes: 39 additions & 0 deletions libs/tasks/domain/src/lib/entities/task.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Task } from './task.entity';

describe('Task', () => {
it('should create a task with all properties', () => {
const task = new Task(
'test-id',
'Test Title',
'Test Description',
['category1', 'category2']
);

expect(task.id).toBe('test-id');
expect(task.title).toBe('Test Title');
expect(task.description).toBe('Test Description');
expect(task.categories).toEqual(['category1', 'category2']);
});

it('should allow null description', () => {
const task = new Task(
'test-id',
'Test Title',
null,
['category1']
);

expect(task.description).toBeNull();
});

it('should create a task with empty categories array', () => {
const task = new Task(
'test-id',
'Test Title',
'Test Description',
[]
);

expect(task.categories).toEqual([]);
});
});
8 changes: 8 additions & 0 deletions libs/tasks/domain/src/lib/entities/task.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class Task {
constructor(
public readonly id: string,
public readonly title: string,
public readonly description: string | null,
public readonly categories: string[],
) {}
}
30 changes: 30 additions & 0 deletions libs/tasks/domain/src/lib/entities/user-task.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { User } from '@users/domain';
import { Task } from './task.entity';
import { UserTask } from './user-task.entity';

describe('UserTask', () => {
it('should create a user task instance', () => {
const task = new Task('task-1', 'Test Task', 'Description', ['category1']);
const user = new User('user-1', '[email protected]', null, null);
const now = new Date();

const userTask = new UserTask(
'user-task-1',
now,
null,
'task-1',
task,
'user-1',
user,
);

expect(userTask).toBeDefined();
expect(userTask.id).toBe('user-task-1');
expect(userTask.createdAt).toBe(now);
expect(userTask.updatedAt).toBeNull();
expect(userTask.taskId).toBe('task-1');
expect(userTask.task).toBe(task);
expect(userTask.userId).toBe('user-1');
expect(userTask.user).toBe(user);
});
});
15 changes: 15 additions & 0 deletions libs/tasks/domain/src/lib/entities/user-task.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { User } from '@users/domain';

import { Task } from './task.entity';

export class UserTask {
constructor(
public readonly id: string,
public readonly createdAt: Date,
public readonly updatedAt: Date | null,
public readonly taskId: string,
public readonly task: Task | null,
public readonly userId: string,
public readonly user: User | null,
) {}
}
7 changes: 0 additions & 7 deletions libs/tasks/domain/src/lib/tasks-domain.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions libs/tasks/domain/src/lib/tasks-domain.ts

This file was deleted.

24 changes: 24 additions & 0 deletions libs/tasks/domain/src/lib/tasks.repository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Task } from './entities/task.entity';
import { TasksRepository } from './tasks.repository';

class MockTasksRepository implements TasksRepository {
async findAll(): Promise<Task[]> {
return [];
}
}

describe('TasksRepository', () => {
let repository: TasksRepository;

beforeEach(() => {
repository = new MockTasksRepository();
});

describe('findAll', () => {
it('should return all tasks', async () => {
const tasks = await repository.findAll();
expect(Array.isArray(tasks)).toBe(true);
expect(tasks.every((task) => task instanceof Task)).toBe(true);
});
});
});
7 changes: 7 additions & 0 deletions libs/tasks/domain/src/lib/tasks.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Task } from './entities/task.entity';

export interface TasksRepository {
findAll(): Promise<Task[]>;
}

export const TASKS_REPOSITORY = Symbol('TASKS_REPOSITORY');
Loading

0 comments on commit 4e1d9f8

Please sign in to comment.