Skip to content

Commit

Permalink
move extract code logic to domain
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Jan 7, 2025
1 parent 1f048b7 commit e39eb53
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/data/repositories/ConfigD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { apiToFuture } from "$/data/api-futures";
import { metadataCodes } from "$/data/repositories/D2ApiMetadata";
import { Config, UserGroup } from "$/domain/entities/Config";
import { Region } from "$/domain/entities/Region";
import { Project } from "$/domain/entities/Project";
import { Region, extractRegionCode } from "$/domain/entities/Region";
import { Future, FutureData } from "$/domain/entities/generic/Future";
import { ConfigRepository } from "$/domain/repositories/ConfigRepository";
import { D2Api } from "$/types/d2-api";
import { extractFirstTwoLetters, extractPrefix } from "$/utils/string";

export class ConfigD2Repository implements ConfigRepository {
constructor(private api: D2Api) {}
Expand Down Expand Up @@ -45,7 +45,7 @@ export class ConfigD2Repository implements ConfigRepository {
id: region.id,
name: region.name,
// org. unit code includes the region code in the first two letters before the underscore
code: extractFirstTwoLetters(region.code),
code: Project.extractCode(region.code),
}));
});
}
Expand All @@ -61,7 +61,7 @@ export class ConfigD2Repository implements ConfigRepository {
id: d2UserGroup.id,
name: d2UserGroup.name,
// user group name includes the region code in the first two letters
code: extractPrefix(d2UserGroup.name),
code: extractRegionCode(d2UserGroup.name),
}));
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/domain/entities/DataSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { validateOrgUnits, validateRequired } from "$/domain/entities/generic/Va
import { Indicator } from "$/domain/entities/Indicator";
import { Config, UserGroup } from "$/domain/entities/Config";
import { DataSetToSave } from "$/domain/entities/DataSetToSave";
import { extractFirstTwoLetters, extractPrefix } from "$/utils/string";
import { extractRegionCode } from "$/domain/entities/Region";

export type DataSetAttrs = {
created: ISODateString;
Expand Down Expand Up @@ -133,7 +133,7 @@ export class DataSet extends Struct<DataSetAttrs>() {
getRegionCodesFromAccess(): string[] {
return _(this.access)
.filter(access => access.type === "groups")
.compactMap(access => extractPrefix(access.name))
.compactMap(access => Project.extractCode(access.name))
.uniq()
.value();
}
Expand Down Expand Up @@ -184,7 +184,7 @@ export class DataSet extends Struct<DataSetAttrs>() {
}

private getAccessFromOrgUnits(orgUnits: OrgUnit[], config: Config): AccessData[] {
const orgsUnitsCodes = orgUnits.map(orgUnit => extractFirstTwoLetters(orgUnit.code));
const orgsUnitsCodes = orgUnits.map(orgUnit => extractRegionCode(orgUnit.code));

const regions = config.regions.filter(region => orgsUnitsCodes.includes(region.code));
const regionsCodes = regions.map(region => region.code);
Expand All @@ -198,7 +198,7 @@ export class DataSet extends Struct<DataSetAttrs>() {

private getAccessFromProject(project: Maybe<Project>, config: Config): AccessData[] {
if (!project || !project.code) return [];
const regionCode = extractFirstTwoLetters(project.code);
const regionCode = extractRegionCode(project.code);

const region = config.regions.find(region => region.code === regionCode);
const userGroups = config.userGroups.filter(userGroup => userGroup.code === region?.code);
Expand Down
13 changes: 13 additions & 0 deletions src/domain/entities/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ export class Project extends Struct<ProjectAttrs>() {
static setDataSets(project: ProjectAttrs, dataSets: DataSet[]): Project {
return Project.build({ ...project, dataSets });
}

/**
* Extracts the portion of a code string before the first underscore ("_")
* and converts it to uppercase.
*
* Example:
* Input: "us_region"
* Output: "US"
*
*/
static extractCode(value: string): string {
return (value.split("_")[0] || "").toUpperCase();
}
}
14 changes: 14 additions & 0 deletions src/domain/entities/Region.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import { NamedCodeRef } from "$/domain/entities/Ref";

export type Region = NamedCodeRef;

/**
* Extracts the first two characters of a string and convert it to uppercase.
* If the input string is empty, returns an empty string.
*
* Example:
* Input: "us123"
* Output: "US"
*
*/

export function extractRegionCode(value: string): string {
return (value.slice(0, 2) || "").toUpperCase();
}
26 changes: 0 additions & 26 deletions src/utils/string.ts

This file was deleted.

0 comments on commit e39eb53

Please sign in to comment.