Skip to content

Commit

Permalink
[M1_TR-204] Backend for M1_TR-16
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoleamber committed Aug 31, 2023
1 parent 942f478 commit b360a8d
Show file tree
Hide file tree
Showing 23 changed files with 510 additions and 77 deletions.
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ services:
container_name: db-postgresql
networks:
- jms

meilisearch:
image: getmeili/meilisearch:v1.3
ports:
- "7700:7700"
volumes:
- meilisearch_data:/data.ms
container_name: meilisearch
networks:
- jms
volumes:
pgdata:
web_node_modules:
server_node_modules:
meilisearch_data:
26 changes: 21 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.10",
"@prisma/client": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"jest-junit": "^16.0.0",
"meilisearch": "^0.34.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@faker-js/faker": "^8.0.2",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/node": "^20.5.7",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
Expand All @@ -52,7 +56,7 @@
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
"typescript": "^5.2.2"
},
"jest": {
"moduleFileExtensions": [
Expand All @@ -70,14 +74,26 @@
"**/*.{js,ts}"
],
"coverageDirectory": "../test/coverage",
"coverageReporters": ["clover", "json", "lcov", "text"],
"coverageReporters": [
"clover",
"json",
"lcov",
"text"
],
"testEnvironment": "node",
"reporters": [
"default",
["jest-junit", {"outputDirectory": "./test/result", "outputName": "report.xml"}]
[
"jest-junit",
{
"outputDirectory": "./test/result",
"outputName": "report.xml"
}
]
]
},
"prisma": {
"schema": "./src/models/schema.prisma"
"schema": "./src/models/schema.prisma",
"seed": "ts-node ./src/models/seed.ts"
}
}
5 changes: 5 additions & 0 deletions server/src/api/jobs/job.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PartialType } from '@nestjs/mapped-types';

export class CreateJobDto {}

export class UpdateJobDto extends PartialType(CreateJobDto) {}
37 changes: 37 additions & 0 deletions server/src/api/jobs/job.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ApiProperty } from '@nestjs/swagger';
import { Job, PaymentMethod, Schedule, Tag } from '@prisma/client';

export class JobEntity implements Job {
@ApiProperty()
id: number;

@ApiProperty()
title: string;

@ApiProperty()
type: string;

@ApiProperty({ nullable: true })
remarks: string;

@ApiProperty()
personInChargeId: number;

@ApiProperty()
customerId: number;

@ApiProperty()
paymentMethod: PaymentMethod;

@ApiProperty({ nullable: true })
tags: Tag[]

@ApiProperty({ nullable: true })
schedules: Schedule[]

@ApiProperty()
createdAt: Date;

@ApiProperty()
updatedAt: Date;
}
22 changes: 22 additions & 0 deletions server/src/api/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger';
import { JobEntity } from './job.entity';
import { JobsService } from './jobs.service';

@ApiTags('jobs')
@Controller('jobs')
export class JobsController {
constructor(private readonly jobsService: JobsService) {}

@ApiOkResponse({ type: JobEntity, isArray: true })
@ApiQuery({
name: 'search',
required: false,
type: String,
description: "Optional search keyword"
})
@Get()
async findAll(@Query('search') search: string) {
return await this.jobsService.findAll(search);
}
}
12 changes: 12 additions & 0 deletions server/src/api/jobs/jobs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { PrismaService } from 'src/database/connection.service';
import { SearchModule } from '../search/search.module';
import { JobsController } from './jobs.controller';
import { JobsService } from './jobs.service';

@Module({
imports: [SearchModule],
controllers: [JobsController],
providers: [JobsService, PrismaService],
})
export class JobsModule {}
26 changes: 26 additions & 0 deletions server/src/api/jobs/jobs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { Job } from '@prisma/client';
import { PrismaService } from 'src/database/connection.service';
import { SearchService } from '../search/search.service';

