generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #699 from bcgov/feat/srs-293-api
Dashboard Api files
- Loading branch information
Showing
13 changed files
with
520 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Field, InputType } from '@nestjs/graphql'; | ||
import { IsNotEmpty, IsString, IsOptional, IsDate, IsInt, Min } from 'class-validator'; | ||
|
||
@InputType() | ||
export class RecentViewDto { | ||
|
||
@Field() | ||
@IsNotEmpty() | ||
@IsString() | ||
userId: string; | ||
|
||
@Field() | ||
@IsNotEmpty() | ||
@IsString() | ||
siteId: string; | ||
|
||
@Field() | ||
@IsNotEmpty() | ||
@IsString() | ||
address: string; | ||
|
||
@Field() | ||
@IsNotEmpty() | ||
@IsString() | ||
city: string; | ||
|
||
@Field({nullable:true}) | ||
@IsOptional() | ||
@IsString() | ||
generalDescription: string | null; | ||
|
||
@Field({nullable:true}) | ||
@IsOptional() | ||
@IsDate() | ||
whenUpdated: Date | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Field, ObjectType } from '@nestjs/graphql'; | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
Index, | ||
ManyToOne, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
JoinColumn, | ||
BeforeUpdate, | ||
} from 'typeorm'; | ||
import { Sites } from './sites.entity'; | ||
import { SiteStaffs } from './siteStaffs.entity'; | ||
|
||
@ObjectType() | ||
@Entity('recent_views') | ||
@Index('idx_user_id', ['userId']) | ||
export class RecentViews { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Field() | ||
@Column('character varying', { name: 'user_id', length: 30 }) | ||
userId: string; | ||
|
||
@Field() | ||
@Column('character varying', { name: 'site_id' }) | ||
siteId: string; | ||
|
||
@Field() | ||
@Column('character varying', { length: 200 }) | ||
address: string; | ||
|
||
@Field() | ||
@Column('character varying', { name: 'city', length: 30 }) | ||
city: string; | ||
|
||
@Field({ nullable: true }) | ||
@Column('character varying', { | ||
name: 'general_description', | ||
length: 225, | ||
nullable: true, | ||
}) | ||
generalDescription: string | null; | ||
|
||
@Field({ nullable: true }) | ||
@Column('timestamp without time zone', { | ||
name: 'when_updated', | ||
nullable: true, | ||
}) | ||
whenUpdated: Date | null; | ||
|
||
@CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) | ||
created: Date; | ||
|
||
@UpdateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) | ||
updated: Date; | ||
|
||
@ManyToOne(() => Sites, (site) => site.recentViewedSites) | ||
@JoinColumn({ name: 'site_id', referencedColumnName: 'id' }) | ||
site: Sites; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
backend/sites/src/app/resolvers/dashboard.resolver.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DashboardResolver } from './dashboard.resolver'; | ||
import { DashboardService } from '../services/dashboard.service'; | ||
import { DashboardResponse } from '../dto/response/fetchSiteResponse'; | ||
import { sampleSites } from '../mockData/site.mockData'; | ||
import { RecentViews } from '../../app/entities/recentViews.entity'; | ||
import { RecentViewDto } from '../dto/recentView.dto'; | ||
|
||
describe('DashboardResolver', () => { | ||
let resolver: DashboardResolver; | ||
let service: DashboardService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
DashboardResolver, | ||
{ | ||
provide: DashboardService, | ||
useValue: { | ||
getRecentViewsByUserId: jest.fn(), | ||
addRecentView: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
resolver = module.get<DashboardResolver>(DashboardResolver); | ||
service = module.get<DashboardService>(DashboardService); | ||
}); | ||
|
||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
|
||
describe('getRecentViewsByUserId', () => { | ||
const res = [ | ||
{ id: 1, userId:'1', siteId: '1', address: '123 Street', city: 'City', generalDescription: 'Description', whenUpdated: new Date(), created: new Date(), updated: new Date(), site: sampleSites[0] }, | ||
{ id: 2, userId:'1', siteId: '2', address: '456 Street', city: 'City', generalDescription: 'Description', whenUpdated: new Date(), created: new Date(), updated: new Date(), site: sampleSites[0] }, | ||
]; | ||
it('should return recent views for valid userId', async () => { | ||
const userId = '1'; | ||
const expectedResult: DashboardResponse = { | ||
httpStatusCode: 200, | ||
message: 'Success', | ||
data: res, | ||
}; | ||
jest.spyOn(service, 'getRecentViewsByUserId').mockResolvedValueOnce(expectedResult.data); | ||
|
||
const result = await resolver.getRecentViewsByUserId(userId); | ||
|
||
expect(result).toEqual(expectedResult); | ||
expect(service.getRecentViewsByUserId).toHaveBeenCalledWith(userId); | ||
}); | ||
|
||
it('should handle data not found for user id', async () => { | ||
const userId = '1'; | ||
jest.spyOn(service, 'getRecentViewsByUserId').mockResolvedValueOnce(null); | ||
|
||
const result = await resolver.getRecentViewsByUserId(userId); | ||
|
||
expect(result.httpStatusCode).toEqual(404); | ||
expect(result.message).toContain(userId); | ||
expect(result.data).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('addRecentView', () => { | ||
const recentViewDto = { | ||
userId: '1', | ||
siteId: '1', | ||
address: '123 Street', | ||
city: 'City', | ||
generalDescription: 'Description', | ||
whenUpdated: new Date(), | ||
}; | ||
|
||
it('should add recent view for valid input', async () => { | ||
const recentView: RecentViewDto = recentViewDto; | ||
const expectedResult ='Record is inserted successfully.'; | ||
jest.spyOn(service, 'addRecentView').mockResolvedValueOnce(expectedResult); | ||
|
||
const result = await resolver.addRecentView(recentView); | ||
|
||
expect(result.httpStatusCode).toEqual(201); | ||
expect(result.message).toEqual(expectedResult); | ||
}); | ||
|
||
it('should handle bad request', async () => { | ||
const recentView: RecentViewDto = recentViewDto; | ||
jest.spyOn(service, 'addRecentView').mockResolvedValueOnce(null); | ||
|
||
const result = await resolver.addRecentView(recentView); | ||
|
||
expect(result.httpStatusCode).toEqual(400); | ||
expect(result.message).toEqual('Bad Request.'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; | ||
import { RecentViews } from '../entities/recentViews.entity'; | ||
import { DashboardService } from '../services/dashboard.service'; | ||
import { ValidationPipe } from '@nestjs/common'; | ||
import { RecentViewDto } from '../dto/recentView.dto'; | ||
import { DashboardResponse } from '../dto/response/fetchSiteResponse'; | ||
import { RoleMatchingMode, Roles } from 'nest-keycloak-connect'; | ||
|
||
@Resolver(() => RecentViews) | ||
export class DashboardResolver { | ||
constructor(private readonly dashboardService: DashboardService) {} | ||
|
||
@Roles({ roles: ['site-admin'], mode: RoleMatchingMode.ANY }) | ||
@Query(() => DashboardResponse, { name: 'getRecentViewsByUserId' }) | ||
async getRecentViewsByUserId( | ||
@Args('userId', { type: () => String }) userId: string, | ||
) { | ||
const result = await this.dashboardService.getRecentViewsByUserId(userId); | ||
if (result) { | ||
return { httpStatusCode: 200, message: 'Success', data: result }; | ||
} | ||
|
||
return { | ||
httpStatusCode: 404, | ||
message: `Data not found for user id: ${userId}`, | ||
data: result, | ||
}; | ||
} | ||
|
||
@Roles({ roles: ['site-admin'], mode: RoleMatchingMode.ANY }) | ||
@Mutation(() => DashboardResponse, { name: 'addRecentView' }) | ||
async addRecentView( | ||
@Args('recentView', { type: () => RecentViewDto }, new ValidationPipe()) | ||
recentView: RecentViewDto, | ||
) { | ||
const result = await this.dashboardService.addRecentView(recentView); | ||
|
||
if (result) { | ||
return { httpStatusCode: 201, message: result }; | ||
} | ||
|
||
return { httpStatusCode: 400, message: 'Bad Request.' }; | ||
} | ||
} |
Oops, something went wrong.