diff --git a/src/app/core/mock-data/platform/v1/platform-project.data.ts b/src/app/core/mock-data/platform/v1/platform-project.data.ts new file mode 100644 index 0000000000..00f015d0dc --- /dev/null +++ b/src/app/core/mock-data/platform/v1/platform-project.data.ts @@ -0,0 +1,23 @@ +import { PlatformApiResponse } from '../../../models/platform/platform-api-response.model'; +import { PlatformProject } from '../../../models/platform/platform-project.model'; +import deepFreeze from 'deep-freeze-strict'; + +export const platformProjectSingleRes: PlatformApiResponse = deepFreeze({ + count: 1, + data: [ + { + is_enabled: true, + code: '1184', + created_at: new Date('2021-05-12T10:28:40.834844'), + description: 'Sage Intacct Project - Customer Mapped Project, Id - 1184', + id: 257528, + name: 'Customer Mapped Project', + category_ids: [122269, 122270, 122271, null], + org_id: 'orFdTTTNcyye', + updated_at: new Date('2021-07-08T10:28:27.686886'), + display_name: 'Customer Mapped Project', + sub_project: null, + }, + ], + offset: 0, +}); diff --git a/src/app/core/services/projects.service.spec.ts b/src/app/core/services/projects.service.spec.ts index 3152ac6a44..2b65fc6556 100644 --- a/src/app/core/services/projects.service.spec.ts +++ b/src/app/core/services/projects.service.spec.ts @@ -16,6 +16,8 @@ import { params, } from '../test-data/projects.spec.data'; import { ProjectsService } from './projects.service'; +import { SpenderPlatformV1ApiService } from './spender-platform-v1-api.service'; +import { platformProjectSingleRes } from '../mock-data/platform/v1/platform-project.data'; const fixDate = (data) => data.map((datum) => ({ @@ -28,10 +30,12 @@ describe('ProjectsService', () => { let projectsService: ProjectsService; let apiService: jasmine.SpyObj; let apiV2Service: jasmine.SpyObj; + let spenderPlatformV1ApiService: jasmine.SpyObj; beforeEach(() => { const apiServiceSpy = jasmine.createSpyObj('ApiService', ['get']); const apiv2ServiceSpy = jasmine.createSpyObj('ApiV2Service', ['get']); + const spenderPlatformApiServiceSpy = jasmine.createSpyObj('SpenderPlatformV1ApiService', ['get']); TestBed.configureTestingModule({ providers: [ @@ -44,11 +48,18 @@ describe('ProjectsService', () => { provide: ApiV2Service, useValue: apiv2ServiceSpy, }, + { + provide: SpenderPlatformV1ApiService, + useValue: spenderPlatformApiServiceSpy, + }, ], }); projectsService = TestBed.inject(ProjectsService); apiService = TestBed.inject(ApiService) as jasmine.SpyObj; apiV2Service = TestBed.inject(ApiV2Service) as jasmine.SpyObj; + spenderPlatformV1ApiService = TestBed.inject( + SpenderPlatformV1ApiService + ) as jasmine.SpyObj; }); it('should be created', () => { @@ -56,17 +67,19 @@ describe('ProjectsService', () => { }); it('should be able to fetch project by id', (done) => { - apiV2Service.get.and.returnValue(of(apiV2ResponseSingle)); + spenderPlatformV1ApiService.get.and.returnValue(of(platformProjectSingleRes)); + const transformToV2ResponseSpy = spyOn(projectsService, 'transformToV2Response').and.callThrough(); + projectsService.getbyId(257528).subscribe((res) => { - expect(res).toEqual(fixDate(apiV2ResponseSingle.data)[0]); + expect(res).toEqual(apiV2ResponseSingle.data[0]); + expect(spenderPlatformV1ApiService.get).toHaveBeenCalledOnceWith('/projects', { + params: { + id: 'eq.257528', + }, + }); + expect(transformToV2ResponseSpy).toHaveBeenCalled(); done(); }); - - expect(apiV2Service.get).toHaveBeenCalledWith('/projects', { - params: { - project_id: 'eq.257528', - }, - }); }); it('should be able to fetch all active projects', (done) => { diff --git a/src/app/core/services/projects.service.ts b/src/app/core/services/projects.service.ts index 312a84710c..862c4f52d4 100644 --- a/src/app/core/services/projects.service.ts +++ b/src/app/core/services/projects.service.ts @@ -10,12 +10,18 @@ import { ProjectParams } from '../models/project-params.model'; import { intersection } from 'lodash'; import { OrgCategory } from '../models/v1/org-category.model'; import { PlatformProject } from '../models/platform/platform-project.model'; +import { SpenderPlatformV1ApiService } from './spender-platform-v1-api.service'; +import { PlatformApiResponse } from '../models/platform/platform-api-response.model'; @Injectable({ providedIn: 'root', }) export class ProjectsService { - constructor(private apiService: ApiService, private apiV2Service: ApiV2Service) {} + constructor( + private apiService: ApiService, + private apiV2Service: ApiV2Service, + private spenderPlatformV1ApiService: SpenderPlatformV1ApiService + ) {} @Cacheable() getByParamsUnformatted( @@ -146,22 +152,13 @@ export class ProjectsService { } getbyId(projectId: number | string): Observable { - return this.apiV2Service - .get('/projects', { + return this.spenderPlatformV1ApiService + .get>('/projects', { params: { - project_id: `eq.${projectId}`, + id: `eq.${projectId}`, }, }) - .pipe( - map( - (res) => - res.data.map((datum) => ({ - ...datum, - project_created_at: new Date(datum.project_created_at), - project_updated_at: new Date(datum.project_updated_at), - }))[0] - ) - ); + .pipe(map((res) => this.transformToV2Response(res.data)[0])); } transformToV1Response(platformProject: PlatformProject[]): ProjectV1[] { @@ -188,11 +185,11 @@ export class ProjectsService { project_created_at: platformProject.created_at, project_description: platformProject.description, project_id: platformProject.id, - project_name: platformProject.name, + project_name: platformProject.display_name, project_org_category_ids: platformProject.category_ids, project_org_id: platformProject.org_id, project_updated_at: platformProject.updated_at, - projectv2_name: platformProject.display_name, + projectv2_name: platformProject.name, sub_project_name: platformProject.sub_project, })); diff --git a/src/app/core/test-data/projects.spec.data.ts b/src/app/core/test-data/projects.spec.data.ts index 64c42dcece..82d81ad6d1 100644 --- a/src/app/core/test-data/projects.spec.data.ts +++ b/src/app/core/test-data/projects.spec.data.ts @@ -1,5 +1,4 @@ import deepFreeze from 'deep-freeze-strict'; - import { ProjectParams } from '../models/project-params.model'; import { ProjectV1 } from '../models/v1/extended-project.model'; import { OrgCategory, OrgCategoryListItem } from '../models/v1/org-category.model'; @@ -146,13 +145,7 @@ export const apiV2ResponseSingle = deepFreeze({ count: 1, data: [ { - ap1_email: null, - ap1_full_name: null, - ap2_email: null, - ap2_full_name: null, project_active: true, - project_approver1_id: null, - project_approver2_id: null, project_code: '1184', project_created_at: new Date('2021-05-12T10:28:40.834844'), project_description: 'Sage Intacct Project - Customer Mapped Project, Id - 1184',