@Injectable()
export class JobsService {
constructor(
private prisma: PrismaService,
private searchService: SearchService
){}

async findAll(search?: string): Promise<Job[] | null> {
let jobs: Job[] = [];

jobs = await this.prisma.job.findMany();
await this.searchService.addDocuments(jobs);

if (search) {
const results = await this.searchService.search(search);
jobs = results.hits.map((hit) => hit._formatted as Job);
}

return jobs;
}
}
18 changes: 0 additions & 18 deletions server/src/api/sample/sample.controller.spec.ts

This file was deleted.

9 changes: 0 additions & 9 deletions server/src/api/sample/sample.controller.ts

This file was deleted.

11 changes: 0 additions & 11 deletions server/src/api/sample/sample.dto.ts

This file was deleted.

7 changes: 0 additions & 7 deletions server/src/api/sample/sample.entities.ts

This file was deleted.

8 changes: 8 additions & 0 deletions server/src/api/search/search.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { SearchService } from './search.service';

@Module({
providers: [SearchService],
exports: [SearchService]
})
export class SearchModule {}
44 changes: 44 additions & 0 deletions server/src/api/search/search.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Injectable } from '@nestjs/common';
import { Job } from '@prisma/client';
import { EnqueuedTask, Index, MeiliSearch, SearchResponse } from 'meilisearch';

@Injectable()
export class SearchService {
private SearchClient: MeiliSearch;

constructor() {
this.SearchClient = new MeiliSearch({
host: 'http://meilisearch:7700',
})

this.SearchClient.createIndex('jobs', { primaryKey: 'id'})
}

private getIndex(): Index {
return this.SearchClient.index('jobs');
}

async addDocuments(docs: Job[]): Promise<EnqueuedTask> {
const index = this.getIndex();

try {
return await index.addDocuments(docs);
} catch(e) {
console.log('Error adding documents');
throw e;
}
}

async search(query: string): Promise<SearchResponse> {
const index = this.getIndex();

return await index.searchGet(
query,
{
attributesToHighlight: ['title'],
attributesToSearchOn: ['title'],
attributesToRetrieve: ['*'],
}
);
}
}
12 changes: 8 additions & 4 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Module } from '@nestjs/common';
import { JobsController } from './api/jobs/jobs.controller';
import { JobsModule } from './api/jobs/jobs.module';
import { JobsService } from './api/jobs/jobs.service';
import { SearchModule } from './api/search/search.module';
import { SearchService } from './api/search/search.service';
import { DatabaseModule } from './database/database.module';
import { SampleController } from './api/sample/sample.controller';

@Module({
imports: [DatabaseModule],
controllers: [SampleController],
providers: [],
imports: [DatabaseModule, JobsModule, SearchModule],
controllers: [JobsController],
providers: [JobsService, SearchService],
})
export class AppModule {}
11 changes: 11 additions & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder()
.setTitle('Sim-JMS')
.setDescription('This is the API documentation for sim-jms')
.setVersion('1.0')
.build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

await app.listen(4000);
}
bootstrap();
16 changes: 16 additions & 0 deletions server/src/models/data/customers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { faker } from '@faker-js/faker';

const customers = [];

for (let i = 0; i < 20; i ++) {
customers.push({
id: i + 1,
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
email: faker.internet.email(),
contact: faker.phone.number('+63 9# ### ## ##'),
address: faker.location.streetAddress(true)
})
}

export { customers };
19 changes: 19 additions & 0 deletions server/src/models/data/jobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { faker } from '@faker-js/faker';
import { PaymentMethod } from '@prisma/client';

const jobs = [];
const paymentMethod = Object.values(PaymentMethod);

for (let i = 0; i < 20; i ++) {
const j = i + 1;
jobs.push({
id: j,
title: faker.lorem.words(),
type: faker.lorem.word(),
personInChargeId: faker.number.int({ min: 1, max: 10 }),
customerId: j,
paymentMethod: faker.helpers.arrayElement(paymentMethod)
})
}

export { jobs };
14 changes: 14 additions & 0 deletions server/src/models/data/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { faker } from '@faker-js/faker';

const users = [];

for (let i = 0; i < 10; i ++) {
users.push({
id: i + 1,
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
email: faker.internet.email(),
})
}

export { users };
Loading

0 comments on commit b360a8d

Please sign in to comment.