Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
freddieptf committed Nov 26, 2024
1 parent 5b4aed5 commit 03727cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
44 changes: 39 additions & 5 deletions src/config/config-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,43 @@ import kenyaConfig from './chis-ke';
import togoConfig from './chis-tg';
import civConfig from './chis-civ';

export enum Feature {
Create = 'create',
ReplaceContact = 'replace-contact',
Move = 'move',
}

const parseConfig = (c: any): PartnerConfig => {
return {
config: {
...c.config,
contact_types: c.config.contact_types.map((t: any) => {
return {
...t,
feature_flags: t.feature_flags?.map((v: string) => {
if ((Object.values(Feature) as string[]).indexOf(v) === -1) {
throw new Error(
'invalid feature flag: ' +
v +
'. Acceptable values are [' +
Object.values(Feature).join(' | ') +
']'
);
}
return v as Feature;
}),
};
}),
},
mutate: c.mutate,
};
};

const CONFIG_MAP: { [key: string]: PartnerConfig } = {
'CHIS-KE': kenyaConfig,
'CHIS-UG': ugandaConfig,
'CHIS-TG': togoConfig,
'CHIS-CIV': civConfig
'CHIS-KE': parseConfig(kenyaConfig),
'CHIS-UG': parseConfig(ugandaConfig),
'CHIS-TG': parseConfig(togoConfig),
'CHIS-CIV': parseConfig(civConfig),
};

export default function getConfigByKey(key: string = 'CHIS-KE'): PartnerConfig {
Expand All @@ -17,7 +49,9 @@ export default function getConfigByKey(key: string = 'CHIS-KE'): PartnerConfig {
const result = CONFIG_MAP[usingKey];
if (!result) {
const available = JSON.stringify(Object.keys(CONFIG_MAP));
throw Error(`Failed to start: Cannot find configuration "${usingKey}". Configurations available are ${available}`);
throw Error(
`Failed to start: Cannot find configuration '${usingKey}'. Configurations available are ${available}`
);
}

return result;
Expand Down
7 changes: 5 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { ChtApi, PlacePayload } from '../lib/cht-api';
import getConfigByKey from './config-factory';
import getConfigByKey, { Feature } from './config-factory';

export type ConfigSystem = {
domains: AuthenticationInfo[];
Expand All @@ -26,7 +26,7 @@ export type ContactType = {
contact_properties: ContactProperty[];
deactivate_users_on_replace?: boolean;
hint?: string;
feature_flags?: string[];
feature_flags?: Feature[];
};

export type HierarchyConstraint = {
Expand Down Expand Up @@ -123,6 +123,9 @@ export class Config {
}

public static hasMultipleRoles(contactType: ContactType): boolean {
if (contactType.feature_flags?.length === 1 && contactType.feature_flags.includes(Feature.Move)) {
return false;
}
if (!contactType.user_role.length || contactType.user_role.some(role => !role.trim())) {
throw Error(`unvalidatable config: 'user_role' property is empty or contains empty strings`);
}
Expand Down
5 changes: 3 additions & 2 deletions src/routes/add-place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SessionCache from '../services/session-cache';
import RemotePlaceResolver from '../lib/remote-place-resolver';
import { UploadManager } from '../services/upload-manager';
import RemotePlaceCache from '../lib/remote-place-cache';
import { Feature } from '../config/config-factory';

export default async function addPlace(fastify: FastifyInstance) {
fastify.get('/add-place', async (req, resp) => {
Expand All @@ -18,8 +19,8 @@ export default async function addPlace(fastify: FastifyInstance) {
: contactTypes[contactTypes.length - 1];
const op = queryParams.op || 'new';
if (contactType.feature_flags) {
if ((op === 'new' && !contactType.feature_flags.includes('create')) ||
(op === 'replace' && !contactType.feature_flags.includes('replace-contact'))) {
if ((op === 'new' && !contactType.feature_flags.includes(Feature.Create)) ||
(op === 'replace' && !contactType.feature_flags.includes(Feature.ReplaceContact))) {
resp.code(404).type('text/html').send('Not Found');
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChtApi } from '../lib/cht-api';
import { FastifyInstance } from 'fastify';
import MoveLib from '../lib/move';
import SessionCache from '../services/session-cache';
import { Feature } from '../config/config-factory';

export default async function sessionCache(fastify: FastifyInstance) {
fastify.get('/move/:placeType', async (req, resp) => {
Expand All @@ -13,7 +14,7 @@ export default async function sessionCache(fastify: FastifyInstance) {
const contactTypes = Config.contactTypes();

const contactType = Config.getContactType(placeType);
if (contactType.feature_flags && !contactType.feature_flags.includes('move')) {
if (contactType.feature_flags && !contactType.feature_flags.includes(Feature.Move)) {
resp.code(404).type('text/html').send('Not Found');
return;
}
Expand Down

0 comments on commit 03727cf

Please sign in to comment.