Skip to content

Commit

Permalink
Merge pull request #35 from richard483/feature/run-week-1
Browse files Browse the repository at this point in the history
Feature/run week 1
  • Loading branch information
richard483 authored Dec 2, 2023
2 parents 3aed53b + 5196efd commit 7c121c1
Show file tree
Hide file tree
Showing 22 changed files with 590 additions and 36 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"seed": "npx prisma db seed",
"migrate": "npx prisma generate && npx prisma migrate dev"
},
"dependencies": {
"@nestjs/common": "^9.4.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- AlterEnum
ALTER TYPE "Role" ADD VALUE 'RECRUITER';

-- AlterTable
ALTER TABLE "User" ADD COLUMN "companyId" TEXT;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE SET NULL ON UPDATE CASCADE;
4 changes: 4 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ model User {
previousWorkplaceCount Int?
ratings Rating[]
ratingsAvg Float?
companyId String?
hasGoogleAccount Boolean @default(false)
company Company? @relation(fields: [companyId], references: [id])
}

model JobVacancy {
Expand All @@ -50,6 +52,7 @@ model Company {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
jobs JobVacancy[]
User User[]
}

model Contract {
Expand All @@ -75,4 +78,5 @@ model Rating {
enum Role {
ADMIN
USER
RECRUITER
}
22 changes: 21 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@ async function users() {
roles: ['ADMIN', 'USER'],
},
});

const recruiter = await prisma.user.upsert({
where: { email: 'recruiter@email.com' },
update: {},
create: {
email: 'recruiter@email.com',
userName: 'Recruiter User',
firstName: 'Recruiter',
lastName: 'User',
password: hashPassword('User123_'),
roles: ['USER', 'RECRUITER'],
description: 'default user description that being created by seed.ts',
companyId: await prisma.company
.findFirst({
where: {
name: 'Nijisanji Anycolor',
},
})
.then((company) => company.id),
},
});
const defaultUser = await prisma.user.upsert({
where: { email: 'default.user@email.com' },
update: {},
Expand Down Expand Up @@ -151,6 +170,7 @@ async function users() {
},
});

console.log('Created recruiter user: ', recruiter);
console.log(
await [
admin,
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UsersModule } from './users/users.module';
import { ConfigModule } from '@nestjs/config';
import { JobModule } from './job/job.module';
import { ContractModule } from './contract/contract.module';
import { RatingModule } from './rating/rating.module';

@Module({
imports: [
Expand All @@ -14,6 +15,7 @@ import { ContractModule } from './contract/contract.module';
UsersModule,
JobModule,
ContractModule,
RatingModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
1 change: 1 addition & 0 deletions src/auth/roles/role.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum Role {
ADMIN = 'ADMIN',
USER = 'USER',
RECRUITER = 'RECRUITER',
}
44 changes: 44 additions & 0 deletions src/prisma/prisma.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function pagination(page: number, size: number) {
if (!page || page < 1) {
page = 1;
}
if (!size || size < 1) {
size = 5;
}
const skip = (page - 1) * size;
const take = size;
return {
skip,
take,
};
}

function returnablePaginated(
data: any,
total: number,
page: number,
size: number,
) {
if (!page || page < 1) {
page = 1;
}
if (!size || size < 1) {
size = 5;
}

const hasPrevious = page > 1;
const hasNext = page * size < total;
const totalPages = Math.ceil(total / size);
const isLast = page === totalPages;
const isFirst = page === 1;
return {
data,
hasPrevious,
hasNext,
totalPages,
isLast,
isFirst,
};
}

export { pagination, returnablePaginated };
19 changes: 19 additions & 0 deletions src/rating/dto/rating-create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';

export class RatingCreateDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
readonly userId: string;

@ApiProperty()
@IsNotEmpty()
@IsString()
readonly givenByUserId: string;

@ApiProperty()
@IsNotEmpty()
@IsNumber()
readonly ratingOf10: number;
}
24 changes: 24 additions & 0 deletions src/rating/dto/rating-update.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';

export class RatingUpdateDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
readonly id: string;

@ApiProperty()
@IsNotEmpty()
@IsString()
readonly userId: string;

@ApiProperty()
@IsNotEmpty()
@IsString()
readonly givenByUserId: string;

@ApiProperty()
@IsNotEmpty()
@IsNumber()
readonly ratingOf10?: number;
}
5 changes: 5 additions & 0 deletions src/rating/interface/rating.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IRating {
id: string;
givenByUserId: string;
ratingOf10?: number;
}
53 changes: 53 additions & 0 deletions src/rating/rating.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
Body,
Controller,
HttpStatus,
Post,
Res,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { RatingService } from './rating.service';
import { RatingCreateDto } from './dto/rating-create.dto';
import { Roles } from '../auth/roles/role.decorator';
import { JwtAuthGuard } from '../auth/jwt/jwt-auth.guard';
import { RoleGuard } from '../auth/roles/role.guard';
import { Role } from '../auth/roles/role.enum';
import { RatingUpdateDto } from './dto/rating-update.dto';

@ApiTags('Rating')
@Controller('rating')
export class RatingController {
constructor(private ratingService: RatingService) {}

@ApiBearerAuth()
@Roles(Role.USER)
@UseGuards(JwtAuthGuard, RoleGuard)
@Post('create')
async createRating(@Res() res, @Body() data: RatingCreateDto) {
try {
console.log(
`#createRating request incoming with res: ${res} and data: ${data}`,
);
const response = await this.ratingService.create(data);
return res.status(HttpStatus.OK).json({ response });
} catch (error) {
console.error('#createJob error caused by: ', error);
return res.status(error.status).json({ error: error.message });
}
}

@ApiBearerAuth()
@Roles(Role.USER)
@UseGuards(JwtAuthGuard, RoleGuard)
@Post('update')
async updateRating(@Res() res, @Body() data: RatingUpdateDto) {
try {
const response = await this.ratingService.update(data);
return res.status(HttpStatus.OK).json({ response });
} catch (error) {
console.error('#updateJob error caused by: ', error);
return res.status(error.status).json({ error: error.message });
}
}
}
12 changes: 12 additions & 0 deletions src/rating/rating.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { RatingService } from './rating.service';
import { PrismaService } from '../prisma/prisma.service';
import { RatingController } from './rating.controller';
import { RatingRepository } from './rating.repository';

@Module({
providers: [RatingService, PrismaService, RatingRepository],
exports: [RatingService],
controllers: [RatingController],
})
export class RatingModule {}
27 changes: 27 additions & 0 deletions src/rating/rating.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { RatingCreateDto } from './dto/rating-create.dto';

@Injectable()
export class RatingRepository {
public model;

constructor(private prisma: PrismaService) {
this.model = this.prisma.rating;
}

async create(rating: RatingCreateDto): Promise<any> {
return this.prisma.rating.create({
data: rating,
});
}

async update(ratingId: string, data: any): Promise<any> {
return this.prisma.rating.update({
where: {
id: ratingId,
},
data,
});
}
}
19 changes: 19 additions & 0 deletions src/rating/rating.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@nestjs/common';
import { RatingRepository } from './rating.repository';
import { IRating } from './interface/rating.interface';
import { RatingCreateDto } from './dto/rating-create.dto';
import { RatingUpdateDto } from './dto/rating-update.dto';

@Injectable()
export class RatingService {
constructor(private ratingRepository: RatingRepository) {}

async create(rating: RatingCreateDto): Promise<IRating> {
return this.ratingRepository.create(rating);
}

async update(rating: RatingUpdateDto): Promise<IRating> {
const { id, ...data } = rating;
return this.ratingRepository.update(id, data);
}
}
Loading

0 comments on commit 7c121c1

Please sign in to comment.