Skip to content

Commit

Permalink
chore(#9561): refactor services to fix cht-from error (#9647)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester authored Nov 20, 2024
1 parent d36ecf1 commit b5c91d9
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 187 deletions.
3 changes: 0 additions & 3 deletions tests/integration/cht-form/default/person-edit.wdio-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ describe('cht-form web component - Edit Person Form', () => {
created_by_place_uuid: ''
}
},
meta: {
instanceID: 'uuid:c558e232-0951-4ed8-8392-5ed8ac3c81b3'
}
};
});

Expand Down
20 changes: 19 additions & 1 deletion tests/integration/cht-form/wdio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ const logPath = path.join('tests', 'logs');
const browserLogPath = path.join(logPath, 'browser.console.log');
const mockConfig = require('./mock-config');

const ALLOWED_LOG_MSGS = [
'favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
'tag was parsed inside of a <select> which was not inserted into the document. This is not valid ' +
'HTML and the behavior may be changed in future versions of chrome.',
'invalid phone number',
'Error submitting form data:'
];
const notAllowedLog = (msg) => {
return !ALLOWED_LOG_MSGS.some(allowedMsg => msg.includes(allowedMsg));
};

// Override specific properties from wdio base config
const defaultConfig = {
...wdioBaseConfig.config,
Expand Down Expand Up @@ -48,7 +59,14 @@ const defaultConfig = {
mockConfig.stopMockApp();
},

afterTest: () => { },
afterTest: async (test) => {
const logs = (await browser.getLogs('browser'))
.map(({ message }) => message)
.filter(notAllowedLog);
if (logs.length) {
test.callback(new Error(`Browser console logs are not empty: ${logs.join('\n')}`));
}
},

onComplete: () => { },
};
Expand Down
24 changes: 13 additions & 11 deletions webapp/src/ts/modules/messages/messages.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { FastAction, FastActionButtonService } from '@mm-services/fast-action-bu
import { PerformanceService } from '@mm-services/performance.service';
import { ExtractLineageService } from '@mm-services/extract-lineage.service';
import { ButtonType } from '@mm-components/fast-action-button/fast-action-button.component';
import { UserContactService } from '@mm-services/user-contact.service';

