Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion admin/src/js/services/contact-types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const contactTypesUtils = require('@medic/contact-types-utils');
const { DOC_TYPES } = require('@medic/constants');

angular.module('inboxServices').service('ContactTypes', function(
Settings
Expand Down Expand Up @@ -34,7 +35,7 @@ angular.module('inboxServices').service('ContactTypes', function(
if (!type) {
return false;
}
return type === 'contact' || // configurable hierarchy
return type === DOC_TYPES.CONTACT || // configurable hierarchy
contactTypesUtils.isHardcodedType(type); // hardcoded
},

Expand Down
73 changes: 73 additions & 0 deletions apply-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const fs = require('fs');

Check warning on line 1 in apply-fix.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:fs` over `fs`.

See more on https://sonarcloud.io/project/issues?id=medic_cht-core&issues=AZ-nPgj6YEazGc7HIUOQ&open=AZ-nPgj6YEazGc7HIUOQ&pullRequest=11303

const edits = [
{
file: 'webapp/src/ts/modules/tasks/tasks-content.component.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [["action.type === 'contact'", "action.type === DOC_TYPES.CONTACT"]],
},
{
file: 'webapp/src/ts/modules/messages/messages-content.component.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [["if (type === 'contact')", "if (type === DOC_TYPES.CONTACT)"]],
},
{
file: 'webapp/src/ts/services/contact-types.service.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [["type === 'contact' || // configurable hierarchy", "type === DOC_TYPES.CONTACT || // configurable hierarchy"]],
},
{
file: 'webapp/src/ts/reducers/contacts.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [["contact.type === 'contact'", "contact.type === DOC_TYPES.CONTACT"]],
},
{
file: 'webapp/src/js/bootstrapper/offline-ddocs/medic-offline-freetext/contacts_by_type_freetext.js',
imports: [{ after: null, add: "const { DOC_TYPES } = require('@medic/constants');\n" }],
replacements: [
["doc.type !== 'contact'", "doc.type !== DOC_TYPES.CONTACT"],
["typeIndex === -1 && doc.type === 'contact'", "typeIndex === -1 && doc.type === DOC_TYPES.CONTACT"],
],
},
{
file: 'webapp/src/ts/modules/contacts/contacts-edit.component.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [["type: 'contact',", "type: DOC_TYPES.CONTACT,"]],
},
{
file: 'webapp/src/ts/services/form.service.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [
[": { type: 'contact', contact_type: type };", ": { type: DOC_TYPES.CONTACT, contact_type: type };"],
["this.type !== 'contact' && this.type !== 'training-card';", "this.type !== DOC_TYPES.CONTACT && this.type !== 'training-card';"],
],
},
{
file: 'webapp/src/ts/services/get-data-records.service.ts',
imports: [{ after: null, add: "import { DOC_TYPES } from '@medic/constants';\n" }],
replacements: [["this.getRecords(ids, 'contact', options);", "this.getRecords(ids, DOC_TYPES.CONTACT, options);"]],
},
];

for (const edit of edits) {
let content = fs.readFileSync(edit.file, 'utf8');

for (const rep of edit.replacements) {
if (!content.includes(rep[0])) {
console.error(`WARNING: pattern not found in ${edit.file}:\n ${rep[0]}`);
continue;
}
content = content.replace(rep[0], rep[1]);
}

// add import at the very top if not already present
const importLine = edit.imports[0].add;
if (!content.includes("from '@medic/constants'") && !content.includes("require('@medic/constants')")) {
content = importLine + content;
}

fs.writeFileSync(edit.file, content, 'utf8');
console.log(`Updated: ${edit.file}`);
}

console.log('\nDone. Run `git diff` to review.');
3 changes: 2 additions & 1 deletion shared-libs/cht-datasource/src/local/person.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Doc, isDoc } from '../libs/doc';
import contactTypeUtils from '@medic/contact-types-utils';
import { DOC_TYPES } from '@medic/constants';

Check warning on line 3 in shared-libs/cht-datasource/src/local/person.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'@medic/constants' imported multiple times.

See more on https://sonarcloud.io/project/issues?id=medic_cht-core&issues=AZ-nPgdtYEazGc7HIUOO&open=AZ-nPgdtYEazGc7HIUOO&pullRequest=11303
import { assertHasRequiredField, DataObject, Nullable, Page } from '../libs/core';
import * as Qualifier from '../qualifier';
import { ContactTypeQualifier, UuidQualifier } from '../qualifier';
Expand All @@ -12,7 +13,7 @@
import * as Input from '../input';
import { assertHasValidParentType, assertSameParentLineage, fetchHydratedDoc, minifyDoc } from './libs/lineage';
import { assertPersonInput } from '../libs/parameter-validators';
import { CONTACT_TYPES } from '@medic/constants';

Check warning on line 16 in shared-libs/cht-datasource/src/local/person.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'@medic/constants' imported multiple times.

See more on https://sonarcloud.io/project/issues?id=medic_cht-core&issues=AZ-nPgdtYEazGc7HIUOP&open=AZ-nPgdtYEazGc7HIUOP&pullRequest=11303

const DEFAULT_PERSON_TYPE = {
id: 'person',
Expand All @@ -29,7 +30,7 @@
throw new InvalidArgumentError(`[${input.type}] is not a valid person type.`);
}
return customType
? { contact_type: input.type, type: 'contact' }
? { contact_type: input.type, type: DOC_TYPES.CONTACT }
: { type: input.type };
};

Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/local/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './libs/lineage';
import { assertPlaceInput } from '../libs/parameter-validators';
import * as LocalContact from './contact';
import { CONTACT_TYPES } from '@medic/constants';
import { CONTACT_TYPES, DOC_TYPES } from '@medic/constants';

const DEFAULT_PLACE_TYPES_DICT: Record<string, { id: string, parents?: string[], person?: boolean } | undefined> = {
[CONTACT_TYPES.DISTRICT_HOSPITAL]: { id: CONTACT_TYPES.DISTRICT_HOSPITAL },
Expand Down Expand Up @@ -49,7 +49,7 @@ const getTypeProperties = (settings: DataObject, input: Input.v1.PlaceInput) =>
getPlaceType(settings, input);
const customType = contactTypeUtils.getTypeById(settings, input.type);
return customType
? { contact_type: input.type, type: 'contact' }
? { contact_type: input.type, type: DOC_TYPES.CONTACT }
: { type: input.type };
};

Expand Down
3 changes: 2 additions & 1 deletion shared-libs/constants/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const DOC_TYPES = {
TOKEN_LOGIN: 'token_login',
TRANSLATIONS: 'translations',
DATA_RECORD: 'data_record',
UI_EXTENSION: 'ui-extension'
UI_EXTENSION: 'ui-extension',
CONTACT: 'contact',
};

// HTTP Headers
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/contact-types-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { CONTACT_TYPES } = require('@medic/constants');
const { CONTACT_TYPES, DOC_TYPES } = require('@medic/constants');

const HARDCODED_PERSON_TYPE = 'person';
const HARDCODED_TYPES = [
Expand All @@ -21,7 +21,7 @@ const getTypeId = (doc) => {
if (!doc) {
return;
}
return doc.type === 'contact' ? doc.contact_type : doc.type;
return doc.type === DOC_TYPES.CONTACT ? doc.contact_type : doc.type;
};

const getTypeById = (config, typeId) => {
Expand Down
4 changes: 1 addition & 3 deletions shared-libs/lineage/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ const isContact = doc => {
if (!doc) {
return;
}

return doc.type === 'contact' || contactTypeUtils.HARDCODED_TYPES.includes(doc.type);
return doc.type === DOC_TYPES.CONTACT || contactTypeUtils.HARDCODED_TYPES.includes(doc.type);
};

const getId = (item) => item && (typeof item === 'string' ? item : item._id);

// don't process linked docs for non-contact types
Expand Down
2 changes: 1 addition & 1 deletion shared-libs/summaries/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const isContact = (doc) => {
if (!type) {
return false;
}
return type === 'contact' || contactTypesUtils.isHardcodedType(type);
return type === DOC_TYPES.CONTACT || contactTypesUtils.isHardcodedType(type);
};

const isReport = (doc) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { DOC_TYPES } = require('@medic/constants');
module.exports.map = (doc) => {
const skip = [ '_id', '_rev', 'type', 'refid', 'geolocation' ];
const keyShouldBeSkipped = key => skip.indexOf(key) !== -1 || /_date$/.test(key);
Expand Down Expand Up @@ -32,7 +33,7 @@ module.exports.map = (doc) => {
};

const getType = () => {
if (doc.type !== 'contact') {
if (doc.type !== DOC_TYPES.CONTACT) {
return doc.type;
}

Expand All @@ -42,7 +43,7 @@ module.exports.map = (doc) => {
const getTypeIndex = type => {
const types = [ 'district_hospital', 'health_center', 'clinic', 'person' ];
const typeIndex = types.indexOf(type);
if (typeIndex === -1 && doc.type === 'contact') {
if (typeIndex === -1 && doc.type === DOC_TYPES.CONTACT) {
return type;
}

Expand Down
3 changes: 2 additions & 1 deletion webapp/src/ts/modules/contacts/contacts-edit.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import { AfterViewInit, Component, NgZone, OnDestroy, OnInit } from '@angular/core';
import { combineLatest, Subscription } from 'rxjs';
import { Store } from '@ngrx/store';
Expand Down Expand Up @@ -252,7 +253,7 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
} else { // adding
this.trackMetadata.action = 'add';
this.contact = {
type: 'contact',
type: DOC_TYPES.CONTACT,
contact_type: this.routeSnapshot.params?.type,
parent: this.routeSnapshot.params?.parent_id || '',
};
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/ts/modules/messages/messages-content.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import {
AfterViewChecked,
AfterViewInit,
Expand Down Expand Up @@ -178,7 +179,7 @@ export class MessagesContentComponent implements OnInit, OnDestroy, AfterViewIni

// See URL parameter "id" note at top of file
private getContactable(id, type) {
if (type === 'contact') {
if (type === DOC_TYPES.CONTACT) {
return this.lineageModelGeneratorService
.contact(id)
.catch(err => {
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/ts/modules/tasks/tasks-content.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { combineLatest, Subject, Subscription } from 'rxjs';
Expand Down Expand Up @@ -307,7 +308,7 @@ export class TasksContentComponent implements OnInit, OnDestroy {
.then(() => this.preloadTaskGroupContact(action));
}

if (action.type === 'contact') {
if (action.type === DOC_TYPES.CONTACT) {
if (action.content?.parent_id && action.content?.type) {
this.router.navigate(['/contacts', action.content.parent_id, 'add', action.content.type || '']);
} else if (action.content?.type) {
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/ts/reducers/contacts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import { createReducer, on } from '@ngrx/store';

import { UniqueSortedList } from '@mm-reducers/utils';
Expand All @@ -16,7 +17,7 @@ const initialState = {
};

const getContactTypeOrder = (contact) => {
if (contact.type === 'contact') {
if (contact.type === DOC_TYPES.CONTACT) {
const idx = ContactTypesService.HARDCODED_TYPES().indexOf(contact.contact_type);
if (idx !== -1) {
// matches a hardcoded type - order by the index
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/ts/services/contact-types.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import { Injectable } from '@angular/core';
import * as contactTypesUtils from '@medic/contact-types-utils';

Expand Down Expand Up @@ -48,7 +49,7 @@ export class ContactTypesService {
if (!type) {
return false;
}
return type === 'contact' || // configurable hierarchy
return type === DOC_TYPES.CONTACT || // configurable hierarchy
contactTypesUtils.isHardcodedType(type); // hardcoded
}

Expand Down
5 changes: 3 additions & 2 deletions webapp/src/ts/services/form.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import { Injectable, NgZone } from '@angular/core';
import { Store } from '@ngrx/store';
import { toBik_text } from 'bikram-sambat';
Expand Down Expand Up @@ -400,7 +401,7 @@ export class FormService {
const { form, xmlVersion, duplicateCheck } = formInfo;
const typeFields = this.contactTypesService.isHardcodedType(type)
? { type }
: { type: 'contact', contact_type: type };
: { type: DOC_TYPES.CONTACT, contact_type: type };

const docs = await this.contactSaveService.save(form, docId, typeFields, xmlVersion);
const preparedDocs = await this.applyTransitions(docs);
Expand Down Expand Up @@ -477,6 +478,6 @@ export class WebappEnketoFormContext implements EnketoFormContext {

requiresContact() {
// Users can access contact forms even when they don't have a contact associated.
return this.type !== 'contact' && this.type !== 'training-card';
return this.type !== DOC_TYPES.CONTACT && this.type !== 'training-card';
}
}
3 changes: 2 additions & 1 deletion webapp/src/ts/services/get-data-records.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DOC_TYPES } from '@medic/constants';
import { Injectable } from '@angular/core';

import { DbService } from '@mm-services/db.service';
Expand Down Expand Up @@ -47,7 +48,7 @@ export class GetDataRecordsService {
}

getContacts(ids: string[], options: { hydrateContactNames?: boolean, include_docs?: boolean } = {}) {
return this.getRecords(ids, 'contact', options);
return this.getRecords(ids, DOC_TYPES.CONTACT, options);
}

getReports(ids: string[], options: { hydrateContactNames?: boolean, include_docs?: boolean } = {}) {
Expand Down
8 changes: 4 additions & 4 deletions webapp/web-components/cht-form/src/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NgIf, DOCUMENT } from '@angular/common';
import { TranslatePipe } from '@ngx-translate/core';
import { CHTDatasourceService } from '@mm-services/cht-datasource.service';
import { CHTDatasourceService as CHTDatasourceServiceStub } from './stubs/cht-datasource.service';
import { CONTACT_TYPES } from '@medic/constants';
import { CONTACT_TYPES, DOC_TYPES } from '@medic/constants';

const DEFAULT_FORM_ID = 'cht-form-id';

Expand Down Expand Up @@ -157,7 +157,7 @@ export class AppComponent {
if (contactType) {
const typeFields = this.HARDCODED_TYPES.includes(contactType)
? { type: contactType }
: { type: 'contact', contact_type: contactType };
: { type: DOC_TYPES.CONTACT, contact_type: contactType };
const { preparedDocs } = await this.contactSaveService.save(currentForm, null, typeFields, null);
return preparedDocs;
}
Expand Down Expand Up @@ -305,6 +305,6 @@ class ChtFormEnketoFormContext implements EnketoFormContext {
}

get type() {
return this.contactType ? 'contact': 'report';
}
return this.contactType ? DOC_TYPES.CONTACT : 'report';
}
}