Skip to content

Commit

Permalink
feat: Migrate getbyId -> spenderPlatformV1ApiService (#2946)
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilK-027 authored May 7, 2024
1 parent 3acb926 commit e110648
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
23 changes: 23 additions & 0 deletions src/app/core/mock-data/platform/v1/platform-project.data.ts
Original file line number Diff line number Diff line change
@@ -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<PlatformProject[]> = 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,
});
29 changes: 21 additions & 8 deletions src/app/core/services/projects.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand All @@ -28,10 +30,12 @@ describe('ProjectsService', () => {
let projectsService: ProjectsService;
let apiService: jasmine.SpyObj<ApiService>;
let apiV2Service: jasmine.SpyObj<ApiV2Service>;
let spenderPlatformV1ApiService: jasmine.SpyObj<SpenderPlatformV1ApiService>;

beforeEach(() => {
const apiServiceSpy = jasmine.createSpyObj('ApiService', ['get']);
const apiv2ServiceSpy = jasmine.createSpyObj('ApiV2Service', ['get']);
const spenderPlatformApiServiceSpy = jasmine.createSpyObj('SpenderPlatformV1ApiService', ['get']);

TestBed.configureTestingModule({
providers: [
Expand All @@ -44,29 +48,38 @@ describe('ProjectsService', () => {
provide: ApiV2Service,
useValue: apiv2ServiceSpy,
},
{
provide: SpenderPlatformV1ApiService,
useValue: spenderPlatformApiServiceSpy,
},
],
});
projectsService = TestBed.inject(ProjectsService);
apiService = TestBed.inject(ApiService) as jasmine.SpyObj<ApiService>;
apiV2Service = TestBed.inject(ApiV2Service) as jasmine.SpyObj<ApiV2Service>;
spenderPlatformV1ApiService = TestBed.inject(
SpenderPlatformV1ApiService
) as jasmine.SpyObj<SpenderPlatformV1ApiService>;
});

it('should be created', () => {
expect(projectsService).toBeTruthy();
});

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) => {
Expand Down
29 changes: 13 additions & 16 deletions src/app/core/services/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -146,22 +152,13 @@ export class ProjectsService {
}

getbyId(projectId: number | string): Observable<ProjectV2> {
return this.apiV2Service
.get<ProjectV2, {}>('/projects', {
return this.spenderPlatformV1ApiService
.get<PlatformApiResponse<PlatformProject[]>>('/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[] {
Expand All @@ -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,
}));

Expand Down
7 changes: 0 additions & 7 deletions src/app/core/test-data/projects.spec.data.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit e110648

Please sign in to comment.