@Component({
templateUrl: './messages.component.html'
Expand All @@ -37,24 +38,25 @@ export class MessagesComponent implements OnInit, OnDestroy {
userLineageLevel;

constructor(
private router: Router,
private store: Store,
private changesService: ChangesService,
private fastActionButtonService:FastActionButtonService,
private messageContactService: MessageContactService,
private exportService: ExportService,
private modalService: ModalService,
private responsiveService: ResponsiveService,
private performanceService: PerformanceService,
private extractLineageService: ExtractLineageService,
private readonly router: Router,
private readonly store: Store,
private readonly changesService: ChangesService,
private readonly fastActionButtonService:FastActionButtonService,
private readonly messageContactService: MessageContactService,
private readonly exportService: ExportService,
private readonly modalService: ModalService,
private readonly responsiveService: ResponsiveService,
private readonly performanceService: PerformanceService,
private readonly extractLineageService: ExtractLineageService,
private readonly userContactService: UserContactService
) {
this.globalActions = new GlobalActions(store);
this.messagesActions = new MessagesActions(store);
}

ngOnInit() {
this.trackPerformance = this.performanceService.track();
this.userLineageLevel = this.extractLineageService.getUserLineageToRemove();
this.userLineageLevel = this.userContactService.getUserLineageToRemove();
this.subscribeToStore();
this.updateConversations().then(() => this.displayFirstConversation(this.conversations));
this.watchForChanges();
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/ts/modules/reports/reports.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class ReportsComponent implements OnInit, AfterViewInit, OnDestroy {
}

async ngAfterViewInit() {
this.userLineageLevel = this.extractLineageService.getUserLineageToRemove();
this.userLineageLevel = this.userContactService.getUserLineageToRemove();
await this.checkPermissions();
this.subscribeSidebarFilter();
this.doInitialSearch();
Expand Down
18 changes: 10 additions & 8 deletions webapp/src/ts/modules/tasks/tasks.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import { GlobalActions } from '@mm-actions/global';
import { LineageModelGeneratorService } from '@mm-services/lineage-model-generator.service';
import { PerformanceService } from '@mm-services/performance.service';
import { ExtractLineageService } from '@mm-services/extract-lineage.service';
import { UserContactService } from '@mm-services/user-contact.service';

@Component({
templateUrl: './tasks.component.html',
})
export class TasksComponent implements OnInit, OnDestroy {
constructor(
private store: Store,
private changesService: ChangesService,
private contactTypesService: ContactTypesService,
private rulesEngineService: RulesEngineService,
private performanceService: PerformanceService,
private lineageModelGeneratorService: LineageModelGeneratorService,
private extractLineageService: ExtractLineageService,
private readonly store: Store,
private readonly changesService: ChangesService,
private readonly contactTypesService: ContactTypesService,
private readonly rulesEngineService: RulesEngineService,
private readonly performanceService: PerformanceService,
private readonly lineageModelGeneratorService: LineageModelGeneratorService,
private readonly extractLineageService: ExtractLineageService,
private readonly userContactService: UserContactService
) {
this.tasksActions = new TasksActions(store);
this.globalActions = new GlobalActions(store);
Expand Down Expand Up @@ -102,7 +104,7 @@ export class TasksComponent implements OnInit, OnDestroy {
this.hasTasks = false;
this.loading = true;
this.debouncedReload = _debounce(this.refreshTasks.bind(this), 1000, { maxWait: 10 * 1000 });
this.userLineageLevel = this.extractLineageService.getUserLineageToRemove();
this.userLineageLevel = this.userContactService.getUserLineageToRemove();
this.refreshTasks();
}

Expand Down
25 changes: 2 additions & 23 deletions webapp/src/ts/services/extract-lineage.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { Injectable } from '@angular/core';

import { UserSettingsService } from '@mm-services/user-settings.service';
import { UserContactService } from '@mm-services/user-contact.service';
import { AuthService } from '@mm-services/auth.service';

@Injectable({
providedIn: 'root'
})
export class ExtractLineageService {

constructor(
private userSettingsService: UserSettingsService,
private userContactService: UserContactService,
private authService: AuthService,
) { }
constructor() {
}

extract(contact) {
if (!contact) {
Expand All @@ -32,20 +25,6 @@ export class ExtractLineageService {
return result;
}

async getUserLineageToRemove(): Promise<string | null> {
if (this.authService.online(true)) {
return null;
}

const { facility_id }:any = await this.userSettingsService.get();
if (!facility_id || (Array.isArray(facility_id) && facility_id.length > 1)) {
return null;
}

const user = await this.userContactService.get();
return user?.parent?.name as string;
}

removeUserFacility(lineage: string[], userLineageLevel: string): string[] | undefined {
if (!lineage?.length) {
return;
Expand Down
18 changes: 17 additions & 1 deletion webapp/src/ts/services/user-contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Person, Qualifier } from '@medic/cht-datasource';

import { UserSettingsService } from '@mm-services/user-settings.service';
import { CHTDatasourceService } from '@mm-services/cht-datasource.service';
import { AuthService } from '@mm-services/auth.service';

@Injectable({
providedIn: 'root'
Expand All @@ -11,7 +12,8 @@ export class UserContactService {
private readonly getPerson: ReturnType<typeof Person.v1.get>;
private readonly getPersonWithLineage: ReturnType<typeof Person.v1.getWithLineage>;
constructor(
private userSettingsService: UserSettingsService,
private readonly userSettingsService: UserSettingsService,
private readonly authService: AuthService,
chtDatasourceService: CHTDatasourceService,
) {
this.getPerson = chtDatasourceService.bind(Person.v1.get);
Expand All @@ -27,6 +29,20 @@ export class UserContactService {
return getPerson(Qualifier.byUuid(user.contact_id));
}

async getUserLineageToRemove(): Promise<string | null> {
if (this.authService.online(true)) {
return null;
}

const { facility_id }:any = await this.getUserSettings();
if (!facility_id || (Array.isArray(facility_id) && facility_id.length > 1)) {
return null;
}

const user = await this.get();
return user?.parent?.name as string;
}

private getUserSettings = async () => {
try {
return await this.userSettingsService.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { SettingsService } from '@mm-services/settings.service';
import { ModalService } from '@mm-services/modal.service';
import { NavigationComponent } from '@mm-components/navigation/navigation.component';
import { NavigationService } from '@mm-services/navigation.service';
import { ExtractLineageService } from '@mm-services/extract-lineage.service';
import { FastActionButtonService } from '@mm-services/fast-action-button.service';
import { SendMessageComponent } from '@mm-modals/send-message/send-message.component';
import { MessagesMoreMenuComponent } from '@mm-modules/messages/messages-more-menu.component';
Expand All @@ -26,6 +25,7 @@ import { ToolBarComponent } from '@mm-components/tool-bar/tool-bar.component';
import { PerformanceService } from '@mm-services/performance.service';
import { ExportService } from '@mm-services/export.service';
import { AuthService } from '@mm-services/auth.service';
import { UserContactService } from '@mm-services/user-contact.service';

describe('Messages Component', () => {
let component: MessagesComponent;
Expand All @@ -37,7 +37,7 @@ describe('Messages Component', () => {
let authService;
let sessionService;
let performanceService;
let extractLineageService;
let userContactService;
let stopPerformanceTrackStub;

beforeEach(waitForAsync(() => {
Expand All @@ -59,9 +59,8 @@ describe('Messages Component', () => {
has: sinon.stub()
};
sessionService = { isAdmin: sinon.stub() };
extractLineageService = {
userContactService = {
getUserLineageToRemove: sinon.stub(),
removeUserFacility: ExtractLineageService.prototype.removeUserFacility,
};
const mockedSelectors = [
{ selector: 'getSelectedConversation', value: {} },
Expand Down Expand Up @@ -96,7 +95,7 @@ describe('Messages Component', () => {
{ provide: AuthService, useValue: authService },
{ provide: FastActionButtonService, useValue: fastActionButtonService },
{ provide: PerformanceService, useValue: performanceService },
{ provide: ExtractLineageService, useValue: extractLineageService },
{ provide: UserContactService, useValue: userContactService },
{ provide: MatBottomSheet, useValue: { open: sinon.stub() } },
{ provide: MatDialog, useValue: { open: sinon.stub() } },
]
Expand Down Expand Up @@ -262,7 +261,7 @@ describe('Messages Component', () => {
];

it('it should retrieve the hierarchy level of the connected user', fakeAsync(async () => {
extractLineageService.getUserLineageToRemove.resolves('CHW Bettys Area');
userContactService.getUserLineageToRemove.resolves('CHW Bettys Area');

component.ngOnInit();
tick();
Expand All @@ -274,7 +273,7 @@ describe('Messages Component', () => {
sinon.resetHistory();

messageContactService.getList.resolves(conversations);
extractLineageService.getUserLineageToRemove.resolves(undefined);
userContactService.getUserLineageToRemove.resolves(undefined);
component.ngOnInit();
tick();
component.updateConversations({ merge: true });
Expand Down Expand Up @@ -307,7 +306,7 @@ describe('Messages Component', () => {
},
];

extractLineageService.getUserLineageToRemove.resolves('CHW Bettys Area');
userContactService.getUserLineageToRemove.resolves('CHW Bettys Area');
messageContactService.getList.resolves(conversations);
component.ngOnInit();
tick();
Expand Down
12 changes: 3 additions & 9 deletions webapp/tests/karma/ts/modules/reports/reports.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { FastActionButtonService } from '@mm-services/fast-action-button.service
import { FeedbackService } from '@mm-services/feedback.service';
import { XmlFormsService } from '@mm-services/xml-forms.service';
import { ReportsMoreMenuComponent } from '@mm-modules/reports/reports-more-menu.component';
import { ExtractLineageService } from '@mm-services/extract-lineage.service';

describe('Reports Component', () => {
let component: ReportsComponent;
Expand All @@ -56,7 +55,6 @@ describe('Reports Component', () => {
let xmlFormsService;
let feedbackService;
let performanceService;
let extractLineageService;
let stopPerformanceTrackStub;
let store;
let route;
Expand Down Expand Up @@ -108,17 +106,14 @@ describe('Reports Component', () => {
modalService = { show: sinon.stub() };
userContactService = {
get: sinon.stub().resolves(userContactDoc),
getUserLineageToRemove: sinon.stub(),
};
fastActionButtonService = {
getReportLeftSideActions: sinon.stub(),
getButtonTypeForContentList: sinon.stub(),
};
xmlFormsService = { subscribe: sinon.stub() };
feedbackService = { submit: sinon.stub() };
extractLineageService = {
getUserLineageToRemove: sinon.stub(),
removeUserFacility: ExtractLineageService.prototype.removeUserFacility,
};
route = { snapshot: { queryParams: { query: '' } } };
router = {
navigate: sinon.stub(),
Expand Down Expand Up @@ -169,7 +164,6 @@ describe('Reports Component', () => {
{ provide: FastActionButtonService, useValue: fastActionButtonService },
{ provide: FeedbackService, useValue: feedbackService },
{ provide: XmlFormsService, useValue: xmlFormsService },
{ provide: ExtractLineageService, useValue: extractLineageService },
]
})
.compileComponents()
Expand Down Expand Up @@ -788,7 +782,7 @@ describe('Reports Component', () => {

it('should not remove the lineage when user lineage level is undefined', fakeAsync(() => {
sinon.resetHistory();
extractLineageService.getUserLineageToRemove.resolves(undefined);
userContactService.getUserLineageToRemove.resolves(undefined);
const expectedReports = [
{
_id: '88b0dfff-4a82-4202-abea-d0cabe5aa9bd',
Expand Down Expand Up @@ -855,7 +849,7 @@ describe('Reports Component', () => {
}));

it('should remove lineage when user lineage level is defined', fakeAsync(() => {
extractLineageService.getUserLineageToRemove.resolves('CHW Bettys Area');
userContactService.getUserLineageToRemove.resolves('CHW Bettys Area');
const expectedReports = [
{
_id: '88b0dfff-4a82-4202-abea-d0cabe5aa9bd',
Expand Down
Loading

0 comments on commit b5c91d9

Please sign in to comment.