Skip to content

Commit

Permalink
Merge pull request #52 from richard483/fix-1
Browse files Browse the repository at this point in the history
fix username update validation, update contract schema, completed rat…
  • Loading branch information
richard483 authored Jan 16, 2024
2 parents c8ffae8 + 7fde345 commit f1cbfcf
Show file tree
Hide file tree
Showing 18 changed files with 349 additions and 42 deletions.
144 changes: 144 additions & 0 deletions docs/rating-controller.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# User controller

---

[UserRatingAverageCount](#userRatingAverageCount)

## get user rating average&count (update authorized user)

| Key | Description |
| ------ | ------------- |
| Method | `GET` |
| Path | `/rating/averageCount/:userId` |

### Success Response

HTTP Code: 200

```json
{
"status": true,
"statusCode": 200,
"data": {
"_avg": {
"ratingOf10": 6
},
"_count": {
"ratingOf10": 4
}
}
}
```

### Invalid credential

HTTP Code: 401

```json
{
"status": true,
"statusCode": 401,
"data": {
"access": "UNAUTHORIZED"
}
}
```

### user not found

HTTP Code: 200

```json
{
"status": true,
"statusCode": 200,
"data": {
"_avg": {
"ratingOf10": null
},
"_count": {
"ratingOf10": 0
}
}
}

```

---

# User controller

---

[CreateUserRating](#createUserRating)

## get user rating average&count (update authorized user)

| Key | Description |
| ------ | ------------- |
| Method | `POST` |
| Path | `/rating/craete` |

### Request Body

| Key | Type |
| ---------- | ------ |
| `userId` | String |
| `recruiterUserId` | String |
| `ratingOf10` | number |
| `contractId` | String? |

### Success Response

HTTP Code: 200

```json
{
"status": true,
"statusCode": 200,
"data": {
"id": "de67c8bd-8637-4aa0-9983-31c602896672",
"userId": "5c03f41e-5c18-4ef2-adf2-9ef560c34ca0",
"recruiterUserId": "741c71e9-94dc-4ad8-98a6-9259c6b72f88",
"ratingOf10": 9,
"createdAt": "2024-01-15T16:16:41.196Z",
"updatedAt": "2024-01-15T16:16:41.196Z"
}
}
```

### Invalid credential

HTTP Code: 401

```json
{
"status": true,
"statusCode": 401,
"data": {
"access": "UNAUTHORIZED"
}
}
```

### invalid field

HTTP Code: 400

```json
{
"status": false,
"statusCode": 400,
"message": {
"name": "PrismaClientKnownRequestError",
"code": "P2003",
"clientVersion": "5.7.1",
"meta": {
"modelName": "Rating",
"field_name": "Rating_userId_fkey (index)"
}
},
"error": "BAD_REQUEST"
}

```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backend-8tech",
"version": "4.0.3",
"version": "4.0.4",
"description": "",
"author": "8tech",
"private": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- AlterTable
ALTER TABLE "Contract" ADD COLUMN "ratingId" TEXT,
ADD COLUMN "workSubmission" TEXT;

-- AddForeignKey
ALTER TABLE "Contract" ADD CONSTRAINT "Contract_ratingId_fkey" FOREIGN KEY ("ratingId") REFERENCES "Rating"("id") ON DELETE SET NULL ON UPDATE CASCADE;
42 changes: 23 additions & 19 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,24 @@ model Company {
}

model Contract {
id String @id @default(uuid())
userId String
jobId String
paymentId String?
title String
description String
paymentRate Int
template String? @db.VarChar()
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status ContractStatus @default(PENDING)
customField String?
JobVacancy JobVacancy? @relation(fields: [jobId], references: [id])
User User? @relation(fields: [userId], references: [id])
Payment Payment? @relation(fields: [paymentId], references: [id])
id String @id @default(uuid())
userId String
jobId String
paymentId String?
title String
description String
paymentRate Int
template String? @db.VarChar()
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status ContractStatus @default(PENDING)
customField String?
workSubmission String?
ratingId String?
JobVacancy JobVacancy? @relation(fields: [jobId], references: [id])
User User? @relation(fields: [userId], references: [id])
Payment Payment? @relation(fields: [paymentId], references: [id])
Rating Rating? @relation(fields: [ratingId], references: [id])
}

model Payment {
Expand All @@ -88,13 +91,14 @@ model Payment {
}

model Rating {
id String @id @default(uuid())
User User @relation(fields: [userId], references: [id])
id String @id @default(uuid())
User User @relation(fields: [userId], references: [id])
userId String
recruiterUserId String
ratingOf10 Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Contract Contract[]
}

enum Role {
Expand Down
12 changes: 12 additions & 0 deletions src/contract/contract.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
IContractPayoutLink,
} from './interface/contract.interface';
import { Contract } from '@prisma/client';
import { ContractUpdateDto } from './dto/contract-update.dto';

@ApiTags('Contract')
@Controller('contract')
Expand All @@ -41,6 +42,17 @@ export class ContractController {
return response;
}

@ApiBearerAuth()
@Roles(Role.USER)
@UseGuards(JwtAuthGuard, RoleGuard)
@Post('update')
async updateContract(@Res() res, @Body() data: ContractUpdateDto) {
console.info('#ContractUpdate request incoming with: ', data);
const { id, ...contract } = data;
const response = await this.contractService.update(id, contract);
return response;
}

@Get('generate/:contractId')
@ApiParam({ name: 'contractId', type: String })
@Roles(Role.USER)
Expand Down
9 changes: 9 additions & 0 deletions src/contract/contract.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export class ContractRepository {
});
}

async update(id: string, contract: any): Promise<Contract> {
return this.prisma.contract.update({
data: contract,
where: {
id: contract.id,
},
});
}

async getAllbyUserId(userId: string): Promise<Contract[]> {
return this.prisma.contract.findMany({
where: {
Expand Down
8 changes: 6 additions & 2 deletions src/contract/contract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export class ContractService {

//#region Contract
//Create Contract
async create(user: any): Promise<Contract> {
return this.contractRepository.create(user);
async create(data: any): Promise<Contract> {
return this.contractRepository.create(data);
}

async update(id: string, data: any): Promise<Contract> {
return this.contractRepository.update(id, data);
}

async generate(contractId) {
Expand Down
51 changes: 51 additions & 0 deletions src/contract/dto/contract-update.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';

export class ContractUpdateDto {
@ApiProperty()
@IsString()
@IsNotEmpty({
message: 'MUST_NOT_BE_EMPTY',
})
readonly id: string;

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

@ApiProperty()
@IsString()
@IsOptional()
readonly jobId: string;

@ApiProperty()
@IsString()
@IsOptional()
readonly title: string;

@ApiProperty()
@IsString()
@IsOptional()
readonly description: string;

@ApiProperty()
@IsNumber()
@IsOptional()
readonly paymentRate: number;

@ApiProperty()
@IsString()
@IsOptional()
readonly paymentRequestId?: string;

@ApiProperty()
@IsString()
@IsOptional()
readonly template?: string;

@ApiProperty()
@IsString()
@IsOptional()
readonly customField?: string;
}
2 changes: 2 additions & 0 deletions src/contract/test/contract.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe('ContractController', () => {
createdAt: new Date(),
updatedAt: new Date(),
customField: null,
workSubmission: null,
ratingId: null,
};

const createSpy = jest
Expand Down
5 changes: 5 additions & 0 deletions src/job/job.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export class JobRepository {
},
};
},
companyId(keyword: string) {
return {
companyId: keyword,
};
},
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/rating/dto/rating-create.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';

export class RatingCreateDto {
@ApiProperty()
Expand All @@ -16,4 +16,9 @@ export class RatingCreateDto {
@IsNotEmpty()
@IsNumber()
readonly ratingOf10: number;

@ApiProperty()
@IsOptional()
@IsString()
readonly contractId?: string;
}
Loading

0 comments on commit f1cbfcf

Please sign in to comment.