Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cozero challenge #1

Open
wants to merge 32 commits into
base: cozero
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
052f4c8
feat: add search project logic
dribeir0 Jul 7, 2023
0e8c769
feat: make listing proposals required on BE
dribeir0 Jul 7, 2023
059609c
feat: add soft-delete login
dribeir0 Jul 7, 2023
8d0029a
fix: array not empty working
dribeir0 Jul 7, 2023
bb2a59d
feat: show error toast if error
dribeir0 Jul 7, 2023
3b6bcdf
feat: block screen is user not valid
dribeir0 Jul 7, 2023
f9e55ea
feat: add redux toolkit
dribeir0 Jul 9, 2023
9288233
fix: conditional new project
dribeir0 Jul 9, 2023
dcd50bc
feat: projects pagination working
dribeir0 Jul 10, 2023
3dd8d74
add prettier
dribeir0 Jul 10, 2023
c1b6fff
fix: store working properly
dribeir0 Jul 10, 2023
e4672f2
feat: restore and search working
dribeir0 Jul 10, 2023
0b75f65
feat: user validation
dribeir0 Jul 10, 2023
dde93b1
fix: fix bug in get list request
dribeir0 Jul 10, 2023
464a5cf
fix: auth guard working
dribeir0 Jul 11, 2023
217b078
fix: refactor frontend
dribeir0 Jul 11, 2023
6ffc3e5
feat: add guard to requests
dribeir0 Jul 11, 2023
b4e9689
feat: add custom errors
dribeir0 Jul 11, 2023
b0f18d2
fix: add missing strings
dribeir0 Jul 11, 2023
f8d8d7d
feat: add min length to password
dribeir0 Jul 11, 2023
149399a
feat: add search to state
dribeir0 Jul 11, 2023
a546659
fix: fix unit tests + add user tests
dribeir0 Jul 11, 2023
b726ec1
test: add project controller tests
dribeir0 Jul 11, 2023
a5cf580
fix: details slice issue
dribeir0 Jul 12, 2023
96a0a54
fix: store error issue
dribeir0 Jul 12, 2023
4fc231b
fix: auth view logic
dribeir0 Jul 12, 2023
df9412f
refactor: add details in be
dribeir0 Jul 12, 2023
f30db55
refactor: fe improvements
dribeir0 Jul 12, 2023
5e1b77d
feat: add health check
dribeir0 Jul 12, 2023
ff8cf55
fix: add missing error
dribeir0 Jul 12, 2023
d5d011a
fix: remove unused imports
dribeir0 Jul 18, 2023
73c8bf4
fix: translation fix
dribeir0 Jul 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 108 additions & 50 deletions cozero-backend/package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cozero-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^9.0.0",
"bcrypt": "^5.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"mysql2": "^2.3.3",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
Expand Down
17 changes: 11 additions & 6 deletions cozero-backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthService } from './auth/auth.service';

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
providers: [
{
provide: AuthService,
useValue: {
login: jest.fn(),
},
},
],
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
it('should be defined', () => {
expect(appController).toBeDefined();
});
});
24 changes: 15 additions & 9 deletions cozero-backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import { Controller, Get, Post, UseGuards, Request, Req } from '@nestjs/common';
import { AppService } from './app.service';
import { Controller, Post, UseGuards, Request, Get } from '@nestjs/common';
import { AuthService } from './auth/auth.service';
import { LocalAuthGuard } from './auth/local-auth.guard';
import { SkipAuth } from './decorators/skipAuth.decorator';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(
private appService: AppService,
private authService: AuthService,
) {}

@SkipAuth()
@UseGuards(LocalAuthGuard)
@Post('auth/login')
async login(@Request() req) {
return this.authService.login(req.user);
}

constructor(
private readonly appService: AppService,
private authService: AuthService,
) {}
@SkipAuth()
@Get('health')
async healthCheck() {
return 'OK';
}

@SkipAuth()
@Get()
getHello(@Request() req): string {
return this.appService.getHello();
@Get('health-db')
async healthDBCheck() {
return this.appService.healthDBCheck();
}
}
8 changes: 6 additions & 2 deletions cozero-backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
constructor(@InjectDataSource() private dataSource: DataSource) {}

async healthDBCheck() {
await this.dataSource.query('SELECT 1=1');
}
}
4 changes: 2 additions & 2 deletions cozero-backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Post, Body } from '@nestjs/common';
import { SkipAuth } from 'src/decorators/skipAuth.decorator';
import { UserLoginDto } from 'src/users/dto/user-login.dto';
import { SkipAuth } from '../decorators/skipAuth.decorator';
import { UserLoginDto } from '../users/dto/user-login.dto';
import { AuthService } from './auth.service';

@Controller('auth')
Expand Down
4 changes: 2 additions & 2 deletions cozero-backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './local.strategy';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './jwt.strategy';
import { UsersService } from 'src/users/users.service';
import { UsersService } from '../users/users.service';
import { UsersModule } from '../users/users.module';
import { User } from 'src/users/entities/user.entity';
import { User } from '../users/entities/user.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as dotenv from 'dotenv';
import { AuthController } from './auth.controller';
Expand Down
23 changes: 22 additions & 1 deletion cozero-backend/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JwtService } from '@nestjs/jwt';
import { AuthService } from './auth.service';
import { UsersService } from '../users/users.service';

describe('AuthService', () => {
let service: AuthService;
let jwtServiceMock: any;
let usersServiceMock: any;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
providers: [
AuthService,
{
provide: JwtService,
useValue: {
sign: jest.fn(),
},
},
{
provide: UsersService,
useValue: {
findOne: jest.fn(),
create: jest.fn(),
},
},
],
}).compile();

service = module.get<AuthService>(AuthService);
jwtServiceMock = module.get(JwtService);
usersServiceMock = module.get(UsersService);
});

it('should be defined', () => {
Expand Down
2 changes: 1 addition & 1 deletion cozero-backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { forwardRef, Inject, Injectable, UseGuards } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';
import { UserLoginDto } from 'src/users/dto/user-login.dto';
import { UserLoginDto } from '../users/dto/user-login.dto';
import * as bcrypt from 'bcrypt';
import { LocalAuthGuard } from './local-auth.guard';

Expand Down
3 changes: 1 addition & 2 deletions cozero-backend/src/auth/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport';
import { IS_PUBLIC_KEY } from 'src/decorators/skipAuth.decorator';
import { IS_PUBLIC_KEY } from '../decorators/skipAuth.decorator';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
Expand All @@ -20,4 +20,3 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
return super.canActivate(context);
}
}

4 changes: 2 additions & 2 deletions cozero-backend/src/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
});
}

async validate(req: e.Request, payload: JwtPayload) {
return { userId: payload.sub, email: payload.email };
async validate(payload: JwtPayload) {
return { email: payload.email };
}
}
7 changes: 7 additions & 0 deletions cozero-backend/src/exceptions/duplicated-email.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from '@nestjs/common';

export class DuplicateEmailException extends HttpException {
constructor() {
super('Email already exists', HttpStatus.BAD_REQUEST);
}
}
7 changes: 7 additions & 0 deletions cozero-backend/src/exceptions/not-owner.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from '@nestjs/common';

export class NotOwnerException extends HttpException {
constructor() {
super('This user is not the owner', HttpStatus.BAD_REQUEST);
}
}
2 changes: 2 additions & 0 deletions cozero-backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

declare const module: any;

async function bootstrap() {
const app = await NestFactory.create(AppModule, { cors: true });
app.useGlobalPipes(new ValidationPipe());
await app.listen(3001);

if (module.hot) {
Expand Down
8 changes: 8 additions & 0 deletions cozero-backend/src/projects/dto/create-project.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { ArrayNotEmpty, IsArray, IsOptional, IsString } from 'class-validator';

export class CreateProjectDto {
@IsString()
name: string;
@IsString()
description: string;
@IsOptional()
@IsArray()
co2EstimateReduction: number[];
@IsString()
owner: string;
@ArrayNotEmpty()
listing: string[];
}
12 changes: 12 additions & 0 deletions cozero-backend/src/projects/dto/page-metadata.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsNumber } from 'class-validator';

export class PageMetadataDto {
@IsNumber()
page: number;
@IsNumber()
perPage: number;
@IsNumber()
total: number;
@IsNumber()
totalPages: number;
}
15 changes: 15 additions & 0 deletions cozero-backend/src/projects/dto/paginated-projects.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IsArray, IsNumber } from 'class-validator';
import { Project } from '../entities/project.entity';

export class PaginatedProjectsDto {
@IsArray()
data: Project[];
@IsNumber()
page: number;
@IsNumber()
perPage: number;
@IsNumber()
totalItems: number;
@IsNumber()
totalPages: number;
}
4 changes: 4 additions & 0 deletions cozero-backend/src/projects/entities/project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
} from 'typeorm';

@Entity()
Expand Down Expand Up @@ -34,4 +35,7 @@ export class Project {

@UpdateDateColumn()
updatedAt: string;

@DeleteDateColumn()
deletedAt: string;
}
Loading